Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit bc759b5

Browse files
committed
ensure database entry exists for mid game joiners
1 parent e55ff76 commit bc759b5

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

src/commands/uno.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import { respond } from "../client.js";
33
import { JoinButtons } from "../components.js";
4-
import { autoStartTimeout, defaultSettings } from "../constants.js";
4+
import { autoStartTimeout } from "../constants.js";
55
import database from "../database.js";
66
import { games, makeStartMessage } from "../gameLogic/index.js";
77
import { startGame } from "../gameLogic/notStarted.js";
@@ -18,13 +18,13 @@ export const cmd = {
1818
Jump: https://discord.com/channels/${existingGame.message.channel.guild.id}/${existingGame.message.channel.id}/${existingGame.message.id}`);
1919
games[msg.channel.id] = { started: false } as UnoGame<false>;
2020

21-
const data = database.get(msg.channel.guild.id, msg.author.id);
21+
const data = database.getOrCreate(msg.channel.guild.id, msg.author.id);
2222
const gameObj = {
2323
uid: Math.random().toString().substring(2),
2424
started: false,
2525
starting: Math.floor(Date.now() / 1000) + autoStartTimeout,
2626
host: msg.author.id,
27-
settings: data?.preferredSettings ?? { ...defaultSettings },
27+
settings: data?.preferredSettings,
2828
players: [msg.author.id],
2929
_allowSolo: args[0]?.toLowerCase() === "solo",
3030
_modified: false,

src/database.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ class PlayerStatsDatabase {
9191
return guild[playerId];
9292
}
9393

94+
getOrCreate(guildId: string, playerId: string): PlayerStorage {
95+
const value = this.get(guildId, playerId);
96+
if (value) return value;
97+
98+
this.set(guildId, playerId, this.defaultValue);
99+
return { ...this.defaultValue };
100+
}
101+
94102
getAllForGuild(guildId: string): GuildStorage | undefined {
95103
if (!this.hasInitalized) return {} as any;
96104

src/gameLogic/playedCards.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function isSabotage(ctx: ComponentInteraction<ComponentTypes.STRING_SELECT>, gam
2626
updateStats(game, game.players[0]);
2727
return true;
2828
} else database.set(game.guildID, ctx.member.id, {
29-
losses: database.get(game.guildID, ctx.member.id).losses + 1
29+
losses: database.getOrCreate(game.guildID, ctx.member.id).losses + 1
3030
});
3131

3232
game.currentPlayer = next(game.players, game.players.indexOf(game.currentPlayer));

src/gameLogic/started.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ButtonStyles, ComponentInteraction, ComponentTypes, MessageActionRow, M
44
import { sendMessage } from "../client.js";
55
import { DrawStackedCardSelect, PickCardSelect } from "../components.js";
66
import { ButtonIDs, cardEmotes } from "../constants.js";
7+
import database from "../database.js";
78
import { config } from "../index.js";
89
import { UnoGame } from "../types.js";
910
import { cardArrayToCount, getUsername, next, toTitleCase } from "../utils.js";
@@ -97,6 +98,7 @@ export function onGameButtonPress(ctx: ComponentInteraction<ComponentTypes.BUTTO
9798
});
9899

99100
game.players.push(ctx.member.id);
101+
database.getOrCreate(ctx.guild.id, ctx.member.id);
100102
// in ascending order
101103
const cardCounts = Object.values(game.cards).map(c => c.length).sort((a, b) => a - b);
102104
// amount of cards is the same as the player with the highest amount of cards

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function updateStats(game: UnoGame<true>, winner: string) {
7373
if (game._modified) return;
7474
const newStats: { [id: string]: PlayerStorage; } = {};
7575
game.players.forEach(id => {
76-
const val: PlayerStorage = database.get(game.guildID, id) ?? database.defaultValue;
76+
const val = database.getOrCreate(game.guildID, id);
7777
if (id === winner) val.wins++;
7878
else val.losses++;
7979
newStats[id] = val;

0 commit comments

Comments
 (0)