-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
1346 lines (1229 loc) · 56.9 KB
/
app.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This web app is designed for Chrome, some animations are disabled in Firefox due to performance issues in Firefox.
const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1
const reduceAnimations = isFirefox
// String literals
const ON_PLANCHETTE = 'onPlanchette'
const ON_USER_MESSAGE = 'onUserMessage'
const ON_BUTTON = 'onButton'
const ON_TEXT_INPUT = 'onTextInput'
const SHUT_UP = 'shutUp'
const RECORDING = 'recording'
const TURN_SPIRIT = 'turnSpirit'
const TURN_USER = 'turnUser'
const OUIJA_USER_ID = 'ouija-user-id'
const OUIJA_PLAYER_NAME = 'ouija-player-name'
const OUIJA_SUICIDE_POPUP_SHOWN_ONCE = 'ouija-suicide-popup-shown-once'
const OUIJA_SUICIDE_PREVENTION_ACTIVATED = 'ouija-suicide-prevention-activated'
// Achievements!
const FALSE_PROPHETS_ACHIEVEMENT = 'achievementFalseProphets'
const EASTER_EGG_ACHIEVEMENT = 'achievementEasterEgg'
const POSSESSED_ACHIEVEMENT = 'achievementPossessed'
const allAchievements = [EASTER_EGG_ACHIEVEMENT, POSSESSED_ACHIEVEMENT, FALSE_PROPHETS_ACHIEVEMENT]
// Global state
let loadingOverlayDisabled = false
let gameOver = false
let popupIsOpen = false
let showTips = true
let easyMode = false
let easterEggVisible = false
let draggingPlanchette = false
let planchetteTransformX = 0 // In % relative to planchette's own size (to support browser resizing)
let planchetteTransformY = 0 // In % relative to planchette's own size (to support browser resizing)
let prevX = undefined
let prevY = undefined
let offsetX = 0 // In % relative to planchette's size (to support browser resizing consistently with planchetteHelper)
let offsetY = 0 // In % relative to planchette's size (to support browser resizing consistently with planchetteHelper)
let userMoveCount = 0
let remainingGoals = ''
let revealedSpiritLetters = ''
let userMessage = ''
let currentExchangeNumber = 0
let turn = TURN_USER
let hoveringOverTooltip = false
let speedMode = false
let revealMouse = false
let debug = {}
// Magnifier constants
const MAG_ZOOM = 1.2
const MAG_LEFT = 0.465
const MAG_TOP = 0.58
// Offset constants
const SPIRIT_MAX_DIST = 43.0 // Values above 43 will cause confusing UX when target is on different "row" of letters than the player is!
const SPIRIT_STOCHASTIC_STR = 0.6
const SPIRIT_ACCEL_STR = 1.4
const OFFSET_CANCELLATION_STR = 0.3
const OFFSET_CANCELLATION_LOW_URGENCY = 0.3
// Planchette drag limitations (board area)
const HARD_Y_MAX = 80
const HARD_Y_MIN = -160
const HARD_X_MAX = 270
const HARD_X_MIN = -255
const SOFT_Y_MAX = HARD_Y_MAX - 10
const SOFT_Y_MIN = HARD_Y_MIN - 10
const SOFT_X_MAX = HARD_X_MAX - 10
const SOFT_X_MIN = HARD_X_MIN - 10
// User message constants
USER_MESSAGE_MAX_LENGTH = 40
// Goal constants. Coordinates are in % relative to planchette transform.
const ALLOWED_CHARS = 'abcdefghijklmnopqrstuvwxyz1234567890'
const CHAR_SELECT_MAX_DIST = 10
const goalCoords = {
"0": { "x": 96.57, "y": -0.67 },
"1": { "x": -110.95, "y": -1.07 },
"2": { "x": -92.67, "y": -0.27 },
"3": { "x": -69.56, "y": -0.27 },
"4": { "x": -45.9, "y": -0.27 },
"5": { "x": -23.86, "y": -0.27 },
"6": { "x": -1.28, "y": -0.67 },
"7": { "x": 20.23, "y": -0.67 },
"8": { "x": 42.27, "y": -0.27 },
"9": { "x": 67.54, "y": -0.67 },
"a": { "x": -159.6, "y": -58.17 },
"b": { "x": -139.2, "y": -77.69 },
"c": { "x": -115.5, "y": -91.63 },
"d": { "x": -91.94, "y": -101.99 },
"e": { "x": -64.52, "y": -110.36 },
"f": { "x": -35.48, "y": -115.5 },
"g": { "x": -6.57, "y": -116.62 },
"h": { "x": 24.94, "y": -115.17 },
"i": { "x": 52.9, "y": -111.67 },
"j": { "x": 74.94, "y": -105.69 },
"k": { "x": 105.05, "y": -94.14 },
"l": { "x": 130.32, "y": -79.4 },
"m": { "x": 152.9, "y": -63.06 },
"n": { "x": -152.8, "y": -7.68 },
"o": { "x": -136.2, "y": -22.82 },
"p": { "x": -117.9, "y": -36.37 },
"q": { "x": -94.81, "y": -47.92 },
"r": { "x": -65.24, "y": -57.48 },
"s": { "x": -36.75, "y": -63.46 },
"t": { "x": -6.75, "y": -67.17 },
"u": { "x": 24.53, "y": -65.21 },
"v": { "x": 55.17, "y": -60.83 },
"w": { "x": 86.36, "y": -51.67 },
"x": { "x": 111.63, "y": -37.72 },
"y": { "x": 134.21, "y": -24.18 },
"z": { "x": 147.65, "y": -9.44 }
}
// Easter egg coordinates (relative to planchette's size).
const easterEggLocation = {
x: -200.78,
y: 41.95
}
// Tooltip hovering area. Coordinates are relative to planchette's size.
const tooltipHoverArea = {
top: 0.57,
left: 0.83,
bottom: 0.87,
right: 0.96
}
const loadAnalyticsScript = function() {
// We need DOMContentLoaded to execute fast, that's why we insert analytics scripts only AFTER the first 'load' event has fired.
const script = document.createElement("script")
script.setAttribute('data-domain', 'ouija.attejuvonen.fi')
script.src = "https://plausible.io/js/plausible.js"
document.getElementsByTagName("head")[0].appendChild(script)
}
const preloadImage = function(url) {
const img = document.createElement("img")
img.src = url
img.style = "display: none;"
img.alt = ""
document.body.appendChild(img)
}
const preloadSomeImages = function() {
// Preloading these images prevents a flicker issue with magnifying glass. This preload is delayed on purpose to improve initial load time.
preloadImage("assets/ouija_bg_face_stare.jpg")
preloadImage("assets/ouija_bg_face_no_eyes.jpg")
preloadImage("assets/ouija_bg_face_outline.jpg")
}
const disableLoadingOverlay = function() {
if (loadingOverlayDisabled) return
loadingOverlayDisabled = true
document.getElementById('loadingOverlay').style.opacity = 0
setTimeout(() => {
document.getElementById('loadingOverlay').style.display = 'none'
}, 1000)
}
// Memoize some values to reduce reflows (improve performance).
let boardWidth = 0
let boardHeight = 0
let boardTop = 0
let boardLeft = 0
let planchetteWidth = 0
let planchetteHeight = 0
let magSize = 0
let windowWidth = 0
let windowHeight = 0
const resizeUpdates = function () {
// Update memoized values
const boardBounds = document.getElementById('boardContainer').getBoundingClientRect()
boardTop = boardBounds.top
boardLeft = boardBounds.left
boardWidth = boardBounds.width
boardHeight = boardBounds.height
const planchette = document.getElementById('planchette')
planchetteWidth = planchette.clientWidth
planchetteHeight = planchette.clientHeight
windowWidth = window.innerWidth
windowHeight = window.innerHeight
// Update magnifying glass
const mag = document.getElementById('magnifying-glass')
magSize = planchetteWidth * 0.40
Object.assign(mag.style, {
left: (100 * MAG_LEFT) + "%",
top: (100 * MAG_TOP) + "%",
width: magSize + 'px',
height: magSize + 'px',
backgroundSize: (MAG_ZOOM * 100 * boardWidth / magSize) + '% ' + (MAG_ZOOM * 100 * boardHeight / magSize) + '%',
visibility: 'visible'
})
updateMagnifyingGlassPosition()
// Disable loading overlay
disableLoadingOverlay()
// Fix edge case where user sees 2 cursors when using keyboard shortcut to resize browser
const cursor = document.getElementById("cursor")
if (cursor) {
cursor.style.visibility = "hidden";
}
}
// Call resizeUpdates after the first complete render and after each resize
window.addEventListener('load', function (event) {
resizeUpdates()
setTimeout(() => {
loadAnalyticsScript()
preloadSomeImages()
}, 500)
});
window.addEventListener('resize', function (event) {
resizeUpdates()
});
const LOG_ENDPOINT = 'https://endpoint1.collection.eu.sumologic.com/receiver/v1/http/ZaVnC4dhaV0vBIvS0oahg-8LYhDkyFCtWZ2zZ_7NbP0x0PYd0DmEk2cLgA4DJlePqnHWB5KjcxPFudwdOmtop0b6isr9VgLeKHYmzJ6eSqkD0cyZ6FNQiQ=='
const LOG_PROXY_ENDPOINT = 'https://lokittaja.atte-cloudflare.workers.dev/'
const logToSumoLogic = function (message) {
if (window.localStorage.getItem('ouija-dont-log-myself') || window.location.href.startsWith("file")) {
// Exclude debug testing messages from logs (use incognito if you need to test logging)
return
}
const augmentedMessage = `${window.localStorage.getItem(OUIJA_USER_ID)}:${Date.now()}:${using_GPT3 ? "GPT-3" : "Simple"}:${message}`
fetch(`${LOG_ENDPOINT}?${augmentedMessage}`)
.catch((error => {
// Adblocker blocks direct requests to SumoLogic? Fallback to using log proxy
fetch(`${LOG_PROXY_ENDPOINT}?${augmentedMessage}`, { mode: 'cors' })
.catch((error2) => {
console.log('Logging failed', error, error2)
})
}))
}
const paintCursorWithOffset = function (cursor, realX, realY) {
let x = realX + offsetX * planchetteWidth / 100
let y = realY + offsetY * planchetteHeight / 100
cursor.style.top = (y - boardTop) + "px";
cursor.style.left = (x - boardLeft) + "px";
}
const updateMagnifyingGlassPosition = function () {
const mag = document.getElementById('magnifying-glass')
const xMoveWithOffset = planchetteTransformX + offsetX
const yMoveWithOffset = planchetteTransformY + offsetY
// Move location of the magnifying glass
const xMag = xMoveWithOffset * planchetteWidth / magSize
const yMag = yMoveWithOffset * planchetteHeight / magSize
mag.style.transform = `translateX(${xMag}%) translateY(${yMag}%)`;
// Move background image inside magnifying glass (using pixels because I couldn't get % to work, this is why we have to call this method inside resizeUpdates)
const xBgPosPx = -(xMoveWithOffset * planchetteWidth + MAG_LEFT * 100 * boardWidth) / 100 * MAG_ZOOM - magSize * MAG_ZOOM * (1 - 1 / MAG_ZOOM) / 2
const yBgPosPx = -(yMoveWithOffset * planchetteHeight + MAG_TOP * 100 * boardHeight) / 100 * MAG_ZOOM - magSize * MAG_ZOOM * (1 - 1 / MAG_ZOOM) / 2
mag.style.backgroundPosition = `${xBgPosPx}px ${yBgPosPx}px`
}
const updatePlanchettePosition = function () {
const planchette = document.getElementById('planchette')
const planchetteHelper = document.getElementById('planchetteHelper')
planchette.style.transform = `translateX(${planchetteTransformX + offsetX}%) translateY(${planchetteTransformY + offsetY}%) rotate(130deg)`;
planchetteHelper.style.transform = `translateX(${planchetteTransformX}%) translateY(${planchetteTransformY}%) rotate(130deg)`;
updateMagnifyingGlassPosition()
}
const spiritGuidanceToOffset = function (x, y, diffX, diffY, goalX, goalY, dist) {
const accelDistanceMultiplier = (SPIRIT_MAX_DIST - dist) / SPIRIT_MAX_DIST
if (Math.random() > SPIRIT_STOCHASTIC_STR) {
return
}
if (diffX != 0 && Math.sign(goalX - x) == Math.sign(diffX)) { // X axis move is in preferable direction
if (dist > 5) { // reduce jittery accels on top of goal
offsetX += diffX * SPIRIT_ACCEL_STR * 1.3 * 100.0 / planchetteWidth // accelerate
}
} else if (diffX != 0) { // X axis move is in undesired direction
if (dist < 10) { // sharp cutoff for deceleration to reinforce feeling of "magnetic force" and to reduce unnecessary offsetting
offsetX -= diffX * SPIRIT_ACCEL_STR * accelDistanceMultiplier * 100.0 / planchetteWidth // decelerate
}
}
if (diffY != 0 && Math.sign(goalY - y) == Math.sign(diffY)) { // Y axis move is in preferable direction
if (dist > 5) {
offsetY += diffY * SPIRIT_ACCEL_STR * 1.3 * 100.0 / planchetteHeight // accelerate
}
} else if (diffY != 0) {
if (dist < 10) {
offsetY -= diffY * SPIRIT_ACCEL_STR * accelDistanceMultiplier * 100.0 / planchetteHeight // decelerate
}
}
}
const clearOffsets = function () {
if (offsetX != 0) {
planchetteTransformX += offsetX // This negates any effect on planchette positioning
offsetX = 0
}
if (offsetY != 0) {
planchetteTransformY += offsetY
offsetY = 0
}
}
const cancelSomeOffset = function (diffX, diffY, urgency) {
// Will accelerate or decelerate latest mouse move in an effort to reduce offset
const e = OFFSET_CANCELLATION_STR * urgency
if (Math.abs(offsetX) > 1) {
const sign = (Math.sign(offsetX) == Math.sign(diffX) ? -1 : 1) // Decelerate or accelerate depending on sign
offsetX += sign * diffX * e
if (!draggingPlanchette) {
// When planchette is not being dragged, we need to do this to "negate" the effect on planchette position
planchetteTransformX -= sign * diffX * e
}
}
if (Math.abs(offsetY) > 1) {
const sign = (Math.sign(offsetY) == Math.sign(diffY) ? -1 : 1)
offsetY += sign * diffY * e
if (!draggingPlanchette) {
planchetteTransformY -= sign * diffY * e
}
}
}
const startedHoverOnTooltip = function () {
document.getElementById('tooltipSymbol').style.filter = 'blur(2px)'
// Set 'from' attribute to make animation smooth even when the user abruptly moves in and out with the mouse
document.getElementById('animateToFocus').setAttributeNS(null, 'from', document.getElementById('smokeFilterMap').scale.animVal)
document.getElementById('animateToFocus').beginElement()
}
const stoppedHoverOnTooltip = function () {
if (!isBlackSmokeClickable) return
document.getElementById('tooltipSymbol').style.filter = 'blur(11px)'
document.getElementById('animateRemoveFocus').setAttributeNS(null, 'from', document.getElementById('smokeFilterMap').scale.animVal)
document.getElementById('animateRemoveFocus').beginElement()
}
const displayEasterEgg = function() {
if (!easterEggVisible) {
easterEggVisible = true
document.getElementById('boardEasterEggHelper').style.display = 'block'
document.getElementById('boardEasterEggHelper').offsetHeight // Trigger reflow.
document.getElementById('boardEasterEggHelper').style.opacity = 1
setTimeout(() => {
document.getElementById('board').src = 'assets/ouija_bg_face_outline.jpg'
document.getElementById('magnifying-glass').style.backgroundImage = 'assets/ouija_bg_face_stare.jpg'
document.getElementById('boardEasterEggHelper').style.display = 'none'
}, 4000)
}
}
const flyBanshee = function() {
easterEggVisible = false
gameOver = true
document.getElementById('audio-scream1').play()
// Stop blinking caret
document.getElementById('userMessagePre').classList = ['orangey-text']
// Clear easter egg from board (because it flies into the screen)
document.getElementById('board').src = 'assets/ouija_bg.jpg'
document.getElementById('magnifying-glass').style.backgroundImage = 'assets/ouija_bg.jpg'
// Fly, banshee, fly
document.getElementById('banshee').style.opacity = 0.7
document.getElementById('banshee').style.animationName = 'banshee-flying'
setTimeout(() => {
document.getElementById('glassCrack').style.display = 'block'
document.getElementById('audio-crack1').play()
turn = 'no-one'
document.getElementById('planchette').classList = ['planchette-no-glow']
}, 300)
setTimeout(() => {
if (!pauseSmokeAnimation) {
document.getElementById('tooltipSymbol').innerText = ''
document.getElementById('tooltipSymbol').innerHTML = '<span>☺</span>' // shape smoke into smiley emoticon
startedHoverOnTooltip() // make smoke focus so smiley emoticon is easier to notice
isBlackSmokeClickable = false
}
}, 600)
setTimeout(() => {
if (!pauseSmokeAnimation) {
stopSmokeAnimation()
}
}, 2500)
setTimeout(() => {
unlockAchievement(FALSE_PROPHETS_ACHIEVEMENT)
}, 3000)
}
const jackSound = function() {
setTimeout(() => {
document.getElementById('audio-jack').play()
}, 1000)
}
const lightFlash = function() {
document.getElementById('lightFlash').style.display = 'block';
setTimeout(() => {
document.getElementById('lightFlash').style.display = 'none';
}, 50)
}
const preloadAudio = function(id) {
const a = document.getElementById(id)
a.volume = 0.0
a.play()
setTimeout(() => {
a.pause()
a.currentTime = 0
a.volume = 1.0
}, 100)
}
const questLineTick = function() {
if (!showTips) {
return
}
if (currentTooltip === 0) delayedCreateTooltip(1)
else if (currentTooltip === 1) {
logToSumoLogic('!REVEALED_FIRST_SPIRIT_MESSAGE')
delayedCreateTooltip(2)
}
else if (currentTooltip === 2 && questGoals.who <= 0) {
logToSumoLogic('!SOLVED_QUEST_1')
delayedCreateTooltip(3)
}
else if (currentTooltip === 3 && questGoals.where <= 0) {
logToSumoLogic('!SOLVED_QUEST_2')
delayedCreateTooltip(4)
preloadAudio('audio-scream1')
preloadAudio('audio-crack1')
}
// Easter egg face on board
if (currentTooltip >= 3 && turn === TURN_SPIRIT) {
displayEasterEgg()
preloadAudio('audio-drum1')
}
}
// When the player picks up the planchette, the spirit tugs slightly in the direction of the goal.
// This tug is different from spiritGuidance, because guidance only accelerates or decelerates movements
// that the player makes. This tug is independent of the player's movements. As such, it provides
// a different sensation and variation to the "feel" of the game. However, the main purpose of this tug
// is to allow the player to discover goals faster. Without the tug players have to slowly and painfully
// hover over all the letters. With the tug players can immediately go in the right direction (once they
// learn to flow along with the mechanic).
//
// Problem: many players ONLY noticed spirit tug, never noticed guidance effects, so they played
// the game by just doing click-click-click. Boring! In order to encourage players to discover all
// mouse effects, a 15-second timer was added, so this tug can not be initiated consecutively.
const TUG_STRENGTH = 1/15
let lastTugTime = 0
const beginSpiritTug = function() {
if (easyMode) {
easyModeRecursiveTimerSpiritTug()
return
}
const currTugTime = Date.now()
if (lastTugTime + 15000 > currTugTime) return
lastTugTime = currTugTime
let maxMovesLeft = 20
let recursiveTimerSpiritTug = function () {
if (remainingGoals.length === 0) return
if (maxMovesLeft <= 0) return
const goalX = goalCoords[remainingGoals[0]].x
const goalY = goalCoords[remainingGoals[0]].y
const diffX = goalX - (planchetteTransformX + offsetX)
const diffY = goalY - (planchetteTransformY + offsetY)
const dist = Math.sqrt(diffX * diffX + diffY * diffY)
offsetX += diffX * maxMovesLeft * TUG_STRENGTH / dist
offsetY += diffY * maxMovesLeft * TUG_STRENGTH / dist
maxMovesLeft -= 1
updatePlanchettePosition()
paintCursorWithOffset(document.getElementById("cursor"), prevX, prevY)
setTimeout(() => { recursiveTimerSpiritTug() }, 10)
}
recursiveTimerSpiritTug()
}
const EASY_MODE_TUG_STRENGTH = 1/25
let easyModeRecursiveTimerSpiritTug = function () {
if (remainingGoals.length === 0) return
const goalX = goalCoords[remainingGoals[0]].x
const goalY = goalCoords[remainingGoals[0]].y
const diffX = goalX - (planchetteTransformX + offsetX)
const diffY = goalY - (planchetteTransformY + offsetY)
offsetX += diffX * EASY_MODE_TUG_STRENGTH
offsetY += diffY * EASY_MODE_TUG_STRENGTH
updatePlanchettePosition()
paintCursorWithOffset(document.getElementById("cursor"), prevX, prevY)
setTimeout(() => {
if (draggingPlanchette) {
easyModeRecursiveTimerSpiritTug()
}
}, 10)
}
const mouseMoved = function (event, onObject) {
const currX = event.clientX
const currY = event.clientY
const diffX = (userMoveCount > 0 ? currX - prevX : 0)
const diffY = (userMoveCount > 0 ? currY - prevY : 0)
prevX = currX
prevY = currY
userMoveCount += 1
if (draggingPlanchette) {
const x = planchetteTransformX + offsetX + diffX * 100.0 / planchetteWidth
const y = planchetteTransformY + offsetY + diffY * 100.0 / planchetteHeight
if (y > HARD_Y_MAX || y < HARD_Y_MIN || x > HARD_X_MAX || x < HARD_X_MIN) {
// Prevent planchette from being moved outside board
stopDraggingPlanchette(event)
return
}
planchetteTransformX += diffX * 100.0 / planchetteWidth
planchetteTransformY += diffY * 100.0 / planchetteHeight
const goalX = remainingGoals.length > 0 ? goalCoords[remainingGoals[0]].x : -10000
const goalY = remainingGoals.length > 0 ? goalCoords[remainingGoals[0]].y : -10000
// Possibly accelerate/decelerate move by modifying cursor offset
const dist = Math.sqrt((x - goalX) * (x - goalX) + (y - goalY) * (y - goalY))
if (dist < SPIRIT_MAX_DIST) {
// Modify cursor offset to guide the user towards goal
spiritGuidanceToOffset(x, y, diffX, diffY, goalX, goalY, dist)
} else {
// User dragging planchette but is not near goal, use this opportunity to
// reduce cursor offset to prevent the real cursor from escaping window.
if (y < SOFT_Y_MAX && y > SOFT_Y_MIN && x < SOFT_X_MAX && x > SOFT_X_MIN) {
// Soft limits are used to prevent us from accelerating the user beyond hard limits where planchette would be trapped
cancelSomeOffset(diffX, diffY, OFFSET_CANCELLATION_LOW_URGENCY)
}
}
} else {
// User is not dragging planchette.
// Take the opportunity to reduce cursor offset to prevent the real cursor from escaping window.
// We need coordinates relative to viewport.
let x = (currX + offsetX * planchetteWidth / 100) / windowWidth
let y = (currY + offsetY * planchetteHeight / 100) / windowHeight
const minimumEscapeDistance = Math.min(Math.abs(x - 0), Math.abs(x - 1), Math.abs(y - 1), Math.abs(y - 0))
if (minimumEscapeDistance > 0.04) {
// TODO instead of linear we need to have exponential urgency towards the edges of window
const urgency = Math.max(1 - minimumEscapeDistance / 0.30, OFFSET_CANCELLATION_LOW_URGENCY)
cancelSomeOffset(diffX, diffY, urgency)
} else {
// We are too close to the edge, immediately cancel all offset
clearOffsets()
}
// Determine if user's fake cursor is hovering over tooltip.
// We need coordinates relative to board (to support browser resizing).
x = (x * windowWidth - boardLeft) / boardWidth
y = (y * windowHeight - boardTop) / boardHeight
if (showTips && !popupIsOpen && !pauseSmokeAnimation && x > tooltipHoverArea.left && x < tooltipHoverArea.right && y > tooltipHoverArea.top && y < tooltipHoverArea.bottom) {
// We are hovering over tooltip.
if (!hoveringOverTooltip) {
// We just now entered the hover area.
hoveringOverTooltip = true
startedHoverOnTooltip()
}
} else {
// We are not hovering over tooltip (or popup is open so we dont do the hover effect anyway).
if (hoveringOverTooltip) {
// We just now left the hover area.
hoveringOverTooltip = false
stoppedHoverOnTooltip()
}
}
}
updatePlanchettePosition()
const chooseCursorIcon = function () {
if (draggingPlanchette) return 'assets/grabbing.cur'
if (onObject === ON_PLANCHETTE && turn === TURN_SPIRIT) return 'assets/grab.cur'
if (onObject === ON_BUTTON) return 'assets/aero_link.cur'
if (hoveringOverTooltip) return 'assets/aero_link.cur'
return 'assets/aero_arrow.cur'
}
const cursor = document.getElementById("cursor")
const newCursorSrc = chooseCursorIcon()
if (cursor.src !== newCursorSrc) cursor.src = newCursorSrc
cursor.style.visibility = "visible";
paintCursorWithOffset(cursor, prevX, prevY)
event.preventDefault()
}
const startDraggingPlanchette = function (event) {
if (turn === TURN_SPIRIT) {
draggingPlanchette = true
cursor.src = "assets/grabbing.cur"
if (easterEggVisible) document.getElementById('magnifying-glass').style.backgroundImage = "url('assets/ouija_bg_face_stare.jpg')"
beginSpiritTug()
event.preventDefault() // Fixes issue in Firefox that made dragging immediately stop.
}
}
const stopDraggingPlanchette = function (event, source) {
draggingPlanchette = false
const newCursor = (source === ON_PLANCHETTE ? 'assets/grab.cur' : 'assets/aero_arrow.cur')
cursor.src = newCursor
const x = planchetteTransformX + offsetX
const y = planchetteTransformY + offsetY
if (easterEggVisible) {
document.getElementById('magnifying-glass').style.backgroundImage = "url('assets/ouija_bg_face_no_eyes.jpg')"
const c = easterEggLocation
const dist = Math.sqrt((x - c.x) * (x - c.x) + (y - c.y) * (y - c.y))
if (dist < CHAR_SELECT_MAX_DIST) {
unlockAchievement(EASTER_EGG_ACHIEVEMENT)
}
}
if (source === ON_PLANCHETTE) {
if (debug[RECORDING]) {
const chars = ALLOWED_CHARS
let i = 0
while (debug[RECORDING][chars[i]]) i++
debug[RECORDING][chars[i]] = {
x: Math.round(100 * x) / 100,
y: Math.round(100 * y) / 100,
}
console.log(debug[RECORDING])
} else {
// What is the closest character to planchette
let closestDist = 99999999
let closestChar = 'a'
for (let i = 0; i < ALLOWED_CHARS.length; i++) {
const char = ALLOWED_CHARS[i]
const c = goalCoords[char]
const dist = Math.sqrt((x - c.x) * (x - c.x) + (y - c.y) * (y - c.y))
if (dist < closestDist) {
closestDist = dist
closestChar = char
}
}
if (closestDist < CHAR_SELECT_MAX_DIST) {
// Close enough, interpret that user intends to select this character
if (remainingGoals.length > 0 && closestChar === remainingGoals[0]) {
// Selected character was goal, move onto the next goal.
addCharToRevealedMessage(closestChar)
remainingGoals = remainingGoals.substring(1)
if (remainingGoals.length === 0) {
thingsToDoAfterSpiritMessageHasBeenRevealed()
}
// Reset tug timer (if player is not on easy mode, when they pick up the planchette, there will be a small tug available again)
lastTugTime = 0
}
}
}
}
event.stopPropagation()
}
const zalgoChars = {
up: [
'\u030d', '\u030e', '\u0311',
'\u0306', '\u0310', '\u0352', '\u0357', '\u0351', '\u0307',
'\u0308', '\u030a', '\u0342', '\u0343', '\u0344', '\u034a',
'\u034b', '\u034c', '\u0303', '\u0302', '\u030c', '\u0350',
'\u0300', '\u0301', '\u030b', '\u030f', '\u0312', '\u0313',
'\u0314', '\u033d', '\u0309', '\u033e', '\u035b',
],
upLetters: {
a: '\u0363',
e: '\u0364',
i: '\u0365',
o: '\u0366',
u: '\u0367',
c: '\u0368',
d: '\u0369',
h: '\u036a',
m: '\u036b',
r: '\u036c',
t: '\u036d',
v: '\u036e',
x: '\u036f',
s: '\u033e'
},
middle: [
'\u0315', '\u031b', '\u0340', '\u0341', '\u0358', '\u0327', '\u0328'
],
down: [
'\u0316', '\u0317', '\u0318', '\u0319', '\u031c', '\u031d',
'\u031e', '\u031f', '\u0320', '\u0324', '\u0325', '\u0326',
'\u0329', '\u032a', '\u032b', '\u032c', '\u032d', '\u032e',
'\u032f', '\u0330', '\u0331', '\u0332', '\u0333', '\u0339',
'\u033a', '\u033b', '\u033c', '\u0347', '\u0348',
'\u0349', '\u034d', '\u034e', '\u0353', '\u0354', '\u0355',
'\u0356', '\u0359', '\u035a', '\u0323'
]
}
function zalgoRandInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
function zalgoAddRandomChars(charIn, conf) {
let charOut = charIn
for (const direction in conf) {
const arr = zalgoChars[direction]
for (let count=0; count<conf[direction]; count++) {
charOut += arr[zalgoRandInt(0, arr.length - 1)]
}
}
return charOut
}
function zalgoAddSpecificLetters(charIn, letters) {
let charOut = charIn
for (let i=letters.length-1; i>=0; i--) {
charOut += zalgoChars.upLetters[letters[i]] || ""
}
return charOut
}
const zalgoMessages = [
'hecomes',
'ithurts',
'cthuihu',
'xerxes',
'doom',
// TODO: more
// no diatrics characters available to represent: tainted, unholy, toolate, scourge, pestilence, zalgo, mortal, breach, corrupt, pestilence, extinguish
]
const zalgoize = function(char) {
if (currentExchangeNumber === 0) {
// Special case here. The interaction begins with no zalgoization and gradually increases in intensity.
return char
}
const MAX_INTENSITY_REACHED_AFTER_EXCHANGE_NUMBER = 20.0
const intensity = Math.min(currentExchangeNumber / MAX_INTENSITY_REACHED_AFTER_EXCHANGE_NUMBER, 1.0)
if (intensity === 1.0 && Math.random() > 0.9) {
// Special case here. Once max intensity is reached we occasionally drop in a hidden message in zalgotext
char = zalgoAddRandomChars(char, { up: 2, down: 2 } )
char = zalgoAddSpecificLetters(char, zalgoMessages[zalgoRandInt(0, zalgoMessages.length-1)])
char = zalgoAddRandomChars(char, { up: 2 } )
return char
}
// Typical case below.
if (intensity < 1.0 && Math.random()*2 > intensity+0.3) {
// Before we reach max intensity, we want only occasional characters to be zalgoized.
// After we reach max intensity, all characters will be zalgoized.
return char
}
conf = {
up: zalgoRandInt(2, Math.max(2, Math.round(14 * intensity))),
middle: zalgoRandInt(0, 5),
down: zalgoRandInt(1, Math.max(1, Math.round(6 * intensity)))
}
char = zalgoAddRandomChars(char, conf)
return char
}
const addCharToRevealedMessage = function (char) {
revealedSpiritLetters += zalgoize(char)
const container = document.getElementById('spiritMessageContainer')
container.innerText = revealedSpiritLetters
container.setAttribute('data-text', revealedSpiritLetters)
}
const switchTurnToSpirit = function () {
turn = TURN_SPIRIT
// Clear previously revealed spirit message
revealedSpiritLetters = ''
// Indicate to user that planchette can be dragged
document.getElementById('planchette').classList = [' planchette-active-glow']
// Clear previously revealed answer from visuals
const container = document.getElementById('spiritMessageContainer')
container.innerText = ''
container.setAttribute('data-text', '')
// Stop blinking caret
document.getElementById('userMessagePre').classList = ['orangey-text']
// Maybe update tooltip
questLineTick()
}
const getInitialDelay = function() {
// Intent is to emulate a human who takes a little bit longer before starting to write
return 700 + Math.round(Math.random() * 1000)
}
const thingsToDoAfterSpiritMessageHasBeenRevealed = function() {
turn = TURN_USER
console.log('Spirit: ' + revealedSpiritLetters.toUpperCase())
document.getElementById('planchette').classList = ['planchette-no-glow']
document.getElementById('userMessagePre').innerText = ''
document.getElementById('userMessagePre').classList = ['blinking-caret']
currentExchangeNumber++
questLineTick()
if (completionEffect) {
setTimeout(() => {
completionEffect()
completionEffect = null
}, 1000 + Math.round(Math.random() * 3000))
}
}
const getConsecutiveDelay = function(nextChar) {
// Intent is to emulate a human with varying delays between key presses
const prev1 = revealedSpiritLetters.charAt(revealedSpiritLetters.length-1)
const prev2 = revealedSpiritLetters.charAt(revealedSpiritLetters.length-2)
if (nextChar == prev1 || nextChar == prev2) {
return 60 + Math.round(Math.random() * 120)
}
return 80 + Math.round(Math.random() * 400)
}
const recursivelyRevealSpiritMessage = function (delayParam) {
turn = 'no-one'
document.getElementById('planchette').classList = ['planchette-no-glow']
if (remainingGoals.length === 0) {
thingsToDoAfterSpiritMessageHasBeenRevealed()
} else {
const delay = delayParam ?? getInitialDelay()
setTimeout(() => {
const c = remainingGoals[0]
const nextDelay = getConsecutiveDelay(c)
remainingGoals = remainingGoals.substring(1)
addCharToRevealedMessage(c)
recursivelyRevealSpiritMessage(nextDelay)
}, delay)
}
}
const spiritIsReadyToCommunicate = function (rawMessage) {
const message = rawMessage.toLocaleLowerCase().replace(/[^0-9a-z]/gi, '')
const container = document.getElementById('spiritMessageContainer')
logToSumoLogic(previousInput + " -> " + message)
remainingGoals = message
if (speedMode) {
recursivelyRevealSpiritMessage()
}
}
let currentTooltip = 0
const delayedCreateTooltip = function(i) {
currentTooltip = i
if (!using_GPT3) stopSmokeAnimation()
setTimeout(() => createTooltip(i), 2500)
}
const createTooltip = function (i) {
if (!SCRIPTED_TOOLTIPS) {
// Possibly slow connection and chatbot.js hasn't loaded yet
setTimeout(() => createTooltip(i), 500)
return
}
const t = SCRIPTED_TOOLTIPS[i]
document.getElementById('tooltipSymbol').innerText = t.tooltip
document.getElementById('tooltipContent').innerHTML = `<h1>${t.headline}</h1>\n${t.paragraphs.map((str) => '<p>' + str + '</p>\n').join("")}`
if (showTips && !using_GPT3) {
startSmokeAnimation()
}
}
const openTooltipPopup = function (e) {
clearOffsets()
mouseMoved(e) // To update cursor location after clearing offsets.
document.getElementById('board').style.filter = `blur(10px) brightness(0.6)`
document.getElementById('planchette').style.filter = `blur(10px) brightness(0.6)`
document.getElementById('planchetteHelper').style.filter = `blur(10px) brightness(0.6)`
document.getElementById('spiritMessageContainer').style.visibility = `hidden`
document.getElementById('userMessageContainer').style.visibility = `hidden`
document.getElementById('tooltipPopup').style.opacity = '1'
document.getElementById('tooltipPopup').style.display = `block`
}
const closeTooltipPopup = function (shutUp) {
if (shutUp) toggleShowTips()
document.getElementById('settingsGear').classList.remove(['one-time-gear-roll'])
document.getElementById('planchette').style.removeProperty('filter')
document.getElementById('tooltipPopup').style.display = 'none'
document.getElementById('board').style.filter = `none`;
document.getElementById('planchetteHelper').style.filter = `none`;
document.getElementById('spiritMessageContainer').style.visibility = `visible`
document.getElementById('userMessageContainer').style.visibility = `visible`;
}
let isBlackSmokeClickable = true
const toggleTooltipPopup = function (e, shutUp) {
if (!isBlackSmokeClickable) return
popupIsOpen = !popupIsOpen
popupIsOpen ? openTooltipPopup(e) : closeTooltipPopup(shutUp)
}
const openSettingsPopup = function () {
document.getElementById('settingsGear').classList.add(['one-time-gear-roll'])
document.getElementById('board').style.filter = `blur(10px) brightness(0.6)`
document.getElementById('planchette').style.filter = `blur(10px) brightness(0.6)`
document.getElementById('planchetteHelper').style.filter = `blur(10px) brightness(0.6)`
document.getElementById('spiritMessageContainer').style.visibility = `hidden`
document.getElementById('userMessageContainer').style.visibility = `hidden`
document.getElementById('settingsPopup').style.opacity = '1'
document.getElementById('settingsPopup').style.display = `block`
}
const closeSettingsPopup = function () {
document.getElementById('settingsGear').classList.remove(['one-time-gear-roll'])
document.getElementById('planchette').style.removeProperty('filter')
document.getElementById('settingsPopup').style.display = 'none'
document.getElementById('board').style.filter = `none`;
document.getElementById('planchetteHelper').style.filter = `none`;
document.getElementById('spiritMessageContainer').style.visibility = `visible`
document.getElementById('userMessageContainer').style.visibility = `visible`;
}
const toggleSettingsPopup = function () {
popupIsOpen = !popupIsOpen
popupIsOpen ? openSettingsPopup() : closeSettingsPopup()
}
const toggleEasyMode = function() {
easyMode = !easyMode
const slider = document.getElementById('easyModeSliderSlider')
if (easyMode) {
slider.classList.add(['checked'])
} else {
slider.classList.remove(['checked'])
}
}
const toggleShowTips = function () {
showTips = !showTips
const slider = document.getElementById('showTipsSliderSlider')
if (showTips) {
slider.classList.add(['checked'])
if (!using_GPT3) startSmokeAnimation()
} else {
slider.classList.remove(['checked'])
if (!using_GPT3) stopSmokeAnimation()
}
}
const toggleRevealMouse = function () {
revealMouse = !revealMouse
const slider = document.getElementById('revealMouseSliderSlider')
if (!revealMouse) {
slider.classList.remove(['checked'])
document.getElementById('planchetteHelper').style.opacity = 0
document.getElementById('bg').style.cursor = 'none'
document.getElementById('hoverBoard').style.cursor = 'none'
document.getElementById('planchette').style.cursor = 'none'
document.getElementById('planchetteHelper').style.cursor = 'none'
} else {
slider.classList.add(['checked'])
document.getElementById('planchetteHelper').style.opacity = 0.5
document.getElementById('bg').style.cursor = 'auto'
document.getElementById('hoverBoard').style.cursor = 'auto'
document.getElementById('planchette').style.cursor = 'auto'
document.getElementById('planchetteHelper').style.cursor = 'auto'
}
}
const toggleSpeedMode = function () {
speedMode = !speedMode
const slider = document.getElementById('speedModeSliderSlider')
if (speedMode) {
slider.classList.add(['checked'])
localStorage.setItem('ouija-speedmode', true)
} else {
slider.classList.remove(['checked'])
localStorage.removeItem('ouija-speedmode')
}
if (speedMode && remainingGoals.length > 0) {
recursivelyRevealSpiritMessage()
}
}
const toggleOpenAI = function () {
using_GPT3 = !using_GPT3
const slider = document.getElementById('openAISliderSlider')
if (!using_GPT3) {
slider.classList.remove(['checked'])
document.getElementById('settingsPopupButtonsContainer').style.marginTop = '1.5vw'
document.getElementById('textInputAPIKey').style.marginTop = '1.7vw'
if (showTips) startSmokeAnimation()
} else {
slider.classList.add(['checked'])
document.getElementById('settingsPopupButtonsContainer').style.marginTop = '4.1vw'
document.getElementById('textInputAPIKey').value = window.localStorage.getItem('ouija-openai-api-key')
document.getElementById('textInputAPIKey').style.marginTop = '1vw'
if (showTips) stopSmokeAnimation()
}
}
const userPastedAPIKey = function () {
window.localStorage.setItem('ouija-openai-api-key', document.getElementById('textInputAPIKey').value)
}