-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathapp.js
More file actions
236 lines (220 loc) · 5.48 KB
/
Copy pathapp.js
File metadata and controls
236 lines (220 loc) · 5.48 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import _ from "utils/utils";
import EventEmitter from "events";
import {STRINGS} from "./config";
import eio from "engine.io-client";
import {times, constant} from "lodash";
import GameState from "./gamestate";
function message(msg) {
let args = JSON.parse(msg);
App.emit(...args);
}
let App = {
__proto__: new EventEmitter,
state: {
id: null,
name: STRINGS.BRANDING.DEFAULT_USERNAME,
serverVersion: null,
numUsers: 0,
numPlayers: 0,
numActiveGames: 0,
roomInfo: [],
seats: 8,
title: "",
gameId: "",
isPrivate: true,
modernOnly: false,
totalChaos: false,
chaosDraftPacksNumber: 3,
chaosSealedPacksNumber: 6,
gametype: "draft",
picksPerPack: 1,
DoubleMasters: -1,
gamesubtype: "regular",
sets: [],
setsDraft: [],
setsSealed: [],
setsDecadentDraft: [],
availableSets: {},
list: "",
cards: 15,
packs: 3,
cubePoolSize: 90,
beep: true,
notify: false,
notificationGranted: false,
chat: false,
cols: false,
hidepicks: false,
deckSize: 40,
filename: "filename",
filetype: "txt",
side: false,
sort: "rarity",
log: {},
cardSize: "normal",
cardLang: "en",
game: {},
mtgJsonVersion: {
version: "0.0.0",
date: "1970-01-01"
},
boosterRulesVersion: "",
messages: [],
pickNumber: 0,
packSize: 15,
gameSeats: 8, // seats of the game being played
gameState: null, // records the current state of cards is a GameState
gameStates: {}, // Object representation of the gameState
get didGameStart() {
// both round === 0 and round is undefined
return App.state.round;
},
get isSealed() {
return /sealed/.test(App.state.game.type);
},
get isGameFinished() {
return App.state.round === -1;
},
get isDecadentDraft() {
return /decadent draft/.test(App.state.game.type);
},
get notificationBlocked() {
return ["denied", "notsupported"].includes(App.state.notificationResult);
}
},
init(router) {
App.on("set", App.set);
App.on("error", App.error);
App.on("route", App.route);
App.restore();
App.connect();
router(App);
},
register(component) {
App.connect();
App.on("set", App.set);
App.on("error", App.error);
App.on("route", App.route);
App.component = component;
},
restore() {
for (let key in this.state) {
let val = localStorage[key];
if (val) {
try {
this.state[key] = JSON.parse(val);
} catch(e) {
delete localStorage[key];
}
}
}
if (!this.state.id) {
this.state.id = _.uid();
localStorage.id = JSON.stringify(this.state.id);
}
},
connect() {
let {id, name} = App.state;
let options = {
query: { id, name }
};
if(!this.ws) {
this.ws = eio(location.href, options);
this.ws.on("message", message);
}
},
send(...args) {
let msg = JSON.stringify(args);
this.ws.send(msg);
},
initGameState(id) {
const { gameStates } = App.state;
if (!gameStates[id]) {
App.state.gameState = new GameState();
} else {
App.state.gameState = new GameState(gameStates[id]);
}
App.state.gameState.on("updateGameState", (gameState) => {
App.save("gameStates", {
// ...App.state.gameStates,
[id]: gameState
});
});
},
error(err) {
App.err = err;
App.route("");
},
route(path) {
if (path === location.hash.slice(1))
App.update();
else
location.hash = path;
},
save(key, val) {
this.state[key] = val;
localStorage[key] = JSON.stringify(val);
App.update();
},
set(state) {
Object.assign(App.state, state);
if (App.state.latestSet) {
// Default sets to the latest set.
const defaultSetCode = App.state.latestSet.code;
const replicateDefaultSet = (desiredLength) => times(desiredLength, constant(defaultSetCode));
const initializeIfEmpty = (sets, desiredLength) => {
if (sets.length === 0) {
sets.push(...replicateDefaultSet(desiredLength));
}
};
initializeIfEmpty(App.state.setsSealed, 6);
initializeIfEmpty(App.state.setsDraft, 3);
initializeIfEmpty(App.state.setsDecadentDraft, 36);
}
App.update();
},
update() {
if(App.component) {
App.component.setState(App.state);
}
},
_emit(...args) {
return App.emit.bind(App, ...args);
},
_save(key, val) {
return App.save.bind(App, key, val);
},
link(key, index) {
let hasIndex = index !== void 0;
let value = App.state[key];
if (hasIndex)
value = value[index];
function requestChange(val) {
if (hasIndex) {
let tmp = App.state[key];
tmp[index] = val;
val = tmp;
}
App.save(key, val);
}
return { requestChange, value };
},
updateGameInfos({type, sets, packsInfo, picksPerPack}) {
const savename = type === "draft" ? sets[0] + "-draft" : type;
const date = new Date();
const currentTime = date.toISOString().slice(0, 10).replace("T", " ") + "_" + date.toString().slice(16, 21).replace(":", "-");
const filename = `${savename.replace(/\W/, "-")}_${currentTime}`;
App.set({
filename,
game: {type, sets, packsInfo },
picksPerPack
});
},
getZone(zoneName){
return App.state.gameState.get(zoneName);
},
getSortedZone(zoneName) {
return App.state.gameState.getSortedZone(zoneName, App.state.sort);
}
};
export default App;