Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track moves in the database, valid and invalid #88

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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
14 changes: 9 additions & 5 deletions server/src/GameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ export default class GameManager {
return this.game;
}

makeMove(userId: string, move: HexCoord[]): Game {
makeMove(
userId: string,
move: HexCoord[]
): { game: Game | null; word: string } {
if (!this.game) {
throw new Error("Game Manager has no game!");
}
Expand Down Expand Up @@ -132,9 +135,10 @@ export default class GameManager {
}
}
console.log("move received word", word);
if (!isValidWord(word, WordsObject)) {
const valid = isValidWord(word, WordsObject);
if (!valid) {
console.log("word invalid", word);
throw new Error("Not a valid word");
return { game: null, word };
}

const gridCopy: { [coord: string]: Cell } = {};
Expand Down Expand Up @@ -251,7 +255,7 @@ export default class GameManager {
this.game.moves.push(gameMove);
this.game.gameOver = true;
this.game.winner = this.game.turn;
return this.game;
return { game: this.game, word };
}

for (const cell of Object.values(this.game.grid)) {
Expand Down Expand Up @@ -292,7 +296,7 @@ export default class GameManager {
? this.game.turn
: (Number(!this.game.turn) as 0 | 1);
this.game.turn = nextTurn;
return this.game;
return { game: this.game, word };
}

createGame(userId: string): Game {
Expand Down
13 changes: 13 additions & 0 deletions server/src/datalayer/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@ export default class Memory implements DataLayer {
authTokens: {
[key: string]: AuthToken;
} = {};
moves: {
[key: string]: {
count: number;
valid: boolean;
};
} = {};
constructor() {
this.games = {};
this.users = {};
}
async saveMove(word: string, valid: boolean): Promise<boolean> {
this.moves[word] = {
valid,
count: (this.moves[word]?.count ?? 0) + 1,
};
return true;
}

async createUser(
id: string,
Expand Down
32 changes: 32 additions & 0 deletions server/src/datalayer/mongo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,38 @@ export default class Mongo implements DataLayer {
}
}

async saveMove(
word: string,
valid: boolean,
options?: Options
): Promise<boolean> {
if (!this.connected) {
throw new Error("Db not connected");
}
try {
const res = await Models.Move.findOneAndUpdate(
{
word,
},
{
word,
valid,
$inc: {
count: 1,
},
},
{
upsert: true,
session: options?.session,
}
);
return true;
} catch (e) {
console.log(e);
throw e;
}
}

async createUser(id: string, options?: Options): Promise<User> {
if (!this.connected) {
throw new Error("Db not connected");
Expand Down
22 changes: 22 additions & 0 deletions server/src/datalayer/mongo/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,31 @@ const migrationSchema = new Schema<{ version: number }>(

const MigrationModel = model<{ version: number }>("Migration", migrationSchema);

const moveSchema = new Schema<{ word: string; count: number; valid: boolean }>({
word: {
type: String,
required: true,
},
count: {
type: Number,
required: true,
default: 0,
},
valid: {
type: Boolean,
required: true,
},
});

const MoveModel = model<{ word: string; count: number; valid: boolean }>(
"Move",
moveSchema
);

export default {
Game: GameModel,
User: UserModel,
AuthToken: AuthTokenModel,
Migration: MigrationModel,
Move: MoveModel,
};
22 changes: 20 additions & 2 deletions server/src/routes/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,13 @@ export default (io: Server) => {
return;
}
console.log("Bot move", botMove);
game = gm.makeMove("AI", botMove);
const response = gm.makeMove("AI", botMove);
const valid = Boolean(response.game);
await dl.saveMove(response.word, valid, { session });
if (!response.game) {
return;
}
game = response.game;
await dl.saveGame(gameId, game, {
session,
});
Expand Down Expand Up @@ -336,7 +342,19 @@ export default (io: Server) => {

let newGame: Game;
try {
newGame = gm.makeMove(user, parsedMove);
const { game, word } = gm.makeMove(user, parsedMove);
const valid = Boolean(game);
await dl.saveMove(word, valid, { session });
if (!valid) {
res.status(400);
res.send("Invalid word");
return;
}
if (!game) {
res.sendStatus(500);
return;
}
newGame = game;
} catch (e: unknown) {
res.status(400);
if (e instanceof Error) {
Expand Down
5 changes: 5 additions & 0 deletions server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export interface AuthToken {
}

export interface DataLayer {
saveMove(
word: string,
valid: boolean,
options?: Record<string, unknown>
): Promise<boolean>;
createUser(id: string, options?: Record<string, unknown>): Promise<User>;
deleteUser(id: string, options?: Record<string, unknown>): Promise<boolean>;
createAuthToken(
Expand Down