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