tplayerbox.go - scoreboard - Interactive scoreboard for CTF-like games
(HTM) git clone git://git.z3bra.org/scoreboard.git
(DIR) Log
(DIR) Files
(DIR) Refs
---
tplayerbox.go (2999B)
---
1 package main
2
3 import (
4 "fmt"
5 "github.com/gdamore/tcell/v2"
6 "github.com/rivo/tview"
7 "github.com/dustin/go-humanize"
8 )
9
10 type PlayerBox struct {
11 name []byte
12 char []int
13 cur int
14 box *tview.TextView
15 score *tview.TextView
16 }
17
18 const (
19 TOKEN_WELCOME string = `
20 %s, your registration is
21 now complete. To update
22 your progression, you
23 will need this token:
24
25 🔑%s
26
27
28
29 Save it carefully.
30 Do not share it.
31 `
32 )
33
34 var charlist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-!"
35 var playerbox = PlayerBox {name: []byte("AAA"), char: []int{0,0,0}, cur: 0}
36
37 func boxtext (b PlayerBox) string {
38 str := ""
39 for i:=0; i<3; i++ {
40 b.name[i] = charlist[b.char[i]]
41 if i == b.cur {
42 str = fmt.Sprintf("%s[::rl]%c[::-]", str, b.name[i])
43 } else {
44 str = fmt.Sprintf("%s%c", str, b.name[i])
45 }
46 }
47 return str
48 }
49
50 func manipulatebox(event *tcell.EventKey) *tcell.EventKey {
51 switch event.Key() {
52 case tcell.KeyLeft:
53 playerbox.cur--
54 if playerbox.cur < 0 {
55 playerbox.cur = 2
56 }
57 case tcell.KeyRight:
58 playerbox.cur++
59 if playerbox.cur > 2 {
60 playerbox.cur = 0
61 }
62 case tcell.KeyUp:
63 playerbox.char[playerbox.cur]++
64 if playerbox.char[playerbox.cur] >= len(charlist) {
65 playerbox.char[playerbox.cur] = 0
66 }
67 case tcell.KeyDown:
68 playerbox.char[playerbox.cur]--
69 if playerbox.char[playerbox.cur] < 0 {
70 playerbox.char[playerbox.cur] = len(charlist) - 1
71 }
72 }
73 playerbox.box.SetText(boxtext(playerbox))
74 return event
75 }
76
77 func PlayerBoxName(p *Player) *tview.TextView {
78 v := tview.NewTextView().
79 SetDynamicColors(true).
80 SetTextAlign(tview.AlignRight).
81 SetText(boxtext(playerbox)).
82 SetChangedFunc(func() {
83 scoreboard.app.Draw()
84 }).
85 SetDoneFunc(func(key tcell.Key) {
86 if key == tcell.KeyEnter {
87 p.name = fmt.Sprintf("%3s", playerbox.name)
88 if ! p.Exists() {
89 err := p.Register()
90 if err != nil {
91 scoreboard.Fatal(err)
92 }
93 scoreboard.HighlightBoard(p.Rank())
94 scoreboard.GenerateHTML()
95 scoreboard.Popup("WELCOME", fmt.Sprintf(TOKEN_WELCOME, p.name, p.token));
96 } else {
97 scoreboard.Popup("NOPE", "Player name unavailable\nPlease pick another one")
98 }
99 }
100 })
101
102 v.Focus(func(p tview.Primitive) {
103 v.SetText(fmt.Sprintf("%4d ", scoreboard.player.score))
104 })
105
106 v.SetInputCapture(manipulatebox)
107
108 return v
109 }
110
111 func PlayerBoxGrid(p *Player, rank int) *tview.Grid {
112 gridcell := func (text string) *tview.TextView {
113 return tview.NewTextView().
114 SetDynamicColors(true).
115 SetTextAlign(tview.AlignRight).
116 SetText(fmt.Sprintf("[::b]%s",text))
117 }
118
119 rankstr := humanize.Ordinal(rank)
120
121 box := PlayerBoxName(p)
122
123 playerbox.box = box
124 playerbox.score = gridcell(fmt.Sprintf("%5d ", p.score))
125
126 grid := tview.NewGrid().
127 SetColumns(5,4,6,7).
128 SetGap(0, 2).
129 SetRows(1).
130 AddItem(gridcell(rankstr), 0, 0, 1, 1, 0, 0, false).
131 AddItem(box, 0, 1, 1, 1, 0, 0, true).
132 AddItem(gridcell(p.FlagStr()), 0, 2, 1, 1, 0, 0, false).
133 AddItem(playerbox.score, 0, 3, 1, 1, 0, 0, false)
134
135 return grid
136 }