Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/routes/gamesession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ router.post('/v:version/joinrandom', async (req: JWTRequest, res) => {
//dorms should be private by default.
const session = GameSessions.create_session(req.body.ActivityLevelIds[0], true, parseInt(req.auth?.sub || "0"))
const gameSession = GameSessions.join_session(parseInt(req.auth?.sub as string), session)

if (!gameSession)
{
res.json({ Result: JoinResult.InsufficientSpace })
return
}

res.json({
Result: JoinResult.Success,
Expand All @@ -52,6 +58,12 @@ router.post('/v:version/joinrandom', async (req: JWTRequest, res) => {
const session = GameSessions.find_activity_id(req.body.ActivityLevelIds)
const gameSession = GameSessions.join_session(parseInt(req.auth?.sub as string), session)

if (!gameSession)
{
res.json({ Result: JoinResult.InsufficientSpace })
return
}

res.json({
Result: JoinResult.Success,
GameSession: gameSession
Expand Down Expand Up @@ -81,6 +93,12 @@ router.post('/v:version/create', async (req: JWTRequest, res) => {

const gameSession = GameSessions.join_session(parseInt(req.auth?.sub as string), session)

if (!gameSession)
{
res.json({ Result: JoinResult.InsufficientSpace })
return
}

res.json({
Result: JoinResult.Success,
GameSession: gameSession
Expand Down Expand Up @@ -111,6 +129,9 @@ router.post('/v:version/joinroomcode', async (req: JWTRequest, res) => {
targetSession = GameSessions.join_session(parseInt(req.auth?.sub as string), session)
}

if (!targetSession)
return res.json({ Result: JoinResult.InsufficientSpace })

res.json({
Result: JoinResult.Success,
GameSession: targetSession
Expand Down Expand Up @@ -154,6 +175,12 @@ router.post('/v:version/join', async (req: JWTRequest, res) => {
{
const gameSession = GameSessions.join_session(parseInt(req.auth?.sub as string), session)

if (!gameSession)
{
res.json({ Result: JoinResult.InsufficientSpace })
return
}

res.json({
Result: JoinResult.Success,
GameSession: gameSession
Expand Down
64 changes: 64 additions & 0 deletions src/sessions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { describe, expect, test } from "bun:test";
import { SessionManager } from "./sessions";

const activity = "activity-id" as RecRoomActivityLevelId;

describe("SessionManager capacity", () => {
test("reports a session as full at its configured capacity", () => {
const manager = new SessionManager(false);
const session = manager.create_session(activity, false, undefined);
session.MaxCapacity = 2;

expect(manager.join_session(1, session)).toBeDefined();
expect(manager.join_session(2, session)).toBeDefined();

expect(session.can_join()).toBe(false);
expect(session.get_rec_room_data().IsFull).toBe(true);
});

test("rejects a new player without disconnecting their current session", () => {
const manager = new SessionManager(false);
const current = manager.create_session(activity, false, undefined);
const full = manager.create_session(activity, false, undefined);
full.MaxCapacity = 1;

expect(manager.join_session(10, current)).toBeDefined();
expect(manager.join_session(20, full)).toBeDefined();
expect(manager.join_session(10, full)).toBeUndefined();

expect(current.players).toEqual([10]);
expect(full.players).toEqual([20]);
});

test("rejoining the same session does not duplicate the player", () => {
const manager = new SessionManager(false);
const session = manager.create_session(activity, false, undefined);

manager.join_session(7, session);
manager.join_session(7, session);

expect(session.players).toEqual([7]);
});

test("random matchmaking creates another session when the match is full", () => {
const manager = new SessionManager(false);
const full = manager.create_session(activity, false, undefined);
full.MaxCapacity = 1;
manager.join_session(1, full);

const available = manager.find_activity_id(activity);

expect(available).not.toBe(full);
expect(manager.sessions).toHaveLength(2);
});

test("disconnect removes every stale duplicate", () => {
const manager = new SessionManager(false);
const session = manager.create_session(activity, false, undefined);
session.players = [3, 3, 4];

manager.disconnect_player(3);

expect(session.players).toEqual([4]);
});
});
28 changes: 16 additions & 12 deletions src/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ export class SessionManager
{
sessions: RecNetGameSession[] = []

constructor()
constructor(start_purge_timer: boolean = true)
{
setInterval(() => this.__purge(), 300000) // purge inactive sessions every 5 minutes
if (start_purge_timer)
setInterval(() => this.__purge(), 300000) // purge inactive sessions every 5 minutes
}

public disconnect_player(player_id: number)
{
this.sessions.forEach(element => {
let _c = element.players.indexOf(player_id)
if (_c !== -1)
element.players.splice(_c, 1)
this.sessions.forEach(session => {
session.players = session.players.filter(id => id !== player_id)
});
}

Expand Down Expand Up @@ -44,10 +43,15 @@ export class SessionManager
return session
}

public join_session(player_id: number, session: RecNetGameSession)
public join_session(player_id: number, session: RecNetGameSession): GameSession | undefined
{
this.disconnect_player(player_id);
if (session.players.includes(player_id))
return session.get_rec_room_data()

if (!session.can_join())
return undefined

this.disconnect_player(player_id);
session.players.push(player_id)

return session.get_rec_room_data()
Expand All @@ -62,7 +66,7 @@ export class SessionManager
}
}

class RecNetGameSession
export class RecNetGameSession
{
id: number;
players: number[] = [];
Expand Down Expand Up @@ -96,13 +100,13 @@ class RecNetGameSession
Private: this.is_private,
GameInProgress: this.game_in_progress,
MaxCapacity: this.MaxCapacity,
IsFull: false
IsFull: !this.can_join()
}
return data;
}

public can_join()
{
return true;
return this.players.length < this.MaxCapacity;
}
}
}