Skip to content

Commit

Permalink
implemented highscore confirming
Browse files Browse the repository at this point in the history
  • Loading branch information
noel-friedrich committed Apr 16, 2024
1 parent b69dc0e commit f02a8e3
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ The following list can also be viewed using the `whatis *` command
| `helloworld` | display the hello-world text |
| `help` | shows this help menu |
| `hi` | say hello to the terminal |
| `highscore-admin` | Login as Admin |
| `highscore-admin` | Highscore Admin Management |
| `highscore-remove` | Remove a highscore |
| `highscores` | Show global highscores for a game |
| `history` | print the command history |
Expand Down
4 changes: 2 additions & 2 deletions gui/highscore-admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h1>
</h1>

<p class="subtitle">
Login as Admin
Highscore Admin Management
</p>

<div id="inputs-container"></div>
Expand Down Expand Up @@ -55,7 +55,7 @@ <h1>
<script src="../../js/terminal.js"></script>

<script>
const command_data = {"description": "Login as Admin", "isSecret": true, "args": {"?d": "Delete password from local storage"}, "name": "highscore-admin"}
const command_data = {"description": "Highscore Admin Management", "isSecret": true, "args": {"?l=list:b": "List all unconfirmed highscores", "?t=tinder:b": "Play Tinder Swiping with highscores", "?d=delete:b": "Delete password from local storage"}, "name": "highscore-admin"}
</script>

<script src="../main.js"></script>
Expand Down
46 changes: 42 additions & 4 deletions js/commands/highscore-admin.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
terminal.addCommand("highscore-admin", async function(args) {
await terminal.modules.import("game", window)

if (args.d) {
if (args.delete) {
localStorage.removeItem("highscore_password")
HighscoreApi.tempPassword = null
terminal.printLine("Removed password from local storage")
return
} else if (args.list) {
await HighscoreApi.loginAdmin(true)
const highscores = await HighscoreApi.getUnconfirmedHighscores()
highscores.sort((a, b) => a.game.localeCompare(b.game))

terminal.printLine(`Showing ${highscores.length} Highscores...`)
terminal.printTable(highscores.map(s => [
s.id, s.game, s.name, s.score, s.time
]), ["id", "game", "name", "score", "time"])
} else if (args.tinder) {
await HighscoreApi.loginAdmin(true)
const highscores = await HighscoreApi.getUnconfirmedHighscores()
highscores.sort((a, b) => a.game.localeCompare(b.game))

let i = 0
for (let s of highscores) {
i++

terminal.printLine(`> Highscore #${i}/${highscores.length}:`)
try {
terminal.printTable([[
s.id, s.game, s.name, s.score, s.time
]], ["id", "game", "name", "score", "time"])
await terminal.acceptPrompt("Looks good?")
await HighscoreApi.confirmHighscore(s.uid, 1)
terminal.addLineBreak()
} catch {
await HighscoreApi.confirmHighscore(s.uid, 2)
terminal.printSuccess("Removed Highscore successfully")
terminal.addLineBreak()
}
}

terminal.printSuccess("You're finished!")
} else {
await HighscoreApi.loginAdmin()
}
await HighscoreApi.loginAdmin()

}, {
description: "Login as Admin",
description: "Highscore Admin Management",
isSecret: true,
args: {
"?d": "Delete password from local storage"
"?l=list:b": "List all unconfirmed highscores",
"?t=tinder:b": "Play Tinder Swiping with highscores",
"?d=delete:b": "Delete password from local storage"
}
})
2 changes: 1 addition & 1 deletion js/commands/highscore-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ terminal.addCommand("highscore-remove", async function(args) {
await terminal.modules.import("game", window)
await HighscoreApi.loginAdmin(true)
if (args.uid) {
await HighscoreApi.removeHighscore(HighscoreApi.tempPassword, args.uid)
await HighscoreApi.removeHighscore(args.uid)
terminal.printSuccess("Removed highscore #" + args.uid)
return
}
Expand Down
2 changes: 1 addition & 1 deletion js/load-commands.js

Large diffs are not rendered by default.

32 changes: 29 additions & 3 deletions js/modules/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,12 @@ class HighscoreApi {
await this.req("upload_highscore", {game, name, score})
}

static async removeHighscore(password, uid) {
await this.req("remove_highscore", {password, uid})
static async removeHighscore(uid) {
if (!this.savedPassword) {
throw new Error("Permission denied [E2]")
}

await this.req("remove_highscore", {password: this.savedPassword, uid})
}

static async getUsername() {
Expand Down Expand Up @@ -456,6 +460,28 @@ class HighscoreApi {
return highscores[0]
}

static get savedPassword() {
return localStorage.getItem("highscore_password")
}

static async getUnconfirmedHighscores() {
if (!this.savedPassword) {
throw new Error("Permission denied")
}

let data = await this.req("get_unconfirmed_highscores", {password: this.savedPassword})
return JSON.parse(data)
}

static async confirmHighscore(uid, value=1) {
if (!this.savedPassword) {
throw new Error("Permission denied")
}

await this.req("confirm_highscore",
{uid, password: this.savedPassword, value})
}

static async loginAdmin(silent=false) {
if (this.tempPassword != null) return

Expand All @@ -476,7 +502,7 @@ class HighscoreApi {
localStorage.setItem("highscore_password", password)
} else {
localStorage.removeItem("highscore_password")
throw new Error("Incorrect password")
throw new Error("Permission denied [E1]")
}
}

Expand Down

0 comments on commit f02a8e3

Please sign in to comment.