Skip to content

Commit e1a6023

Browse files
Merge pull request #34 from michaelchadwick/switch-bot-from-text-to-json
Switch Bot from Text to JSON
2 parents 10dcce0 + 3c12731 commit e1a6023

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

assets/css/modal/root.css

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@
154154
.share {
155155
margin: 0 auto;
156156
}
157+
158+
.bot-meta {
159+
text-align: center;
160+
161+
.bot-statistics {
162+
font-size: 1rem;
163+
line-height: 1;
164+
}
165+
}
157166
}
158167

159168
.modal-window {
@@ -210,7 +219,7 @@
210219
}
211220

212221
&.perm-small {
213-
height: 100px;
222+
height: 130px;
214223
width: 250px;
215224

216225
.modal-text {

assets/js/app/lib/helpers.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,20 @@ Deckdle._getNebyooApps = async () => {
300300

301301
Deckdle._getBotScore = async () => {
302302
// will have negative numbers or 0 for wins, and positive number for losses
303-
const dailyBatchPath = '/_debug/text/daily_batch.txt'
303+
const dailyBatchPath = '/_debug/text/daily_batch.json'
304304

305-
const response = await fetch(dailyBatchPath)
306-
const score = await response.text()
307-
const lastModified = new Date(response.headers.get('Date')).toISOString().split('T')[0]
305+
try {
306+
const response = await fetch(dailyBatchPath)
307+
const result = await response.json()
308+
const lastModified = new Date(response.headers.get('Date')).toISOString().split('T')[0]
308309

309-
if (score) {
310-
return { lastModified, score }
311-
} else {
312-
return { lastModified: null, score: 'N/A' }
310+
if (result) {
311+
return { lastModified, result }
312+
} else {
313+
return { lastModified: null, result: 'N/A' }
314+
}
315+
} catch (e) {
316+
console.error('could not get bot score', e)
317+
return { lastModified: null, result: 'N/A' }
313318
}
314319
}

assets/js/app/modals.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Deckdle.modalOpen = async (type) => {
7575
const finishedGamesFree = Deckdle._getFinishedGameCount('free')
7676
const bestComboFree = Deckdle._getBestCombo('free')
7777
const bestScoreFree = Deckdle._getBestScore('free')
78-
const botScoreDaily = await Deckdle._getBotScore()
78+
const botScoreDaily = (await Deckdle._getBotScore()).result.score
7979

8080
if (Deckdle.myModal) {
8181
Deckdle.myModal._destroyModal()
@@ -95,7 +95,7 @@ Deckdle.modalOpen = async (type) => {
9595
<div class="statistic-setupid">
9696
${
9797
Deckdle.__getState('daily')['setupId']
98-
} <button class="fa-solid fa-robot icon tiny solo" title="${botScoreDaily.score.trim()}" onclick="Deckdle.modalOpen('bot-score')"></button>
98+
} <button class="fa-solid fa-robot icon tiny solo" title="${botScoreDaily}" onclick="Deckdle.modalOpen('bot-score')"></button>
9999
</div>
100100
`
101101
}
@@ -345,7 +345,7 @@ Deckdle.modalOpen = async (type) => {
345345
break
346346

347347
case 'game-over': {
348-
const botScoreDaily = await Deckdle._getBotScore()
348+
const botScoreDaily = (await Deckdle._getBotScore()).result.score
349349

350350
if (Deckdle.myModal) {
351351
Deckdle.myModal._destroyModal()
@@ -404,7 +404,7 @@ Deckdle.modalOpen = async (type) => {
404404
}
405405

406406
// did you beat the bot?
407-
const botScore = parseInt(botScoreDaily.score)
407+
const botScore = parseInt(botScoreDaily)
408408

409409
// win
410410
if (tableauCount == 0) {
@@ -483,7 +483,7 @@ Deckdle.modalOpen = async (type) => {
483483
<button class="game-over share" onclick="Deckdle._shareResults()">Share <i class="fa-solid fa-share-nodes"></i></button>
484484
</div>
485485
<div>
486-
<button class="fa-solid fa-robot icon tiny solo" title="${botScoreDaily.score.trim()}" onclick="Deckdle.modalOpen('bot-score')"></button>
486+
<button class="fa-solid fa-robot icon tiny solo" title="${botScoreDaily}" onclick="Deckdle.modalOpen('bot-score')"></button>
487487
</div>
488488
`
489489
}
@@ -629,11 +629,19 @@ Deckdle.modalOpen = async (type) => {
629629
}
630630

631631
const botScoreDaily = await Deckdle._getBotScore()
632+
const score = botScoreDaily.result.score
633+
const max = botScoreDaily.result.max
634+
const min = botScoreDaily.result.min
635+
const mean = botScoreDaily.result.mean
636+
const median = botScoreDaily.result.median
637+
const mode = botScoreDaily.result.mode
632638

633639
Deckdle.myModalModal = new Modal(
634640
'perm-small',
635641
`Bot Score for ${botScoreDaily.lastModified}`,
636-
botScoreDaily.score,
642+
`<div class="bot-meta">${
643+
score > 0 ? '+' + score : score
644+
}<br /><div class="bot-statistics">max: <strong>${max}</strong>, min: <strong>${min}</strong>, mean: <strong>${mean}</strong>, median: <strong>${median}</strong>, mode: <strong>${mode}</strong></div></div>`,
637645
null,
638646
null
639647
)

0 commit comments

Comments
 (0)