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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- CreateEnum
CREATE TYPE "RaceHandler" AS ENUM ('LOCAL', 'RACETIME');

-- AlterTable
ALTER TABLE "Player" ADD COLUMN "finishedAt" TIMESTAMP(3);

-- AlterTable
ALTER TABLE "Room" ADD COLUMN "finishedAt" TIMESTAMP(3),
ADD COLUMN "raceHandler" "RaceHandler",
ADD COLUMN "startedAt" TIMESTAMP(3);
29 changes: 19 additions & 10 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,23 @@ model Room {
variantId String?
exploration Boolean @default(false)
explorationStart String?
raceHandler RaceHandler?
startedAt DateTime?
finishedAt DateTime?
}

model Player {
id String @id @unique @default(cuid())
key String
user User? @relation(fields: [userId], references: [id])
room Room @relation(fields: [roomId], references: [id])
nickname String
color String @default("blue")
spectator Boolean
monitor Boolean @default(false)
roomId String
userId String?
id String @id @unique @default(cuid())
key String
user User? @relation(fields: [userId], references: [id])
room Room @relation(fields: [roomId], references: [id])
nickname String
color String @default("blue")
spectator Boolean
monitor Boolean @default(false)
roomId String
userId String?
finishedAt DateTime?

@@unique([key, roomId])
}
Expand Down Expand Up @@ -229,3 +233,8 @@ enum GenerationGlobalAdjustments {
SYNERGIZE // add an additional copy of each goal sharing a type to increase final synergy
BOARD_TYPE_MAX // enforce a maximum number of goals of a specific type in the board
}

enum RaceHandler {
LOCAL
RACETIME
}
4 changes: 4 additions & 0 deletions api/src/auth/RoomAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export const hasPermission = (
return payload.isMonitor;
case 'changeColor':
return !payload.isSpectating;
case 'startTimer':
case 'changeRaceHandler':
case 'resetTimer':
return payload.isMonitor;
default:
return true;
}
Expand Down
53 changes: 7 additions & 46 deletions api/src/core/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ import {
} from '@playbingo/types';
import { OPEN, WebSocket } from 'ws';
import { RoomTokenPayload } from '../auth/RoomAuth';
import { getAccessToken } from '../lib/RacetimeConnector';
import RaceHandler from './integration/races/RaceHandler';
import { computeRevealedMask, rowColToMask } from '../util/RoomUtils';
import Room from './Room';
import {
computeRevealedMask,
rowColToBitIndex,
rowColToMask,
} from '../util/RoomUtils';

/**
* Represents a player connected to a room. While largely just a data class, this
Expand Down Expand Up @@ -61,8 +55,7 @@ export default class Player {
* is authorized for the connection */
connections: Map<string, WebSocket>;

raceHandler: RaceHandler;
raceId: string;
finishedAt?: string;

constructor(
room: Room,
Expand All @@ -87,9 +80,6 @@ export default class Player {
this.exploredGoals = 0n;

this.connections = new Map<string, WebSocket>();

this.raceHandler = room.raceHandler;
this.raceId = '';
}

doesTokenMatch(token: RoomTokenPayload) {
Expand Down Expand Up @@ -153,7 +143,7 @@ export default class Player {
* @returns Client representation of this player's data
*/
toClientData(): PlayerClientData {
const raceUser = this.raceHandler.getPlayer(this.raceId);
const raceUser = this.room.raceHandler.getPlayer(this);
return {
id: this.id,
nickname: this.nickname,
Expand Down Expand Up @@ -303,48 +293,19 @@ export default class Player {
//#endregion

//#region Races
private async tryRaceAction(
action: (token: string) => Promise<boolean>,
failMsg: string,
) {
if (this.userId) {
const token = await getAccessToken(this.userId);
if (token) {
return action(token);
} else {
this.room.logInfo(`${failMsg} - failed to generate token`);
return false;
}
} else {
this.room.logInfo(`${failMsg} - player is anonymous`);
return false;
}
}
async joinRace() {
return this.tryRaceAction(
this.raceHandler.joinPlayer.bind(this.raceHandler),
'Unable to join race room',
);
return this.room.raceHandler.joinPlayer(this);
}

async leaveRace() {
return this.tryRaceAction(
this.raceHandler.leavePlayer.bind(this.raceHandler),
'Unable to leave race room',
);
return this.room.raceHandler.leavePlayer(this);
}

async ready() {
return this.tryRaceAction(
this.raceHandler.readyPlayer.bind(this.raceHandler),
'Unable to ready in race room',
);
return this.room.raceHandler.readyPlayer(this);
}
async unready() {
return this.tryRaceAction(
this.raceHandler.unreadyPlayer.bind(this.raceHandler),
'Unable to unready in race room',
);
return this.room.raceHandler.unreadyPlayer(this);
}
//#endregion
}
Loading
Loading