From 8ab29bece825410330fb7869033072c994b523d8 Mon Sep 17 00:00:00 2001 From: Alexander Polynomdivision Date: Thu, 20 Sep 2018 19:54:03 +0200 Subject: [PATCH] feat: Display the level cap --- frontend/src/components/Drawer.tsx | 6 ++- frontend/src/models/__tests__/userLevels.ts | 11 ++++++ frontend/src/models/user.ts | 43 +++++++++++++++++++++ frontend/src/reducers/index.ts | 4 +- 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 frontend/src/models/__tests__/userLevels.ts diff --git a/frontend/src/components/Drawer.tsx b/frontend/src/components/Drawer.tsx index 1499ff0..5c3309f 100644 --- a/frontend/src/components/Drawer.tsx +++ b/frontend/src/components/Drawer.tsx @@ -22,7 +22,7 @@ import HomeIcon from "@material-ui/icons/Home"; import BookIcon from "@material-ui/icons/Book"; import ViewWeekIcon from "@material-ui/icons/ViewWeek"; -import { IUser } from "../models/user"; +import { IUser, userScoreToLevel } from "../models/user"; interface IProps { logout: () => void; @@ -43,6 +43,8 @@ export default class Drawer extends React.Component { } render() { + const level = userScoreToLevel(this.props.user.score); + return (
@@ -62,7 +64,7 @@ export default class Drawer extends React.Component { { this.props.authenticated ? ( ) : undefined } diff --git a/frontend/src/models/__tests__/userLevels.ts b/frontend/src/models/__tests__/userLevels.ts new file mode 100644 index 0000000..ff43183 --- /dev/null +++ b/frontend/src/models/__tests__/userLevels.ts @@ -0,0 +1,11 @@ +import { userScoreToLevel } from "../user"; + +it("Should return level 1 for 34", () => { + const level = userScoreToLevel(34); + expect(level.level).toBe(1); +}); + +it("Should return level 2 for 35", () => { + const level = userScoreToLevel(35); + expect(level.level).toBe(2); +}); diff --git a/frontend/src/models/user.ts b/frontend/src/models/user.ts index 9d7232e..cc15ea2 100644 --- a/frontend/src/models/user.ts +++ b/frontend/src/models/user.ts @@ -6,3 +6,46 @@ export interface IUser { sessionToken: string; }; + +export interface IUserLevel { + // The numerical representation + level: number; + // The string representation + name: string; + // The user has this level until: score => levelCap + levelCap: number; +}; + +function levelFactory(): (name: string, levelCap: number) => IUserLevel { + let level = 1; + return (name: string, levelCap: number) => { + return { + level: level++, + name, + levelCap, + }; + }; +} +const l = levelFactory(); + +export const UserLevels: IUserLevel[] = [ + l("Sklave", 35), + l("Farmer", 75), + l("Soldat", 120), + l("Gladiator", 170), + l("Zenturio", 220), + l("Prätor", 270), + l("Reiter", 320), + l("General", 370), + l("Konsul", 420), + l("Caesar", 470), +]; + +export function userScoreToLevel(userScore: number): IUserLevel { + // NOTE: The failure level should never be returned + return UserLevels.find((el) => userScore < el.levelCap) || { + level: 0, + name: "Failure", + levelCap: 10000000, + }; +} diff --git a/frontend/src/reducers/index.ts b/frontend/src/reducers/index.ts index 5c2b5d4..5f09ecc 100644 --- a/frontend/src/reducers/index.ts +++ b/frontend/src/reducers/index.ts @@ -60,7 +60,9 @@ const initialState: IState = { // TODO: Set this to false authenticated: true, - user: {}, + user: { + score: 0, + }, login: { loading: false,