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