-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
188 lines (163 loc) · 6.84 KB
/
main.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
/*
This file is responsible for everything that happens into the main div
*/
loadingText.innerText = lang.loading
rotateOverlay.innerText = lang.rotate
const gameArea = document.querySelector("canvas")
const upper = document.getElementById("upper")
const left = document.getElementById("left")
const center = document.getElementById("center")
const right = document.getElementById("right")
const promotion = document.getElementById("promotion")
const touchDevice = navigator.maxTouchPoints > 0 ? true : false
const isInIframe = window.self !== window.top
let afterResizeTimeout
let lastWidth
let lastHeigth
let lastPrompt = 0
let lastAdUpdate = 0
let deferredPrompt
let showPromotion
window.addEventListener("load", () => {
let loadingInterval = setInterval(async () => {
if (gameAssetsLoaded) {
clearInterval(loadingInterval)
window.scrollTo(0, 0)
loadingText.innerHTML = lang.ready
initializeGame()
showPromotion = (window.matchMedia("(display-mode: standalone)").matches || window.matchMedia("(display-mode: fullscreen)").matches || window.navigator.standalone) ? false : true
await waitForInteractionWithElement(loadingOverlay, ["click"], undefined, true)
keyboardMapping.style.display = physicalKeyboard ? "block" : "none"
document.getElementsByTagName('body')[0].style.overflow = 'unset'
await fullScreenOrientationLock()
await showRotateOverlay()
await calculateDivs()
loadingOverlay.style.display = "none"
updateAds()
screen.orientation.addEventListener("change", showRotateOverlay)
window.addEventListener("resize", showRotateOverlay)
window.addEventListener("resize", () => {
clearTimeout(afterResizeTimeout)
afterResizeTimeout = setTimeout(calculateDivs, 500)
})
}
})
})
//prevents scroll with space bar
window.onkeydown = function (e) {
return !(e.keyCode == 32 && e.target == document.body);
};
if ("serviceWorker" in navigator) {
window.addEventListener("beforeinstallprompt", definePrompt = (ev) => {
ev.preventDefault()
deferredPrompt = ev
waitForInteractionWithElement(gameArea, ["touchstart", "mousedown"], endScreenInstallPrompt, true)
})
function endScreenInstallPrompt() {
setTimeout(async () => {
let installed
if (
currentScreen.name == "end" &&
((mode == "single" && players.second.status == "dead" && currentLevel > 1) || mode == "multi") &&
new Date() - lastPrompt > 180000
) {
installed = await installPrompt()
}
if (!installed) {
waitForInteractionWithElement(gameArea, ["touchstart", "mousedown"], endScreenInstallPrompt, true)
}
}, 500)
}
window.addEventListener("appinstalled", () => {
window.removeEventListener("beforeinstallprompt", definePrompt)
deferredPrompt = null
})
navigator.serviceWorker.register("./serviceworker.js")
}
function waitForInteractionWithElement(element, interactions, callback = undefined, keyboard = false) {
return new Promise((resolve) => {
function triggeredEvent() {
for (interaction of interactions) {
element.removeEventListener(interaction, triggeredEvent)
}
if (keyboard) document.removeEventListener("keydown", triggeredEvent)
if (callback) callback()
resolve()
}
for (interaction of interactions) {
element.addEventListener(interaction, triggeredEvent)
}
if (keyboard) document.addEventListener("keydown", triggeredEvent)
})
}
function showRotateOverlay() {
rotateOverlay.style.display = (window.innerWidth > window.innerHeight || !touchDevice) ? "none" : "flex"
}
async function fullScreenOrientationLock() {
if (!touchDevice) return
if (document.fullscreenElement === null) {
await waitForInteractionLeave()
try {
await document.documentElement.requestFullscreen()
try {
await screen.orientation.lock("landscape")
} catch {
console.log("unable to get orientation locked")
}
} catch {
console.log("unable to get fullscreen")
}
}
waitForInteractionWithElement(document, ["touchstart", "mousedown"], fullScreenOrientationLock, true)
}
async function installPrompt() {
await waitForInteractionLeave()
deferredPrompt.prompt()
lastPrompt = new Date()
const { outcome } = await deferredPrompt.userChoice
if (outcome == "accepted") {
try {
gtag('event', 'install')
} catch { }
}
return outcome === "accepted"
}
function calculateDivs() {
if (rotateOverlay.style.display != "none") return
if (lastHeigth === window.innerHeight && lastWidth === window.innerWidth) return
if (touchDevice && document.fullscreenElement === null && !isInIframe) return
let sideDivWidth = parseInt((bottom.clientWidth - center.clientWidth) / 2)
left.style.width = right.style.width = (sideDivWidth >= 0 ? sideDivWidth : 0) + "px"
promotion.style.display = (window.innerHeight >= gameArea.height + 50 && showPromotion) ? "flex" : "none"
//Bottom row divs must have zero height so the upper div height can be evaluated without intervention when screen is downsized
left.style.height = center.style.height = right.style.height = 0
if (window.innerHeight >= (showPromotion ? 350 : 300)) {
upper.style.height = (window.innerHeight - (showPromotion ? 300 : 250)) + "px"
} else {
upper.style.height = 0
}
left.style.height = center.style.height = right.style.height = window.innerHeight - upper.clientHeight + "px"
lastWidth = window.innerWidth
lastHeigth = window.innerHeight
insertAds()
}
function insertAds() {
lastAdUpdate = new Date()
let containers = document.getElementsByClassName("adContainer")
for (container of containers) {
let containerWidth = container.clientWidth
let containerHeight = container.clientHeight
container.innerHTML = containerWidth >= 135 && containerHeight >= 50 && !isInIframe ? adString(containerHeight) : ""
}
function adString(adHeight = 50) {
//console.log("Ad placed")
setTimeout(() => { (adsbygoogle = window.adsbygoogle || []).push({}); }, 100)
return `<ins class="adsbygoogle" style="display:block; height: ${adHeight}px;" data-ad-client="ca-pub-4327628330003063" data-ad-slot="3752036653" data-full-width-responsive="true"></ins>`
}
}
function updateAds() {
if (new Date() - lastAdUpdate > 10000) {
insertAds()
}
waitForInteractionWithElement(gameArea, ["mousedown", "touchstart"], updateAds, true)
}