Skip to content

Commit

Permalink
Merge pull request #9 from Retro-Rewind-Team/showreason
Browse files Browse the repository at this point in the history
Send kick and ban reasons to players
  • Loading branch information
mattsumi authored Sep 17, 2024
2 parents 7fa30d9 + c776ad5 commit d90d82d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
15 changes: 10 additions & 5 deletions api/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ func handleBanImpl(w http.ResponseWriter, r *http.Request) (*database.User, bool
return nil, false, "Failed to ban user", http.StatusInternalServerError
}

if req.Tos {
gpcm.KickPlayer(req.Pid, "banned")
} else {
gpcm.KickPlayer(req.Pid, "restricted")
}
gpcm.KickPlayerCustomMessage(req.Pid, req.Reason, gpcm.WWFCErrorMessage{
ErrorCode: 22002,
MessageRMC: map[byte]string{
gpcm.LangEnglish: "" +
"You have been banned from Retro WFC\n" +
"Reason: " + req.Reason + "\n" +
"Error Code: %[1]d\n" +
"Support Info: NG%08[2]x",
},
})

var message string
user, success := database.GetProfile(pool, ctx, req.Pid)
Expand Down
16 changes: 15 additions & 1 deletion api/kick.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func HandleKick(w http.ResponseWriter, r *http.Request) {

type KickRequestSpec struct {
Secret string
Reason string
Pid uint32
}

Expand All @@ -66,7 +67,20 @@ func handleKickImpl(w http.ResponseWriter, r *http.Request) (*database.User, boo
return nil, false, "pid missing or 0 in request", http.StatusBadRequest
}

gpcm.KickPlayer(req.Pid, "moderator_kick")
if req.Reason == "" {
return nil, false, "Missing kick reason in request", http.StatusBadRequest
}

gpcm.KickPlayerCustomMessage(req.Pid, "moderator_kick", gpcm.WWFCErrorMessage{
ErrorCode: 22004,
MessageRMC: map[byte]string{
gpcm.LangEnglish: "" +
"You have been kicked from\n" +
"Retro WFC by a moderator.\n" +
"Reason: " + req.Reason + "\n" +
"Error Code: %[1]d",
},
})

var message string
user, success := database.GetProfile(pool, ctx, req.Pid)
Expand Down
6 changes: 4 additions & 2 deletions database/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ func LoginUserToGPCM(pool *pgxpool.Pool, ctx context.Context, userId uint64, gsb
var banExists bool
var banTOS bool
var bannedDeviceId uint32
var banReason string
timeNow := time.Now()
err = pool.QueryRow(ctx, SearchUserBan, user.ProfileId, ipAddress, timeNow).Scan(&banExists, &banTOS, &bannedDeviceId)
err = pool.QueryRow(ctx, SearchUserBan, user.ProfileId, ipAddress, timeNow).Scan(&banExists, &banTOS, &bannedDeviceId, &banReason)
if err != nil {
if err != pgx.ErrNoRows {
return User{}, err
Expand All @@ -117,12 +118,13 @@ func LoginUserToGPCM(pool *pgxpool.Pool, ctx context.Context, userId uint64, gsb
if banExists {
if banTOS {
logging.Warn("DATABASE", "Profile", aurora.Cyan(user.ProfileId), "is banned")
return User{RestrictedDeviceId: bannedDeviceId}, ErrProfileBannedTOS
return User{RestrictedDeviceId: bannedDeviceId, BanReason: banReason}, ErrProfileBannedTOS
}

logging.Warn("DATABASE", "Profile", aurora.Cyan(user.ProfileId), "is restricted")
user.Restricted = true
user.RestrictedDeviceId = bannedDeviceId
user.BanReason = banReason
}

return user, nil
Expand Down
3 changes: 2 additions & 1 deletion database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
GetUserProfileID = `SELECT profile_id, ng_device_id, email, unique_nick, firstname, lastname, open_host FROM users WHERE user_id = $1 AND gsbrcd = $2`
UpdateUserLastIPAddress = `UPDATE users SET last_ip_address = $2, last_ingamesn = $3 WHERE profile_id = $1`
UpdateUserBan = `UPDATE users SET has_ban = true, ban_issued = $2, ban_expires = $3, ban_reason = $4, ban_reason_hidden = $5, ban_moderator = $6, ban_tos = $7 WHERE profile_id = $1`
SearchUserBan = `SELECT has_ban, ban_tos, ng_device_id FROM users WHERE has_ban = true AND (profile_id = $1 OR last_ip_address = $2) AND (ban_expires IS NULL OR ban_expires > $3) ORDER BY ban_tos DESC LIMIT 1`
SearchUserBan = `SELECT has_ban, ban_tos, ng_device_id, ban_reason FROM users WHERE has_ban = true AND (profile_id = $1 OR last_ip_address = $2) AND (ban_expires IS NULL OR ban_expires > $3) ORDER BY ban_tos DESC LIMIT 1`
DisableUserBan = `UPDATE users SET has_ban = false WHERE profile_id = $1`

GetMKWFriendInfoQuery = `SELECT mariokartwii_friend_info FROM users WHERE profile_id = $1`
Expand All @@ -42,6 +42,7 @@ type User struct {
LastName string
Restricted bool
RestrictedDeviceId uint32
BanReason string
OpenHost bool
LastInGameSn string
LastIPAddress string
Expand Down
15 changes: 15 additions & 0 deletions gpcm/kick.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ func KickPlayer(profileID uint32, reason string) {

kickPlayer(profileID, reason)
}

// Exists because the above function is used in too many places to be updated easily
func KickPlayerCustomMessage(profileID uint32, reason string, message WWFCErrorMessage) {
mutex.Lock()
defer mutex.Unlock()

if session, exists := sessions[profileID]; exists {
session.replyError(GPError{
ErrorCode: ErrConnectionClosed.ErrorCode,
ErrorString: "The player was kicked from the server. Reason: " + reason,
Fatal: true,
WWFCMessage: message,
})
}
}
11 changes: 10 additions & 1 deletion gpcm/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,16 @@ func (g *GameSpySession) performLoginWithDatabase(userId uint64, gsbrCode string
ErrorCode: ErrLogin.ErrorCode,
ErrorString: "The profile is banned from the service.",
Fatal: true,
WWFCMessage: WWFCMsgProfileBannedTOS,
WWFCMessage: WWFCErrorMessage{
ErrorCode: 22002,
MessageRMC: map[byte]string{
LangEnglish: "" +
"You are banned from Retro WFC\n" +
"Reason: " + user.BanReason + "\n" +
"Error Code: %[1]d\n" +
"Support Info: NG%08[2]x",
},
},
})
} else {
g.replyError(GPError{
Expand Down

0 comments on commit d90d82d

Please sign in to comment.