Automatically refresh match listings

This commit is contained in:
Trevor Slocum 2023-10-23 23:22:44 -07:00
parent eaae0012e8
commit c5ab851ca8
3 changed files with 62 additions and 32 deletions

View file

@ -2,6 +2,7 @@
- Add connect button
- Support touch input
- Change layout based on portrait or landscape view
- Automatically refresh match listings
1.0.2:
- Allow specifying match points

View file

@ -376,8 +376,7 @@ type Game struct {
Board *board
lobby *lobby
pendingGames []bgammon.GameListing
lobby *lobby
runeBuffer []rune
userInput string
@ -399,6 +398,8 @@ type Game struct {
pressedKeys []ebiten.Key
cursorX, cursorY int
lastRefresh time.Time
}
func NewGame() *Game {
@ -531,9 +532,26 @@ func NewGame() *Game {
etk.SetRoot(connectGrid)
etk.SetFocus(g.connectUsername)
go g.handleAutoRefresh()
return g
}
func (g *Game) handleAutoRefresh() {
g.lastRefresh = time.Now()
t := time.NewTicker(19 * time.Second)
for range t.C {
if viewBoard {
continue
}
if g.Client != nil && g.Client.Username != "" {
g.Client.Out <- []byte("ls")
g.lastRefresh = time.Now()
}
}
}
func (g *Game) handleEvents() {
for e := range g.Client.Events {
switch ev := e.(type) {
@ -560,15 +578,9 @@ func (g *Game) handleEvents() {
case *bgammon.EventSay:
l(fmt.Sprintf("<%s> %s", ev.Player, ev.Message))
case *bgammon.EventList:
if viewBoard || g.lobby.refresh {
g.lobby.setGameList(ev.Games)
if g.lobby.refresh {
scheduleFrame()
g.lobby.refresh = false
}
} else {
g.pendingGames = ev.Games
g.lobby.setGameList(ev.Games)
if !viewBoard {
scheduleFrame()
}
case *bgammon.EventJoined:
g.Board.Lock()
@ -794,11 +806,6 @@ func (g *Game) Update() error {
}
}
if g.pendingGames != nil && viewBoard {
g.lobby.setGameList(g.pendingGames)
g.pendingGames = nil
}
if ebiten.IsKeyPressed(ebiten.KeyControl) && inpututil.IsKeyJustPressed(ebiten.KeyP) {
err := g.toggleProfiling()
if err != nil {
@ -1007,18 +1014,19 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
g.Board.Unlock()
bufferPadding := int(g.Board.horizontalBorderSize / 2)
bufferPaddingX := int(g.Board.horizontalBorderSize / 2)
bufferPaddingY := int(g.Board.verticalBorderSize / 2)
y := g.screenW
availableHeight := g.screenH - y - inputBufferHeight
statusBufferRect = image.Rect(bufferPadding, y+bufferPadding, g.screenW-bufferPadding, y+availableHeight/2-bufferPadding/2)
statusBufferRect = image.Rect(bufferPaddingX, y+bufferPaddingY, g.screenW-bufferPaddingX, y+availableHeight/2-bufferPaddingY/2)
gameBuffer.SetRect(image.Rect(bufferPadding, y+bufferPadding/2+availableHeight/2, g.screenW-bufferPadding, y+availableHeight-bufferPadding))
gameBuffer.SetRect(image.Rect(bufferPaddingX, y+bufferPaddingY/2+availableHeight/2, g.screenW-bufferPaddingX, y+availableHeight-bufferPaddingY))
inputBuffer.SetRect(image.Rect(bufferPadding, g.screenH-inputBufferHeight, g.screenW-bufferPadding, g.screenH-bufferPadding))
inputBuffer.SetRect(image.Rect(bufferPaddingX, g.screenH-inputBufferHeight, g.screenW-bufferPaddingX, g.screenH-bufferPaddingY))
g.lobby.buttonBarHeight = inputBufferHeight + int(float64(bufferPadding)*1.5)
g.lobby.buttonBarHeight = inputBufferHeight + int(float64(bufferPaddingY)*1.5)
g.lobby.fullscreen = true
g.lobby.setRect(0, 0, g.screenW, g.screenH-lobbyStatusBufferHeight)
} else { // Landscape view.
@ -1040,21 +1048,22 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
g.Board.Unlock()
bufferPadding := int(g.Board.horizontalBorderSize / 2)
bufferPaddingX := int(g.Board.horizontalBorderSize / 2)
bufferPaddingY := int(g.Board.verticalBorderSize)
g.lobby.buttonBarHeight = inputBufferHeight + int(float64(bufferPadding)*1.5)
g.lobby.buttonBarHeight = inputBufferHeight + int(float64(bufferPaddingY)*1.5)
g.lobby.fullscreen = true
g.lobby.setRect(0, 0, g.screenW, g.screenH-lobbyStatusBufferHeight)
statusBufferHeight := g.screenH - inputBufferHeight - bufferPadding*3
statusBufferHeight := g.screenH - inputBufferHeight - bufferPaddingY*2 - bufferPaddingX
x, y, w, h := (g.screenW-bufferWidth)+bufferPadding, bufferPadding, bufferWidth-(bufferPadding*2), statusBufferHeight
statusBufferRect = image.Rect(x, y, x+w, y+h/2-bufferPadding/2)
x, y, w, h := (g.screenW-bufferWidth)+bufferPaddingX, bufferPaddingY, bufferWidth-(bufferPaddingX*2), statusBufferHeight
statusBufferRect = image.Rect(x, y, x+w, y+h/2-bufferPaddingY/2)
g.updateStatusBufferPosition()
gameBuffer.SetRect(image.Rect(x, y+h/2+bufferPadding/2, x+w, y+h))
gameBuffer.SetRect(image.Rect(x, y+h/2+bufferPaddingX, x+w, y+h))
inputBuffer.SetRect(image.Rect(x, g.screenH-bufferPadding-inputBufferHeight, x+w, g.screenH-bufferPadding))
inputBuffer.SetRect(image.Rect(x, g.screenH-bufferPaddingX-inputBufferHeight, x+w, g.screenH-bufferPaddingY))
etk.Layout(g.screenW, g.screenH-lobbyStatusBufferHeight-g.lobby.buttonBarHeight)
}

View file

@ -101,9 +101,24 @@ type lobby struct {
}
func NewLobby() *lobby {
return &lobby{
l := &lobby{
refresh: true,
}
go l.handleRefreshTimer()
return l
}
func (l *lobby) handleRefreshTimer() {
t := time.NewTicker(time.Second)
for range t.C {
if !game.loggedIn || viewBoard {
continue
}
l.bufferDirty = true
l.bufferButtonsDirty = true
scheduleFrame()
}
}
func (l *lobby) setGameList(games []bgammon.GameListing) {
@ -145,6 +160,11 @@ func (l *lobby) _drawBufferButtons() {
buttonWidth := l.w / len(buttons)
for i, button := range buttons {
label := button.label
if i == 0 && label == "Refresh" {
label = fmt.Sprintf("Refresh (%02.0f)", 19-(time.Since(game.lastRefresh)%(time.Second*19)).Seconds())
}
// Draw border
if i > 0 {
for ly := 0; ly < l.buttonBarHeight; ly++ {
@ -153,12 +173,12 @@ func (l *lobby) _drawBufferButtons() {
}
}
}
bounds := text.BoundString(mediumFont, button.label)
bounds := text.BoundString(mediumFont, label)
labelColor := lightCheckerColor
img := ebiten.NewImage(bounds.Dx()*2, bounds.Dy()*2)
text.Draw(img, button.label, mediumFont, 0, standardLineHeight, labelColor)
text.Draw(img, label, mediumFont, 0, standardLineHeight, labelColor)
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(buttonWidth*i)+float64((buttonWidth-bounds.Dx())/2), float64(l.buttonBarHeight-standardLineHeight*1.5)/2)
@ -361,7 +381,7 @@ func (l *lobby) click(x, y int) {
switch buttonIndex {
case lobbyButtonRefresh:
l.refresh = true
l.c.Out <- []byte("list")
l.c.Out <- []byte("ls")
case lobbyButtonCreate:
l.showCreateGame = true
etk.SetRoot(createGameGrid)