Allow joining a game by specifying a player name

This commit is contained in:
Trevor Slocum 2023-09-09 20:08:43 -07:00
parent dcf67d62c3
commit 433ba94719
2 changed files with 40 additions and 7 deletions

View file

@ -15,6 +15,7 @@ All commands and events are separated by newlines.
- `login [username] [password]`
- Log in to bgammon. A random username is assigned when none is provided.
- This must be the first command sent when a client connects to bgammon.
- Usernames must contain at least one non-numeric character.
- *Aliases:* `l`
- `loginjson [username] [password]`
@ -38,8 +39,8 @@ formatted responses are more easily parsed by computers.
- List all games.
- *Aliases:* `c`
- `join <id> [password]`
- Join game.
- `join <id>/<username> [password]`
- Join game by game ID or by player.
- *Aliases:* `j`
- `roll`

View file

@ -452,10 +452,38 @@ COMMANDS:
sendUsage()
continue
}
gameID, err := strconv.Atoi(string(params[0]))
if err != nil || gameID < 1 {
sendUsage()
continue
var joinGameID int
if onlyNumbers.Match(params[0]) {
gameID, err := strconv.Atoi(string(params[0]))
if err == nil && gameID > 0 {
joinGameID = gameID
}
if joinGameID == 0 {
sendUsage()
continue
}
} else {
paramLower := bytes.ToLower(params[0])
s.clientsLock.Lock()
for _, sc := range s.clients {
if bytes.Equal(paramLower, bytes.ToLower(sc.name)) {
g := s.gameByClient(sc)
if g != nil {
joinGameID = g.id
}
break
}
}
s.clientsLock.Unlock()
if joinGameID == 0 {
cmd.client.sendEvent(&bgammon.EventFailedJoin{
Reason: "Game not found.",
})
continue
}
}
s.gamesLock.Lock()
@ -463,7 +491,7 @@ COMMANDS:
if g.terminated() {
continue
}
if g.id == gameID {
if g.id == joinGameID {
if len(g.password) != 0 && (len(params) < 2 || !bytes.Equal(g.password, bytes.Join(params[2:], []byte(" ")))) {
cmd.client.sendEvent(&bgammon.EventFailedJoin{
Reason: "Invalid password.",
@ -482,6 +510,10 @@ COMMANDS:
}
}
s.gamesLock.Unlock()
cmd.client.sendEvent(&bgammon.EventFailedJoin{
Reason: "Game not found.",
})
case bgammon.CommandLeave, "l":
if clientGame == nil {
cmd.client.sendEvent(&bgammon.EventFailedLeave{