mirror of
https://github.com/morbalint/kemkas.git
synced 2026-07-18 03:13:46 +00:00
use Redux for tulajdonsagok
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
import { Fetched } from "../models/Fetched";
|
||||
import React from "react";
|
||||
|
||||
export const userDefaults: Fetched<string | null> = {
|
||||
state: "not-started",
|
||||
data: null,
|
||||
}
|
||||
|
||||
export const UserContext = React.createContext(userDefaults)
|
||||
UserContext.displayName = "UserEmailContext";
|
||||
@@ -0,0 +1,31 @@
|
||||
import {RollAbility} from "./rollAbility";
|
||||
|
||||
describe('RollAbility', () => {
|
||||
it('returns 3 if all rolls are 1', () => {
|
||||
const actual = RollAbility({
|
||||
d6: () => 1
|
||||
})
|
||||
expect(actual).toEqual(3)
|
||||
})
|
||||
it('returns 18 if at least 3 rolls are 6', () => {
|
||||
let counter = 0;
|
||||
const actual = RollAbility({
|
||||
d6: () => {
|
||||
return ++counter > 1 ? 6 : 1;
|
||||
}
|
||||
})
|
||||
expect(actual).toEqual(18)
|
||||
})
|
||||
it('sums up rolls are removes the smallest roll', () => {
|
||||
let counter = 1;
|
||||
const actual = RollAbility({
|
||||
d6: () => {
|
||||
if (counter++ > 4) {
|
||||
throw "Cheater! You can only roll 4 times!"
|
||||
}
|
||||
return counter
|
||||
}
|
||||
})
|
||||
expect(actual).toEqual(12)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,21 @@
|
||||
import {d6} from "./kockak";
|
||||
|
||||
export function RollAbility(dependencies: {
|
||||
d6: () => number
|
||||
} = {d6}) {
|
||||
const { d6: roll6 } = dependencies;
|
||||
let min = 6;
|
||||
let sum = 0;
|
||||
let rolls: number[] = []
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const roll = roll6();
|
||||
if (roll < min) {
|
||||
min = roll;
|
||||
}
|
||||
sum += roll;
|
||||
rolls.push(roll)
|
||||
}
|
||||
const result = sum - min;
|
||||
console.log("Rolled: " + rolls + " got: " + result)
|
||||
return result;
|
||||
}
|
||||
@@ -6,19 +6,22 @@ export const userSlice = createSlice({
|
||||
name: 'user',
|
||||
initialState: {
|
||||
state: "not-started" as LoadingState,
|
||||
email: null,
|
||||
email: null as string | null,
|
||||
},
|
||||
reducers: {
|
||||
load: (state) => {
|
||||
state.state = "loading"
|
||||
return state
|
||||
},
|
||||
setUser: (state, action) => {
|
||||
setUser: (state, action: {payload: string}) => {
|
||||
state.state = "finished"
|
||||
state.email = action.payload
|
||||
return state
|
||||
},
|
||||
unsetUser: (state) => {
|
||||
state.state = "finished"
|
||||
state.email = null
|
||||
return state
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user