Skip to content

Commit 5b241a1

Browse files
authored
Merge pull request #51 from WilliamQiufeng/mp-disable-preview
Add ability to disable preview
2 parents f3c578c + 99d2b65 commit 5b241a1

9 files changed

+76
-0
lines changed

handlers/ClientGameEnablePreview.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 wants to enable/disable preview for their multiplayer game
10+
func handleClientGameEnablePreview(user *sessions.User, packet *packets.ClientGameEnablePreview) {
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.RunLocked(func() {
22+
game.SetEnablePreview(user, packet.Enabled)
23+
})
24+
}

handlers/packets.go

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ func HandleIncomingPackets(conn net.Conn, msg string) {
116116
handleClientGameAutoHost(user, unmarshalPacket[packets.ClientGameAutoHost](msg))
117117
case packets.PacketIdClientLogout:
118118
handleClientLogout(user, unmarshalPacket[packets.ClientLogout](msg))
119+
case packets.PacketIdClientGameChangeEnablePreview:
120+
handleClientGameEnablePreview(user, unmarshalPacket[packets.ClientGameEnablePreview](msg))
119121
default:
120122
log.Println(fmt.Errorf("unknown packet: %v", msg))
121123
}

multiplayer/chat_bot.go

+12
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func handleMultiplayerCommands(user *sessions.User, channel *chat.Channel, args
7979
message = handleCommandChangeMap(user, game, args)
8080
case "hostrotation":
8181
message = handleCommandHostRotation(user, game)
82+
case "preview":
83+
message = handleCommandEnablePreview(user, game)
8284
case "maxplayers":
8385
message = handleCommandMaxPlayers(user, game, args)
8486
case "start":
@@ -242,6 +244,16 @@ func handleCommandHostRotation(user *sessions.User, game *Game) string {
242244
return ""
243245
}
244246

247+
// Handles the command to enable/disable preview
248+
func handleCommandEnablePreview(user *sessions.User, game *Game) string {
249+
if !game.isUserHost(user) {
250+
return ""
251+
}
252+
253+
game.SetEnablePreview(user, !game.Data.EnablePreview)
254+
return ""
255+
}
256+
245257
// Handles the command to set the max player count
246258
func handleCommandMaxPlayers(user *sessions.User, game *Game, args []string) string {
247259
if !game.isUserHost(user) {

multiplayer/game.go

+14
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,20 @@ func (game *Game) SetHostRotation(requester *sessions.User, enabled bool) {
639639
sendLobbyUsersGameInfoPacket(game, true)
640640
}
641641

642+
// SetEnablePreview Sets whether previewing the map would be allowed for the game
643+
func (game *Game) SetEnablePreview(requester *sessions.User, enabled bool) {
644+
if !game.isUserHost(requester) {
645+
return
646+
}
647+
648+
game.Data.EnablePreview = enabled
649+
game.sendPacketToPlayers(packets.NewServerGameEnablePreview(game.Data.EnablePreview))
650+
game.validateAndCacheSettings()
651+
652+
game.sendBotMessage(fmt.Sprintf("Preview has been %v.", utils.BoolToEnabledString(game.Data.EnablePreview)))
653+
sendLobbyUsersGameInfoPacket(game, true)
654+
}
655+
642656
// SetLongNotePercent Sets the minimum and maximum long note percentage filters for the game
643657
func (game *Game) SetLongNotePercent(requester *sessions.User, min int, max int) {
644658
if !game.isUserHost(requester) {

multiplayer/redis.go

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (game *Game) cacheMatchSettings() {
4141
"host", strconv.Itoa(game.Data.HostId),
4242
"r", strconv.Itoa(int(game.Data.Ruleset)),
4343
"hr", strconv.Itoa(utils.BoolToInt(game.Data.IsHostRotation)),
44+
"ep", strconv.Itoa(utils.BoolToInt(game.Data.EnablePreview)),
4445
"gm", strconv.Itoa(int(game.Data.MapGameMode)),
4546
"d", strconv.FormatFloat(game.Data.MapDifficultyRating, 'f', -1, 64),
4647
"inp", strconv.Itoa(utils.BoolToInt(game.Data.InProgress)),

objects/multiplayer_game.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type MultiplayerGame struct {
2020
MapDifficultyRatingAll []float64 `json:"adr"` // The difficulty rating for all rates of the map. Host provides this for scoring on unsubmitted maps
2121
Ruleset MultiplayerGameRuleset `json:"r"` // The rules of the match (free-for-all, team, etc)
2222
IsHostRotation bool `json:"hr"` // Whether the server will control host rotation for the game
23+
EnablePreview bool `json:"ep"` // Whether previewing the map is allowed for the game
2324
InProgress bool `json:"inp"` // IF the match is currently in progress
2425
HostId int `json:"h"` // The id of the host
2526
RefereeId int `json:"ref"` // The id of the referee of the game
@@ -63,4 +64,5 @@ func (mg *MultiplayerGame) SetDefaults() {
6364
mg.FilterMaxLongNotePercent = 100
6465
mg.FilterMinAudioRate = 0.5
6566
mg.IsTournamentMode = false
67+
mg.EnablePreview = true
6668
}

packets/ClientGameEnablePreview.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package packets
2+
3+
type ClientGameEnablePreview struct {
4+
Packet
5+
Enabled bool `json:"o"`
6+
}

packets/ServerGameEnablePreview.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package packets
2+
3+
type ServerGameEnablePreview struct {
4+
Packet
5+
Enabled bool `json:"e"`
6+
}
7+
8+
func NewServerGameEnablePreview(enabled bool) *ServerGameEnablePreview {
9+
return &ServerGameEnablePreview{
10+
Packet: Packet{Id: PacketIdServerGameEnablePreviewChanged},
11+
Enabled: enabled,
12+
}
13+
}

packets/packet_id.go

+2
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,6 @@ const (
139139
PacketIdClientGameAutoHost
140140
PacketIdServerGameAutoHost
141141
PacketIdClientLogout
142+
PacketIdClientGameChangeEnablePreview
143+
PacketIdServerGameEnablePreviewChanged
142144
)

0 commit comments

Comments
 (0)