-
Notifications
You must be signed in to change notification settings - Fork 14
/
teleport.js
465 lines (430 loc) · 11.1 KB
/
teleport.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
/**
* wrapper to cleanly inject the code and pass the gameSpace context to the developer
* we keep the wrapper for future breaking updates
*/
function wrapper(fn) {
fn(gameSpace);
}
/**
* teleports n time around the map
* @param n: number : number of time to teleport around the map
* @param delay: number: (optional) number of ms between each teleport (defaults to 690)
*/
function breakAnkles(n, delay) {
let dim // dimensions of the current map
wrapper(gameSpace => {
[x, y] = gameSpace.mapState[gameSpace.mapId].dimensions
dim = { x, y }
})
for (let i = 0; i < n; i++) {
setTimeout(() => teleport(Math.round(Math.random() * dim.x), Math.round(Math.random() * dim.y)), (delay ?? 690) * i)
}
}
/**
* teleports the user to the designated location
*/
function teleport(x, y, space) {
wrapper((gameSpace) => {
if (!space) space = gameSpace.mapId || undefined; // set space to current space if undefined
game.teleport(space, x, y)
})
}
/**
* teleport to your desk
* Note: you must have first set your desk position with the `setDesk()` function
*/
function desk() {
const desk = window.localStorage.getItem("desk")
if (!desk) {
console.error("Desk not set use the setDesk() function")
return
}
const { x, y, mapId } = JSON.parse(desk);
teleport(x, y, mapId)
}
/**
* Get the current possiton as an object of the form
* {
* x: int,
* y: int,
* mapId: string
* }
*/
function position() {
let position
wrapper((gameSpace) => {
const { x, y } = gameSpace.gameState[gameSpace.id]
position = { x, y, mapId: gameSpace.mapId }
})
return position
}
/**
* Get the current map
*/
function getMap() {
let map
wrapper((gameSpace) => {
map = gameSpace.mapId
})
return map
}
/* returns the maps available
* returns the map ids and their dimensions
* {
* <mapName>:{
* id: <mapId>
* sizeX: <X size>
* sizeY: <Y size>
* }
* }
*/
function getMaps() {
let maps
wrapper((gameSpace) => {
maps = gameSpace.mapState
})
maps = Object.values(maps)
.map(m => ({ id: m.id, sizeX: m.dimensions[0], sizeY: m.dimensions[1] }))
.reduce((acc, cur) => ({ ...acc, [cur.id]: cur }), {})
return maps
}
/**
* lists the maps available
* lists the map ids, their dimensions
*/
function listMaps() {
console.table(getMaps())
}
/**
* get your player information
*/
function getPlayer() {
let player
wrapper((gameSpace) => {
const p = gameSpace.gameState[gameSpace.id]
player = {
id: gameSpace.id,
name: p.name,
emojiStatus: p.emojiStatus,
map: p.map,
x: p.x,
y: p.y,
}
})
return player
}
/* returns an array of players online
* [
* {
* id: <player id>
* name: <player name>
* emojiStatus: <players emojiStatus if available
* map: <player current map name>
* x: <position X>
* y: <position Y>
* }
* ]
*/
function getPlayers() {
let players = []
wrapper((gameSpace) => {
players = Object.keys(gameSpace.gameState)
.map(id => {
const p = gameSpace.gameState[id]
return {
id,
name: p.name,
emojiStatus: p.emojiStatus,
map: p.map,
x: p.x,
y: p.y,
}
})
})
return players
}
/*
* get maps containing an item with a certain name.
*/
function getMapsWithItemName(itemName) {
let mapsWithItemName = [];
wrapper((gameSpace) => {
const mapKeys = Object.keys(gameSpace.mapState);
mapsWithItemName = mapKeys
.filter(
key =>
Object.values(gameSpace.mapState[key].objects).filter(o =>
(o._name || "").includes(itemName)
).length > 0
)
.map(key => gameSpace.mapState[key]);
})
return mapsWithItemName;
}
/*
* activates ghost mode until g is pressed.
*/
function ghost() {
game.ghost(1)
}
/**
* teleports the user to the players name or id if it exists
*/
function teleportToPlayer(name) {
const selectedPlayer = findPlayer(name)
teleport(selectedPlayer.x, selectedPlayer.y, selectedPlayer.map)
}
/**
* teleports the user to the map's spawn location if it exists
*/
function teleportToSpawn(mapId) {
let maps = []
wrapper((gameSpace) => {
maps = gameSpace.mapState
const selectedMap = maps[mapId]
if (!selectedMap) {
console.error(`Cannot find map with id ${mapId}`)
return
}
const spawns = selectedMap.spawns
teleport(spawns[0].x, spawns[0].y, mapId)
})
}
/**
* lists online players in console
*/
function listPlayers() {
console.table(getPlayers())
}
/**
* Set your desk location to your current position
*/
function setDesk() {
const pos = position()
window.localStorage.setItem("desk", JSON.stringify(pos))
}
/**
* teleports the user to a toilet of their choice
* NOTE: You must save your custom toilet object with the word Toilet in it
*/
function shit() {
const mapsWithShitters = getMapsWithItemName("Toilet");
const mapIdsWithIndexes = mapsWithShitters.map((m, idx) => `${idx}: ${m.id}`);
const selectedMapIdOrIndex = prompt(
`In which room would you like to go to the bathroom? Enter index or name. \nAvailable rooms:\n${mapIdsWithIndexes.join(
"\n"
)}`
);
// get map
const selectedMap =
mapsWithShitters[selectedMapIdOrIndex] ||
mapsWithShitters[
mapsWithShitters.findIndex(m => m.id === selectedMapIdOrIndex)
];
if (!selectedMap) {
console.error("Your input is invalid.");
return;
}
// get toilets
const shitters = selectedMap.objects.filter(o =>
(o._name || "").includes("Toilet")
);
if (shitters.length < 1) {
console.error("Map doesn't have a bathroom.");
}
// get non-occupied toilets
const currentPlayersInMap = getPlayers().filter(
p => p.map === selectedMap.id
);
shitters.forEach((s, shitterIndex) => {
const playerTakingAShit = currentPlayersInMap.filter(
p => p.x === s.x && p.y === s.y
)[0];
shitters[shitterIndex].occupied = playerTakingAShit;
});
let availableShitterIndexes = [];
for (let i = 0; i < shitters.length; i++) {
if (!shitters[i].occupied) {
availableShitterIndexes.push(i + 1);
}
}
const selectedShitterIndex = prompt(
`Which seat would you like to take?\nAvailable seats: ${availableShitterIndexes.join(
", "
)}`
);
if (!shitters[selectedShitterIndex - 1]) {
alert("Toilet isn't valid.");
} else {
teleport(
shitters[selectedShitterIndex - 1].x,
shitters[selectedShitterIndex - 1].y,
selectedMap.id
);
}
}
/*
* teleports the user to an available seat at a bar of their choice.
*/
function shot() {
const mapsWithBarStools = getMapsWithItemName("bar-stool");
const mapIdsWithIndexes = mapsWithBarStools.map(
(m, idx) => `${idx}: ${m.id}`
);
const selectedMapIdOrIndex = prompt(
`Which bar would you like to go to? Enter index or name. \nAvailable bars:\n${mapIdsWithIndexes.join(
"\n"
)}`
);
// get map
const selectedMap =
mapsWithBarStools[selectedMapIdOrIndex] ||
mapsWithBarStools[
mapsWithBarStools.findIndex(m => m.id === selectedMapIdOrIndex)
];
if (!selectedMap) {
console.error("Your input is invalid.");
return;
}
// get bar stools
const barStools = selectedMap.objects.filter(o =>
(o._name || "").includes("bar-stool")
);
if (barStools.length < 1) {
console.error("Map doesn't have a bar.");
return;
}
// get non-occupied bar stools
const currentPlayersAtBar = getPlayers().filter(
p => p.map === selectedMap.id
);
barStools.forEach((s, seatIndex) => {
const playerAtSeat = currentPlayersAtBar.filter(
p => p.x === s.x && p.y === s.y
)[0];
barStools[seatIndex].occupied = playerAtSeat;
});
let availableBarStoolsIndexes = [];
for (let i = 0; i < barStools.length; i++) {
if (!barStools[i].occupied) {
availableBarStoolsIndexes.push(i + 1);
}
}
const selectedStoolIndex = prompt(
`Which seat would you like to take?\nAvailable seats: ${availableBarStoolsIndexes.join(
", "
)}`
);
if (!barStools[selectedStoolIndex - 1]) {
alert("Seat isn't valid.");
} else {
teleport(
barStools[selectedStoolIndex - 1].x,
barStools[selectedStoolIndex - 1].y,
selectedMap.id
);
}
}
/*
* teleports you to a random dnd tile in the same room.
*/
function dnd() {
const DND_TILE_NAME = "dnd-tile";
let dndTiles;
let currentMapId;
wrapper(gameSpace => {
dndTiles = Object.values(gameSpace.mapState[gameSpace.mapId].objects).filter(o =>
(o._name || "").includes(DND_TILE_NAME)
);
currentMapId = gameSpace.mapId;
});
if (dndTiles.length < 1) {
console.error("No DND tiles found on current map.");
return;
}
const currentPlayersInRoom = getPlayers().filter(p => p.map === currentMapId);
dndTiles.forEach((t, tileIndex) => {
const playerOnTile = currentPlayersInRoom.filter(
p => p.x === t.x && p.y === t.y
)[0];
dndTiles[tileIndex].occupied = playerOnTile;
});
const availableDNDTiles = dndTiles.filter(t => !t.occupied);
const randomDNDTile =
availableDNDTiles[Math.floor(Math.random() * availableDNDTiles.length + 1)];
teleport(randomDNDTile.x, randomDNDTile.y, currentMapId);
ghost();
}
/**
* Find a specific user
*/
function findPlayer(filter) {
const players = getPlayers()
const selectedPlayer = players.find(p => {
normalize = (w) => w.toLowerCase().replace(' ', '');
return [p.id, p.name, `${p.name} ${p.emojiStatus}`].map(normalize).includes(normalize(filter));
})
if (!selectedPlayer) console.error(`Cannot find player ${filter}`)
return selectedPlayer
}
/**
* Ring a user
*/
function ring(name) {
const player = findPlayer(name)
wrapper((gameSpace) => {
gameSpace.ringUser(player.id)
})
}
/**
* Make some one come to you.
* Still needs to cancel the move of the player that is calling joinMe.
*/
function joinMe(name) {
const player = findPlayer(name)
game.enterWhisper(player.id)
// need to cancel your player move.
}
/*
* update jukebox song
* requires object with jukebox (case-sensitive) as a name
*/
function changeSong(
songURL,
playStartHours,
playStartMinutes,
playStartSeconds
) {
const JUKEBOX_NAME = "jukebox";
let jukebox;
let songToPlay;
let timeToStartPlayingInSeconds;
wrapper(gameSpace => {
jukebox = gameSpace.mapState[gameSpace.mapId].objects.filter(o =>
(o._name || "").includes(JUKEBOX_NAME)
)[0];
});
if (!jukebox || jukebox.length === 0) {
console.error("There are no public jukeboxes in this map!")
return;
}
if (!songURL) {
songToPlay = jukebox.properties.video;
} else {
songToPlay = songURL;
}
if (!playStartHours || !playStartMinutes || !playStartSeconds) {
timeToStartPlayingInSeconds = new Date().getTime() / 1000; // now
} else {
const timeToStartPlaying = new Date();
timeToStartPlaying.setHours(
playStartHours,
playStartMinutes,
playStartSeconds
);
timeToStartPlayingInSeconds = timeToStartPlaying.getTime() / 1000;
}
jukebox.properties.video = songToPlay;
jukebox.properties.startTime._seconds = timeToStartPlayingInSeconds;
jukebox.objectStartTime._seconds = timeToStartPlayingInSeconds;
}