-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
47 lines (43 loc) · 1.32 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { BehaviorSubject, merge } from "rxjs";
import { map, withLatestFrom } from "rxjs/operators";
import { GameStateLamda, initialGameBoard } from "./constants";
import { eggs } from "./egg";
import { player } from "./player";
import { createBoard, renderGameBoard, sTryAgain } from "./rendering";
import { boxes } from "./boxes";
const sState = new BehaviorSubject(initialGameBoard);
const { sTime, sGameOver, sChange: sPlayerChange } = player(sState);
const { sChange: sBoxessChange } = boxes(sState, sTime);
const { sAdd: sChangeEggAdd, sUpdate: sChangeEggUpdate } = eggs(sState, sTime);
merge(
sPlayerChange.pipe(
withLatestFrom(
sTime,
sState,
sBoxessChange,
sChangeEggAdd,
sChangeEggUpdate,
sGameOver
),
map(([playerL, time, state, boxLs, eggAddLs, eggUpdateLs, gameOverL]) => {
if (state.gameOver) {
return state;
}
const allLamdas: GameStateLamda[] = [
playerL,
...boxLs,
...eggAddLs,
...eggUpdateLs,
gameOverL,
];
return {
...allLamdas.reduce((state, lamda) => {
return lamda(state);
}, state),
score: time,
};
})
),
sTryAgain.pipe(map(() => initialGameBoard))
).subscribe((state) => sState.next(state));
sState.subscribe((state) => renderGameBoard(state));