tUse switch/case to handle built-in commands - scoreboard - Interactive scoreboard for CTF-like games
(HTM) git clone git://git.z3bra.org/scoreboard.git
(DIR) Log
(DIR) Files
(DIR) Refs
---
(DIR) commit 8a7650dfacce3928c15355cff32ba81179141352
(DIR) parent 307cde02b7be14d34c37e7f02eeaea907d1c0d84
(HTM) Author: Willy Goiffon <contact@z3bra.org>
Date: Thu, 26 Sep 2024 10:54:40 +0200
Use switch/case to handle built-in commands
Diffstat:
M main.go | 27 +++++++++++++++++----------
1 file changed, 17 insertions(+), 10 deletions(-)
---
(DIR) diff --git a/main.go b/main.go
t@@ -182,15 +182,21 @@ func main() {
if len(args) > 1 {
usage()
- } else if len(args) == 1 {
- if args[0] == "help" {
+ }
+ if len(args) == 0 {
+ scoreboard.pages.SwitchToPage("board")
+ scoreboard.DrawBoard()
+ }
+
+ if len(args) == 1 {
+ switch args[0] {
+ case "help":
usage()
- }
- if args[0] == "refresh" {
+ case "refresh":
scoreboard.GenerateHTML()
os.Exit(0)
- }
- if args[0] == "register" {
+
+ case "register":
scoreboard.player.flag = 0
scoreboard.player.score = 0
scoreboard.player.ts = time.Now().Unix()
t@@ -198,7 +204,9 @@ func main() {
rank, _ := db_count_players(scoreboard.db)
scoreboard.NewPlayer(rank + 1)
scoreboard.pages.SwitchToPage("board")
- } else {
+
+ /* anything not a command is treated as a flag */
+ default:
if flagid(args[0]) < 0 {
fmt.Println("Incorrect flag")
return
t@@ -206,16 +214,15 @@ func main() {
scoreboard.flag = args[0]
scoreboard.pages.SwitchToPage("token")
}
- } else {
- scoreboard.pages.SwitchToPage("board")
- scoreboard.DrawBoard()
}
+ /* Run application */
if err := scoreboard.app.SetRoot(scoreboard.pages, true).EnableMouse(true).Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
+ /* Print a token reminder on exit in case one has been generated or provided */
if scoreboard.player.token != "" {
fmt.Printf(TOKEN_REMINDER, scoreboard.player.name, scoreboard.player.token)
}