Skip to content

Commit 06e0603

Browse files
committed
Add individual slot passwords
1 parent 8b3fc82 commit 06e0603

File tree

7 files changed

+47
-26
lines changed

7 files changed

+47
-26
lines changed

controllers/socket/handler/lobby.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (Lobby) LobbyCreate(so *wsevent.Client, args struct {
222222
ServerPassword: serverPwd,
223223
}
224224

225-
lob := lobby.NewLobby(*args.Map, lobbyType, *args.League, info, *args.WhitelistID, *args.Mumble, steamGroup, *args.Password)
225+
lob := lobby.NewLobby(*args.Map, lobbyType, *args.League, info, *args.WhitelistID, *args.Mumble, steamGroup)
226226

227227
if args.TwitchWhitelistSubscribers || args.TwitchWhitelistFollowers {
228228
if p.TwitchName == "" {
@@ -313,6 +313,19 @@ func (Lobby) LobbyCreate(so *wsevent.Client, args struct {
313313

314314
}
315315
}
316+
317+
if *args.Password != "" {
318+
for i := 0; i < 2*format.NumberOfClassesMap[lob.Type]; i++ {
319+
req := &lobby.Requirement{
320+
LobbyID: lob.ID,
321+
Slot: i,
322+
Password: *args.Password,
323+
}
324+
req.Save()
325+
}
326+
}
327+
328+
lobby.BroadcastLobbyList()
316329
return newResponse(
317330
struct {
318331
ID uint `json:"id"`
@@ -746,9 +759,10 @@ func (Lobby) LobbyChangeOwner(so *wsevent.Client, args struct {
746759
func (Lobby) LobbySetRequirement(so *wsevent.Client, args struct {
747760
ID *uint `json:"id"` // lobby ID
748761

749-
Slot *int `json:"slot"` // -1 if to set for all slots
750-
Type *string `json:"type"`
751-
Value *json.Number `json:"value"`
762+
Slot *int `json:"slot"` // -1 if to set for all slots
763+
Type *string `json:"type"`
764+
Value *json.Number `json:"value"`
765+
Password *string `json:"password" empty:"-"`
752766
}) interface{} {
753767

754768
lob, err := lobby.GetLobbyByID(*args.ID)
@@ -776,14 +790,16 @@ func (Lobby) LobbySetRequirement(so *wsevent.Client, args struct {
776790

777791
switch *args.Type {
778792
case "hours":
779-
n, err = (*args.Value).Int64()
793+
n, err = args.Value.Int64()
780794
req.Hours = int(n)
781795
case "lobbies":
782-
n, err = (*args.Value).Int64()
796+
n, err = args.Value.Int64()
783797
req.Lobbies = int(n)
784798
case "reliability":
785-
f, err = (*args.Value).Float64()
799+
f, err = args.Value.Float64()
786800
req.Reliability = f
801+
case "password":
802+
req.Password = *args.Password
787803
default:
788804
return errors.New("Invalid requirement type.")
789805
}

internal/testhelpers/modelhelpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func CreatePlayerAdmin() *player.Player {
4141
}
4242

4343
func CreateLobby() *lobby.Lobby {
44-
lobby := lobby.NewLobby("cp_badlands", format.Sixes, "etf2l", gameserver.ServerRecord{}, "0", false, "", "")
44+
lobby := lobby.NewLobby("cp_badlands", format.Sixes, "etf2l", gameserver.ServerRecord{}, "0", false, "")
4545
lobby.Save()
4646
lobby.CreateLock()
4747
return lobby

models/lobby/lobby.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ type Lobby struct {
110110

111111
Slots []LobbySlot // List of occupied slots
112112

113-
SlotPassword string // Slot password, if any
114113
PlayerWhitelist string // URL of steam group
115114
TwitchChannel string // twitch channel, slots will be restricted
116115
TwitchRestriction TwitchRestriction // restricted to either followers or subs
@@ -173,7 +172,7 @@ func MapRegionFormatExists(mapName, region string, lobbytype format.Format) bool
173172

174173
// Returns a new lobby object with the given parameters
175174
// Call CreateLock after saving this lobby.
176-
func NewLobby(mapName string, lobbyType format.Format, league string, serverInfo gameserver.ServerRecord, whitelist string, mumble bool, whitelistGroup, password string) *Lobby {
175+
func NewLobby(mapName string, lobbyType format.Format, league string, serverInfo gameserver.ServerRecord, whitelist string, mumble bool, whitelistGroup string) *Lobby {
177176
lobby := &Lobby{
178177
Mode: getGamemode(mapName, lobbyType),
179178
Type: lobbyType,
@@ -184,7 +183,6 @@ func NewLobby(mapName string, lobbyType format.Format, league string, serverInfo
184183
Mumble: mumble,
185184
ServerInfo: serverInfo,
186185
PlayerWhitelist: whitelistGroup,
187-
SlotPassword: password,
188186
}
189187

190188
// Must specify CreatedBy manually if the lobby is created by a player
@@ -374,10 +372,6 @@ func (lobby *Lobby) AddPlayer(p *player.Player, slot int, password string) error
374372
* Player has already joined a lobby
375373
* anything else?
376374
*/
377-
//check if slot password is valid
378-
if lobby.SlotPassword != "" && lobby.SlotPassword != password {
379-
return ErrInvalidPassword
380-
}
381375

382376
//Check if player is banned
383377
if lobby.IsPlayerBanned(p) {
@@ -400,6 +394,11 @@ func (lobby *Lobby) AddPlayer(p *player.Player, slot int, password string) error
400394
if ok, err := lobby.FitsRequirements(p, slot); !ok {
401395
return err
402396
}
397+
398+
req, _ := lobby.GetSlotRequirement(slot)
399+
if password != req.Password {
400+
return ErrInvalidPassword
401+
}
403402
}
404403

405404
var slotChange bool

models/lobby/lobby_decorators.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type SlotDetails struct {
2222
Ready *bool `json:"ready,omitempty"`
2323
InGame *bool `json:"ingame,omitempty"`
2424
Requirements *Requirement `json:"requirements,omitempty"`
25+
Password bool `json:"password"`
2526
}
2627

2728
type ClassDetails struct {
@@ -48,7 +49,6 @@ type LobbyData struct {
4849
TwitchRestriction string `json:"twitchRestriction"`
4950

5051
SteamGroup string `json:"steamGroup"`
51-
Password bool `json:"password"`
5252

5353
Region struct {
5454
Name string `json:"name"`
@@ -130,7 +130,11 @@ func decorateSlotDetails(lobby *Lobby, slot int, playerInfo bool) SlotDetails {
130130
}
131131

132132
if lobby.HasSlotRequirement(slot) {
133-
slotDetails.Requirements, _ = lobby.GetSlotRequirement(slot)
133+
req, _ := lobby.GetSlotRequirement(slot)
134+
if req != nil {
135+
slotDetails.Requirements = req
136+
slotDetails.Password = req.Password != ""
137+
}
134138
}
135139

136140
return slotDetails
@@ -166,7 +170,6 @@ func DecorateLobbyData(lobby *Lobby, playerInfo bool) LobbyData {
166170
TwitchRestriction: lobby.TwitchRestriction.String(),
167171

168172
SteamGroup: lobby.PlayerWhitelist,
169-
Password: lobby.SlotPassword != "",
170173
}
171174

172175
lobbyData.Region.Name = lobby.RegionName
@@ -270,23 +273,25 @@ func DecorateLobbyClosed(lobby *Lobby) LobbyEvent {
270273
}
271274

272275
func DecorateSubstitute(slot *LobbySlot) SubstituteData {
273-
lobby := &Lobby{}
274-
db.DB.First(lobby, slot.LobbyID)
276+
lobby, _ := GetLobbyByID(slot.LobbyID)
277+
275278
substitute := SubstituteData{
276279
LobbyID: lobby.ID,
277280
Format: formatMap[lobby.Type],
278281
MapName: lobby.MapName,
279282
Mumble: lobby.Mumble,
280-
Password: lobby.SlotPassword != "",
281283
TwitchChannel: lobby.TwitchChannel,
282284
SteamGroup: lobby.PlayerWhitelist,
283285
}
284286

287+
req, _ := lobby.GetSlotRequirement(slot.Slot)
288+
if req != nil {
289+
substitute.Password = req.Password != ""
290+
}
291+
285292
substitute.Region.Name = lobby.RegionName
286293
substitute.Region.Code = lobby.RegionCode
287-
288294
substitute.Team, substitute.Class, _ = format.GetSlotTeamClass(lobby.Type, slot.Slot)
289-
substitute.Password = lobby.SlotPassword != ""
290295

291296
return substitute
292297
}

models/lobby/lobby_requirement.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Requirement struct {
1717
Hours int `json:"hours"` // minimum hours needed
1818
Lobbies int `json:"lobbies"` // minimum lobbies played
1919
Reliability float64 `json:"reliability"` // minimum reliability needed
20+
Password string `json:"-"` // Slot password, if any
2021
}
2122

2223
func NewRequirement(lobbyID uint, slot int, hours int, lobbies int) *Requirement {

models/rpc/twitchbot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TwitchBotLeave(channel string) {
1515
}
1616

1717
func TwitchBotAnnouce(channel string, lobbyid uint) {
18-
if !*twitchbotDisabled {
18+
if *twitchbotDisabled {
1919
return
2020
}
2121

views/static.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
params = {};
2727
messageFields.forEach(function(field) {
2828
var val = $("#"+messageName+"_"+field).val();
29-
if ((field.toLowerCase() !== "steamid" && field.endsWith("id")) || field == "room" || field == "role") {
29+
if ((field.toLowerCase() !== "steamid" && field.endsWith("id")) || field == "room" || field == "role" || field == "value" || field == "slot") {
3030
val = parseInt(val);
3131
} else if (field == "mumbleRequired" || field == "ban") {
3232
val = (val == "true")
@@ -116,7 +116,7 @@
116116
})
117117
$(function() {
118118
createBox(so, "lobbyCreate", ["map", "type", "league", "whitelistID", "serverType", "server", "rconpwd", "mumbleRequired", "password", "steamGroupWhitelist", "twitchWhitelist"]);
119-
createBox(so, "lobbySetRequirement", ["id", "slot", "type", "value"]);
119+
createBox(so, "lobbySetRequirement", ["id", "slot", "type", "value", "password"]);
120120
createBox(so, "serverVerify", ["server", "rconpwd", "password"]);
121121
createBox(so, "lobbyClose", ["id"]);
122122
createBox(so, "lobbyJoin", ["id", "team", "class", "password"]);

0 commit comments

Comments
 (0)