This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Vyctor661/games
Games
- Loading branch information
Showing
17 changed files
with
311 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/api/db/migrations/20200706102336_create_pbs_table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Knex from "knex"; | ||
|
||
export const up = async (knex: Knex) => { | ||
return knex.schema.createTable("pbs", table => { | ||
table.integer("userid").notNullable(); | ||
table.bigInteger("date").notNullable(); | ||
table.integer("wpm").notNullable(); | ||
table.integer("rawwpm").notNullable(); | ||
table.integer("accuracy").notNullable(); | ||
}); | ||
}; | ||
|
||
export const down = async (knex: Knex) => { | ||
return knex.schema.dropTable("pbs"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import Router from "./Router"; | ||
|
||
import authRouter from "./auth/router"; | ||
import gamesRouter from "./games/router"; | ||
import usersRouter from "./users/router"; | ||
|
||
const apiRouter = new Router({ prefix: "/api" }); | ||
|
||
apiRouter.use(authRouter); | ||
apiRouter.use(gamesRouter); | ||
apiRouter.use(usersRouter); | ||
|
||
export default apiRouter.routes(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Game from "../types/Game"; | ||
import knex from "../../../../db/knex"; | ||
import PB from "../types/PB"; | ||
|
||
export default async (rawGame: Game) => { | ||
const { gameid: _, ...game } = rawGame; | ||
const playerPBs = await knex<PB>("pbs").where({ userid: game.userid }); | ||
if (playerPBs.length === 0) { | ||
await knex<PB>("pbs").insert(game); | ||
return true; | ||
} | ||
const playerPB = playerPBs.reduce((acc, cur) => | ||
acc.wpm > cur.wpm ? acc : cur | ||
); | ||
if (game.wpm > playerPB.wpm) { | ||
await knex<PB>("pbs").insert(game); | ||
return true; | ||
} | ||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Game from "../types/Game"; | ||
import { highestGameId } from "./highestGameId"; | ||
import knex from "../../../../db/knex"; | ||
|
||
export const createGame = async ( | ||
userid: number, | ||
wpm: number, | ||
rawwpm: number, | ||
accuracy: number | ||
): Promise<Game> => { | ||
const newGame = { | ||
gameid: (await highestGameId()) + 1, | ||
userid, | ||
wpm, | ||
rawwpm, | ||
accuracy, | ||
date: Date.now() | ||
}; | ||
return (await knex<Game>("games").insert(newGame, "*"))[0]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import PB from "../types/PB"; | ||
import knex from "../../../../db/knex"; | ||
|
||
export default async (userid: number) => { | ||
const pbs = await knex<PB>("pbs").where({ userid }); | ||
return pbs.length !== 0 ? pbs.sort((a, b) => a.wpm - b.wpm) : null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import knex from "../../../../db/knex"; | ||
import Game from "../types/Game"; | ||
|
||
export const highestGameId = async () => { | ||
const games = await knex<Game>("games"); | ||
const gameIds = games.map(l => l.gameid); | ||
const highestGameId = gameIds.length === 0 ? 1 : Math.max(...gameIds); | ||
return highestGameId; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Game from "../types/Game"; | ||
import knex from "../../../../db/knex"; | ||
|
||
export const removeOldGame = async (userid: number): Promise<void> => { | ||
const userGames = await knex<Game>("games").where({ userid }); | ||
if (userGames.length >= 11) { | ||
const removalGames = userGames.slice(0, -10); | ||
for (const game of removalGames) { | ||
await knex<Game>("games") | ||
.delete() | ||
.where({ gameid: game.gameid }); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import request from "supertest"; | ||
import { expect } from "chai"; | ||
|
||
import { server } from "../../index"; | ||
import users from "../../../db/seeds/examples/users"; | ||
import { createGame } from "./actions/createGame"; | ||
import knex from "../../../db/knex"; | ||
import Game from "./types/Game"; | ||
import { removeOldGame } from "./actions/removeOldGame"; | ||
|
||
const agent = request.agent(server); | ||
|
||
const newGame = { | ||
userid: 1, | ||
wpm: 90, | ||
rawwpm: 100, | ||
accuracy: 90, | ||
seconds: 60 | ||
}; | ||
|
||
describe("Game routes", async () => { | ||
// We don't need to rerun migrations or seeds because we did in the auth route | ||
|
||
it("Creates a game", async function() { | ||
this.timeout(5000); | ||
const { email, password } = users[0]; | ||
|
||
await agent.post(`/api/auth/login`).send({ email, password }); | ||
|
||
const response = await agent | ||
.post(`/api/games/newGame/`) | ||
.send(newGame) | ||
.set("Accept", "application/text") | ||
.expect("Content-Type", /text/) | ||
.expect(201); | ||
|
||
expect(response.text).to.deep.equal("Successfully created a game!"); | ||
}); | ||
|
||
it("Deletes games past 10 games", async function() { | ||
this.timeout(5000); | ||
|
||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
await createGame(13, 0, 0, 0); | ||
|
||
await Promise.all([ | ||
removeOldGame(13), | ||
removeOldGame(13), | ||
removeOldGame(13) | ||
]); | ||
|
||
const games = await knex<Game>("games").where({ userid: 13 }); | ||
|
||
expect(games.length).to.equal(10); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Router from "../Router"; | ||
import { requireAuthenticated } from "../auth/middleware/requireAuthenticated"; | ||
import { createGame } from "./actions/createGame"; | ||
import { removeOldGame } from "./actions/removeOldGame"; | ||
import checkPB from "./actions/checkPB"; | ||
|
||
const router = new Router({ prefix: "/games" }); | ||
|
||
router.post("/newGame", requireAuthenticated(), async (ctx, next) => { | ||
const { wpm, rawwpm, accuracy } = ctx.request.body; | ||
const { user } = ctx.session!; | ||
const newGame = await createGame(user, wpm, rawwpm, accuracy); | ||
await removeOldGame(user); | ||
await checkPB(newGame); | ||
ctx.status = 201; | ||
ctx.body = "Successfully created a game!"; | ||
await next(); | ||
}); | ||
|
||
export default router.routes(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default interface Game { | ||
gameid: number; | ||
userid: number; | ||
date: number; | ||
wpm: number; | ||
rawwpm: number; | ||
accuracy: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default interface PB { | ||
userid: number; | ||
date: number; | ||
wpm: number; | ||
rawwpm: number; | ||
accuracy: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import User from "../types/User"; | ||
import Game from "../../games/types/Game"; | ||
import knex from "../../../../db/knex"; | ||
|
||
export default async <T extends keyof Omit<User, "password">>( | ||
property: T, | ||
value: User[T] | ||
) => { | ||
if (!property || !value) return null; | ||
const result = await knex<User>("users") | ||
.select() | ||
.first() | ||
.where({ [property]: value }); | ||
if (!result) return null; | ||
|
||
const userGames = await knex<Game>("games").where({ userid: result.id }); | ||
return userGames; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters