Skip to content

Commit 918f985

Browse files
committed
Implement ClientJoinGame packet handling
1 parent 1670b9c commit 918f985

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

handlers/ClientJoinGame.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package handlers
2+
3+
import (
4+
"example.com/Quaver/Z/multiplayer"
5+
"example.com/Quaver/Z/packets"
6+
"example.com/Quaver/Z/sessions"
7+
)
8+
9+
// Handles when a user attempts to join a game
10+
func handleClientJoinGame(user *sessions.User, packet *packets.ClientJoinGame) {
11+
if packet == nil {
12+
return
13+
}
14+
15+
game := multiplayer.GetGameByIdString(packet.GameId)
16+
17+
if game == nil {
18+
sessions.SendPacketToUser(packets.NewServerJoinGameFailed(packets.JoinGameErrorMatchNoExists), user)
19+
return
20+
}
21+
22+
game.AddPlayer(user.Info.Id, packet.Password)
23+
}

handlers/packets.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func HandleIncomingPackets(conn net.Conn, msg string) {
4848
handleClientCreateGame(user, unmarshalPacket[packets.ClientCreateGame](msg))
4949
case packets.PacketIdClientLeaveGame:
5050
handleClientLeaveGame(user, unmarshalPacket[packets.ClientLeaveGame](msg))
51+
case packets.PacketIdClientJoinGame:
52+
handleClientJoinGame(user, unmarshalPacket[packets.ClientJoinGame](msg))
5153
default:
5254
log.Println(fmt.Errorf("unknown packet: %v", msg))
5355
}

multiplayer/game.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ func (game *Game) AddPlayer(userId int, password string) {
5858
return
5959
}
6060

61+
currentGame := GetGameById(user.GetMultiplayerGameId())
62+
63+
if currentGame != nil && currentGame.Data.Id != game.Data.Id {
64+
currentGame.RemovePlayer(user.Info.Id)
65+
}
66+
6167
if len(game.Data.PlayerIds) >= maxPlayerCount {
6268
sessions.SendPacketToUser(packets.NewServerJoinGameFailed(packets.JoinGameErrorFull), user)
6369
return

multiplayer/lobby.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ func GetGameById(id int) *Game {
8080
return lobby.games[id]
8181
}
8282

83+
// GetGameByIdString Retrieves a game by its stringified id
84+
func GetGameByIdString(id string) *Game {
85+
lobby.mutex.Lock()
86+
defer lobby.mutex.Unlock()
87+
88+
for _, game := range lobby.games {
89+
if game.Data.GameId == id {
90+
return game
91+
}
92+
}
93+
94+
return nil
95+
}
96+
8397
// SendLobbyUsersGameInfoPacket Sends all the users in the lobby a packet with game information
8498
// Be careful of deadlocks when calling this. Make sure not to call the mutex twice.
8599
func sendLobbyUsersGameInfoPacket(game *Game, lock bool) {

packets/ClientJoinGame.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package packets
2+
3+
type ClientJoinGame struct {
4+
Packet
5+
GameId string `json:"gid"`
6+
Password string `json:"p"`
7+
}

0 commit comments

Comments
 (0)