1 | const computed = require('mutant/computed');
|
2 | const GameComparer = require('./gameComparer')();
|
3 | const MutantArray = require('mutant/array');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | module.exports = (myIdent, ssbGameCtrl, gameDb, userGamesUpdateWatcher, myGameUpdates) => {
|
15 |
|
16 | function getGamesWhereMyMove() {
|
17 | const myGamesInProgress = getMyGamesInProgress();
|
18 |
|
19 | return computed([myGamesInProgress],
|
20 | gamesInProgress => computed(
|
21 | [gamesInProgress],
|
22 | games => filterGamesMyMove(games).sort(compareGameTimestamps),
|
23 | ));
|
24 | }
|
25 |
|
26 | function getSituationObservable(gameId) {
|
27 | return ssbGameCtrl.getSituationObservable(gameId);
|
28 | }
|
29 |
|
30 | function getMyGamesInProgress() {
|
31 | return getGamesInProgress(myIdent);
|
32 | }
|
33 |
|
34 | function getSituation(gameId) {
|
35 | return ssbGameCtrl.getSituation(gameId);
|
36 | }
|
37 |
|
38 | function getSituationSummaryObservable(gameId) {
|
39 | return ssbGameCtrl.getSituationSummaryObservable(gameId);
|
40 | }
|
41 |
|
42 | function getFriendsObservableGames(startArg, endArg) {
|
43 | const observable = MutantArray([]);
|
44 |
|
45 | const start = startArg || 0;
|
46 | const end = endArg || 20;
|
47 |
|
48 | gameDb.getObservableGames(myIdent, start, end).then(
|
49 | gameIds => Promise.all(
|
50 | gameIds.map(ssbGameCtrl.getSmallGameSummary),
|
51 | ),
|
52 | ).then(a => a.sort(compareGameTimestamps)).then(observable.set);
|
53 |
|
54 | const observingUpdates = userGamesUpdateWatcher.latestGameMessageForOtherPlayersObs(myIdent);
|
55 |
|
56 | const unlistenObservingUpdates = observingUpdates(
|
57 | newUpdate => updateObservableWithGameChange(
|
58 | () => gameDb.getObservableGames(myIdent, start, end).then(
|
59 | gameIds => Promise.all(
|
60 | gameIds.map(ssbGameCtrl.getSmallGameSummary),
|
61 | ),
|
62 | ),
|
63 | newUpdate,
|
64 | observable,
|
65 | ),
|
66 | );
|
67 |
|
68 | return computed(
|
69 | [observable],
|
70 | a => a.sort(compareGameTimestamps), {
|
71 | comparer: GameComparer.hasSameGames,
|
72 | onUnlisten: unlistenObservingUpdates,
|
73 | },
|
74 | );
|
75 | }
|
76 |
|
77 | function gamesAgreedToPlaySummaries(playerId) {
|
78 | return gameDb.getGamesAgreedToPlayIds(playerId).then(gamesInProgress => Promise.all(
|
79 | gamesInProgress.map(ssbGameCtrl.getSmallGameSummary),
|
80 | ));
|
81 | }
|
82 |
|
83 | function getGamesInProgress(playerId) {
|
84 | const observable = MutantArray([]);
|
85 | gamesAgreedToPlaySummaries(playerId).then(g => observable.set(g.sort(compareGameTimestamps)));
|
86 |
|
87 | const playerGameUpdates = getGameUpdatesObservable(playerId);
|
88 |
|
89 | const unlistenUpdates = playerGameUpdates(
|
90 | newUpdate => updateObservableWithGameChange(
|
91 | () => gamesAgreedToPlaySummaries(playerId),
|
92 | newUpdate,
|
93 | observable,
|
94 | ),
|
95 | );
|
96 |
|
97 | return computed(
|
98 | [observable],
|
99 | a => a, {
|
100 | onUnlisten: unlistenUpdates,
|
101 | },
|
102 | );
|
103 | }
|
104 |
|
105 | function updateObservableWithGameChange(summaryListPromiseFn, newUpdate, observable) {
|
106 | const type = newUpdate.value ? newUpdate.value.content.type : null;
|
107 | if (type === 'chess_move') {
|
108 | ssbGameCtrl.getSmallGameSummary(newUpdate.value.content.root).then(
|
109 | (summary) => {
|
110 | const { gameId } = summary;
|
111 | const idx = observable().findIndex(s => s.gameId === gameId);
|
112 |
|
113 | if (idx !== -1) {
|
114 | observable.put(idx, summary);
|
115 | } else {
|
116 | summaryListPromiseFn().then(g => observable.set(g.sort(compareGameTimestamps)));
|
117 | }
|
118 | },
|
119 | );
|
120 | } else {
|
121 | summaryListPromiseFn().then(g => observable.set(g.sort(compareGameTimestamps)));
|
122 | }
|
123 | }
|
124 |
|
125 | function filterGamesMyMove(gameSummaries) {
|
126 | return gameSummaries.filter(summary => summary.toMove === myIdent);
|
127 | }
|
128 |
|
129 | function compareGameTimestamps(g1, g2) {
|
130 | return g2.lastUpdateTime - g1.lastUpdateTime;
|
131 | }
|
132 |
|
133 | function getGameUpdatesObservable(ident) {
|
134 | if (ident === myIdent) {
|
135 | return myGameUpdates;
|
136 | }
|
137 | return userGamesUpdateWatcher.latestGameMessageForPlayerObs(ident);
|
138 | }
|
139 |
|
140 | function getAllGamesInDbIds() {
|
141 | return gameDb.getAllGameIds();
|
142 | }
|
143 |
|
144 | return {
|
145 | getGamesWhereMyMove,
|
146 | getMyGamesInProgress,
|
147 | getGamesInProgress,
|
148 | getFriendsObservableGames,
|
149 | getSituation,
|
150 | getSituationObservable,
|
151 | getSituationSummaryObservable,
|
152 | getAllGamesInDbIds
|
153 | }
|
154 | } |
\ | No newline at end of file |