tEnsure player names are uniques - scoreboard - Interactive scoreboard for CTF-like games
(HTM) git clone git://git.z3bra.org/scoreboard.git
(DIR) Log
(DIR) Files
(DIR) Refs
---
(DIR) commit e74b632684c58ebb06b40f395409f7759a3ca9fb
(DIR) parent 8abd5edce6b5776b4288254ea3f19fd4d7406c18
(HTM) Author: Willy Goiffon <contact@z3bra.org>
Date: Mon, 5 Dec 2022 14:10:51 +0100
Ensure player names are uniques
Diffstat:
M db.go | 34 ++++++++++++++++++++++---------
M main.go | 2 +-
M playerbox.go | 8 ++++++--
M ui.go | 19 +++++++++++++++++++
4 files changed, 50 insertions(+), 13 deletions(-)
---
(DIR) diff --git a/db.go b/db.go
t@@ -70,32 +70,46 @@ func (a *Application) db_count() int {
return count
}
-func (a *Application) db_rank(score int, ts int64) int {
- var rank int
+func (a *Application) db_count_flag(flag int) int {
+ var count int
query := `SELECT
count(id)
FROM score
WHERE
- score >= ?
+ flags >= ?
;`
- row := a.db.QueryRow(query, score)
- row.Scan(&rank)
- return rank + 1
+ row := a.db.QueryRow(query, flag)
+ row.Scan(&count)
+ return count
}
-func (a *Application) db_count_flag(flag int) int {
+func (a *Application) db_checkname(nick string) bool {
var count int
query := `SELECT
count(id)
FROM score
WHERE
- flags >= ?
+ name = ?
;`
- row := a.db.QueryRow(query, flag)
+ row := a.db.QueryRow(query, nick)
row.Scan(&count)
- return count
+ return (count > 0)
+}
+
+func (a *Application) db_rank(score int, ts int64) int {
+ var rank int
+ query := `SELECT
+ count(id)
+ FROM score
+ WHERE
+ score >= ?
+ ;`
+
+ row := a.db.QueryRow(query, score)
+ row.Scan(&rank)
+ return rank + 1
}
func (a *Application) db_ranked(offset, limit int) ([]Player, error) {
(DIR) diff --git a/main.go b/main.go
t@@ -151,7 +151,7 @@ func main() {
cyboard.player.ts = time.Now().Unix()
/* Bonus points for the first 10 players to get first flag */
- n := cyboard.db_count()
+ n := cyboard.db_count_flag(1)
if n < 10 {
cyboard.player.score += 10 - n
}
(DIR) diff --git a/playerbox.go b/playerbox.go
t@@ -70,8 +70,12 @@ func PlayerBoxName(p Player) *tview.TextView {
SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
p.name = fmt.Sprintf("%3s", playerbox.name)
- cyboard.db_save(p)
- cyboard.pages.SwitchToPage("board")
+ if cyboard.db_checkname(p.name) == false {
+ cyboard.db_save(p)
+ cyboard.DrawBoard()
+ } else {
+ cyboard.Popup("Error", "Name already registered\nPlease pick another one")
+ }
}
})
(DIR) diff --git a/ui.go b/ui.go
t@@ -84,3 +84,22 @@ func (a *Application) NewPlayer(rank int) {
AddItem(t2, BOARD_HEIGHT - rank, 0, false)
}
+func (a *Application) Popup(title, text string) {
+ p := tview.NewTextView().
+ SetDynamicColors(true).
+ SetTextAlign(tview.AlignCenter).
+ SetText("[::b]" + text).
+ SetDoneFunc(func(key tcell.Key) {
+ a.pages.RemovePage("popup")
+ })
+
+ frame := tview.NewFrame(p).
+ SetBorders(1, 0, 2, 2, 1, 1).
+ AddText("Press RET", false, tview.AlignRight, tcell.ColorGray)
+
+ frame.SetBorder(true).SetTitle(title)
+
+ c := center(30, 8, frame)
+
+ a.pages.AddAndSwitchToPage("popup", c, true)
+}