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
542
543
544
545
546
547
|
/* eslint indent: ["error", 2, { "SwitchCase": 1 }] */
import { client, delay } from "../client";
import { UPDATE_INTERVAL } from "../client";
import setEmote from "../client/setEmote";
import setEmoteFromUrl from "../client/setEmoteFromUrl";
import { AO_HOST } from "../client/aoHost";
import { Viewport } from "./interfaces/Viewport";
import { createBlipsChannels } from "./utils/createBlipChannels";
import { defaultChatMsg } from "./constants/defaultChatMsg";
import { createMusic } from "./utils/createMusic";
import { createSfxAudio } from "./utils/createSfxAudio";
import { createShoutAudio } from "./utils/createShoutAudio";
import { createTestimonyAudio } from "./utils/createTestimonyAudio";
import { Testimony } from "./interfaces/Testimony";
import { COLORS } from "./constants/colors";
import { set_side } from "./utils/setSide";
import { ChatMsg } from "./interfaces/ChatMsg";
import {
setStartFirstTickCheck,
setStartSecondTickCheck,
startFirstTickCheck,
startSecondTickCheck,
} from "./utils/handleICSpeaking";
const viewport = (): Viewport => {
let animating = false;
const blipChannels = createBlipsChannels();
let chatmsg = defaultChatMsg;
let currentBlipChannel = 0;
let lastChar = "";
let lastEvi = 0;
const music = createMusic();
let sfxAudio = createSfxAudio();
let sfxplayed = 0;
let shoutTimer = 0;
const shoutaudio = createShoutAudio();
const testimonyAudio = createTestimonyAudio();
let testimonyTimer = 0;
let testimonyUpdater: any;
let textnow = "";
let theme: string;
let tickTimer = 0;
let updater: any;
let backgroundName = "";
const getSfxAudio = () => sfxAudio;
const setSfxAudio = (value: HTMLAudioElement) => {
sfxAudio = value;
};
const getBackgroundName = () => backgroundName;
const setBackgroundName = (value: string) => {
backgroundName = value;
};
const getBackgroundFolder = () =>
`${AO_HOST}background/${encodeURI(backgroundName)}/`;
const getTextNow = () => {
return textnow;
};
const setTextNow = (val: string) => {
textnow = val;
};
const getChatmsg = () => {
return chatmsg;
};
const setChatmsg = (val: ChatMsg) => {
chatmsg = val;
};
const getSfxPlayed = () => sfxplayed;
const setSfxPlayed = (val: number) => {
sfxplayed = val;
};
const getTickTimer = () => tickTimer;
const setTickTimer = (val: number) => {
tickTimer = val;
};
const getAnimating = () => animating;
const setAnimating = (val: boolean) => {
animating = val;
};
const getLastEvidence = () => lastEvi;
const setLastEvidence = (val: number) => {
lastEvi = val;
};
const setLastCharacter = (val: string) => {
lastChar = val;
};
const getLastCharacter = () => lastChar;
const getShoutTimer = () => shoutTimer;
const setShoutTimer = (val: number) => {
shoutTimer = val;
};
const getTheme = () => theme;
const setTheme = (val: string) => {
theme = val;
};
const getTestimonyTimer = () => testimonyTimer;
const setTestimonyTimer = (val: number) => {
testimonyTimer = val;
};
const setTestimonyUpdater = (val: any) => {
testimonyUpdater = val;
};
const getTestimonyUpdater = () => testimonyUpdater;
const playSFX = async (sfxname: string, looping: boolean) => {
sfxAudio.pause();
sfxAudio.loop = looping;
sfxAudio.src = sfxname;
sfxAudio.play().catch(() => {});
};
/**
* Updates the testimony overaly
*/
const updateTestimony = () => {
const testimonyFilenames: Testimony = {
1: "witnesstestimony",
2: "crossexamination",
3: "notguilty",
4: "guilty",
};
// Update timer
testimonyTimer += UPDATE_INTERVAL;
const testimony = testimonyFilenames[client.testimonyID];
const resource = client.resources[testimony];
if (!resource) {
disposeTestimony();
return;
}
if (testimonyTimer >= resource.duration) {
disposeTestimony();
} else {
testimonyUpdater = setTimeout(() => updateTestimony(), UPDATE_INTERVAL);
}
};
/**
* Dispose the testimony overlay
*/
const disposeTestimony = () => {
client.testimonyID = 0;
testimonyTimer = 0;
document.getElementById("client_testimony").style.opacity = "0";
clearTimeout(testimonyUpdater);
};
const handleTextTick = async (charLayers: HTMLImageElement) => {
const chatBox = document.getElementById("client_chat");
const waitingBox = document.getElementById("client_chatwaiting");
const chatBoxInner = document.getElementById("client_inner_chat");
const charName = chatmsg.name;
const charEmote = chatmsg.sprite;
if (chatmsg.content.charAt(textnow.length) !== " ") {
blipChannels[currentBlipChannel].play().catch(() => {});
currentBlipChannel++;
currentBlipChannel %= blipChannels.length;
}
textnow = chatmsg.content.substring(0, textnow.length + 1);
const characterElement = chatmsg.parsed[textnow.length - 1];
if (characterElement) {
const COMMAND_IDENTIFIER = "\\";
const nextCharacterElement = chatmsg.parsed[textnow.length];
const flash = async () => {
const effectlayer = document.getElementById("client_fg");
const realizationUrl = chatmsg.preloadedAssets?.realizationSfxUrl
?? `${AO_HOST}sounds/general/sfx-realization.opus`;
playSFX(realizationUrl, false);
effectlayer.style.animation = "flash 0.4s 1";
await delay(400);
effectlayer.style.removeProperty("animation");
};
const shake = async () => {
const gamewindow = document.getElementById("client_gamewindow");
const stabUrl = chatmsg.preloadedAssets?.stabSfxUrl
?? `${AO_HOST}sounds/general/sfx-stab.opus`;
playSFX(stabUrl, false);
gamewindow.style.animation = "shake 0.2s 1";
await delay(200);
gamewindow.style.removeProperty("animation");
};
const pause = async (digits?: string) => {
// Default to 100ms if no number specified
const multiplier = !digits || digits === "" ? 1 : parseInt(digits, 10) || 1;
await delay(multiplier * 100);
};
const commands = new Map(
Object.entries({
s: shake,
f: flash,
p: pause,
}),
);
const textSpeeds = new Set(["{", "}"]);
// Changing Text Speed
if (textSpeeds.has(characterElement.innerHTML)) {
// Grab them all in a row
const MAX_SLOW_CHATSPEED = 120;
for (let i = textnow.length; i < chatmsg.content.length; i++) {
const currentCharacter = chatmsg.parsed[i - 1].innerHTML;
if (currentCharacter === "}") {
if (chatmsg.speed > 0) {
chatmsg.speed -= 20;
}
} else if (currentCharacter === "{") {
if (chatmsg.speed < MAX_SLOW_CHATSPEED) {
chatmsg.speed += 20;
}
} else {
// No longer at a speed character
textnow = chatmsg.content.substring(0, i);
break;
}
}
}
if (
characterElement.innerHTML === COMMAND_IDENTIFIER &&
(commands.has(nextCharacterElement?.innerHTML) ||
nextCharacterElement?.innerHTML === "p")
) {
textnow = chatmsg.content.substring(0, textnow.length + 1);
const commandChar = nextCharacterElement.innerHTML;
if (commandChar === "p") {
// Collect digits after \p for pause duration
const startPos = textnow.length;
let digits = "";
let offset = 1;
while (
startPos + offset <= chatmsg.content.length &&
/\d/.test(chatmsg.parsed[startPos + offset - 1]?.innerHTML || "")
) {
digits += chatmsg.parsed[startPos + offset - 1].innerHTML;
textnow = chatmsg.content.substring(0, startPos + offset);
offset++;
}
await pause(digits);
} else {
await commands.get(commandChar)();
}
} else {
chatBoxInner.appendChild(chatmsg.parsed[textnow.length - 1]);
}
}
// scroll to bottom
chatBox.scrollTop = chatBox.scrollHeight;
if (textnow === chatmsg.content) {
animating = false;
if (chatmsg.preloadedAssets) {
setEmoteFromUrl(chatmsg.preloadedAssets.idleUrl, false, chatmsg.side);
} else {
setEmote(AO_HOST, client, charName, charEmote, "(a)", false, chatmsg.side);
}
charLayers.style.opacity = "1";
waitingBox.style.opacity = "1";
clearTimeout(updater);
}
};
/**
* Updates the chatbox based on the given text.
*
* OK, here's the documentation on how this works:
*
* 1 _animating
* If we're not done with this characters animation, i.e. his text isn't fully there, set a timeout for the next tick/step to happen
*
* 2 startpreanim
* If the shout timer is over it starts with the preanim
* The first thing it checks for is the shake effect (TODO on client this is handled by the @ symbol and not a flag )
* Then is the flash/realization effect
* After that, the shout image set to be transparent
* and the main characters preanim gif is loaded
* If pairing is supported the paired character will just stand around with his idle sprite
*
* 3 preanimdelay over
* this animates the evidence popup and finally shows the character name and message box
* it sets the text color and the character speaking sprite
*
* 4 textnow != content
* this adds a character to the textbox and stops the animations if the entire message is present in the textbox
*
* 5 sfx
* independent of the stuff above, this will play any sound effects specified by the emote the character sent.
* happens after the shout delay + an sfx delay that comes with the message packet
*
* XXX: This relies on a global variable `chatmsg`!
*/
const chat_tick = async () => {
// note: this is called fairly often
// do not perform heavy operations here
await delay(chatmsg.speed);
const gamewindow = document.getElementById("client_gamewindow");
const waitingBox = document.getElementById("client_chatwaiting");
const eviBox = <HTMLImageElement>document.getElementById("client_evi");
const shoutSprite = <HTMLImageElement>(
document.getElementById("client_shout")
);
const effectlayer = <HTMLImageElement>document.getElementById("client_fg");
const chatBoxInner = document.getElementById("client_inner_chat");
let charLayers = <HTMLImageElement>document.getElementById("client_char");
let pairLayers = <HTMLImageElement>(
document.getElementById("client_pair_char")
);
const validSides: string[] = ["def", "pro", "wit"]; // these are for the full view pan, the other positions use 'client_char'
if (validSides.includes(chatmsg.side)) {
charLayers = <HTMLImageElement>(
document.getElementById(`client_${chatmsg.side}_char`)
);
pairLayers = <HTMLImageElement>(
document.getElementById(`client_${chatmsg.side}_pair_char`)
);
}
const charName = chatmsg.name;
const charEmote = chatmsg.sprite;
const pairName = chatmsg.other_name;
const pairEmote = chatmsg.other_emote;
// TODO: preanims sometimes play when they're not supposed to
const isShoutOver = tickTimer >= shoutTimer;
const isShoutAndPreanimOver =
tickTimer >= shoutTimer + chatmsg.preanimdelay;
if (isShoutOver && startFirstTickCheck) {
// Effect stuff
if (chatmsg.screenshake === 1) {
// Shake screen
const stabUrl = chatmsg.preloadedAssets?.stabSfxUrl
?? `${AO_HOST}sounds/general/sfx-stab.opus`;
playSFX(stabUrl, false);
gamewindow.style.animation = "shake 0.2s 1";
}
if (chatmsg.flash === 1) {
// Flash screen
const realizationUrl = chatmsg.preloadedAssets?.realizationSfxUrl
?? `${AO_HOST}sounds/general/sfx-realization.opus`;
playSFX(realizationUrl, false);
effectlayer.style.animation = "flash 0.4s 1";
}
// Pre-animation stuff
if (chatmsg.preanimdelay > 0) {
shoutSprite.style.display = "none";
shoutSprite.style.animation = "";
if (chatmsg.preloadedAssets) {
setEmoteFromUrl(chatmsg.preloadedAssets.preanimUrl, false, chatmsg.side);
} else {
const preanim = chatmsg.preanim;
setEmote(AO_HOST, client, charName, preanim, "", false, chatmsg.side);
}
}
if (chatmsg.other_name) {
pairLayers.style.opacity = "1";
} else {
pairLayers.style.opacity = "0";
}
// Done with first check, move to second
setStartFirstTickCheck(false);
setStartSecondTickCheck(true);
chatmsg.startpreanim = false;
chatmsg.startspeaking = true;
}
const hasNonInterruptingPreAnim = chatmsg.noninterrupting_preanim === 1;
if (textnow !== chatmsg.content && hasNonInterruptingPreAnim) {
const chatContainerBox = document.getElementById("client_chatcontainer");
chatContainerBox.style.opacity = "1";
await handleTextTick(charLayers);
} else if (isShoutAndPreanimOver && startSecondTickCheck) {
if (chatmsg.startspeaking) {
chatmsg.startspeaking = false;
// Evidence Bullshit
if (chatmsg.evidence > 0) {
// Prepare evidence
eviBox.src = client.evidences[chatmsg.evidence - 1].icon;
eviBox.style.width = "auto";
eviBox.style.height = "36.5%";
eviBox.style.opacity = "1";
testimonyAudio.src = `${AO_HOST}sounds/general/sfx-evidenceshoop.opus`;
testimonyAudio.play().catch(() => {});
if (chatmsg.side === "def") {
// Only def show evidence on right
eviBox.style.right = "1em";
eviBox.style.left = "initial";
} else {
eviBox.style.right = "initial";
eviBox.style.left = "1em";
}
}
chatBoxInner.className = `text_${COLORS[chatmsg.color]}`;
if (chatmsg.preanimdelay === 0) {
shoutSprite.style.display = "none";
shoutSprite.style.animation = "";
}
switch (Number(chatmsg.deskmod)) {
case 2:
set_side({
position: chatmsg.side,
showSpeedLines: false,
showDesk: true,
});
break;
case 3:
set_side({
position: chatmsg.side,
showSpeedLines: false,
showDesk: false,
});
break;
case 4:
set_side({
position: chatmsg.side,
showSpeedLines: false,
showDesk: true,
});
break;
case 5:
set_side({
position: chatmsg.side,
showSpeedLines: false,
showDesk: false,
});
break;
}
if (chatmsg.other_name) {
if (chatmsg.preloadedAssets) {
setEmoteFromUrl(chatmsg.preloadedAssets.pairIdleUrl, true, chatmsg.side);
} else {
setEmote(AO_HOST, client, pairName, pairEmote, "(a)", true, chatmsg.side);
}
pairLayers.style.opacity = "1";
} else {
pairLayers.style.opacity = "0";
}
if (chatmsg.preloadedAssets) {
setEmoteFromUrl(chatmsg.preloadedAssets.talkingUrl, false, chatmsg.side);
} else {
setEmote(AO_HOST, client, charName, charEmote, "(b)", false, chatmsg.side);
}
charLayers.style.opacity = "1";
if (textnow === chatmsg.content) {
if (chatmsg.preloadedAssets) {
setEmoteFromUrl(chatmsg.preloadedAssets.idleUrl, false, chatmsg.side);
} else {
setEmote(AO_HOST, client, charName, charEmote, "(a)", false, chatmsg.side);
}
charLayers.style.opacity = "1";
waitingBox.style.opacity = "1";
animating = false;
clearTimeout(updater);
return;
}
} else if (textnow !== chatmsg.content) {
const chatContainerBox = document.getElementById(
"client_chatcontainer",
);
chatContainerBox.style.opacity = "1";
await handleTextTick(charLayers);
}
}
if (!sfxplayed && chatmsg.snddelay + shoutTimer >= tickTimer) {
sfxplayed = 1;
if (
chatmsg.sound !== "0" &&
chatmsg.sound !== "1" &&
chatmsg.sound !== "" &&
chatmsg.sound !== undefined &&
(chatmsg.type == 1 || chatmsg.type == 2 || chatmsg.type == 6)
) {
const sfxUrl = chatmsg.preloadedAssets?.emoteSfxUrl
?? `${AO_HOST}sounds/general/${encodeURI(chatmsg.sound)}.opus`;
playSFX(sfxUrl, chatmsg.looping_sfx);
}
}
if (textnow === chatmsg.content && !startFirstTickCheck && !startSecondTickCheck) {
return;
}
if (animating) {
chat_tick();
}
tickTimer += UPDATE_INTERVAL;
};
return {
getTextNow,
setTextNow,
getChatmsg,
setChatmsg,
getSfxPlayed,
setSfxPlayed,
setTickTimer,
getTickTimer,
setAnimating,
getAnimating,
getLastEvidence,
setLastEvidence,
setLastCharacter,
getLastCharacter,
getShoutTimer,
setShoutTimer,
setTheme,
getTheme,
setTestimonyTimer,
getTestimonyTimer,
setTestimonyUpdater,
getTestimonyUpdater,
testimonyAudio,
chat_tick,
playSFX,
set_side,
setBackgroundName,
updateTestimony,
disposeTestimony,
handleTextTick,
getBackgroundFolder,
getBackgroundName,
getSfxAudio,
setSfxAudio,
blipChannels,
music,
shoutaudio,
updater,
};
};
export default viewport;
|