From 41f8caf8029dbd0097f056f24a097ba057122ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rivi=C3=A8re?= Date: Mon, 9 Jun 2025 01:35:39 +0200 Subject: [PATCH] Logs de la partie --- traque-back/Dockerfile | 2 ++ traque-back/game.js | 5 ++- traque-back/index.js | 2 ++ traque-back/trajectory.js | 65 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 traque-back/trajectory.js diff --git a/traque-back/Dockerfile b/traque-back/Dockerfile index 535f9f1..ca5e449 100644 --- a/traque-back/Dockerfile +++ b/traque-back/Dockerfile @@ -15,6 +15,8 @@ COPY . . RUN if [ ! -d uploads ]; then mkdir uploads; fi +RUN if [ ! -d trajectories ]; then mkdir trajectories; fi + EXPOSE 3001 # Start the application diff --git a/traque-back/game.js b/traque-back/game.js index 6fe5b74..5f54088 100644 --- a/traque-back/game.js +++ b/traque-back/game.js @@ -9,6 +9,7 @@ import penaltyController from "./penalty_controller.js"; import zoneManager from "./zone_manager.js"; import { getDistanceFromLatLon } from "./map_utils.js"; +import { writePosition, writeCapture, writeSeePosition } from "./trajectory.js"; /** * The possible states of the game @@ -237,6 +238,7 @@ export default { if (location == null) { return false; } + writePosition(Date.now(), teamId, location[0], location[1]); team.currentLocation = location; //Update the team ready status if they are in their starting area if (this.state == GameState.PLACEMENT && team.startingArea && team.startingArea && location) { @@ -274,7 +276,7 @@ export default { if (team.currentLocation == null) { return false; } - + writeSeePosition(Date.now(), teamId, team.chasing); team.locationSendDeadline = Number(new Date()) + penaltyController.settings.allowedTimeBetweenPositionUpdate * 60 * 1000; team.lastSentLocation = team.currentLocation; if (this.getTeam(team.chasing) != null) { @@ -309,6 +311,7 @@ export default { requestCapture(teamId, captureCode) { let enemyTeam = this.getTeam(this.getTeam(teamId).chasing) if (enemyTeam && enemyTeam.captureCode == captureCode) { + writeCapture(Date.now(), teamId, enemyTeam.id); this.capture(enemyTeam.id); this.updateTeamChasing(); return true; diff --git a/traque-back/index.js b/traque-back/index.js index 358435d..189e09c 100644 --- a/traque-back/index.js +++ b/traque-back/index.js @@ -7,6 +7,7 @@ import { readFileSync } from "fs"; import { initAdminSocketHandler } from "./admin_socket.js"; import { initTeamSocket } from "./team_socket.js"; import { initPhotoUpload } from "./photo.js"; +import { initTrajectories } from "./trajectory.js"; //extract admin password from .env file config(); const HOST = process.env.HOST; @@ -33,3 +34,4 @@ export const io = new Server(httpServer, { initAdminSocketHandler(); initTeamSocket(); initPhotoUpload(); +initTrajectories(); diff --git a/traque-back/trajectory.js b/traque-back/trajectory.js new file mode 100644 index 0000000..6aa0ab9 --- /dev/null +++ b/traque-back/trajectory.js @@ -0,0 +1,65 @@ +import fs from "fs"; +import path from "path"; +const UPLOAD_DIR = "trajectories"; +const EXTENSION = "txt"; + +// Useful functions + +function teamIDToPath(teamID) { + return path.join(UPLOAD_DIR, teamID + "." + EXTENSION); +} + +function dataToLine(...data) { + return data.join(','); +} + +const errorFile = (err) => { + if (err) console.error("Error appending to file:", err); +}; + +function addLineToFile(teamID, line) { + // Insert the line in the file of teamID depending on the date (lines are sorted by date) + if (!fs.existsSync(teamIDToPath(teamID))) { + fs.writeFile(teamIDToPath(teamID), line + '\n', errorFile); + } else { + fs.readFile(teamIDToPath(teamID), 'utf8', (err, data) => { + if (err) { + errorFile(err); + return; + } + let lines = data.trim().split('\n'); + const newDate = parseInt(line.split(',')[0], 10); + let insertIndex = lines.length; + for (let i = lines.length - 1; i >= 0; i--) { + const date = parseInt(lines[i].split(',')[0], 10); + if (date <= newDate) { + insertIndex = i + 1; + break; + } + } + lines.splice(insertIndex, 0, line); + fs.writeFile(teamIDToPath(teamID), lines.join('\n') + '\n', errorFile); + }); + } +} + +// Export functions + +export async function initTrajectories() { + const files = fs.readdirSync(UPLOAD_DIR); + for (const file of files) fs.unlinkSync(path.join(UPLOAD_DIR, file)); +} + +export function writePosition(date, teamID, lon, lat) { + addLineToFile(teamID, dataToLine(date, "position", lon, lat)); +} + +export function writeCapture(date, teamID, capturedTeamID) { + addLineToFile(teamID, dataToLine(date, "capture", capturedTeamID)); + addLineToFile(capturedTeamID, dataToLine(date, "captured", teamID)); +} + +export function writeSeePosition(date, teamID, seenTeamID) { + addLineToFile(teamID, dataToLine(date, "see")); + addLineToFile(seenTeamID, dataToLine(date, "seen")); +}