-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice.html
262 lines (228 loc) · 9.39 KB
/
dice.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Dice Substitution</title>
<script>
class DiceSettings {
constructor() {
this._defaultValues = {
dieCount: 3,
minDieValue: 1,
maxDieValue: 6
}
this._fallBackValues = {}
for (let key in this._defaultValues) {
this._fallBackValues[key] = this._defaultValues[key]
}
}
get dieCount() {
return this._getDieSetting("dieCount", this._defaultValues.dieCount)
}
set dieCount(value) {
this._setDieSetting("dieCount", value)
}
get minDieValue() {
return this._getDieSetting("minDieValue", this._defaultValues.minDieValue)
}
set minDieValue(value) {
this._setDieSetting("minDieValue", value)
}
get maxDieValue() {
return this._getDieSetting("maxDieValue", this._defaultValues.maxDieValue)
}
set maxDieValue(value) {
this._setDieSetting("maxDieValue", value)
}
_getDieSetting(valueName, defaultValue) {
let value
try {
value = parseInt(localStorage.getItem(valueName))
} catch (err) {
if (!err instanceof DOMException && err.message.includes("localStorage")) {
throw err
}
value = this._fallBackValues[valueName]
}
return isNaN(value) ? defaultValue : value
}
_setDieSetting(valueName, value) {
try {
localStorage.setItem(valueName, value)
} catch (err) {
if (!err instanceof DOMException && err.message.includes("localStorage")) {
throw err
}
this._fallBackValues[valueName] = value
}
}
}
const _diceSettings = new DiceSettings()
function unfocusButton(id) {
if (id) {
document.getElementById(id).blur()
}
}
function currentDateString() {
const date = new Date()
return `${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}:${date.getMilliseconds()}`
}
function addToHistory(dieValues) {
document.getElementById("history-area").style.display = "inline"
const historyTable = document.getElementById("history-table")
const row = document.createElement("tr")
const diceString = dieValues
.map(value => `<td class="die-table-cell">${value}</td>`)
.join("")
row.innerHTML = `<td>${currentDateString()}</td>${diceString}`
historyTable.insertBefore(row, historyTable.firstChild)
}
function throwDice(id) {
const dieValues = []
const dieElements = document.getElementsByClassName("die-text")
for (let i = 0; i < dieElements.length; i++) {
const value =
Math.floor(
Math.random() *
Math.abs(_diceSettings.minDieValue - _diceSettings.maxDieValue - 1)) +
_diceSettings.minDieValue
dieValues.push(value)
dieElements[i].textContent = value
}
addToHistory(dieValues)
unfocusButton(id)
}
function resetDice(count) {
const diceContainer = document.getElementById("dice-area")
diceContainer.innerHTML = ""
for (let i = 0; i < count; i++) {
const dieElement = document.createElement("span")
dieElement.setAttribute("class", "die")
dieElement.innerHTML = '<span class="die-text">X</span>'
diceContainer.appendChild(dieElement)
}
document.getElementById("history-area").style.display = "none"
document.getElementById("history-table").innerHTML = ""
}
function changeDieCount(count, id) {
_diceSettings.dieCount += count
document.getElementById("die-count-label").textContent = _diceSettings.dieCount
document.getElementById("decrease-die-count-button").disabled = _diceSettings.dieCount <= 1
resetDice(_diceSettings.dieCount)
unfocusButton(id)
}
function followUpDieRangeChange(valueLabelId, value, buttonId) {
document.getElementById(valueLabelId).textContent = value
resetDice(_diceSettings.dieCount)
unfocusButton(buttonId)
}
function changeMinDieValue(amount, id) {
_diceSettings.minDieValue += amount
followUpDieRangeChange("min-value-label", _diceSettings.minDieValue, id)
}
function changeMaxDieValue(amount, id) {
_diceSettings.maxDieValue += amount
followUpDieRangeChange("max-value-label", _diceSettings.maxDieValue, id)
}
function reset(shallClearLocalStorage, id) {
if (shallClearLocalStorage) {
localStorage.clear()
}
changeDieCount(0)
followUpDieRangeChange("min-value-label", _diceSettings.minDieValue)
followUpDieRangeChange("max-value-label", _diceSettings.maxDieValue)
unfocusButton(id)
}
document.addEventListener("DOMContentLoaded", () => {
reset(false)
document.body.onkeypress = event => {
if (event.key === " ") {
event.preventDefault()
throwDice()
}
}
})
</script>
<style>
body {
font-family: sans-serif;
}
#throw-dice-button {
font-size: 15pt;
font-weight: bold;
}
.die {
background-color: lightgray;
border-style: solid;
display: inline-block;
font-size: 30pt;
font-weight: bold;
margin-bottom: 5px;
margin-right: 5px;
margin-top: 5px;
min-height: 40pt;
min-width: 40pt;
padding: 10px;
text-align: center;
}
.die-text {
vertical-align: middle;
}
#die-count,
#die-range,
#reset-to-default-button {
margin-top: 20px;
}
#history-area {
display: none;
}
.die-table-cell {
border-style: solid;
border-width: 1px;
min-width: 15pt;
text-align: center;
}
</style>
</head>
<body>
<button id="throw-dice-button" onclick="throwDice(this.id)">Throw dice</button>
<div id="dice-area"></div>
<div id="die-count">
<span>Count of dice:</span>
<span>
<button id="inrease-die-count-button" onclick="changeDieCount(1, this.id)">+</button>
</span>
<span>
<button id="decrease-die-count-button" onclick="changeDieCount(-1, this.id)">-</button>
</span>
<span id="die-count-label"></span>
</div>
<div id="die-range">
<div>
<span>Minimum die value:</span>
<span>
<button id="increase-min-value-button" onclick="changeMinDieValue(1, this.id)">+</button>
</span>
<span>
<button id="decrease-min-value-button" onclick="changeMinDieValue(-1, this.id)">-</button>
</span>
<span id="min-value-label"></span>
</div>
<div>
<span>Maximum die value:</span>
<span>
<button id="increase-max-value-button" onclick="changeMaxDieValue(1, this.id)">+</button>
</span>
<span>
<button id="decrease-max-value-button" onclick="changeMaxDieValue(-1, this.id)">-</button>
</span>
<span id="max-value-label"></span>
</div>
</div>
<div id="history-area">
<h1>Session history</h1>
<table id="history-table"></table>
</div>
<button id="reset-to-default-button" onclick="reset(true, this.id)">Reset to default</button>
</body>
</html>