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

update config

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