Return to main menu from lobby when Escape is pressed

Resolves #22.
This commit is contained in:
Trevor Slocum 2024-08-30 23:32:44 -07:00
parent d619742371
commit 8d322cfd28
3 changed files with 97 additions and 20 deletions

View file

@ -1,5 +1,6 @@
1.4.0:
- Auto-scale font size to fit text
- Support returning to the main menu
1.3.9:
- Add shadow behind player rating labels

View file

@ -7,6 +7,7 @@ import (
"fmt"
"log"
"net"
"runtime/debug"
"strings"
"time"
@ -24,6 +25,7 @@ type Client struct {
connecting bool
loggedIn bool
resetPassword bool
close func()
}
func newClient(address string, username string, password string, resetPassword bool) *Client {
@ -55,6 +57,16 @@ func (c *Client) Connect() {
c.connectTCP(nil)
}
func (c *Client) Disconnect() {
if !c.connecting {
return
}
c.connecting = false
if c.close != nil {
c.close()
}
}
func (c *Client) logIn() []byte {
if c.resetPassword {
return []byte(fmt.Sprintf("resetpassword %s\n", c.Username))
@ -75,6 +87,11 @@ func (c *Client) LoggedIn() bool {
}
func (c *Client) connectWebSocket() {
if !c.connecting {
debug.PrintStack()
return
}
connectTime := time.Now()
reconnect := func() {
if c.resetPassword || time.Since(connectTime) < 20*time.Second {
@ -82,6 +99,10 @@ func (c *Client) connectWebSocket() {
return
}
for {
if !c.connecting {
debug.PrintStack()
return
}
if !focused() {
time.Sleep(2 * time.Second)
continue
@ -100,6 +121,9 @@ func (c *Client) connectWebSocket() {
reconnect()
return
}
c.close = func() {
conn.Close(websocket.StatusNormalClosure, "")
}
for _, msg := range bytes.Split(c.logIn(), []byte("\n")) {
if len(msg) == 0 {
@ -174,6 +198,10 @@ func (c *Client) handleWebSocketRead(conn *websocket.Conn) {
}
func (c *Client) connectTCP(conn net.Conn) {
if !c.connecting {
return
}
address := c.Address
if strings.HasPrefix(c.Address, "tcp://") {
address = c.Address[6:]
@ -186,6 +214,9 @@ func (c *Client) connectTCP(conn net.Conn) {
return
}
for {
if !c.connecting {
return
}
if !focused() {
time.Sleep(2 * time.Second)
continue
@ -205,6 +236,9 @@ func (c *Client) connectTCP(conn net.Conn) {
return
}
}
c.close = func() {
conn.Close()
}
// Read a single line of text and parse remaining output as JSON.
buf := make([]byte, 1)

View file

@ -626,6 +626,8 @@ type Game struct {
replaySummary1 []byte
replaySummary2 []byte
localServer chan net.Conn
*sync.Mutex
}
@ -1357,27 +1359,57 @@ func (g *Game) playOffline() {
return
}
// Start the local BEI server.
beiServer := &tabula.BEIServer{}
beiConns := beiServer.ListenLocal()
if g.localServer == nil {
// Start the local BEI server.
beiServer := &tabula.BEIServer{}
beiConns := beiServer.ListenLocal()
// Connect to the local BEI server.
beiClient := bot.NewLocalBEIClient(<-beiConns, false)
// Connect to the local BEI server.
beiClient := bot.NewLocalBEIClient(<-beiConns, false)
// Start the local bgammon server.
server := server.NewServer("", "", "", "", "", false, true, false)
serverConns := server.ListenLocal()
// Start the local bgammon server.
server := server.NewServer("", "", "", "", "", false, true, false)
g.localServer = server.ListenLocal()
// Connect the bots.
go bot.NewLocalClient(<-serverConns, "", "BOT_tabula", "", 1, bgammon.VariantBackgammon, false, beiClient)
go bot.NewLocalClient(<-serverConns, "", "BOT_tabula_acey", "", 1, bgammon.VariantAceyDeucey, false, beiClient)
go bot.NewLocalClient(<-serverConns, "", "BOT_tabula_tabula", "", 1, bgammon.VariantTabula, false, beiClient)
// Connect the bots.
go bot.NewLocalClient(<-g.localServer, "", "BOT_tabula", "", 1, bgammon.VariantBackgammon, false, beiClient)
go bot.NewLocalClient(<-g.localServer, "", "BOT_tabula_acey", "", 1, bgammon.VariantAceyDeucey, false, beiClient)
go bot.NewLocalClient(<-g.localServer, "", "BOT_tabula_tabula", "", 1, bgammon.VariantTabula, false, beiClient)
// Wait for the bots to finish creating matches.
time.Sleep(250 * time.Millisecond)
// Wait for the bots to finish creating matches.
time.Sleep(250 * time.Millisecond)
}
// Connect the player.
go g.ConnectLocal(<-serverConns)
go g.ConnectLocal(<-g.localServer)
}
func (g *Game) showMainMenu() {
if !g.loggedIn {
return
}
g.loggedIn = false
if g.client == nil {
return
}
g.client.Disconnect()
g.client = nil
g.client = nil
g.lobby.c = nil
g.board.client = nil
g.setRoot(connectFrame)
statusBuffer.SetText("")
gameBuffer.SetText("")
inputBuffer.SetText("")
statusLogged = false
gameLogged = false
g.loggedIn = false
}
func (g *Game) handleUpdateTimeLabels() {
@ -1798,8 +1830,8 @@ func (g *Game) handleEvent(e interface{}) {
}
}
func (g *Game) handleEvents() {
for e := range g.client.Events {
func (g *Game) handleEvents(c *Client) {
for e := range c.Events {
g.board.Lock()
g.Lock()
g.board.Unlock()
@ -2262,7 +2294,7 @@ func (g *Game) Connect() {
g.lobby.c = g.client
g.board.client = g.client
go g.handleEvents()
go g.handleEvents(g.client)
if g.Password != "" {
g.board.recreateAccountGrid()
@ -2311,10 +2343,12 @@ func (g *Game) ConnectLocal(conn net.Conn) {
g.lobby.c = g.client
g.board.client = g.client
g.client.connecting = true
g.Username = ""
g.Password = ""
go g.handleEvents()
go g.handleEvents(g.client)
go g.client.connectTCP(conn)
}
@ -2543,8 +2577,16 @@ func (g *Game) handleInput(keys []ebiten.Key) error {
g.lobby.rebuildButtonsGrid()
g.setRoot(listGamesFrame)
}
} else if g.lobby.showCreateGame {
g.lobby.showCreateGame = false
g.lobby.rebuildButtonsGrid()
g.setRoot(listGamesFrame)
} else if g.lobby.showJoinGame {
g.lobby.showJoinGame = false
g.lobby.rebuildButtonsGrid()
g.setRoot(listGamesFrame)
} else {
setViewBoard(!viewBoard)
g.showMainMenu()
}
}
}