-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
86 lines (81 loc) · 1.59 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
const SFX_PATH_BASE = 'assets/sfx';
const SFX = {
allo: {
text: 'Allo',
samples: 3
},
correc: {
text: 'C\'est correc',
samples: 7
},
csharp: {
text: 'C#',
samples: 1
},
donc: {
text: 'Donc',
samples: 9
},
euh: {
text: 'Euuuh',
samples: 16
},
hein: {
text: 'Hein ?',
samples: 2
},
ok: {
text: 'OK',
samples: 5
},
pistolet: {
text: 'Pistolet',
samples: 10
},
regardez: {
text: 'Regardez !',
samples: 4
}
};
/**
* Returns a random number between two bounds
* @param {*} min
* @param {*} max
*/
function getRandomNumber (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Returns a random sample path for the given SFX
* @param {*} name
*/
function getSFXPath (name) {
if (!(name in SFX)) throw new Error(`Unknown SFX: ${name}`);
return SFX_PATH_BASE + '/' + name + '/' + getRandomNumber(1, SFX[name].samples) + '.wav';
}
const audio = new Audio();
const main = document.querySelector('main');
// Register service worker
if ('serviceWorker' in navigator) {
window.addEventListener('load', async _ => {
try {
const registration = await navigator.serviceWorker.register('/khrissbox/sw.js', {
scope: '/khrissbox/'
});
console.log(registration);
} catch (err) {
console.log(err.message);
}
})
}
// Create buttons
for (const [name, sfx] of Object.entries(SFX)) {
const button = document.createElement('button');
button.innerText = `${sfx.text} (${sfx.samples})`;
button.addEventListener('click', _ => {
audio.src = getSFXPath(name);
console.log(audio.src);
audio.play();
});
main.appendChild(button);
}