diff --git a/packages/api/db/migrations/20200716182227_add_difficulty_to_games.ts b/packages/api/db/migrations/20200716182227_add_difficulty_to_games.ts new file mode 100644 index 0000000..a0c8a5a --- /dev/null +++ b/packages/api/db/migrations/20200716182227_add_difficulty_to_games.ts @@ -0,0 +1,16 @@ +import Knex from "knex"; + +export const up = async (knex: Knex) => { + return knex.schema.alterTable("games", table => { + table + .integer("difficulty") + .notNullable() + .defaultTo(0); + }); +}; + +export const down = async (knex: Knex) => { + return knex.schema.alterTable("games", table => { + return table.dropColumn("difficulty"); + }); +}; diff --git a/packages/api/src/modules/games/actions/createGame.ts b/packages/api/src/modules/games/actions/createGame.ts index a0de024..eb181bc 100644 --- a/packages/api/src/modules/games/actions/createGame.ts +++ b/packages/api/src/modules/games/actions/createGame.ts @@ -9,7 +9,8 @@ export const createGame = async ( userid: number, wpm: number, rawwpm: number, - accuracy: number + accuracy: number, + difficulty: number ): Promise => { const newGame = { gameid: (await highestGameId()) + 1, @@ -17,6 +18,7 @@ export const createGame = async ( wpm, rawwpm, accuracy, + difficulty, date: Date.now() }; const possibleAchievements: Achievement[] = await findAchievementsByRequirement( @@ -29,6 +31,15 @@ export const createGame = async ( const achievements = possibleAchievements.filter( async l => !(await userHasAchievement(userid, l.id)) ); + + const user = await knex("users") + .where({ id: userid }) + .first(); + + await knex("users") + .where({ id: userid }) + .update({ totaltests: user.totaltests + 1 }); + achievements.map(async l => { await knex("users") .update({ diff --git a/packages/api/src/modules/games/router.test.ts b/packages/api/src/modules/games/router.test.ts index c2e3c94..7cca452 100644 --- a/packages/api/src/modules/games/router.test.ts +++ b/packages/api/src/modules/games/router.test.ts @@ -42,18 +42,18 @@ describe("Game routes", async () => { it("Deletes games past 10 games", async function() { this.timeout(5000); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); - await createGame(4, 0, 0, 0); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); + await createGame(4, 0, 0, 0, 1); await Promise.all([ removeOldGame(4), @@ -67,7 +67,7 @@ describe("Game routes", async () => { }); it("Checks for achievements", async () => { - await createGame(4, 32, 40, 80); + await createGame(4, 32, 40, 80, 5); await removeOldGame(4); const achievements = await agent.get("/api/users/achievements/4"); diff --git a/packages/api/src/modules/games/router.ts b/packages/api/src/modules/games/router.ts index f19eeae..ce4f76c 100644 --- a/packages/api/src/modules/games/router.ts +++ b/packages/api/src/modules/games/router.ts @@ -13,9 +13,15 @@ router.post( requireAuthenticated(), validateSchema(newGameBody, "body"), async (ctx, next) => { - const { wpm, rawwpm, accuracy } = ctx.request.body; + const { wpm, rawwpm, accuracy, difficulty } = ctx.request.body; const { user } = ctx.session!; - const newGame = await createGame(user, wpm, rawwpm, accuracy); + const newGame = await createGame( + user, + wpm, + rawwpm, + accuracy, + difficulty + ); await removeOldGame(user); await checkPB(newGame); ctx.status = 201;