Skip to content

Commit

Permalink
store currentSession in file instead
Browse files Browse the repository at this point in the history
  • Loading branch information
b4iterdev committed Feb 7, 2025
1 parent f357ea8 commit 16244cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/out-tsc
/bazel-out
/result
/sessions
# Node
/node_modules
npm-debug.log
Expand Down
44 changes: 32 additions & 12 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ interface ValorantApiResponse {
data: ValorantMap[];
}

const sessionsDir = path.join(__dirname, '../../sessions');
const resultDir = path.join(__dirname, '../../result');

if (!fs.existsSync(resultDir)) {
fs.mkdirSync(resultDir, { recursive: true });
}

if (!fs.existsSync(sessionsDir)) {
fs.mkdirSync(sessionsDir, { recursive: true });
}

async function getMapSplashByName(mapName: string): Promise<string | null> {
try {
const response = await fetch('https://valorant-api.com/v1/maps');
Expand Down Expand Up @@ -88,22 +93,34 @@ async function initializeMapStates(mapList: string[]): Promise<MapState[]> {
return mapStates;
}

function saveFinishedSession(session: Session) {
// Save session to file
const filePath = path.join(resultDir, `${session.id}.json`);
function saveSession(session: Session) {
const filePath = path.join(sessionsDir, `${session.id}.json`);
fs.writeFileSync(filePath, JSON.stringify(session, null, 2));
}

function getSession(sessionId: string): Session | null {
const filePath = path.join(sessionsDir, `${sessionId}.json`);
if (fs.existsSync(filePath)) {
const data = fs.readFileSync(filePath, 'utf8');
return JSON.parse(data);
}
return null;
}

// Remove session from memory
sessions.delete(session.id);
function deleteSession(sessionId: string) {
const filePath = path.join(sessionsDir, `${sessionId}.json`);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
}


const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: { origin: '*' },
});

const sessions = new Map<string, Session>();
// Socket event handlers
io.on('connection', (socket) => {
console.log('Client connected');
Expand All @@ -120,15 +137,15 @@ io.on('connection', (socket) => {
vetoOrder,
finished: false,
};
sessions.set(session.id, session);
session.mapStates = await initializeMapStates(mapList);
saveSession(session);
console.log('Session created:', session);
socket.emit('sessionCreated', session);
},
);

socket.on('getSession', (sessionId: string) => {
const session = sessions.get(sessionId);
const session = getSession(sessionId);
if (session) {
socket.emit('sessionData', session);
} else {
Expand All @@ -137,19 +154,22 @@ io.on('connection', (socket) => {
});

socket.on('updateMapStates', ({ sessionId, mapStates }) => {
const session = sessions.get(sessionId);
const session = getSession(sessionId);
if (session) {
session.mapStates = mapStates;
sessions.set(sessionId, session);
saveSession(session);
io.emit('mapStatesUpdated', session);
console.log('Map states updated:', mapStates);
}
});
socket.on('finishSession', (sessionId: string) => {
const session = sessions.get(sessionId);
const session = getSession(sessionId);
if (session) {
session.finished = true;
saveFinishedSession(session);
// Move to result directory instead of sessions
const resultPath = path.join(resultDir, `${session.id}.json`);
fs.writeFileSync(resultPath, JSON.stringify(session, null, 2));
deleteSession(sessionId); // Remove from sessions directory
console.log('Session finished:', sessionId);
}
});
Expand Down

0 comments on commit 16244cf

Please sign in to comment.