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

fix end game

parent 505cc4ca
...@@ -72,7 +72,7 @@ function callApi(url, method, data, token) { ...@@ -72,7 +72,7 @@ function callApi(url, method, data, token) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
const body = JSON.stringify(data); const body = JSON.stringify(data);
let res = yield fetch(url, { const res = yield fetch(url, {
method: method, method: method,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...@@ -81,6 +81,9 @@ function callApi(url, method, data, token) { ...@@ -81,6 +81,9 @@ function callApi(url, method, data, token) {
}, },
body, body,
}); });
if (res.status > 200) {
console.log('error callApi: ', res, 'data: ', data);
}
const result = yield res.json(); const result = yield res.json();
return (result === null || result === void 0 ? void 0 : result.code) == 'success'; return (result === null || result === void 0 ? void 0 : result.code) == 'success';
} }
......
...@@ -40,26 +40,12 @@ const io = new socket_io_1.Server(server, { ...@@ -40,26 +40,12 @@ const io = new socket_io_1.Server(server, {
cors: { cors: {
methods: ["GET", "POST"], methods: ["GET", "POST"],
// origin: "*", // origin: "*",
// origin: [ origin: [
// "https://admin.socket.io", "https://admin.socket.io",
// "https://play.gamee.vn", "https://play.gamee.vn",
// "https://dev.gamee.vn", "https://dev.gamee.vn",
// "http://localhost:7456" "http://localhost:7456"
// ], ],
origin: (origin, callback) => {
const allowedOrigins = [
"https://admin.socket.io",
"https://play.gamee.vn",
"https://dev.gamee.vn",
"http://localhost:7456"
];
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
}
else {
callback(new Error("Not allowed by CORS"));
}
},
credentials: true, credentials: true,
} }
}); });
......
...@@ -59,7 +59,7 @@ function setupSocket(io) { ...@@ -59,7 +59,7 @@ function setupSocket(io) {
socket.on(config_1.default.EVT.START_GAME, (data) => (0, gameCtrl_1.startGameCtrl)(socket, data, user)); socket.on(config_1.default.EVT.START_GAME, (data) => (0, gameCtrl_1.startGameCtrl)(socket, data, user));
socket.on(config_1.default.EVT.PASS_TOWER, (data) => __awaiter(this, void 0, void 0, function* () { return yield (0, gameCtrl_1.passTowerCtrl)(socket, data, user); })); socket.on(config_1.default.EVT.PASS_TOWER, (data) => __awaiter(this, void 0, void 0, function* () { return yield (0, gameCtrl_1.passTowerCtrl)(socket, data, user); }));
socket.on(config_1.default.EVT.HISTORY, () => __awaiter(this, void 0, void 0, function* () { return yield (0, gameCtrl_1.getHistory)(socket, user); })); socket.on(config_1.default.EVT.HISTORY, () => __awaiter(this, void 0, void 0, function* () { return yield (0, gameCtrl_1.getHistory)(socket, user); }));
socket.on(config_1.default.EVT.END_GAME, () => (0, gameCtrl_1.endGameCtrl)(socket, user)); socket.on(config_1.default.EVT.END_GAME, () => (0, gameCtrl_1.endGameCtrl)(user));
socket.on("disconnect", () => __awaiter(this, void 0, void 0, function* () { return yield onDisconnect(socket, user); })); socket.on("disconnect", () => __awaiter(this, void 0, void 0, function* () { return yield onDisconnect(socket, user); }));
} }
catch (error) { catch (error) {
......
...@@ -4,8 +4,6 @@ import { endGameApiCtrl, startGameApiCtrl } from "./networkCtrl"; ...@@ -4,8 +4,6 @@ import { endGameApiCtrl, startGameApiCtrl } from "./networkCtrl";
import v2 from "../Model/v2"; import v2 from "../Model/v2";
import CONFIG from "../Config/config"; import CONFIG from "../Config/config";
// const users: Map<string, User> = new Map<string, User>();
const Y_RADIO: number = 0.5560472; const Y_RADIO: number = 0.5560472;
const HALF_SIZE_TOWER: number = 56.43580423808985; const HALF_SIZE_TOWER: number = 56.43580423808985;
const INIT_SPEED: number = 300; const INIT_SPEED: number = 300;
...@@ -14,7 +12,7 @@ const DT: number = 0.016; ...@@ -14,7 +12,7 @@ const DT: number = 0.016;
const MAX_TOWER: number = 50; const MAX_TOWER: number = 50;
const RATE_BLACK_LIST = 0.15; const RATE_BLACK_LIST = 0.15;
export async function endGameCtrl(socket: Socket, user: User) { export async function endGameCtrl(user: User) {
try { try {
await endGameApiCtrl(user); await endGameApiCtrl(user);
user.reset(); user.reset();
...@@ -135,7 +133,6 @@ export async function passTowerCtrl(socket: Socket, data: IRequestPassTower, use ...@@ -135,7 +133,6 @@ export async function passTowerCtrl(socket: Socket, data: IRequestPassTower, use
} else { } else {
socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history); socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history);
await endGameApiCtrl(user); await endGameApiCtrl(user);
user.reset();
} }
} catch (error) { } catch (error) {
console.log('error', error) console.log('error', error)
...@@ -162,6 +159,7 @@ export async function addBlockCtrl(socket: Socket, user: User) { ...@@ -162,6 +159,7 @@ export async function addBlockCtrl(socket: Socket, user: User) {
direction: user.direction, direction: user.direction,
towerNumber: user.towerNumber, towerNumber: user.towerNumber,
}; };
user.timeStart = Date.now(); user.timeStart = Date.now();
socket.emit(CONFIG.EVT.REQUEST_SPAWN_TOWER, data); socket.emit(CONFIG.EVT.REQUEST_SPAWN_TOWER, data);
} catch (error) { } catch (error) {
......
...@@ -60,7 +60,7 @@ export async function endGameApiCtrl(user: User) { ...@@ -60,7 +60,7 @@ export async function endGameApiCtrl(user: User) {
async function callApi(url: string, method: string, data: any, token: string) { async function callApi(url: string, method: string, data: any, token: string) {
try { try {
const body = JSON.stringify(data); const body = JSON.stringify(data);
let res = await fetch(url, { const res = await fetch(url, {
method: method, method: method,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...@@ -69,6 +69,9 @@ async function callApi(url: string, method: string, data: any, token: string) { ...@@ -69,6 +69,9 @@ async function callApi(url: string, method: string, data: any, token: string) {
}, },
body, body,
}); });
if (res.status > 200) {
console.log('error callApi: ', res, 'data: ', data);
}
const result = await res.json(); const result = await res.json();
return result?.code == 'success'; return result?.code == 'success';
} catch (error) { } catch (error) {
......
const { io } = require("socket.io-client");
const URL = "https://dev.gamee.vn";
// const URL = "http://127.0.0.1:6636";
const PATH = '/socketws/socket.io';
const CLIENTS = 1026;
const clients = [];
let tokenTest = 'eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MTI1LCJ1c2VybmFtZSI6IlFIU18wMUpEVkZHOE5DUEdBNEtFRUhLWDA4RUJWOCIsImRpc3BsYXlOYW1lIjoiVFJBTiBEVUMgS0hBTkgiLCJlbmFibGVkM'
+ 'kZBIjpmYWxzZSwibGFzdExvZ2luVGltZSI6IjIwMjUtMDgtMDhUMTQ6MTc6MDcuNzczMTEyMjUzIiwicmVmZXJlbmNlQ29kZSI6IklRVEtUQzRkbGk4NiIsImdlbmRlciI6Ik1BTEUiLCJ2dG1BY2NvdW50'
+ 'SWQiOiIwMUpEVkZHOE5DUEdBNEtFRUhLWDA4RUJWOCIsImRvYiI6IjE5OTktMDItMTciLCJjb2RlIjoiMTIzNDU2NzYiLCJzdWIiOiIxMjUiLCJpYXQiOjE3NTQ2Mzc0MjcsImV4cCI6MTc2MzI3NzQyN30.'
+ 'Wf7PDX2rgqRPajkTL8oHHeYFwZrLpTKmuBO8Va9OGMs';
for (let i = 0; i < CLIENTS; i++) {
const random = Math.floor(Math.random() * 1000000000000000);
const socket = io(URL, {
path: PATH,
transports: ["websocket"],
query: { userId: random, token: tokenTest },
reconnection: false
});
socket.on("connect", () => {
console.log(`✅ Client ${i} connected: ${socket.id}`);
setTimeout(() => {
socket.emit('200');
}, 3000);
// // test gửi sự kiện
// socket.emit("ping", { msg: "hello from client " + i });
});
socket.on("600", (data) => {
console.log(`Client ${i} start game`, data);
});
socket.on("602", (data) => {
console.log(`Client ${i} spawn tower`, data.towerNumber);
});
socket.on("connect_error", (err) => {
console.error(`❌ Client ${i} connect error:`, err.message);
});
clients.push(socket);
}
console.log(`🚀 ${CLIENTS} clients are running...`);
\ No newline at end of file
...@@ -10,7 +10,6 @@ export const users: Map<string, User> = new Map<string, User>(); ...@@ -10,7 +10,6 @@ export const users: Map<string, User> = new Map<string, User>();
const TIME_EX = 24 * 60 * 60; const TIME_EX = 24 * 60 * 60;
export async function setupSocket(io: Server) { export async function setupSocket(io: Server) {
io.use(async (socket: Socket, next) => { io.use(async (socket: Socket, next) => {
try { try {
const token = String(socket.handshake.query.token); const token = String(socket.handshake.query.token);
...@@ -52,7 +51,7 @@ export async function setupSocket(io: Server) { ...@@ -52,7 +51,7 @@ export async function setupSocket(io: Server) {
socket.on(CONFIG.EVT.START_GAME, (data: string) => startGameCtrl(socket, data, user)); socket.on(CONFIG.EVT.START_GAME, (data: string) => startGameCtrl(socket, data, user));
socket.on(CONFIG.EVT.PASS_TOWER, async (data: IRequestPassTower) => await passTowerCtrl(socket, data, user)); socket.on(CONFIG.EVT.PASS_TOWER, async (data: IRequestPassTower) => await passTowerCtrl(socket, data, user));
socket.on(CONFIG.EVT.HISTORY, async () => await getHistory(socket, user)); socket.on(CONFIG.EVT.HISTORY, async () => await getHistory(socket, user));
socket.on(CONFIG.EVT.END_GAME, () => endGameCtrl(socket, user)); socket.on(CONFIG.EVT.END_GAME, () => endGameCtrl(user));
socket.on("disconnect", async () => await onDisconnect(socket, user)); socket.on("disconnect", async () => await onDisconnect(socket, user));
} catch (error) { } catch (error) {
......
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