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

update map users, reset user

parent ef9542ce
......@@ -33,6 +33,11 @@ const CONFIG = {
RECONNECT: '500',
REQUSET_START_GAME: '600',
REQUSET_PASS_TOWER: '601',
REQUSET_SPAWN_TOWER: '602',
REQUSET_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) {
......
......@@ -21,9 +21,27 @@ class User {
constructor(token: string) {
this.token = token;
}
public reset() {
this.score = 0;
this.totalScore = 0;
this.combo = 0;
this.isEndGame = true;
this.history = [];
this.timeStart = 0;
this.towerNumber = 0;
this.direction = 0;
this.distance2Tower = 0;
this.curBlock.x = 0;
this.curBlock.y = 0;
this.nextBlock.x = 0;
this.nextBlock.y = 0;
this.screenPos.x = 0;
this.screenPos.y = 0;
}
}
class History{
class History {
public timeStart: number = 0;
public timePlayed: number = 0;
public totalScore: number = 0;
......
......@@ -4,24 +4,22 @@ import User from "./Model/User";
import v2 from "./Model/v2";
import { endGameApi, startGameApi } from "./Controller/networkCtrl";
const users: { [key: string]: User } = {};
const yRadio: number = 0.5560472;
const distanceBlock: number = 56.43580423808985;
const users: Map<string, User> = new Map<string, User>();
const Y_RATIO: number = 0.5560472;
const DISTANCE_BLOCK: number = 56.43580423808985;
export function setupSocket(io: Server) {
io.use((socket: Socket, next) => {
const token = socket.handshake.query.token;
console.log('token', token)
if (!users[socket.id]) {
users[socket.id] = new User(String(token));
const token = String(socket.handshake.query.token);
if (!users.has(token)) {
users.set(token, new User(token));
}
next();
});
io.on("connection", (socket: Socket) => {
console.log(`🟢 Client connected: ${socket.id}`);
socket.on(CONFIG.EVT.JOIN_ROOM, (data: string) => joinRoom(socket, data));
socket.on(CONFIG.EVT.START_GAME, (data: string) => startGame(socket, data));
socket.on(CONFIG.EVT.PASS_TOWER, async (data: { nextPos: v2, time: number, distance: number }) => await passTower(socket, data));
......@@ -32,8 +30,8 @@ export function setupSocket(io: Server) {
}
function onDisconnect(socket: Socket): void {
const user = getUserBySocketId(socket.id);
if (user.isEndGame) {
const user = getUserBySocket(socket);
if (user?.isEndGame) {
return;
}
endGameApi(user);
......@@ -42,38 +40,41 @@ function onDisconnect(socket: Socket): void {
}
async function getHistory(socket: Socket) {
const user = getUserBySocketId(socket.id);
socket.emit('requestHistory', user.history);
const user = getUserBySocket(socket);
socket.emit(CONFIG.EVT.REQUSET_HISTORY, user.history);
}
async function startGame(socket: Socket, data: any) {
const user = getUserBySocketId(socket.id);
const user = getUserBySocket(socket);
const result = await startGameApi(data);
if (result) {
user.isEndGame = false;
addBlock(socket);
} else {
console.log('start game fail');
socket.emit('requestStartGame', { code: 9999 });
socket.emit(CONFIG.EVT.REQUSET_START_GAME, { code: 9999 });
}
}
async function passTower(socket: Socket, data: { nextPos: v2, time: number, distance: number }) {
const user = getUserBySocketId(socket.id);
// console.log('data', data);
async function passTower(socket: Socket, data: { nextPos: v2, distance: number }) {
const user = getUserBySocket(socket);
const lastDis = user.curBlock.distanceTo(data.nextPos);
let score = 0
// const lastDis = user.curBlock.distanceTo(data.nextPos);
// const distance = Math.abs(lastDis - user.distance2Tower);
let score = 0
const distance = Math.abs(data.distance - user.distance2Tower);
if (distance < distanceBlock) {
if (distance < DISTANCE_BLOCK) {
score = 1;
}
if (distance < distanceBlock / 3) {
if (distance < DISTANCE_BLOCK / 3) {
score = 2;
}
if (user.towerNumber > 2) {
score = 0;//test
}
if (score == 0) {
user.isEndGame = true;
}
......@@ -85,10 +86,6 @@ async function passTower(socket: Socket, data: { nextPos: v2, time: number, dist
user.combo = 0;
}
if (user.towerNumber > 2) {
score = 0;//test
}
user.totalScore += score;
const dataSend = {
totalScore: user.totalScore,
......@@ -104,21 +101,24 @@ async function passTower(socket: Socket, data: { nextPos: v2, time: number, dist
user.history.unshift(history);
socket.emit(CONFIG.EVT.REQUSET_PASS_TOWER, dataSend);
if (score != 0) {
addBlock(socket);
} else {
endGameApi(user);
user.totalScore = 0;
socket.emit(CONFIG.EVT.REQUSET_HISTORY, user.history);
await endGameApi(user);
user.reset();
socket.emit(CONFIG.EVT.REQUSET_PASS_TOWER, 'END');
console.log('END')
}
socket.emit('requestPassTower', dataSend);
}
async function addBlock(socket: Socket) {
const user = getUserBySocketId(socket.id);
const user = getUserBySocket(socket);
const towerNumber = ++user.towerNumber;
const xDistance = ((Math.min(towerNumber, 100) / 100 + 1) + Math.random() * (towerNumber <= 10 ? 0.5 : 1)) * 150;
const yDistance = xDistance * yRadio;
const yDistance = xDistance * Y_RATIO;
user.curBlock = new v2(user.nextBlock.x, user.nextBlock.y);
user.direction = (Math.random() < 0.5) ? -1 : 1;
......@@ -135,15 +135,10 @@ async function addBlock(socket: Socket) {
towerNumber: user.towerNumber - 1,
};
user.timeStart = Date.now();
socket.emit('requestSpawnTower', data);
socket.emit(CONFIG.EVT.REQUSET_SPAWN_TOWER, data);
}
function joinRoom(socket: Socket, data: string) {
const { roomName, id } = JSON.parse(data);
console.log(`JOIN_ROOM ${roomName}: ${id}`);
}
function getUserBySocketId(socketId: string) {
return users[socketId];
function getUserBySocket(socket: Socket) {
const token = String(socket.handshake.query.token);
return users.get(token)!;
}
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