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

Commit 0d79e27

Browse files
committed
debug tools
1 parent ae84063 commit 0d79e27

File tree

4 files changed

+51
-55
lines changed

4 files changed

+51
-55
lines changed

src/commands/eval.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import database from "../database.js";
99
import { games } from "../gameLogic/index.js";
1010
import * as gameLogic from "../gameLogic/index.js";
1111
import { config } from "../index.js";
12-
import { Command } from "../types";
12+
import { Command, UnoGame } from "../types";
1313
import * as utils from "../utils.js";
1414
client; components; gameLogic; database; constants; utils;
1515

@@ -19,6 +19,14 @@ const bash = (cmd: string) => execSync(cmd, { encoding: "utf8" });
1919
const update = () => bash("git pull && npm run build");
2020
update;
2121

22+
function sendDrawProxyStatus(game: UnoGame<true>) {
23+
return clientUtils.sendMessage(game.message.channelID, Object.entries(game.cards).map(([k, v]) =>
24+
// @ts-ignore
25+
`\`${k}:\` ${v.owner ? `yes (${v.owner})` : "no"}`
26+
).join("\n"));
27+
}
28+
sendDrawProxyStatus;
29+
2230
export const cmd = {
2331
name: "eval",
2432
aliases: ["adminabuse"],

src/gameLogic/notStarted.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,42 @@ import { Card, DebugState, DebugStateType, UnoGame } from "../types.js";
1010
import { getUsername, hasStarted, next, shuffle, toTitleCase, updateStats, without } from "../utils.js";
1111
import { games, makeStartMessage, sendGameMessage } from "./index.js";
1212

13-
export function handleDrawCardProxy(startedGame: UnoGame<true>, userId: string, t, p, n) {
14-
t[p] = n;
15-
if (p === "length") startedGame._debug.pushState({
16-
type: "set-cards",
17-
newState: t,
18-
meetsEndCondition: [p === "length" && n === 0, n]
19-
});
20-
if (p === "length" && n === 0) {
21-
// TODO: check that the card shown here is the correct one and dont just pray it is
22-
const card = startedGame.currentCard;
23-
timeouts.delete(startedGame.channelID);
24-
updateStats(startedGame, userId);
25-
delete games[startedGame.channelID];
26-
sendMessage(startedGame.channelID, {
27-
content: `**${getUsername(userId, true, client.guilds.get(startedGame.guildID))}** \
13+
export function makeDrawCardProxy(cards: Card[], game: UnoGame<true>, userId: string) {
14+
return new Proxy([...cards], {
15+
get(t, p) {
16+
if (p === "owner") return userId;
17+
else return t[p];
18+
},
19+
set(t, p, n) {
20+
t[p] = n;
21+
if (p === "length") game._debug.pushState({
22+
type: "set-cards",
23+
newState: t,
24+
meetsEndCondition: [p === "length" && n === 0, n]
25+
});
26+
if (p === "length" && n === 0) {
27+
// TODO: check that the card shown here is the correct one and dont just pray it is
28+
const card = game.currentCard;
29+
timeouts.delete(game.channelID);
30+
updateStats(game, userId);
31+
delete games[game.channelID];
32+
sendMessage(game.channelID, {
33+
content: `**${getUsername(userId, true, client.guilds.get(game.guildID))}** \
2834
played ${cardEmotes[card]} ${toTitleCase(card)}, and won`,
29-
components: new ComponentBuilder<MessageActionRow>()
30-
.addInteractionButton({
31-
style: ButtonStyles.SUCCESS,
32-
label: "gg",
33-
emoji: ComponentBuilder.emojiToPartial("🏆", "default"),
34-
disabled: true,
35-
customID: "we-have-a-nerd-here🤓"
36-
})
37-
.toJSON()
38-
});
39-
}
40-
return true;
35+
components: new ComponentBuilder<MessageActionRow>()
36+
.addInteractionButton({
37+
style: ButtonStyles.SUCCESS,
38+
label: "gg",
39+
emoji: ComponentBuilder.emojiToPartial("🏆", "default"),
40+
disabled: true,
41+
customID: "we-have-a-nerd-here🤓"
42+
})
43+
.toJSON()
44+
});
45+
}
46+
return true;
47+
}
48+
});
4149
}
4250

4351
const drawUntilNotSpecial = (game: UnoGame<true>) => {
@@ -147,11 +155,7 @@ won by default`,
147155
[p, startedGame.draw(7).cards.sort((a, b) => cards.indexOf(a) - cards.indexOf(b))]
148156
));
149157
Object.keys(cardsToBeUsed).forEach(id => {
150-
cardsToBeUsed[id] = new Proxy(cardsToBeUsed[id], {
151-
set(t, p, n) {
152-
return handleDrawCardProxy(startedGame, id, t, p, n);
153-
}
154-
});
158+
cardsToBeUsed[id] = makeDrawCardProxy(cardsToBeUsed[id], startedGame, id);
155159
});
156160

157161
startedGame.cards = cardsToBeUsed;

src/gameLogic/playedCards.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import timeouts from "../timeouts.js";
99
import { Card, UnoGame } from "../types.js";
1010
import { getUsername, next, toTitleCase, updateStats, wasLastTurnBlocked } from "../utils.js";
1111
import { games, onTimeout, sendGameMessage } from "./index.js";
12-
import { handleDrawCardProxy } from "./notStarted.js";
12+
import { makeDrawCardProxy } from "./notStarted.js";
1313

1414
function isSabotage(ctx: ComponentInteraction<ComponentTypes.STRING_SELECT>, game: UnoGame<true>): boolean {
1515
// first card is duration=0, second is duration=1, etc
@@ -106,16 +106,8 @@ export function onSevenPlayed(ctx: ComponentInteraction<ComponentTypes.STRING_SE
106106
game.currentPlayer = next(game.players, game.players.indexOf(game.currentPlayer));
107107

108108
const otherPlayerCards = [...game.cards[id]];
109-
game.cards[id] = new Proxy([...game.cards[ctx.member.id]], {
110-
set(t, p, n) {
111-
return handleDrawCardProxy(game, id, t, p, n);
112-
}
113-
});
114-
game.cards[ctx.member.id] = new Proxy(otherPlayerCards, {
115-
set(t, p, n) {
116-
return handleDrawCardProxy(game, ctx.member.id, t, p, n);
117-
}
118-
});
109+
game.cards[id] = makeDrawCardProxy(game.cards[ctx.member.id], game, id);
110+
game.cards[ctx.member.id] = makeDrawCardProxy(otherPlayerCards, game, ctx.member.id);
119111

120112
sendMessage(ctx.channel.id, `**${getUsername(ctx.member.id, true, ctx.guild)}** played \
121113
${cardEmotes[game.currentCard]} ${toTitleCase(game.currentCard)}
@@ -249,11 +241,7 @@ You drew ${cardEmotes[newCards[0]]}`,
249241
const keys = Object.keys(game.cards);
250242
keys.unshift(keys.pop());
251243
game.cards = Object.fromEntries(Object.entries(game.cards).map(([_, value], i) =>
252-
[keys[i], new Proxy([...value], {
253-
set(t, p, n) {
254-
return handleDrawCardProxy(game, keys[i], t, p, n);
255-
}
256-
})]
244+
[keys[i], makeDrawCardProxy(value, game, keys[i])]
257245
));
258246
break;
259247
}

src/gameLogic/started.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { config } from "../index.js";
99
import { UnoGame } from "../types.js";
1010
import { cardArrayToCount, getUsername, next, toTitleCase } from "../utils.js";
1111
import { sendGameMessage } from "./index.js";
12-
import { handleDrawCardProxy } from "./notStarted.js";
12+
import { makeDrawCardProxy } from "./notStarted.js";
1313

1414
export function leaveGame(ctx: ComponentInteraction<ComponentTypes.BUTTON>, game: UnoGame<true>) {
1515
if (game.players.includes(ctx.member.id)) {
@@ -113,11 +113,7 @@ export function onGameButtonPress(ctx: ComponentInteraction<ComponentTypes.BUTTO
113113
const secondHighest = cardCounts.pop();
114114
const { cards, newDeck } = game.draw(Math.min(highest, secondHighest + 5, 7));
115115

116-
game.cards[ctx.member.id] = new Proxy(cards, {
117-
set(t, p, n) {
118-
return handleDrawCardProxy(game, ctx.member.id, t, p, n);
119-
}
120-
});
116+
game.cards[ctx.member.id] = makeDrawCardProxy(cards, game, ctx.member.id);
121117
game.deck = newDeck;
122118

123119
sendMessage(ctx.channel.id, `**${getUsername(ctx.member.id, true, ctx.guild)}** has joined the game!`);

0 commit comments

Comments
 (0)