feat: Display the level cap

This commit is contained in:
Alexander Polynomdivision 2018-09-20 19:54:03 +02:00
parent 466e5a3107
commit 8ab29bece8
4 changed files with 61 additions and 3 deletions

View File

@ -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<IProps> {
}
render() {
const level = userScoreToLevel(this.props.user.score);
return (
<div>
<AppBar position="static">
@ -62,7 +64,7 @@ export default class Drawer extends React.Component<IProps> {
{
this.props.authenticated ? (
<Button color="inherit">
{`${this.props.user.score} / 200`}
{`${this.props.user.score} / ${level.levelCap}`}
</Button>
) : undefined
}

View File

@ -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);
});

View File

@ -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,
};
}

View File

@ -60,7 +60,9 @@ const initialState: IState = {
// TODO: Set this to false
authenticated: true,
user: {},
user: {
score: 0,
},
login: {
loading: false,