Skip to content

Commit 7ea395e

Browse files
Improve QR2 kick order by sending it to all QR2-authenticated sessions
1 parent 2e83f14 commit 7ea395e

2 files changed

Lines changed: 31 additions & 77 deletions

File tree

gpcm/kick.go

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,11 @@
11
package gpcm
22

33
import (
4-
"strconv"
54
"time"
65
"wwfc/common"
76
"wwfc/qr2"
87
)
98

10-
// Expects a global mutex to be locked
11-
// This function will return all sessions that are matched with the player `profileID`
12-
func getSessionsMatchedWithPlayer(gameName string, profileID uint32) []*GameSpySession {
13-
14-
var matchedPlayers []*GameSpySession
15-
16-
// Check if the user is part of a group
17-
var foundGroup *qr2.GroupInfo
18-
groups := qr2.GetGroups([]string{gameName}, []string{}, false)
19-
for _, group := range groups {
20-
for _, playerInfo := range group.Players {
21-
pid, err := strconv.ParseUint(playerInfo.ProfileID, 10, 32)
22-
if err == nil {
23-
if uint32(pid) == profileID {
24-
foundGroup = &group
25-
break
26-
}
27-
}
28-
}
29-
}
30-
31-
// If the user is part of a group, send a kick order to all players in the group
32-
if foundGroup != nil {
33-
for _, playerInfo := range foundGroup.Players {
34-
pid, err := strconv.ParseUint(playerInfo.ProfileID, 10, 32)
35-
if err == nil {
36-
if session, exists := sessions[uint32(pid)]; exists {
37-
matchedPlayers = append(matchedPlayers, session)
38-
}
39-
}
40-
}
41-
}
42-
43-
return matchedPlayers
44-
}
45-
469
func kickPlayer(profileID uint32, reason string) {
4710
if session, exists := sessions[profileID]; exists {
4811
errorMessage := WWFCMsgKickedGeneric
@@ -72,24 +35,21 @@ func kickPlayer(profileID uint32, reason string) {
7235
return
7336
}
7437

75-
players := getSessionsMatchedWithPlayer(session.GameName, profileID)
7638
session.replyError(GPError{
7739
ErrorCode: ErrConnectionClosed.ErrorCode,
7840
ErrorString: "The player was kicked from the server. Reason: " + reason,
7941
Fatal: true,
8042
WWFCMessage: errorMessage,
8143
})
82-
83-
// After 3 seconds, send kick order to all players in the group
84-
// This is to prevent the restricted player from staying in the group if he ignores the GPCM kick
85-
go func(players []*GameSpySession) {
86-
time.AfterFunc(3*time.Second, func() {
87-
for _, player := range players {
88-
qr2.OrderKickFromGroup(player.User.ProfileId, profileID)
89-
}
90-
})
91-
}(players)
9244
}
45+
46+
// After 3 seconds, send kick order to all players
47+
// This is to prevent the restricted player from staying in the group if he ignores the GPCM kick
48+
go func() {
49+
time.AfterFunc(3*time.Second, func() {
50+
qr2.OrderKickFromGroups(profileID)
51+
})
52+
}()
9353
}
9454

9555
func KickPlayer(profileID uint32, reason string) {
@@ -104,23 +64,20 @@ func KickPlayerCustomMessage(profileID uint32, reason string, message WWFCErrorM
10464
defer mutex.Unlock()
10565

10666
if session, exists := sessions[profileID]; exists {
107-
players := getSessionsMatchedWithPlayer(session.GameName, profileID)
10867
session.replyError(GPError{
10968
ErrorCode: ErrConnectionClosed.ErrorCode,
11069
ErrorString: "The player was kicked from the server. Reason: " + reason,
11170
Fatal: true,
11271
WWFCMessage: message,
11372
Reason: reason,
11473
})
115-
116-
// After 3 seconds, send kick order to all players in the group
117-
// This is to prevent the restricted player from staying in the group if he ignores the GPCM kick
118-
go func(players []*GameSpySession) {
119-
time.AfterFunc(3*time.Second, func() {
120-
for _, player := range players {
121-
qr2.OrderKickFromGroup(player.User.ProfileId, profileID)
122-
}
123-
})
124-
}(players)
12574
}
75+
76+
// After 3 seconds, send kick order to all players
77+
// This is to prevent the restricted player from staying in the group if he ignores the GPCM kick
78+
go func() {
79+
time.AfterFunc(3*time.Second, func() {
80+
qr2.OrderKickFromGroups(profileID)
81+
})
82+
}()
12683
}

qr2/group.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -584,28 +584,25 @@ func loadGroups() error {
584584
return nil
585585
}
586586

587-
func OrderKickFromGroup(profileID uint32, profileToKick uint32) {
588-
moduleName := "QR2:OrderKickFromGroup/" + strconv.FormatUint(uint64(profileID), 10)
589-
590-
login, exists := logins[profileID]
591-
if !exists || login == nil {
592-
return
593-
}
594-
595-
session := login.session
596-
if session == nil {
597-
return
598-
}
587+
// Send kick order to all QR2-authenticated session to kick a player from their group
588+
func OrderKickFromGroups(profileToKick uint32) {
589+
moduleName := "QR2:OrderKickFromGroup/" + strconv.FormatUint(uint64(profileToKick), 10)
599590

600591
mutex.Lock()
601592
defer mutex.Unlock()
602593

603-
message := createResponseHeader(ClientKickPeerOrder, session.SessionID)
604-
message = append(message, 0) // padding for 4 byte alignment
605-
message = binary.BigEndian.AppendUint32(message, profileToKick)
606-
_, err := masterConn.WriteTo(message, &session.Addr)
607-
if err != nil {
608-
logging.Error(moduleName, "Error sending message:", err.Error())
594+
var numSent int = 0
595+
for _, session := range sessions {
596+
message := createResponseHeader(ClientKickPeerOrder, session.SessionID)
597+
message = append(message, 0) // padding for 4 byte alignment
598+
message = binary.BigEndian.AppendUint32(message, profileToKick)
599+
_, err := masterConn.WriteTo(message, &session.Addr)
600+
if err != nil {
601+
logging.Error(moduleName, "Error sending message:", err.Error())
602+
}
603+
604+
numSent++
609605
}
610606

607+
logging.Notice(moduleName, "Sent kick order message to", aurora.Cyan(numSent), "player(s)")
611608
}

0 commit comments

Comments
 (0)