richardspirit / mtga_stats

Originated as way to manage Magic The Gathering decks on MGTA so that I knew which decks were performing the best and which decks should be deleted.
GNU Lesser General Public License v3.0
1 stars 0 forks source link

Grid display #6

Closed richardspirit closed 2 years ago

richardspirit commented 3 years ago

Add dynamic grid to display query results. Use below as a guide.

package main

import ( "errors" "fmt" "time" )

type Grid struct { height, width int grid []byte }

func (g Grid) String() string { return string(g.grid) }

func NewGrid(x, y int) Grid { wth := 2x + 2 // +2 for right column + '\n' hgt := 2y + 1 // +1 for bottom row

g := make([]byte, wth*hgt)

for i := 0; i < hgt; i += 2 {
    row0 := i * wth
    row1 := (i + 1) * wth
    for j := 0; j < wth-2; j += 2 {
        g[row0+j], g[row0+j+1] = '+', '-'
        if row1+j+1 <= wth*hgt {
            g[row1+j], g[row1+j+1] = '|', ' '
        }
    }
    g[row0+wth-2], g[row0+wth-1] = '+', '\n'
    if row1+wth < wth*hgt {
        g[row1+wth-2], g[row1+wth-1] = '|', '\n'
    }
}

return Grid{
    height: y,
    width:  x,
    grid:   g,
}

}

func (g Grid) Set(c byte, x, y int) error { idx, err := g.cellAt(x, y) if err != nil { return err } g.grid[idx] = c return nil }

func (g Grid) cellAt(x, y int) (int, error) { woff := g.width2 + 2 // width offset foff := (y2+1)woff + x2 + 1

if foff > len(g.grid) {
    return 0, errors.New("out of range")
}

return (y*2+1)*woff + x*2 + 1, nil

}

func (g Grid) Draw() { fmt.Print("\033[H\033[2J") // Clear screen fmt.Print("\x0c", g, "\n") // Print frame time.Sleep(250 * time.Millisecond) // Delay between frames }

func main() { const ( w = 11 h = 11 )

g := NewGrid(w, h)
g.Draw()

// Draw X
m := max(w, h)
for i := 0; i < m; i++ {
    g.Set('\\', i, i)
    g.Set('/', w-1-i, i)
    if i == w-1-i {
        g.Set('X', i, i)
    }
    g.Draw()
}

// top-left corner
g.Set('*', 0, 0)
g.Set('*', 0, 1)
g.Set('*', 1, 0)
g.Draw()

// top-right corner
g.Set('*', w-1, 0)
g.Set('*', w-2, 0)
g.Set('*', w-1, 1)
g.Draw()

// bottom-right corner
g.Set('*', w-1, h-2)
g.Set('*', w-1, h-1)
g.Set('*', w-2, h-1)
g.Draw()

// bottom-left corner
g.Set('*', 1, h-1)
g.Set('*', 0, h-1)
g.Set('*', 0, h-2)
g.Draw()

}

func max(is ...int) int { if len(is) == 0 { return 0 }

m := is[0]
for _, v := range is[1:] {
    if v > m {
        m = v
    }
}
return m

}

richardspirit commented 3 years ago

Created temporary fix using map but it will be high maintenance if options change.

m := make(map[string]string)

// Set key/value pairs using typical `name[key] = val`
m["k1"] = "Enter New Deck"
m["k2"] = "Add New Game"
m["k3"] = "View Deck Records"
m["k4"] = "View Game Count"
m["k5"] = "Quit"

// print menu options
fmt.Println("1:", m["k1"]+"     "+"2:", m["k2"])
fmt.Println("3:", m["k3"]+"  "+"4:", m["k4"])
fmt.Println("              10:", m["k5"])
richardspirit commented 3 years ago

Modified above to use verb for spacing to improve formatting and ease of reading code.

richardspirit commented 3 years ago
m["k1"] = fmt.Sprintf("%-20s", "Enter New Deck")
m["k2"] = fmt.Sprintf("%-20s", "Add New Game")
m["k3"] = fmt.Sprintf("%-20s", "View Deck Records")
m["k4"] = fmt.Sprintf("%-20s", "View Game Count")
m["k5"] = fmt.Sprintf("%-20s", "View All Decks")
m["k10"] = fmt.Sprintf("%20s", "10: Quit")

// print menu options
fmt.Println("1:", m["k1"]+"2:", m["k2"])
fmt.Println("3:", m["k3"]+"4:", m["k4"])
fmt.Println("5:", m["k5"])
fmt.Println(m["k10"])
richardspirit commented 2 years ago

Need a fix for this section - List Favorites Deck: Armed and Dangerous Date Entered: 2021-08-24 Wins: 11 Loses: 5 Deck: Avalanche! Date Entered: 2021-08-25 Wins: 15 Loses: 6 Deck: Back for More Date Entered: 2021-08-22 Wins: 9 Loses: 4 Deck: Forest's Might Date Entered: 2021-08-17 Wins: 17 Loses: 8 Deck: Large and in Charge Date Entered: 2021-08-22 Wins: 8 Loses: 7 Deck: Life Skills Date Entered: 2021-08-17 Wins: 9 Loses: 8 Deck: Savage Lands Date Entered: 2021-08-24 Wins: 8 Loses: 5 Deck: Sky Patrol Date Entered: 2021-08-24 Wins: 4 Loses: 7 Deck: Stomp Stomp Date Entered: 2021-08-17 Wins: 7 Loses: 9 Deck: Treasure Hunt Date Entered: 2021-08-24 Wins: 10 Loses: 4 Deck: White Flame Date Entered: 2021-08-17 Wins: 8 Loses: 6