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

add end game, update heightJump

parent c7be7051
Pipeline #7135 canceled with stages
...@@ -10,10 +10,12 @@ const io = new Server(server, { ...@@ -10,10 +10,12 @@ const io = new Server(server, {
cors: { cors: {
origin: '*', origin: '*',
methods: ["GET", "POST"], methods: ["GET", "POST"],
credentials: true // origin: ["http://localhost:7456"],
// credentials: true,
} }
}); });
setupSocket(io); setupSocket(io);
server.listen(PORT, () => { server.listen(PORT, () => {
......
import { Server, Socket } from "socket.io"; import { Server, Socket } from "socket.io";
import CONFIG from "./Config/config"; // import CONFIG from "./Config/config";
import User, { IDataPassTower, IDataSpawnTower } from "./Model/User"; import User, { IDataPassTower, IDataSpawnTower } 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";
...@@ -7,7 +7,19 @@ import { endGameApi, startGameApi } from "./Controller/networkCtrl"; ...@@ -7,7 +7,19 @@ import { endGameApi, startGameApi } from "./Controller/networkCtrl";
const users: Map<string, User> = new Map<string, User>(); const users: Map<string, User> = new Map<string, User>();
const Y_RADIO: number = 0.5560472; const Y_RADIO: number = 0.5560472;
const HALF_SIZE_TOWER: number = 56.43580423808985; const HALF_SIZE_TOWER: number = 56.43580423808985;
const CONFIG = {
EVT: {
START_GAME: '200',
END_GAME: '204',
SPAWN_ITEM: '205',
PASS_TOWER: '207',
HISTORY: '208',
REQUEST_START_GAME: '600',
REQUEST_PASS_TOWER: '601',
REQUEST_SPAWN_TOWER: '602',
REQUEST_HISTORY: '603',
}
};
export function setupSocket(io: Server) { export function setupSocket(io: Server) {
io.use((socket: Socket, next) => { io.use((socket: Socket, next) => {
...@@ -28,12 +40,13 @@ export function setupSocket(io: Server) { ...@@ -28,12 +40,13 @@ export function setupSocket(io: Server) {
socket.on(CONFIG.EVT.PASS_TOWER, async (data: { playerPos: v2, time: number, distance: number }) => await passTower(socket, data)); socket.on(CONFIG.EVT.PASS_TOWER, async (data: { playerPos: v2, time: number, distance: number }) => await passTower(socket, data));
socket.on(CONFIG.EVT.HISTORY, async () => getHistory(socket)); socket.on(CONFIG.EVT.HISTORY, async () => getHistory(socket));
socket.on(CONFIG.EVT.END_GAME, () => endGame(socket));
socket.on("disconnect", () => onDisconnect(socket)); socket.on("disconnect", () => onDisconnect(socket));
}); });
} }
function onDisconnect(socket: Socket): void { async function endGame(socket: Socket) {
console.log(`🔴 Client disconnected: ${socket.id}`)
const user = getUserBySocket(socket); const user = getUserBySocket(socket);
user.reset(); user.reset();
...@@ -43,6 +56,11 @@ function onDisconnect(socket: Socket): void { ...@@ -43,6 +56,11 @@ function onDisconnect(socket: Socket): void {
endGameApi(user); endGameApi(user);
} }
function onDisconnect(socket: Socket): void {
console.log(`🔴 Client disconnected: ${socket.id}`)
endGame(socket);
}
async function getHistory(socket: Socket) { async function getHistory(socket: Socket) {
const user = getUserBySocket(socket); const user = getUserBySocket(socket);
socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history); socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history);
...@@ -64,7 +82,6 @@ async function startGame(socket: Socket, data: any) { ...@@ -64,7 +82,6 @@ async function startGame(socket: Socket, data: any) {
async function passTower(socket: Socket, data: { distance: number }) { async function passTower(socket: Socket, data: { distance: number }) {
const user = getUserBySocket(socket); const user = getUserBySocket(socket);
console.log('distance', data.distance.toFixed(), user.distance2Tower.toFixed());
const xDistance_ = data.distance; const xDistance_ = data.distance;
const { direction, nextBlock, towerNumber, position } = user; const { direction, nextBlock, towerNumber, position } = user;
...@@ -85,7 +102,7 @@ async function passTower(socket: Socket, data: { distance: number }) { ...@@ -85,7 +102,7 @@ async function passTower(socket: Socket, data: { distance: number }) {
score = 0; score = 0;
} }
if (minDistance < HALF_SIZE_TOWER * 0.8) { if (minDistance < HALF_SIZE_TOWER * 0.8) {
target = nextTowerPos.clone().sub(pointEdge.clone().mul(0.6)); target = nextTowerPos.clone().sub(pointEdge.clone().mul(0.5));
score = 1; score = 1;
} }
if (minDistance < HALF_SIZE_TOWER * 0.3) { if (minDistance < HALF_SIZE_TOWER * 0.3) {
...@@ -93,7 +110,10 @@ async function passTower(socket: Socket, data: { distance: number }) { ...@@ -93,7 +110,10 @@ async function passTower(socket: Socket, data: { distance: number }) {
score = 2; score = 2;
} }
const heightJump = nextBlock.y + 100 - position.y; // target = nextTowerPos.clone();
// score = 2;
const heightJump = user.distance2Tower * 0.7;
if (score <= 0) { if (score <= 0) {
user.isEndGame = true; user.isEndGame = true;
......
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