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

update startGame

parent df5ef839
...@@ -47,13 +47,14 @@ async function getHistory(socket: Socket) { ...@@ -47,13 +47,14 @@ async function getHistory(socket: Socket) {
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; // const result = false;
if (result) { if (result) {
socket.emit(CONFIG.EVT.REQUEST_START_GAME, true);
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.REQUEST_START_GAME, { code: 9999 }); socket.emit(CONFIG.EVT.REQUEST_START_GAME, false);
} }
} }
...@@ -67,21 +68,26 @@ async function passTower(socket: Socket, data: { nextPos: v2, distance: number } ...@@ -67,21 +68,26 @@ async function passTower(socket: Socket, data: { nextPos: v2, distance: number }
let score = 0 let score = 0
const distance = Math.abs(data.distance - user.distance2Tower); const distance = Math.abs(data.distance - user.distance2Tower);
if (distance < DISTANCE_BLOCK) { if (distance < DISTANCE_BLOCK * 0.8) {
score = 1; score = 1;
} }
if (distance < DISTANCE_BLOCK / 3) { if (distance <= DISTANCE_BLOCK / 3) {
score = 2; score = 2;
} }
if (distance > DISTANCE_BLOCK * 0.8 && distance <= DISTANCE_BLOCK * 1.2) {
score = -1;
}
// 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) 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;
...@@ -89,15 +95,21 @@ async function passTower(socket: Socket, data: { nextPos: v2, distance: number } ...@@ -89,15 +95,21 @@ async function passTower(socket: Socket, data: { nextPos: v2, distance: number }
user.combo = 0; user.combo = 0;
} }
let pointJump;
user.totalScore += score; user.totalScore += score;
const jumpDirection = (data.distance > user.distance2Tower ? 1 : -1) * user.direction;
const dataSend = { const dataSend = {
totalScore: user.totalScore, totalScore: user.totalScore,
tower: user.towerNumber, tower: user.towerNumber,
score, jumpScore: score,
jumpDirection,
pointJump
}; };
const history = { const history = {
...dataSend, ...dataSend,
score: Math.max(score, 0),
timeStart: user.timeStart, timeStart: user.timeStart,
timePlayed: Date.now() - user.timeStart, timePlayed: Date.now() - user.timeStart,
} }
...@@ -105,29 +117,30 @@ async function passTower(socket: Socket, data: { nextPos: v2, distance: number } ...@@ -105,29 +117,30 @@ async function passTower(socket: Socket, data: { nextPos: v2, distance: number }
user.history.unshift(history); user.history.unshift(history);
socket.emit(CONFIG.EVT.REQUEST_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.REQUEST_HISTORY, user.history); socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history);
await endGameApi(user); // await endGameApi(user);
user.reset(); user.reset();
console.log('END') console.log('END');
} }
} }
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) 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){ 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);
......
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