Skip to content

Commit

Permalink
Add optional "participants" parameters to create draft API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ZetaTwo committed Dec 9, 2024
1 parent f500720 commit c023356
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/DraftServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ export class DraftServer {
socket.emit('message', 'This draft does not exist.');
return;
}

const draft = draftsStore.getDraftOrThrow(draftId)
if(draft.fixedNames) {
logger.info("Draft is using fixed names. Setting roles but not changing names", {draftId});
}

const role: Player = Util.sanitizeRole(message.role);
let assignedRole = Util.getAssignedRole(socket, roomHost, roomGuest);
let hasAssignedRole = false;
Expand All @@ -160,14 +166,16 @@ export class DraftServer {
logger.info("Setting player role to 'HOST': %s", message.name, {draftId});
socket.join(roomHost); // async
socket.leave(roomSpec); // async
draftsStore.connectPlayer(draftId, Player.HOST, message.name);
const newNameHost = draft.fixedNames ? draft.nameHost : message.name;
draftsStore.connectPlayer(draftId, Player.HOST, newNameHost);
assignedRole = Player.HOST;
hasAssignedRole = true;
} else if (role === Player.GUEST && !draftsStore.isPlayerConnected(draftId, role)) {
logger.info("Setting player role to 'GUEST': %s", message.name, {draftId});
socket.join(roomGuest); // async
socket.leave(roomSpec); // async
draftsStore.connectPlayer(draftId, Player.GUEST, message.name);
const newNameGuest = draft.fixedNames ? draft.nameGuest : message.name;
draftsStore.connectPlayer(draftId, Player.GUEST, newNameGuest);
assignedRole = Player.GUEST;
hasAssignedRole = true;
} else {
Expand Down Expand Up @@ -201,6 +209,13 @@ export class DraftServer {
socket.emit('message', 'This draft does not exist.');
return;
}

const draft = draftsStore.getDraftOrThrow(draftId)
if(draft.fixedNames) {
logger.info("Draft is using fixed names. Not changing names", {draftId});
return;
}

let assignedRole = Util.getAssignedRole(socket, roomHost, roomGuest);
if (assignedRole === Player.HOST) {
logger.info("Setting HOST player name to: %s", message.name, {draftId});
Expand Down Expand Up @@ -400,6 +415,16 @@ export class DraftServer {
const validationErrors = Validator.validatePreset(preset);
if (validationErrors.length === 0) {
draftsStore.initDraft(draftId, preset as Preset);

const nameHost: string | null | undefined = req.body.participants?.host;
const nameGuest: string | null | undefined = req.body.participants?.guest;
if(nameHost && nameGuest) {
logger.info('Setting names to "%s" and "%s"', nameHost, nameGuest, {draftId});
draftsStore.setPlayerName(draftId, Player.HOST, nameHost);
draftsStore.setPlayerName(draftId, Player.GUEST, nameGuest);
draftsStore.setFixedPlayerNames(draftId, true);
}

res.json({status: 'ok', draftId});
logger.info('Created new draft with id: %s', draftId, {draftId});
} else {
Expand Down
1 change: 1 addition & 0 deletions src/models/Draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Draft implements IDraftState {
public nextAction: number = 0;
public events: DraftEvent[] = [];
public startTimestamp: number;
public fixedNames: boolean = false;

constructor(nameHost: string, nameGuest: string, preset: Preset) {
this.nameHost = nameHost;
Expand Down
5 changes: 5 additions & 0 deletions src/models/DraftsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ export class DraftsStore {
}
}

public setFixedPlayerNames(draftId: string, fixedNames: boolean) {
const draft: Draft = this.getDraftOrThrow(draftId);
draft.fixedNames = fixedNames;
}

public setPlayerName(draftId: string, player: Player, name: string) {
const draft: Draft = this.getDraftOrThrow(draftId);
switch (player) {
Expand Down

0 comments on commit c023356

Please sign in to comment.