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

update try catch, get user by userId

parent 0ab5c370
...@@ -25,36 +25,47 @@ let matchId = ''; ...@@ -25,36 +25,47 @@ let matchId = '';
let eventId = null; let eventId = null;
function startGameApi(data) { function startGameApi(data) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
timeStart = new Date().getTime(); try {
userId = (Math.random() * 100 >> 0) + 10; timeStart = new Date().getTime();
matchId = `${userId}-${GAME_CODE}-${timeStart}`; userId = (Math.random() * 100 >> 0) + 10;
data = { matchId = `${userId}-${GAME_CODE}-${timeStart}`;
matchId, data = {
gameCode: GAME_CODE, matchId,
}; gameCode: GAME_CODE,
const result = yield callApi(URL_START_GAME, 'POST', data); };
return result; const result = yield callApi(URL_START_GAME, 'POST', data);
return result;
}
catch (error) {
console.log('error', error);
}
}); });
} }
function endGameApi(user) { function endGameApi(user) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const playedSeconds = (new Date().getTime() - timeStart) / 1e3; try {
const details = user.history.map(h => ({ const playedSeconds = (new Date().getTime() - timeStart) / 1e3;
level: h.tower, const details = user.history.map(h => ({
score: h.score, level: h.tower,
timeStart: h.timeStart, score: h.score,
timePlayed: h.timePlayed / 1e3, timeStart: h.timeStart,
})); timePlayed: h.timePlayed / 1e3,
const params = { }));
matchId, const params = {
gameCode: GAME_CODE, matchId,
eventId, gameCode: GAME_CODE,
playedSeconds, eventId,
score: user.totalScore, playedSeconds,
gameLevel: user.towerNumber - 1, score: user.totalScore,
details gameLevel: user.towerNumber - 1,
}; details
const res = yield callApi(URL_END_GAME, 'POST', params); };
const res = yield callApi(URL_END_GAME, 'POST', params);
return res;
}
catch (error) {
console.log('error', error);
}
}); });
} }
function callApi(url, method, data) { function callApi(url, method, data) {
...@@ -69,11 +80,10 @@ function callApi(url, method, data) { ...@@ -69,11 +80,10 @@ function callApi(url, method, data) {
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
const result = yield res.json(); const result = yield res.json();
console.log(url, result.code, result.message);
return (result === null || result === void 0 ? void 0 : result.code) == 'success'; return (result === null || result === void 0 ? void 0 : result.code) == 'success';
} }
catch (error) { catch (error) {
console.log('error call api: ', error); console.log('error callApi: ', error);
} }
}); });
} }
...@@ -5,7 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) { ...@@ -5,7 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const v2_1 = __importDefault(require("./v2")); const v2_1 = __importDefault(require("./v2"));
class User { class User {
constructor(token) { constructor(token, id) {
this.id = '';
this.token = ''; this.token = '';
this.towerNumber = 0; this.towerNumber = 0;
this.direction = 1; this.direction = 1;
...@@ -21,6 +22,7 @@ class User { ...@@ -21,6 +22,7 @@ class User {
this.isEndGame = true; this.isEndGame = true;
this.history = []; this.history = [];
this.token = token; this.token = token;
this.id = id;
this.reset(); this.reset();
} }
reset() { reset() {
......
...@@ -21,3 +21,9 @@ const io = new socket_io_1.Server(server, { ...@@ -21,3 +21,9 @@ const io = new socket_io_1.Server(server, {
server.listen(PORT, () => { server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}!`); console.log(`Server is running on port ${PORT}!`);
}); });
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
This diff is collapsed.
...@@ -15,38 +15,47 @@ let matchId = ''; ...@@ -15,38 +15,47 @@ let matchId = '';
let eventId: string | null = null; let eventId: string | null = null;
export async function startGameApi(data: any) { export async function startGameApi(data: any) {
timeStart = new Date().getTime(); try {
userId = (Math.random() * 100 >> 0) + 10; timeStart = new Date().getTime();
matchId = `${userId}-${GAME_CODE}-${timeStart}`; userId = (Math.random() * 100 >> 0) + 10;
matchId = `${userId}-${GAME_CODE}-${timeStart}`;
data = { data = {
matchId, matchId,
gameCode: GAME_CODE, gameCode: GAME_CODE,
}; };
const result = await callApi(URL_START_GAME, 'POST', data); const result = await callApi(URL_START_GAME, 'POST', data);
return result; return result;
} catch (error) {
console.log('error', error)
}
} }
export async function endGameApi(user: User) { export async function endGameApi(user: User) {
const playedSeconds = (new Date().getTime() - timeStart) / 1e3; try {
const details = user.history.map(h=>({ const playedSeconds = (new Date().getTime() - timeStart) / 1e3;
const details = user.history.map(h => ({
level: h.tower, level: h.tower,
score: h.score, score: h.score,
timeStart: h.timeStart, timeStart: h.timeStart,
timePlayed: h.timePlayed / 1e3, timePlayed: h.timePlayed / 1e3,
})); }));
const params = { const params = {
matchId, matchId,
gameCode: GAME_CODE, gameCode: GAME_CODE,
eventId, eventId,
playedSeconds, playedSeconds,
score: user.totalScore, score: user.totalScore,
gameLevel: user.towerNumber - 1, gameLevel: user.towerNumber - 1,
details details
} }
const res = await callApi(URL_END_GAME, 'POST', params); const res = await callApi(URL_END_GAME, 'POST', params);
return res;
} catch (error) {
console.log('error', error)
}
} }
async function callApi(url: string, method: string, data: any) { async function callApi(url: string, method: string, data: any) {
...@@ -60,10 +69,9 @@ async function callApi(url: string, method: string, data: any) { ...@@ -60,10 +69,9 @@ async function callApi(url: string, method: string, data: any) {
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
const result = await res.json(); const result = await res.json();
console.log(url, result.code, result.message);
return result?.code == 'success'; return result?.code == 'success';
} catch (error) { } catch (error) {
console.log('error call api: ', error); console.log('error callApi: ', error);
} }
} }
import v2 from "./v2"; import v2 from "./v2";
class User { class User {
public id: string = '';
public token: string = ''; public token: string = '';
public towerNumber: number = 0; public towerNumber: number = 0;
public direction: number = 1; public direction: number = 1;
...@@ -10,17 +11,18 @@ class User { ...@@ -10,17 +11,18 @@ class User {
public totalScore: number = 0; public totalScore: number = 0;
public combo: number = 0; public combo: number = 0;
public curBlock: v2 = new v2(0,0); public curBlock: v2 = new v2(0, 0);
public nextBlock: v2 = new v2(0,0); public nextBlock: v2 = new v2(0, 0);
public screenPos: v2 = new v2(0,0); public screenPos: v2 = new v2(0, 0);
public position: v2 = new v2(0,0); public position: v2 = new v2(0, 0);
public timeStart: number = 0; public timeStart: number = 0;
public isEndGame: boolean = true; public isEndGame: boolean = true;
public history: History[] = []; public history: History[] = [];
constructor(token: string) { constructor(token: string, id: string) {
this.token = token; this.token = token;
this.id = id;
this.reset(); this.reset();
} }
...@@ -61,14 +63,6 @@ export interface IDataPassTower { ...@@ -61,14 +63,6 @@ export interface IDataPassTower {
totalScore: number, totalScore: number,
} }
// {
// nextBlock: user.nextBlock,
// screenPos: user.screenPos,
// direction: user.direction,
// distance2Tower: user.distance2Tower,
// towerNumber: user.towerNumber - 1,
// };
export interface IDataSpawnTower { export interface IDataSpawnTower {
nextBlock: v2, nextBlock: v2,
screenPos: v2, screenPos: v2,
...@@ -77,4 +71,9 @@ export interface IDataSpawnTower { ...@@ -77,4 +71,9 @@ export interface IDataSpawnTower {
towerNumber: number, towerNumber: number,
} }
export interface IQuery {
token?: string,
userId?: string
}
export default User; export default User;
\ No newline at end of file
...@@ -20,4 +20,12 @@ setupSocket(io); ...@@ -20,4 +20,12 @@ setupSocket(io);
server.listen(PORT, () => { server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}!`); console.log(`Server is running on port ${PORT}!`);
}) });
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
This diff is collapsed.
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