tRefactor shit - scoreboard - Interactive scoreboard for CTF-like games
 (HTM) git clone git://git.z3bra.org/scoreboard.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
 (DIR) commit d4209a84c73bef28336b2f1f63b39181f7dfc8da
 (DIR) parent 21e47edfb41eee155915a67d456a48755a070321
 (HTM) Author: Willy Goiffon <contact@z3bra.org>
       Date:   Tue,  6 Dec 2022 15:42:20 +0100
       
       Refactor shit
       
       Diffstat:
         M main.go                             |      13 ++++++++++++-
         M player.go                           |      15 ++++++++++++++-
         M playerbox.go                        |       6 +++---
         M ui.go                               |      92 +++++++++++++++++--------------
       
       4 files changed, 80 insertions(+), 46 deletions(-)
       ---
 (DIR) diff --git a/main.go b/main.go
       t@@ -19,6 +19,7 @@ import (
                "fmt"
                "os"
                "regexp"
       +        "strings"
                "time"
                "database/sql"
                "github.com/gdamore/tcell/v2"
       t@@ -63,6 +64,16 @@ func usage() {
                os.Exit(0)
        }
        
       +
       +func flagid(hash string) int {
       +        for i := 0; i<len(flag_sha256); i++ {
       +                if strings.ToUpper(hash) == flag_sha256[i] {
       +                        return i
       +                }
       +        }
       +        return -1
       +}
       +
        func pageToken() tview.Primitive {
                input := tview.NewInputField().
                        SetLabel("TOKEN ").
       t@@ -100,7 +111,7 @@ func pageBoard() tview.Primitive {
                cyboard.SetupFrame()
                cyboard.DrawBoard()
        
       -        return center(30, 20, cyboard.frame)
       +        return center(28, 19, cyboard.frame)
        }
        
        func main() {
 (DIR) diff --git a/player.go b/player.go
       t@@ -88,6 +88,19 @@ func (p *Player) FlagRank() int {
                return count
        }
        
       +func (p *Player) FlagStr() string {
       +        var str [5]byte
       +        for i:=0; i <len(str); i++ {
       +                if i < p.flag {
       +                        str[i] = 'X'
       +                } else {
       +                        str[i] = '.'
       +                }
       +        }
       +
       +        return fmt.Sprintf("%s", str)
       +}
       +
        func (p *Player) Exists() bool {
                forbidden := []string{"KKK", "WGS"}
                for i := 0; i<len(forbidden); i++ {
       t@@ -111,7 +124,7 @@ func (p *Player) Exists() bool {
        
        func (p *Player) Submit(flag int) error {
                if flag <= p.flag {
       -                return errors.New("Flag already submitted")
       +                return errors.New(fmt.Sprintf("Flag already set for %s", p.name))
                }
        
                if flag != p.flag + 1 {
 (DIR) diff --git a/playerbox.go b/playerbox.go
       t@@ -72,9 +72,9 @@ func PlayerBoxName(p Player) *tview.TextView {
                                        p.name = fmt.Sprintf("%3s", playerbox.name)
                                        if ! p.Exists() {
                                                p.Register()
       -                                        cyboard.DrawBoard()
       +                                        cyboard.HighlightBoard(p.ScoreRank())
                                        } else {
       -                                        cyboard.Popup("Error", "Player name unavailable\nPlease pick another one")
       +                                        cyboard.Popup("NOPE", "Player name unavailable\nPlease pick another one")
                                        }
                                }
                        })
       t@@ -109,7 +109,7 @@ func PlayerBoxGrid(p Player, rank int) *tview.Grid {
                        SetRows(1).
                        AddItem(gridcell(rankstr), 0, 0, 1, 1, 0, 0, false).
                        AddItem(box, 0, 1, 1, 1, 0, 0, true).
       -                AddItem(gridcell(flag2str(p.flag)), 0, 2, 1, 1, 0, 0, false).
       +                AddItem(gridcell(p.FlagStr()), 0, 2, 1, 1, 0, 0, false).
                        AddItem(playerbox.score, 0, 3, 1, 1, 0, 0, false)
        
                return grid
 (DIR) diff --git a/ui.go b/ui.go
       t@@ -8,6 +8,17 @@ import (
                "github.com/dustin/go-humanize"
        )
        
       +func center(width, height int, p tview.Primitive) tview.Primitive {
       +        return tview.NewFlex().
       +                AddItem(nil, 0, 1, false).
       +                AddItem(tview.NewFlex().
       +                        SetDirection(tview.FlexRow).
       +                        AddItem(nil, 0, 1, false).
       +                        AddItem(p, height, 1, true).
       +                        AddItem(nil, 0, 1, false), width, 1, true).
       +                AddItem(nil, 0, 1, false)
       +}
       +
        func BoardHeader() *tview.TextView {
                return tview.NewTextView().
                        SetDynamicColors(true).
       t@@ -22,17 +33,24 @@ func RankTable(offset, limit, rank int, fill bool) *tview.Table {
                        panic(err)
                }
        
       +        newcell := func(text string) *tview.TableCell {
       +                return tview.NewTableCell(text).
       +                        SetTransparency(true).
       +                        SetSelectable(true).
       +                        SetAlign(tview.AlignRight).
       +                        SetExpansion(1)
       +        }
       +
                t.SetSelectable(true, false).
                        SetSelectedStyle(tcell.StyleDefault.Reverse(true))
        
                for i := 0; i < len(players); i++ {
                        p := players[i]
                        rankstr := fmt.Sprintf("%4s", humanize.Ordinal(rank + i + 1))
       -                flagstr := flag2str(p.flag)
                        scorestr := fmt.Sprintf("%4d", p.score)
                        t.SetCell(i, 0, newcell(rankstr))
                        t.SetCell(i, 1, newcell(p.name))
       -                t.SetCell(i, 2, newcell(flagstr))
       +                t.SetCell(i, 2, newcell(p.FlagStr()))
                        t.SetCell(i, 3, newcell(scorestr))
                }
        
       t@@ -40,11 +58,10 @@ func RankTable(offset, limit, rank int, fill bool) *tview.Table {
                        bsize := int(math.Max(float64(BOARD_HEIGHT), float64(limit)))
                        for i:=t.GetRowCount(); i<bsize; i++ {
                                rankstr  := fmt.Sprintf("%4s", humanize.Ordinal(rank + i + 1))
       -                        flagstr  := "....."
                                scorestr := fmt.Sprintf("%4d", 0)
                                t.SetCell(i, 0, newcell(rankstr).SetTextColor(tcell.ColorGray))
                                t.SetCell(i, 1, newcell("AAA").SetTextColor(tcell.ColorGray))
       -                        t.SetCell(i, 2, newcell(flagstr).SetTextColor(tcell.ColorGray))
       +                        t.SetCell(i, 2, newcell(".....").SetTextColor(tcell.ColorGray))
                                t.SetCell(i, 3, newcell(scorestr).SetTextColor(tcell.ColorGray))
                        }
                }
       t@@ -54,25 +71,26 @@ func RankTable(offset, limit, rank int, fill bool) *tview.Table {
        
        
        func (a *Application) SetupFrame() {
       -        cyboard.frame.
       +        a.frame.
                        SetColumns(BOARD_WIDTH).
                        SetRows(1, BOARD_HEIGHT).
                        SetBorders(true)
        
       -        cyboard.frame.AddItem(BoardHeader(), 0, 0, 1, 1, 1, BOARD_WIDTH, false)
       -        cyboard.frame.AddItem(cyboard.board, 1, 0, 1, 1, BOARD_HEIGHT, BOARD_WIDTH, true)
       +        a.frame.AddItem(BoardHeader(), 0, 0, 1, 1, 1, BOARD_WIDTH, false)
       +        a.frame.AddItem(a.board, 1, 0, 1, 1, BOARD_HEIGHT, BOARD_WIDTH, true)
        }
        
        func (a *Application) DrawBoard() {
       -        cyboard.board.Clear().
       +        a.board.Clear().
                        SetDirection(tview.FlexRow).
                        AddItem(RankTable(0, -1, 0, true), BOARD_HEIGHT, 1, true)
        }
        
        func (a *Application) HighlightBoard(line int) {
       -        cyboard.board.Clear().
       +        a.board.Clear().
                        SetDirection(tview.FlexRow).
                        AddItem(RankTable(0, -1, 0, true).Select(line - 1, 0), BOARD_HEIGHT, 1, true)
       +        a.app.SetFocus(a.board)
        }
        
        func (a *Application) NewPlayer(rank int) {
       t@@ -86,53 +104,45 @@ func (a *Application) NewPlayer(rank int) {
                        t1_sz = rank - 1
                        t2_sz = BOARD_HEIGHT - rank - 1
                }
       -        t1 := RankTable(offset, BOARD_HEIGHT, t1_sz, false).SetSelectable(false, false)
       +        t1 := RankTable(offset, t1_sz, rank - t1_sz - 1, false).SetSelectable(false, false)
                t2 := RankTable(rank - 1, BOARD_HEIGHT, t2_sz, true).SetSelectable(false, false)
       -        box := PlayerBoxGrid(cyboard.player, rank)
       +        box := PlayerBoxGrid(a.player, rank)
        
       -        cyboard.board.Clear().
       +        a.board.Clear().
                        SetDirection(tview.FlexRow).
       -                AddItem(t1, rank-1, 0, false).
       +                AddItem(t1, t1_sz, 0, false).
                        AddItem(box, 1, 0, true).
       -                AddItem(t2, BOARD_HEIGHT - rank, 0, false)
       +                AddItem(t2, t2_sz, 0, false)
        }
        
       -func (a *Application) Popup(title, text string) {
       +func popup(title, text string, callback func(key tcell.Key)) tview.Primitive {
                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)
       +                SetText(fmt.Sprintf("[::b]%s", text)).
       +                SetDoneFunc(callback)
        
       -        frame.SetBorder(true).SetTitle(title)
       +        popup := tview.NewFrame(p).
       +                SetBorders(7, 0, 0, 0, 1, 1).
       +                AddText("PRESS ENTER", false, tview.AlignRight, tcell.ColorGray)
        
       -        c := center(30, 8, frame)
       +        popup.SetBorder(true).SetTitle(fmt.Sprintf(" %s ", title))
        
       -        a.pages.AddAndSwitchToPage("popup", c, true)
       +        return center(28, 19, popup)
        }
        
       -func (a *Application) Fatal(err error) {
       -        p := tview.NewTextView().
       -                SetDynamicColors(true).
       -                SetTextAlign(tview.AlignCenter).
       -                SetText(fmt.Sprintf("[::b]%s", err)).
       -                SetDoneFunc(func(key tcell.Key) {
       -                        a.app.Stop()
       -                })
       -
       -        frame := tview.NewFrame(p).
       -                SetBorders(1, 0, 2, 2, 1, 1).
       -                AddText("Press RET", false, tview.AlignRight, tcell.ColorGray)
       +func (a *Application) Popup(title, text string) {
       +        p := popup(title, text, func(key tcell.Key) {
       +                a.pages.RemovePage("popup")
       +        })
        
       -        frame.SetBorder(true).SetTitle(" ERROR ")
       +        a.pages.AddAndSwitchToPage("popup", p, true)
       +}
        
       -        c := center(30, 8, frame)
       +func (a *Application) Fatal(err error) {
       +        p := popup("ERROR", fmt.Sprintf("%s", err), func(key tcell.Key) {
       +                a.app.Stop()
       +        })
        
       -        a.pages.AddAndSwitchToPage("fatal", c, true)
       +        a.pages.AddAndSwitchToPage("popup", p, true)
        }