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

change git -> dist

parent 6246cb5b
Pipeline #7136 failed with stages
......@@ -8,7 +8,7 @@
/build/
node_modules/
dist/
# dist/
build/
library/
temp/
......
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const CONFIG = {
GAMEID: {
HUNGQUA: 1000,
},
STATE: {
WATING: 'WATING',
PLAYING: 'PLAYING',
END: 'END',
SELECT: 'SELECT',
},
TIME: {
COUNTDOWN: 10,
SELECT_PLAYER: 5,
PLAY: 60
},
EVT: {
CREATE_ROOM: '100',
JOIN_ROOM: '101',
LEAVE_ROOM: '103',
ROOM_MESSAGE: '104',
START_GAME: '200',
UPDATE_STATE: '201',
UPDATE_SCORE: '202',
UPDATE_TIME_REMAINING: '203',
END_GAME: '204',
SPAWN_TOWER: '205',
CLOSE_QR_VIEW: '206',
PASS_TOWER: '207',
HISTORY: '208',
GAME_MESSAGE: '300',
RECONNECT: '500',
REQUEST_START_GAME: '600',
REQUEST_PASS_TOWER: '601',
REQUEST_SPAWN_TOWER: '602',
REQUEST_HISTORY: '603',
},
HTTP_RESPONSE: {
SUCCESS: {
code: 200,
message: "Success!",
},
ERROR: {
code: 500,
message: "Error!",
}
},
};
exports.default = CONFIG;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startGameApi = startGameApi;
exports.endGameApi = endGameApi;
const URL = 'https://dev.gamee.vn/api/web';
const URL_START_GAME = `${URL}/game/start`;
const URL_END_GAME = `${URL}/game/end`;
const GAME_CODE = 'flip-jump';
let token = 'eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MTI1LCJ1c2VybmFtZSI6IlFIU18wMUpEVkZHOE5DUEdBNEtFRUhLWDA4RUJWOCIsImRpc3BsYXlOYW1lIjoiVFJBTiBEVUMgS0hBTkgiLCJlbmFibGVkM'
+ 'kZBIjpmYWxzZSwibGFzdExvZ2luVGltZSI6IjIwMjUtMDgtMDhUMTQ6MTc6MDcuNzczMTEyMjUzIiwicmVmZXJlbmNlQ29kZSI6IklRVEtUQzRkbGk4NiIsImdlbmRlciI6Ik1BTEUiLCJ2dG1BY2NvdW50'
+ 'SWQiOiIwMUpEVkZHOE5DUEdBNEtFRUhLWDA4RUJWOCIsImRvYiI6IjE5OTktMDItMTciLCJjb2RlIjoiMTIzNDU2NzYiLCJzdWIiOiIxMjUiLCJpYXQiOjE3NTQ2Mzc0MjcsImV4cCI6MTc2MzI3NzQyN30.'
+ 'Wf7PDX2rgqRPajkTL8oHHeYFwZrLpTKmuBO8Va9OGMs';
let timeStart = 0;
let userId = 0;
let matchId = '';
let eventId = null;
function startGameApi(data) {
return __awaiter(this, void 0, void 0, function* () {
timeStart = new Date().getTime();
userId = (Math.random() * 100 >> 0) + 10;
matchId = `${userId}-${GAME_CODE}-${timeStart}`;
data = {
matchId,
gameCode: GAME_CODE,
};
const result = yield callApi(URL_START_GAME, 'POST', data);
return result;
});
}
function endGameApi(user) {
return __awaiter(this, void 0, void 0, function* () {
const playedSeconds = (new Date().getTime() - timeStart) / 1e3;
const details = user.history.map(h => ({
level: h.tower,
score: h.score,
timeStart: h.timeStart,
timePlayed: h.timePlayed / 1e3,
}));
const params = {
matchId,
gameCode: GAME_CODE,
eventId,
playedSeconds,
score: user.totalScore,
gameLevel: user.towerNumber - 1,
details
};
const res = yield callApi(URL_END_GAME, 'POST', params);
});
}
function callApi(url, method, data) {
return __awaiter(this, void 0, void 0, function* () {
try {
let res = yield fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify(data),
});
const result = yield res.json();
console.log(url, result.code, result.message);
return (result === null || result === void 0 ? void 0 : result.code) == 'success';
}
catch (error) {
console.log('error call api: ', error);
}
});
}
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const v2_1 = __importDefault(require("./v2"));
class User {
constructor(token) {
this.token = '';
this.towerNumber = 0;
this.direction = 1;
this.distance2Tower = 0;
this.score = 0;
this.totalScore = 0;
this.combo = 0;
this.curBlock = new v2_1.default(0, 0);
this.nextBlock = new v2_1.default(0, 0);
this.screenPos = new v2_1.default(0, 0);
this.position = new v2_1.default(0, 0);
this.timeStart = 0;
this.isEndGame = true;
this.history = [];
this.token = token;
this.reset();
}
reset() {
this.score = 0;
this.totalScore = 0;
this.combo = 0;
this.isEndGame = true;
this.history = [];
this.timeStart = 0;
this.towerNumber = 0;
this.direction = 1;
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;
this.position.x = 0;
this.position.y = 0;
}
}
class History {
constructor() {
this.timeStart = 0;
this.timePlayed = 0;
this.totalScore = 0;
this.tower = 0;
this.score = 0;
}
}
exports.default = User;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class v2 {
constructor(x, y) {
this.x = 0;
this.y = 0;
this.x = x || 0;
this.y = y || 0;
}
distanceTo(other) {
return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2));
}
add(other) {
return new v2(this.x + other.x, this.y + other.y);
}
sub(other) {
return new v2(this.x - other.x, this.y - other.y);
}
mul(s) {
return new v2(this.x * s, this.y * s);
}
normalize() {
const length = this.distanceTo(new v2(0, 0));
return new v2(this.x / length, this.y / length);
}
clone() {
return new v2(this.x, this.y);
}
mag() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}
exports.default = v2;
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const server_1 = __importDefault(require("./server"));
const http_1 = __importDefault(require("http"));
const socket_io_1 = require("socket.io");
const socket_1 = require("./socket");
const PORT = 3001;
const server = http_1.default.createServer(server_1.default);
const io = new socket_io_1.Server(server, {
cors: {
origin: '*',
methods: ["GET", "POST"],
// origin: ["http://localhost:7456"],
// credentials: true,
}
});
(0, socket_1.setupSocket)(io);
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}!`);
});
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
const app = (0, express_1.default)();
app.use((0, cors_1.default)());
app.use(express_1.default.json());
app.get('/', (req, res, next) => {
res.send('Hello World');
});
exports.default = app;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupSocket = setupSocket;
// import CONFIG from "./Config/config";
const User_1 = __importDefault(require("./Model/User"));
const v2_1 = __importDefault(require("./Model/v2"));
const networkCtrl_1 = require("./Controller/networkCtrl");
const users = new Map();
const Y_RADIO = 0.5560472;
const HALF_SIZE_TOWER = 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',
}
};
function setupSocket(io) {
io.use((socket, next) => {
const token = String(socket.handshake.query.token);
if (!token) {
return;
}
if (!users.has(token)) {
users.set(token, new User_1.default(token));
}
next();
});
io.on("connection", (socket) => {
console.log(`🟢 Client connected: ${socket.id}`);
socket.on(CONFIG.EVT.START_GAME, (data) => startGame(socket, data));
socket.on(CONFIG.EVT.PASS_TOWER, (data) => __awaiter(this, void 0, void 0, function* () { return yield passTower(socket, data); }));
socket.on(CONFIG.EVT.HISTORY, () => __awaiter(this, void 0, void 0, function* () { return getHistory(socket); }));
socket.on(CONFIG.EVT.END_GAME, () => endGame(socket));
socket.on("disconnect", () => onDisconnect(socket));
});
}
function endGame(socket) {
return __awaiter(this, void 0, void 0, function* () {
const user = getUserBySocket(socket);
user.reset();
if (user === null || user === void 0 ? void 0 : user.isEndGame) {
return;
}
(0, networkCtrl_1.endGameApi)(user);
});
}
function onDisconnect(socket) {
console.log(`🔴 Client disconnected: ${socket.id}`);
endGame(socket);
}
function getHistory(socket) {
return __awaiter(this, void 0, void 0, function* () {
const user = getUserBySocket(socket);
socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history);
});
}
function startGame(socket, data) {
return __awaiter(this, void 0, void 0, function* () {
const user = getUserBySocket(socket);
const result = yield (0, networkCtrl_1.startGameApi)(data);
// const result = true;
if (result) {
socket.emit(CONFIG.EVT.REQUEST_START_GAME, true);
user.isEndGame = false;
addBlock(socket);
}
else {
console.log('start game fail');
socket.emit(CONFIG.EVT.REQUEST_START_GAME, false);
}
});
}
function passTower(socket, data) {
return __awaiter(this, void 0, void 0, function* () {
const user = getUserBySocket(socket);
const xDistance_ = data.distance;
const { direction, nextBlock, towerNumber, position } = user;
const point = new v2_1.default(xDistance_ * direction, xDistance_ * Y_RADIO);
const nextTowerPos = nextBlock;
const distancePlayer2NextTower = nextTowerPos.clone().sub(position).mag();
const minDistance = Math.abs(xDistance_ - distancePlayer2NextTower);
const isHead = distancePlayer2NextTower > xDistance_;
let score = -1;
const pointEdge = new v2_1.default(HALF_SIZE_TOWER * direction, HALF_SIZE_TOWER * Y_RADIO);
let target = position.clone().add(point);
if (minDistance < HALF_SIZE_TOWER * 1.5) {
target = nextTowerPos.clone().sub(isHead ? pointEdge : pointEdge.clone().mul(-1));
score = 0;
}
if (minDistance < HALF_SIZE_TOWER * 0.8) {
target = nextTowerPos.clone().sub(pointEdge.clone().mul(0.5));
score = 1;
}
if (minDistance < HALF_SIZE_TOWER * 0.3) {
target = nextTowerPos.clone();
score = 2;
}
// target = nextTowerPos.clone();
// score = 2;
const heightJump = user.distance2Tower * 0.7;
if (score <= 0) {
user.isEndGame = true;
}
if (score == 2) {
user.combo = Math.min(user.combo + 1, 2);
score *= user.combo;
}
else {
user.combo = 0;
}
user.totalScore += score;
const dataSocket = {
target,
score,
isHead,
heightJump,
totalScore: user.totalScore,
};
const history = {
totalScore: user.totalScore,
tower: towerNumber,
score: Math.max(score, 0),
timeStart: user.timeStart,
timePlayed: Date.now() - user.timeStart,
};
user.history.unshift(history);
user.position = target;
socket.emit(CONFIG.EVT.REQUEST_PASS_TOWER, dataSocket);
if (score > 0) {
addBlock(socket);
}
else {
socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history);
yield (0, networkCtrl_1.endGameApi)(user);
user.reset();
console.log('END');
}
});
}
function addBlock(socket) {
return __awaiter(this, void 0, void 0, function* () {
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 * Y_RADIO;
user.curBlock = new v2_1.default(user.nextBlock.x, user.nextBlock.y);
if (towerNumber > 1) {
user.direction = (Math.random() < 0.5) ? -1 : 1;
}
user.nextBlock.x = user.curBlock.x + (xDistance * user.direction);
user.nextBlock.y = user.curBlock.y + yDistance;
user.distance2Tower = user.nextBlock.distanceTo(user.curBlock);
user.screenPos = user.nextBlock.add(user.curBlock).mul(0.5).sub(new v2_1.default(0, 100));
const data = {
nextBlock: user.nextBlock,
screenPos: user.screenPos,
direction: user.direction,
distance2Tower: user.distance2Tower,
towerNumber: user.towerNumber,
};
user.timeStart = Date.now();
socket.emit(CONFIG.EVT.REQUEST_SPAWN_TOWER, data);
});
}
function getUserBySocket(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