This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
541 lines (515 loc) · 20.3 KB
/
index.js
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import Vue from 'vue'
import Vuex from 'vuex'
import { ipcRenderer } from 'electron'
Vue.use(Vuex)
const colors = [
"#698935",
"#358969",
"#356989",
"#693589",
"#893569",
"#893535",
"#896935"
]
const randomColor = () => {
return colors[Math.floor(Math.random() * colors.length)]
}
const ipcPlugin = (ipc) => {
return store => {
ipc.on("CONNECTED", (event, data) => {
store.commit("setUser", data)
})
ipc.on("USER_EXTRA", (event, data) => {
store.commit("setUserExtra", data)
})
ipc.on("SAVED_USER", (event, data) => {
store.commit("setSavedUserName", data)
})
/*
ipc.on("DEBUG_EVT", (event, data) => {
// eslint-disable-next-line no-console
console.log(data)
})
*/
ipc.on("UPDATE_DOWNLOADED", () => {
store.dispatch("errDialog", {
title: "Update available",
body: "App finished downloading an update and will apply the next time you launch.",
canClose: false
})
})
ipc.on("CONNECTION_LOST", () => {
store.dispatch("errDialog", {
title: "Disconnected from server",
body: "",
canClose: false
})
})
ipc.on("sRoomUpdated", (event, data) => {
store.commit("roomUpdated", data)
})
ipc.on("sRoomFlagsUpdated", (event, data) => {
store.commit("roomFlagsUpdated", data)
})
ipc.on("sRoomDeleted", (event, data) => {
store.commit("resetRoom", data)
})
ipc.on("sUserJoinedRoom", (event, data) => {
store.commit("userJoinedRoom", data)
})
ipc.on("sUserLeftRoom", (event, data) => {
//store.commit("chatMsg", `[System] ${store.state.rooms.users[data.userId]} has left the room.`)
if (data.userId == store.state.user.id) {
store.commit("resetRoom")
}
else {
store.commit("userLeftRoom", data)
}
})
ipc.on("sUserKicked", (event, data) => {
//store.commit("chatMsg", `[System] ${store.state.rooms.users[data.userId]} has been kicked.`)
if (data.userId == store.state.user.id) {
store.commit("resetRoom")
}
else {
store.commit("userLeftRoom", data)
}
})
ipc.on("sUserBanned", (event, data) => {
//store.commit("chatMsg", `[System] ${store.state.rooms.users[data.userId]} has been banned.`)
if (data.userId == store.state.user.id) {
store.commit("resetRoom")
}
else {
store.commit("userLeftRoom", data)
}
})
ipc.on("sUserReadyState", (event, data) => {
//console.log({ gotready: data })
store.commit("userReadyState", data)
})
ipc.on("sRoomAddToList", (event, data) => {
store.commit("addRoom", data.room)
})
ipc.on("sRoomDeleted", (event, data) => {
store.commit("deleteRoom", data.id)
})
ipc.on("sRoomList", (event, data) => {
store.commit("setRooms", data.rooms)
})
ipc.on("sChat", (event, data) => {
store.commit("pushChat", data)
})
/*
ipc.on("sDisconnected", (e, reason) => {
//Show disconnection msg ?
})
*/
}
}
const ipcStuff = ipcPlugin(ipcRenderer)
export default new Vuex.Store({
state: {
defaultFlags: {
0: [
{ id: "sync_perks", name: "Share all perks", tooltip: "When grabbing perks the whole team will also get them.", type: "boolean", value: false },
{ id: "team_perks", name: "Team Perks", tooltip: "When grabbing certain perks (not all) the whole team will also get them.", type: "boolean", value: true },
{ id: "sync_steve", name: "Sync Steve", tooltip: "Angers the gods for everyone.", type: "boolean", value: true },
{ id: "sync_hearts", name: "Sync Hearts", tooltip: "When someone picks up a heart everyone else gets it too.", type: "boolean", value: true },
{ id: "sync_orbs", name: "Sync Orbs", tooltip: "When someone picks up an orb everyone else gets it too.", type: "boolean", value: true },
{ id: "sync_shift", name: "Sync Shifts", tooltip: "When someone fungal shifts everyone also gets the same shift, cooldown also applies.", type: "boolean", value: true },
{ id: "send_wands", name: "Send Wands", tooltip: "Allow players to deposit/take wands.", type: "boolean", value: true },
{ id: "send_flasks", name: "Send Flasks", tooltip: "Allow players to deposit/take flasks.", type: "boolean", value: true },
{ id: "send_gold", name: "Send Gold", tooltip: "Allow players to deposit/take gold.", type: "boolean", value: true },
{ id: "send_items", name: "Send Items", tooltip: "Allow players to deposit/take items.", type: "boolean", value: true },
{ id: "world_randomize_loot", name: "Randomize loot", tooltip: "Only applies when playing on the same seed, makes it so everyone gets different loot.", type: "boolean", value: true },
{ id: "sync_world_seed", name: "Sync Seed", tooltip: "All players play in the same world seed (requires everyone to start a new game) 0 means random seed.", type: "number", value: 0 },
{ id: "death_penalty_end", name: "End run", tooltip: "Run ends for all players when someone dies.", type: "boolean", value: true },
{ id: "death_penalty_weak_respawn", name: "Respawn Penalty", tooltip: "Player respawns and everyone takes a % drop on their max hp, once it goes below certain threshold on the weakest player the run ends for everyone.", type: "boolean", value: true },
{ id: "death_penalty_full_respawn", name: "Respawn", tooltip: "Player will respawn on their last checkpoint and no penalties.", type: "boolean", value: true },
{ id: "_ondeath_kick", name: "Kick on death", tooltip: "Kicks whoever dies, more customisable soon™.", type: "boolean", value: false }
],
2: [
{ id: "ban_ambrosia", name: "Ban Ambrosia", tooltip: "will shift ambrosia away.", type: "boolean", value: true },
{ id: "ban_invis", name: "Ban Invisibility", tooltip: "will shift invisibility away and remove the perk.", type: "boolean", value: true },
{ id: "nemesis_abilities", name: "Nemesis abilities", tooltip: "Abilities will appear in each holy mountain with an NP cost.", type: "boolean", value: true },
{ id: "sync_steve", name: "Sync Steve", tooltip: "Angers the gods for everyone.", type: "boolean", value: false },
{ id: "sync_orbs", name: "Sync Orbs", tooltip: "When someone picks up an orb everyone else gets it too.", type: "boolean", value: false },
{ id: "world_randomize_loot", name: "Randomize loot", tooltip: "Only applies when playing on the same seed, makes it so everyone gets different loot.", type: "boolean", value: true },
{ id: "sync_world_seed", name: "Sync Seed", tooltip: "All players play in the same world seed (requires everyone to start a new game) 0 means random seed.", type: "number", value: 0 },
{ id: "death_penalty_weak_respawn", name: "Last noita standing.", tooltip: "Run ends when there's only one player left.", type: "boolean", value: true },
{ id: "_ondeath_kick", name: "Kick on death (do not disable)", tooltip: "Kicks whoever dies, more customisable soon™.", type: "boolean", value: true }
]
},
gamemodes: {
"0": "Co-op",
"1": "Race",
"2": "Nemesis"
},
user: {
name: "",
id: 0,
extra: 0
},
savedUser: false,
savedUserName: "",
lobbies: [],
room: {
id: "",
name: "",
gamemode: 0,
maxUsers: 0,
password: "",
//users[id]
users: [
/*{
userId: String,
name: String,
owner: Boolean,
readyState: {
ready: Boolean,
seed: String,
mods: [String]
}
}*/
]
},
roomFlags: [],
roomChat: [],
loading: false,
joining: false,
errDialog: {
title: "",
body: "",
canClose: true
},
showErrDialog: false
},
getters: {
isHost: (state) => {
const users = state.room.users
for (const user of users) {
if (user.userId == state.user.id) {
return user.owner ? true : false
}
}
return false
},
userName: (state) => {
return state.user.name
},
userId: (state) => {
return state.user.id
},
userExtra: (state) => {
return state.user.extra
},
roomId: (state) => {
return state.room.id
},
roomName: (state) => {
return state.room.name
},
roomGamemode: (state) => {
return state.room.gamemode
},
roomMaxUsers: (state) => {
return state.room.maxUsers
},
roomHasPassword: (state) => {
return state.room.protected
},
flags: (state) => {
const mode = state.room.gamemode
const fDefaults = state.defaultFlags[mode]
return fDefaults.map(flag => {
const found = state.roomFlags.find(f => f.id == flag.id)
if (!found && flag.type == "boolean") {
return { ...flag, value: false }
}
else if (found) {
return flag
}
else { return undefined }
}).filter(v => typeof v !== "undefined")
}
},
mutations: {
setSavedUserName: (state, value) => {
if (value) {
state.savedUser = true
}
state.savedUserName = value
},
setLoading: (state, value) => {
state.loading = value
},
joinState: (state, payload) => {
state.joining = payload
},
userReadyState: (state, payload) => {
state.room.users = state.room.users.map(user => {
if (user.userId == payload.userId) {
user.ready = payload.ready
user.seed = payload.seed
user.mods = payload.mods
user.version = payload.version,
user.beta = payload.beta
}
return user
})
},
setUser: (state, payload) => {
state.user.name = payload.display_name
state.user.id = payload.id
},
setUserExtra: (state, payload) => {
state.user.extra = payload
},
addRoom: (state, payload) => {
state.lobbies.push(payload)
},
deleteRoom: (state, id) => {
state.lobbies = state.lobbies.filter(room => room.id != id)
},
setRooms: (state, payload) => {
state.lobbies = payload
},
setRoom: (state, payload) => {
state.room = payload
for (const user of state.room.users) {
user.color = randomColor()
}
},
roomUpdated: (state, payload) => {
let room = Object.assign(state.room)
state.room = Object.assign(room, payload)
},
roomFlagsUpdated: (state, payload) => {
const mode = state.room.gamemode
const fDefaults = state.defaultFlags[mode]
if (!fDefaults) { return }
state.roomFlags = payload.flags.map(val => {
const flag = fDefaults.find(f => f.id == val.flag)
if (!flag) { return }
else {
if (typeof val.value == "number") { flag.value = val.value }
if (flag.type == "boolean") { flag.value = true }
return flag
}
}).filter(v => typeof v !== "undefined")
},
resetRoom: (state) => {
state.room = {
id: "",
name: "",
gamemode: 0,
maxUsers: 0,
password: "",
users: [],
locked: false
}
state.roomFlags = []
state.roomChat = []
},
userJoinedRoom: (state, payload) => {
state.room.users.push({
userId: payload.userId,
name: payload.name,
owner: false,
color: randomColor(),
readyState: {
ready: false,
seed: "",
mods: []
}
})
},
userLeftRoom: (state, payload) => {
const users = state.room.users
for (const [i, user] of users.entries()) {
if (user.userId == payload.userId) {
users.splice(i, 1)
}
}
},
setErrDialog: (state, payload) => {
state.errDialog = payload
},
showErrDialog: (state, payload) => {
state.showErrDialog = payload
},
pushChat: (state, payload) => {
const time = new Date()
let timeStr = ("0" + time.getHours()).slice(-2) + ":" + ("0" + time.getMinutes()).slice(-2)
const found = state.room.users.find(user => user.userId == payload.userId)
let color = randomColor()
color = found && found.color || color
if (payload.userId === "-1") { color = "#e69569" }
state.roomChat.push({
id: payload.id,
time: timeStr,
userId: payload.userId,
name: payload.name.trim(),
message: payload.message.trim(),
color
})
if (state.roomChat.length > 250) {
state.roomChat.shift()
}
},
setDefaultFlags: (state, mode) => {
if (mode == 0 || mode == 2) {
state.roomFlags = [...state.defaultFlags[mode]]
}
}
},
actions: {
continueSavedUser: ({ state, commit, dispatch }) => {
commit("setLoading", true)
ipcRenderer.send("TRY_LOGIN", state.savedUserName)
ipcRenderer.once("TRY_LOGIN_FAILED", () => {
dispatch("errDialog", {
title: "Failed to login",
body: "Login manually with the remember me checkbox checked to refresh your login info.",
canClose: true
})
commit("setLoading", false)
})
},
errDialog: ({ commit }, payload) => {
commit("setErrDialog", payload)
commit("showErrDialog", true)
},
joinRoom: ({ dispatch, commit }, payload) => {
commit("setLoading", true)
ipcRenderer.send("CLIENT_MESSAGE", { key: "cJoinRoom", payload })
ipcRenderer.once("sJoinRoomSuccess", (event, data) => {
commit("setRoom", data)
commit("setLoading", false)
})
ipcRenderer.once("sJoinRoomFailed", (event, data) => {
dispatch("errDialog", {
title: "Failed to join room",
body: data.reason,
canClose: true
})
commit("setLoading", false)
})
},
createRoom: async ({ commit, dispatch }, payload) => {
commit("setLoading", true)
ipcRenderer.send("CLIENT_MESSAGE", { key: "cRoomCreate", payload })
ipcRenderer.once("sRoomCreated", (event, data) => {
commit("setDefaultFlags", data.gamemode)
commit("setRoom", data)
commit("setLoading", false)
dispatch("sendFlags")
return true
})
ipcRenderer.once("sRoomCreateFailed", (event, data) => {
dispatch("errDialog", {
title: "Failed to create room",
body: data.reason,
canClose: true
})
commit("setLoading", false)
return false
})
},
updateRoom: async ({ dispatch, commit }, payload) => {
commit("setLoading", true)
ipcRenderer.send("CLIENT_MESSAGE", { key: "cRoomUpdate", payload })
ipcRenderer.once("sRoomUpdated", () => {
commit("setLoading", false)
return true
})
ipcRenderer.once("sRoomUpdateFailed", (event, data) => {
dispatch("errDialog", {
title: "Failed to update room",
body: data.reason,
canClose: true
})
commit("setLoading", false)
return false
})
},
leaveRoom: async ({ getters, commit }) => {
commit("setLoading", true)
const key = getters.isHost ? "cRoomDelete" : "cLeaveRoom"
const payload = {
id: getters.roomId,
userId: getters.userId
}
ipcRenderer.send("CLIENT_MESSAGE", { key, payload })
const evt = getters.isHost ? "sRoomDeleted" : "sUserLeftRoom"
ipcRenderer.once(evt, (event, data) => {
if (evt == "sUserLeftRoom") {
if (data.userId == getters.userId) {
commit("setLoading", false)
return true
}
}
else {
commit("setLoading", false)
return true
}
})
},
kickUser: async ({ commit }, payload) => {
commit("setLoading", true)
ipcRenderer.send("CLIENT_MESSAGE", { key: "cKickUser", payload })
ipcRenderer.on("sUserKicked", () => {
commit("setLoading", false)
return true
})
},
banUser: async ({ commit }, payload) => {
commit("setLoading", true)
ipcRenderer.send("CLIENT_MESSAGE", { key: "cBanUser", payload })
ipcRenderer.on("sUserBanned", () => {
commit("setLoading", false)
return true
})
},
requestRooms: async (context, payload) => {
ipcRenderer.send("CLIENT_MESSAGE", {
key: "cRequestRoomList",
payload: { page: payload && payload > 0 ? payload : 0 }
})
},
sendChat: ({ commit, state }, payload) => {
ipcRenderer.send("CLIENT_CHAT", {
key: "cChat",
payload
})
commit("pushChat", {
id: payload.id,
userId: state.user.id,
name: state.user.name.trim(),
message: payload.message.trim()
})
},
sendFlags: ({ getters }) => {
const flags = getters.flags.map(val => {
let flag = { flag: val.id }
if (typeof val.value == "number") { flag.uIntVal = val.value }//temp fix
if (val.type == "boolean" && !val.value) {
flag = undefined
}
return flag
}).filter(v => typeof v !== "undefined")
ipcRenderer.send("CLIENT_MESSAGE", {
key: "cRoomFlagsUpdate",
payload: { flags }
})
},
startRun: (context, payload) => {
ipcRenderer.send("CLIENT_MESSAGE", {
key: "cStartRun",
payload
})
}
},
plugins: [ipcStuff]
})