Add client label method

This commit is contained in:
Trevor Slocum 2023-09-09 00:01:01 -07:00
parent daa8bce760
commit edeb5806d9
6 changed files with 35 additions and 22 deletions

View file

@ -10,7 +10,7 @@ All commands and events are separated by newlines.
`command <required argument> [optional argument]`
### Commands
### Client commands
- `login [username] [password]`
- Log in to bgammon. A random username is assigned when none is provided.
@ -76,7 +76,7 @@ must write some data to the server at least once every ten minutes.
- `disconnect`
- Disconnect from the server.
## Events (server responses)
## Server events
All events are sent in either JSON or human-readable format. The structure of
messages sent in JSON format is available via [godoc](https://docs.rocket9labs.com/code.rocket9labs.com/tslocum/bgammon/#Event).
@ -114,7 +114,7 @@ This document lists events in human-readable format.
- `joined <id:integer> <playerNumber:integer> <playerName:text>`
- Sent after successfully creating or joining a game, and when another player
joins a game you are in.
- The server will always send a `board` response immediately after `joined` to
- The server will always send a `board` event immediately after `joined` to
provide clients with the initial game state.
- `failedjoin <message:line>`

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"strconv"
"code.rocket9labs.com/tslocum/bgammon"
)
@ -125,3 +126,10 @@ func (c *serverClient) sendNotice(message string) {
Message: message,
})
}
func (c *serverClient) label() string {
if len(c.name) > 0 {
return string(c.name)
}
return strconv.Itoa(c.id)
}

View file

@ -3,7 +3,6 @@ package main
import (
"bufio"
"bytes"
"log"
"math/rand"
"time"
@ -71,8 +70,6 @@ func (g *serverGame) sendBoard(client *serverClient) {
// Reverse spaces for white.
if client.playerNumber == 2 {
log.Println(g.Game.Board)
ev.GameState.Game = ev.GameState.Copy()
// Flip board.

View file

@ -1,13 +1,27 @@
package main
import "flag"
import (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
)
func main() {
var address string
flag.StringVar(&address, "tcp", "localhost:1337", "TCP listen address")
var tcpAddress string
var debug int
flag.StringVar(&tcpAddress, "tcp", "localhost:1337", "TCP listen address")
flag.IntVar(&debug, "debug", 0, "print debug information and serve pprof on specified port")
flag.Parse()
if debug > 0 {
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", debug), nil))
}()
}
s := newServer()
s.listen("tcp", address)
s.listen("tcp", tcpAddress)
select {}
}

View file

@ -116,8 +116,6 @@ func (s *server) handleTerminatedGames() {
}
func (s *server) handleConnection(conn net.Conn) {
log.Printf("new conn %+v", conn)
const bufferSize = 8
commands := make(chan []byte, bufferSize)
events := make(chan []byte, bufferSize)
@ -135,6 +133,8 @@ func (s *server) handleConnection(conn net.Conn) {
s.sendHello(c)
s.addClient(c)
log.Printf("Client %s connected", c.label())
go s.handlePingClient(c)
go s.handleClientCommands(c)
@ -142,6 +142,8 @@ func (s *server) handleConnection(conn net.Conn) {
// Remove client.
s.removeClient(c)
log.Printf("Client %s disconnected", c.label())
}
func (s *server) handlePingClient(c *serverClient) {
@ -243,11 +245,7 @@ COMMANDS:
continue
}
keyword = strings.ToLower(keyword)
log.Printf("server client %+v command %s with keyword %s", cmd.client, cmd.command, keyword)
params := bytes.Fields(cmd.command[startParameters:])
log.Printf("params %+v", params)
// Require users to send login command first.
if cmd.client.account == -1 {
@ -281,7 +279,7 @@ COMMANDS:
Games: len(s.games),
})
log.Printf("login as %s - %s", username, password)
log.Printf("Client %d logged in as %s", cmd.client.id, cmd.client.name)
continue
} else {
cmd.client.Terminate("You must login before using other commands.")
@ -543,10 +541,7 @@ COMMANDS:
continue COMMANDS
}
originalFrom, originalTo := from, to
from, to = bgammon.FlipSpace(from, cmd.client.playerNumber), bgammon.FlipSpace(to, cmd.client.playerNumber)
log.Printf("translated player %d %d-%d as %d-%d", cmd.client.playerNumber, originalFrom, originalTo, from, to)
moves = append(moves, []int{from, to})
}
@ -663,7 +658,7 @@ COMMANDS:
clientGame.sendBoard(client)
})
default:
log.Printf("unknown command %s", keyword)
log.Printf("Received unknown command from client %s: %s", cmd.client.label(), cmd.command)
}
}
}

View file

@ -210,7 +210,6 @@ ADDMOVES:
if checkWin {
var foundChecker bool
for space := 1; space <= 24; space++ {
log.Println(space)
if PlayerCheckers(g.Board[space], g.Turn) != 0 {
foundChecker = true
break