Commit e282a1b2 authored by Vũ Gia Vương's avatar Vũ Gia Vương

init

parents
#/////////////////////////////////////////////////////////////////////////////
# Fireball Projects
#/////////////////////////////////////////////////////////////////////////////
/library/
/temp/
/local/
/build/
node_modules/
dist/
build/
library/
temp/
local/
#/////////////////////////////////////////////////////////////////////////////
# npm files
#/////////////////////////////////////////////////////////////////////////////
npm-debug.log
node_modules/
#/////////////////////////////////////////////////////////////////////////////
# Logs and databases
#/////////////////////////////////////////////////////////////////////////////
*.log
*.sql
*.sqlite
#/////////////////////////////////////////////////////////////////////////////
# files for debugger
#/////////////////////////////////////////////////////////////////////////////
*.sln
*.pidb
*.suo
#/////////////////////////////////////////////////////////////////////////////
# OS generated files
#/////////////////////////////////////////////////////////////////////////////
.DS_Store
ehthumbs.db
Thumbs.db
#/////////////////////////////////////////////////////////////////////////////
# WebStorm files
#/////////////////////////////////////////////////////////////////////////////
.idea/
#//////////////////////////
# VS Code files
#//////////////////////////
.vscode/
{
"watch":["src"],
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./src/index.ts",
"ext": "ts, json, html, css, ejs"
}
\ No newline at end of file
This diff is collapsed.
{
"name": "game-sever-flip-jump",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon",
"build": "tsc",
"start": "node dist/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^5.0.3",
"cors": "^2.8.5",
"express": "^5.1.0",
"http": "^0.0.1-security",
"nodemon": "^3.1.10",
"socket.io": "^4.8.1",
"ts-node": "^10.9.2",
"typescript": "^5.9.2"
}
}
const CONFIG = {
GAMEID: {
HUNGQUA: 1000,
},
STATE: {
WATING: 'WATING',
PLAYING: 'PLAYING',
END: 'END',
SELECT: 'SELECT',
},
TIME: {
COUNTDOWN: 10,
SELECT_PLAYER: 5,
PLAY: 60
},
EVT: {
CREATE_ROOM: '100',
JOIN_ROOM: '101',
LEAVE_ROOM: '103',
ROOM_MESSAGE: '104',
START_GAME: '200',
UPDATE_STATE: '201',
UPDATE_SCORE: '202',
UPDATE_TIME_REMAINING: '203',
END_GAME: '204',
SPAWN_TOWER: '205',
PASS_TOWER: '207',
CLOSE_QR_VIEW: '206',
GAME_MESSAGE: '300',
RECONNECT: '500',
},
HTTP_RESPONSE: {
SUCCESS: {
code: 200,
message: "Success!",
},
ERROR: {
code: 500,
message: "Error!",
}
},
}
export default CONFIG;
\ No newline at end of file
export class Global {
public static rooms: { [room: string]: Room };
public static users: { [uuid: string]: User };
}
export class Room {
public name: string = '';
public users: string[] = [];
public key: string = '';
constructor(name: string, key: string) {
this.name = name;
this.users = [];
this.key = key;
}
}
export class User {
public uuid: string = '';
public room: string = '';
constructor(room: string, id: string) {
this.room = room;
this.uuid = id;
}
}
import app from "./server";
import http from 'http';
import { Server } from 'socket.io';
import { setupSocket } from "./socket";
const PORT = 3001;
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: '*',
methods: ["GET", "POST"],
credentials: true
}
});
setupSocket(io);
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}!`);
})
import express, { Request, Response, NextFunction } from 'express'
import cors from 'cors';
const app = express();
app.use(cors());
app.get('/', (req: Request, res: Response, next: NextFunction) => {
res.send('Hello World')
})
export default app;
import { Server, Socket } from "socket.io";
import CONFIG from "./Config/config";
const users: { [key: string]: User } = {}
const y_radio: number = 0.5560472
class User {
public token: string = '';
public towerNumber: number = 0;
public direction: number = 0;
public cur_block: v2 = new v2();
public next_block: v2 = new v2();
}
class v2 {
public x: number = 0;
public y: number = 0;
constructor(x?: number, y?: number) {
this.x = x || 0;
this.y = y || 0;
}
}
export function getUserBySocketId(socketId: string) {
return users[socketId];
}
export function setupSocket(io: Server) {
io.use((socket: Socket, next) => {
const token = socket.handshake.query.token; // Lấy ID từ client gửi lên
console.log('token', token)
users[socket.id] = {} as User;
users[socket.id].token = token as string; // Gán ID này vào socket
users[socket.id].towerNumber = 0;
users[socket.id].cur_block = new v2(0, 0);
next();
});
io.on("connection", (socket: Socket) => {
console.log(`🟢 Client connected: ${socket.id}`);
joinRoom(socket);
spawmItem(socket);
passTower(socket);
disconnectSocket(socket);
});
}
function passTower(socket: Socket) {
socket.on(CONFIG.EVT.PASS_TOWER, async (data: string) => {
const user = users[socket.id];
const towerNumber = ++user.towerNumber;
const { pos, dis } = JSON.parse(data) as { pos: v2, dis: number };
user.direction = (Math.random() < 0.5) ? -1 : 1;
const token = user.token;
console.log('towerNumber', towerNumber, token)
var x_distance = ((Math.min(towerNumber, 100) / 100 + 1) + Math.random() * (towerNumber <= 10 ? 0.5 : 1)) * 150;//vg
var y_distance = x_distance * y_radio;
var next_pos = user.cur_block;
next_pos.x += (x_distance * user.direction);
next_pos.y += y_distance;
const response = await socket.emitWithAck('request', { next_pos, direction: user.direction, towerNumber, x_distance }, 'bar');
});
}
function spawmItem(socket: Socket) {
socket.on(CONFIG.EVT.SPAWN_TOWER, async () => {
const user = users[socket.id];
const towerNumber = ++user.towerNumber;
user.direction = (Math.random() < 0.5) ? -1 : 1;
const token = user.token;
console.log('towerNumber', towerNumber, token)
var x_distance = ((Math.min(towerNumber, 100) / 100 + 1) + Math.random() * (towerNumber <= 10 ? 0.5 : 1)) * 150;//vg
var y_distance = x_distance * y_radio;
var next_pos = user.cur_block;
next_pos.x += (x_distance * user.direction);
next_pos.y += y_distance;
const response = await socket.emitWithAck('request', { next_pos, direction: user.direction, towerNumber, x_distance });
});
}
function disconnectSocket(socket: Socket) {
socket.on("disconnect", () => {
console.log(`🔴 Client disconnected: ${socket.id}`);
});
}
function joinRoom(socket: Socket) {
socket.on(CONFIG.EVT.JOIN_ROOM, (data: string) => {
const { roomName, id } = JSON.parse(data);
console.log(`JOIN_ROOM💬 ${roomName}: ${id}`);
});
}
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment