Skip to content

Commit a91107c

Browse files
committed
Implement ClientGameTransferHost packet & refactor SetHost()
1 parent 3629519 commit a91107c

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

handlers/ClientGameTransferHost.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 the client wishes to transfer host to another player
10+
func handleClientGameTransferHost(user *sessions.User, packet *packets.ClientGameTransferHost) {
11+
if packet == nil {
12+
return
13+
}
14+
15+
game := multiplayer.GetGameById(user.GetMultiplayerGameId())
16+
17+
if game == nil {
18+
return
19+
}
20+
21+
game.SetHost(user, packet.UserId, true)
22+
}

handlers/packets.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func HandleIncomingPackets(conn net.Conn, msg string) {
8686
handleClientRequestUserStats(user, unmarshalPacket[packets.ClientRequestUserStats](msg))
8787
case packets.PacketIdClientGameKickPlayer:
8888
handleClientGameKickPlayer(user, unmarshalPacket[packets.ClientGameKickPlayer](msg))
89+
case packets.PacketIdClientGameTransferHost:
90+
handleClientGameTransferHost(user, unmarshalPacket[packets.ClientGameTransferHost](msg))
8991
default:
9092
log.Println(fmt.Errorf("unknown packet: %v", msg))
9193
}

multiplayer/game.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (game *Game) AddPlayer(userId int, password string) {
9292
sessions.SendPacketToUser(packets.NewServerJoinGame(game.Data.GameId), user)
9393

9494
if len(game.Data.PlayerIds) == 1 {
95-
game.SetHost(user.Info.Id, false)
95+
game.SetHost(nil, user.Info.Id, false)
9696
}
9797

9898
RemoveUserFromLobby(user)
@@ -120,7 +120,7 @@ func (game *Game) RemovePlayer(userId int) {
120120
return
121121
}
122122

123-
game.SetHost(game.Data.PlayerIds[0], false)
123+
game.SetHost(nil, game.Data.PlayerIds[0], false)
124124
game.sendPacketToPlayers(packets.NewServerUserLeftGame(userId))
125125
sendLobbyUsersGameInfoPacket(game, true)
126126
}
@@ -146,20 +146,19 @@ func (game *Game) KickPlayer(requester *sessions.User, userId int) {
146146
sessions.SendPacketToUser(packets.NewServerGameKicked(), user)
147147
}
148148

149-
// SetHost Sets the host of the game
150-
func (game *Game) SetHost(userId int, lock bool) {
149+
// SetHost Sets the host of the game. Set requester to nil if this is meant to be a forced action.
150+
// Otherwise, set the requester if a user is transferring host to another.
151+
func (game *Game) SetHost(requester *sessions.User, userId int, lock bool) {
151152
if lock {
152153
game.mutex.Lock()
153154
defer game.mutex.Unlock()
154155
}
155156

156-
user := sessions.GetUserById(userId)
157-
158-
if user == nil {
157+
if !game.isUserHost(requester) {
159158
return
160159
}
161160

162-
if user.GetMultiplayerGameId() != game.Data.Id {
161+
if !utils.Includes(game.Data.PlayerIds, userId) {
163162
return
164163
}
165164

@@ -590,9 +589,9 @@ func (game *Game) rotateHost() {
590589

591590
// Cyclically rotates the host
592591
if index+1 < len(game.Data.PlayerIds) {
593-
game.SetHost(game.Data.PlayerIds[index+1], false)
592+
game.SetHost(nil, game.Data.PlayerIds[index+1], false)
594593
} else {
595-
game.SetHost(game.Data.PlayerIds[0], false)
594+
game.SetHost(nil, game.Data.PlayerIds[0], false)
596595
}
597596
}
598597

packets/ClientGameTransferHost.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package packets
2+
3+
type ClientGameTransferHost struct {
4+
Packet
5+
UserId int `json:"u"`
6+
}

0 commit comments

Comments
 (0)