Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
feat: added difficulty column to games table
Browse files Browse the repository at this point in the history
  • Loading branch information
vycdev committed Jul 16, 2020
1 parent 5b0a64b commit e661bc6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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");
});
};
13 changes: 12 additions & 1 deletion packages/api/src/modules/games/actions/createGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ export const createGame = async (
userid: number,
wpm: number,
rawwpm: number,
accuracy: number
accuracy: number,
difficulty: number
): Promise<Game> => {
const newGame = {
gameid: (await highestGameId()) + 1,
userid,
wpm,
rawwpm,
accuracy,
difficulty,
date: Date.now()
};
const possibleAchievements: Achievement[] = await findAchievementsByRequirement(
Expand All @@ -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({
Expand Down
26 changes: 13 additions & 13 deletions packages/api/src/modules/games/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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");
Expand Down
10 changes: 8 additions & 2 deletions packages/api/src/modules/games/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e661bc6

Please sign in to comment.