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

update config

parent e5a1641f
......@@ -34,10 +34,10 @@ const CONFIG = {
RECONNECT: '500',
REQUSET_START_GAME: '600',
REQUSET_PASS_TOWER: '601',
REQUSET_SPAWN_TOWER: '602',
REQUSET_HISTORY: '603',
REQUEST_START_GAME: '600',
REQUEST_PASS_TOWER: '601',
REQUEST_SPAWN_TOWER: '602',
REQUEST_HISTORY: '603',
},
HTTP_RESPONSE: {
SUCCESS: {
......
......@@ -46,7 +46,7 @@ export async function endGameApi(user: User) {
details
}
// const res = await callApi(URL_END_GAME, 'POST', params);
const res = await callApi(URL_END_GAME, 'POST', params);
}
async function callApi(url: string, method: string, data: any) {
......
......@@ -3,7 +3,7 @@ import v2 from "./v2";
class User {
public token: string = '';
public towerNumber: number = 0;
public direction: number = 0;
public direction: number = 1;
public distance2Tower: number = 0;
public score: number = 0;
......@@ -20,6 +20,7 @@ class User {
constructor(token: string) {
this.token = token;
this.reset();
}
public reset() {
......@@ -30,7 +31,7 @@ class User {
this.history = [];
this.timeStart = 0;
this.towerNumber = 0;
this.direction = 0;
this.direction = 1;
this.distance2Tower = 0;
this.curBlock.x = 0;
this.curBlock.y = 0;
......
......@@ -30,33 +30,35 @@ export function setupSocket(io: Server) {
}
function onDisconnect(socket: Socket): void {
console.log(`🔴 Client disconnected: ${socket.id}`)
const user = getUserBySocket(socket);
if (user?.isEndGame) {
return;
}
endGameApi(user);
user.totalScore = 0;
return console.log(`🔴 Client disconnected: ${socket.id}`);
user.reset();
}
async function getHistory(socket: Socket) {
const user = getUserBySocket(socket);
socket.emit(CONFIG.EVT.REQUSET_HISTORY, user.history);
socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history);
}
async function startGame(socket: Socket, data: any) {
const user = getUserBySocket(socket);
const result = await startGameApi(data);
// const result = true;
if (result) {
user.isEndGame = false;
addBlock(socket);
} else {
console.log('start game fail');
socket.emit(CONFIG.EVT.REQUSET_START_GAME, { code: 9999 });
socket.emit(CONFIG.EVT.REQUEST_START_GAME, { code: 9999 });
}
}
async function passTower(socket: Socket, data: { nextPos: v2, distance: number }) {
console.log('distance', data.distance)
const user = getUserBySocket(socket);
// const lastDis = user.curBlock.distanceTo(data.nextPos);
......@@ -71,14 +73,15 @@ async function passTower(socket: Socket, data: { nextPos: v2, distance: number }
if (distance < DISTANCE_BLOCK / 3) {
score = 2;
}
if (user.towerNumber > 2) {
score = 0;//test
}
// if (user.towerNumber > 2) {
// score = 0;//test
// }
if (score == 0) {
user.isEndGame = true;
}
console.log('score', score)
if (score == 2) {
user.combo = Math.min(user.combo + 1, 2);
score *= user.combo;
......@@ -101,14 +104,13 @@ async function passTower(socket: Socket, data: { nextPos: v2, distance: number }
user.history.unshift(history);
socket.emit(CONFIG.EVT.REQUSET_PASS_TOWER, dataSend);
if (score != 0) {
socket.emit(CONFIG.EVT.REQUEST_PASS_TOWER, dataSend);
if (score > 0) {
addBlock(socket);
} else {
socket.emit(CONFIG.EVT.REQUSET_HISTORY, user.history);
socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history);
await endGameApi(user);
user.reset();
socket.emit(CONFIG.EVT.REQUSET_PASS_TOWER, 'END');
console.log('END')
}
}
......@@ -116,12 +118,16 @@ async function passTower(socket: Socket, data: { nextPos: v2, distance: number }
async function addBlock(socket: Socket) {
const user = getUserBySocket(socket);
const towerNumber = ++user.towerNumber;
console.log('towerNumber', towerNumber)
const xDistance = ((Math.min(towerNumber, 100) / 100 + 1) + Math.random() * (towerNumber <= 10 ? 0.5 : 1)) * 150;
const yDistance = xDistance * Y_RATIO;
user.curBlock = new v2(user.nextBlock.x, user.nextBlock.y);
user.direction = (Math.random() < 0.5) ? -1 : 1;
if(towerNumber > 1){
user.direction = (Math.random() < 0.5) ? -1 : 1;
}
console.log('user.direction', user.direction)
user.nextBlock.x = user.curBlock.x + (xDistance * user.direction);
user.nextBlock.y = user.curBlock.y + yDistance;
user.distance2Tower = user.nextBlock.distanceTo(user.curBlock);
......@@ -135,7 +141,7 @@ async function addBlock(socket: Socket) {
towerNumber: user.towerNumber - 1,
};
user.timeStart = Date.now();
socket.emit(CONFIG.EVT.REQUSET_SPAWN_TOWER, data);
socket.emit(CONFIG.EVT.REQUEST_SPAWN_TOWER, data);
}
function getUserBySocket(socket: Socket) {
......
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