itAdd login key in app to show personal badge collection - scoreboard - Interactive scoreboard for CTF-like games Err z3bra.org 70 hgit clone git://git.z3bra.org/scoreboard.git URL:git://git.z3bra.org/scoreboard.git z3bra.org 70 1Log /scm/scoreboard/log.gph z3bra.org 70 1Files /scm/scoreboard/files.gph z3bra.org 70 1Refs /scm/scoreboard/refs.gph z3bra.org 70 i--- Err z3bra.org 70 1commit cb97cb5f224a91c0f4b47db43fae0f37f6e1a079 /scm/scoreboard/commit/cb97cb5f224a91c0f4b47db43fae0f37f6e1a079.gph z3bra.org 70 1parent f77371dc092804946f8d32c60dbdf1a10b9516da /scm/scoreboard/commit/f77371dc092804946f8d32c60dbdf1a10b9516da.gph z3bra.org 70 hAuthor: Willy Goiffon URL:mailto:contact@z3bra.org z3bra.org 70 iDate: Fri, 27 Sep 2024 18:08:23 +0200 Err z3bra.org 70 i Err z3bra.org 70 iAdd login key in app to show personal badge collection Err z3bra.org 70 i Err z3bra.org 70 iDiffstat: Err z3bra.org 70 i M main.go | 28 +++++++++++----------------- Err z3bra.org 70 i M player.go | 8 ++++++-- Err z3bra.org 70 i M ui.go | 40 +++++++++++++++++++++++++------- Err z3bra.org 70 i Err z3bra.org 70 i3 files changed, 49 insertions(+), 27 deletions(-) Err z3bra.org 70 i--- Err z3bra.org 70 1diff --git a/main.go b/main.go /scm/scoreboard/file/main.go.gph z3bra.org 70 it@@ -120,7 +120,13 @@ func main() { Err z3bra.org 70 i scoreboard.app = tview.NewApplication() Err z3bra.org 70 i scoreboard.pages = tview.NewPages() Err z3bra.org 70 i scoreboard.board = tview.NewFlex() Err z3bra.org 70 i- scoreboard.player = &Player{ db: scoreboard.db } Err z3bra.org 70 i+ scoreboard.player = &Player{ Err z3bra.org 70 i+ db: scoreboard.db, Err z3bra.org 70 i+ flag: 0, Err z3bra.org 70 i+ score: 0, Err z3bra.org 70 i+ ts: time.Now().Unix(), Err z3bra.org 70 i+ token: "", Err z3bra.org 70 i+ } Err z3bra.org 70 i Err z3bra.org 70 i scoreboard.pages.SetBackgroundColor(tcell.ColorDefault) Err z3bra.org 70 i Err z3bra.org 70 it@@ -145,22 +151,11 @@ func main() { Err z3bra.org 70 i os.Exit(0) Err z3bra.org 70 i Err z3bra.org 70 i case "register": Err z3bra.org 70 i- scoreboard.player.flag = 0 Err z3bra.org 70 i- scoreboard.player.score = 0 Err z3bra.org 70 i- scoreboard.player.ts = time.Now().Unix() Err z3bra.org 70 i- Err z3bra.org 70 i rank, _ := db_count_players(scoreboard.db) Err z3bra.org 70 i scoreboard.NewPlayer(rank + 1) Err z3bra.org 70 i scoreboard.pages.SwitchToPage("board") Err z3bra.org 70 i reminder = true Err z3bra.org 70 i Err z3bra.org 70 i- case "badges": Err z3bra.org 70 i- badgepage := scoreboard.Token(func () { Err z3bra.org 70 i- scoreboard.app.Stop() Err z3bra.org 70 i- fmt.Printf("Collection: %d/%d\n\n%s", scoreboard.player.flag, len(scoreboard.flag_ref), scoreboard.player.BadgeStr()) Err z3bra.org 70 i- }) Err z3bra.org 70 i- scoreboard.pages.AddAndSwitchToPage("badge", badgepage, true) Err z3bra.org 70 i- Err z3bra.org 70 i /* anything not a command is treated as a flag */ Err z3bra.org 70 i default: Err z3bra.org 70 i scoreboard.flag, err = db_get_flag(scoreboard.db, args[0]) Err z3bra.org 70 it@@ -169,19 +164,18 @@ func main() { Err z3bra.org 70 i return Err z3bra.org 70 i } Err z3bra.org 70 i Err z3bra.org 70 i- submitpage := scoreboard.Token(func () { Err z3bra.org 70 i+ page := scoreboard.Token(func () { Err z3bra.org 70 i err = scoreboard.player.Submit(scoreboard.flag) Err z3bra.org 70 i if err != nil { Err z3bra.org 70 i scoreboard.Fatal(err) Err z3bra.org 70 i return Err z3bra.org 70 i } Err z3bra.org 70 i scoreboard.HighlightBoard(scoreboard.player.ScoreRank() + 1) Err z3bra.org 70 i- scoreboard.pages.RemovePage("submit") Err z3bra.org 70 i- scoreboard.pages.ShowPage("board") Err z3bra.org 70 i+ scoreboard.pages.RemovePage("token") Err z3bra.org 70 i scoreboard.GenerateHTML() Err z3bra.org 70 i }) Err z3bra.org 70 i Err z3bra.org 70 i- scoreboard.pages.AddAndSwitchToPage("submit", submitpage, true) Err z3bra.org 70 i+ scoreboard.pages.AddAndSwitchToPage("token", page, true) Err z3bra.org 70 i } Err z3bra.org 70 i } Err z3bra.org 70 i Err z3bra.org 70 it@@ -192,7 +186,7 @@ func main() { Err z3bra.org 70 i } Err z3bra.org 70 i Err z3bra.org 70 i /* Print a token reminder on exit in case one has been generated or provided */ Err z3bra.org 70 i- if reminder { Err z3bra.org 70 i+ if reminder && scoreboard.player.token != "" { Err z3bra.org 70 i fmt.Printf(TOKEN_REMINDER, scoreboard.player.name, scoreboard.player.token) Err z3bra.org 70 i } Err z3bra.org 70 i } Err z3bra.org 70 1diff --git a/player.go b/player.go /scm/scoreboard/file/player.go.gph z3bra.org 70 it@@ -147,7 +147,7 @@ func (p *Player) FlagStr() string { Err z3bra.org 70 i return fmt.Sprintf("%2d/%d", p.flag, len(scoreboard.flag_ref)) Err z3bra.org 70 i } Err z3bra.org 70 i Err z3bra.org 70 i-func (p *Player) BadgeStr() string { Err z3bra.org 70 i+func (p *Player) BadgeStr(full bool) string { Err z3bra.org 70 i var badges strings.Builder Err z3bra.org 70 i Err z3bra.org 70 i query := `SELECT Err z3bra.org 70 it@@ -168,7 +168,11 @@ func (p *Player) BadgeStr() string { Err z3bra.org 70 i if err != nil { Err z3bra.org 70 i return "" Err z3bra.org 70 i } Err z3bra.org 70 i- badges.WriteString(fmt.Sprintf("%s %s (%d pts)\n", b, v, s)) Err z3bra.org 70 i+ if full { Err z3bra.org 70 i+ badges.WriteString(fmt.Sprintf("%s %s\n", b, v)) Err z3bra.org 70 i+ } else { Err z3bra.org 70 i+ badges.WriteString(b) Err z3bra.org 70 i+ } Err z3bra.org 70 i } Err z3bra.org 70 i Err z3bra.org 70 i return badges.String(); Err z3bra.org 70 1diff --git a/ui.go b/ui.go /scm/scoreboard/file/ui.go.gph z3bra.org 70 it@@ -58,6 +58,19 @@ func RankTable(offset, limit, rank int, fill bool) *tview.Table { Err z3bra.org 70 i t.SetCell(i, 3, newcell(scorestr)) Err z3bra.org 70 i } Err z3bra.org 70 i Err z3bra.org 70 i+ t.SetSelectedFunc(func (row, col int) { Err z3bra.org 70 i+ var player Player Err z3bra.org 70 i+ col = 1 Err z3bra.org 70 i+ player.name = t.GetCell(row, col).Text Err z3bra.org 70 i+ player.db = scoreboard.db Err z3bra.org 70 i+ player.Fetch() Err z3bra.org 70 i+ if scoreboard.player.name == player.name { Err z3bra.org 70 i+ scoreboard.Message(player.name, player.BadgeStr(true)) Err z3bra.org 70 i+ } else { Err z3bra.org 70 i+ scoreboard.Popup(player.name, player.BadgeStr(false)) Err z3bra.org 70 i+ } Err z3bra.org 70 i+ }) Err z3bra.org 70 i+ Err z3bra.org 70 i if fill == true { Err z3bra.org 70 i bsize := int(math.Max(float64(BOARD_HEIGHT), float64(limit))) Err z3bra.org 70 i for i:=t.GetRowCount(); i