Skip to content

Commit 00575fa

Browse files
feat(lobby): move lobby plugin to pkg/plugin and streamline the initialization function (#891)
1 parent 1b316ea commit 00575fa

7 files changed

Lines changed: 217 additions & 130 deletions

File tree

File renamed without changes.
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@ func (PlayerComponent) Name() string { return "player" }
2727

2828
// Team represents a team within a lobby.
2929
type Team struct {
30+
// TeamID is the stable identifier used to reference this team in all
31+
// commands and events. Server-assigned at lobby creation from the
32+
// preset registry and immutable afterward.
3033
TeamID string `json:"team_id"`
31-
Name string `json:"name"`
3234
PlayerIDs []string `json:"player_ids"` // References to player IDs (source of truth)
3335
MaxPlayers int `json:"max_players"`
3436
}
3537

38+
// TeamConfig is a creation-time team specification used inside a lobby preset.
39+
// Server operators declare presets in lobby.Config.LobbyPresets; clients choose
40+
// a preset by name on CreateLobbyCommand. MaxPlayers <= 0 means unlimited.
41+
type TeamConfig struct {
42+
TeamID string `json:"team_id"`
43+
MaxPlayers int `json:"max_players"`
44+
}
45+
3646
// Session represents the current session state of a lobby.
3747
type Session struct {
3848
State SessionState `json:"state"`
@@ -97,16 +107,6 @@ func (l *LobbyComponent) GetTeam(teamID string) *Team {
97107
return nil
98108
}
99109

100-
// GetTeamByName returns a team by its name.
101-
func (l *LobbyComponent) GetTeamByName(name string) *Team {
102-
for i := range l.Teams {
103-
if l.Teams[i].Name == name {
104-
return &l.Teams[i]
105-
}
106-
}
107-
return nil
108-
}
109-
110110
// HasPlayer returns true if the player is in any team.
111111
func (l *LobbyComponent) HasPlayer(playerID string) bool {
112112
for _, team := range l.Teams {
@@ -428,6 +428,12 @@ type ConfigComponent struct {
428428
// start itself and returns to Idle. Values <= 0 disable timeout
429429
// enforcement entirely.
430430
MaxAllocationTimeout int64 `json:"max_allocation_timeout,omitempty"`
431+
432+
// LobbyPresets is the server-owned registry of team configurations
433+
// that clients can reference by label in CreateLobbyCommand.Preset.
434+
// Server is the source of truth for team caps; clients cannot supply
435+
// arbitrary TeamConfig values.
436+
LobbyPresets map[string][]TeamConfig `json:"lobby_presets,omitempty"`
431437
}
432438

433439
// Name returns the component name for ECS registration.

pkg/lobby/component/lobby_internal_test.go renamed to pkg/plugin/lobby/component/lobby_internal_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ func TestLobbyComponent_GetTeam(t *testing.T) {
3939

4040
lobby := &LobbyComponent{
4141
Teams: []Team{
42-
{TeamID: "team1", Name: "Team One"},
43-
{TeamID: "team2", Name: "Team Two"},
42+
{TeamID: "team1"},
43+
{TeamID: "team2"},
4444
},
4545
}
4646

4747
team := lobby.GetTeam("team1")
4848
require.NotNil(t, team)
49-
assert.Equal(t, "Team One", team.Name)
49+
assert.Equal(t, "team1", team.TeamID)
5050

5151
assert.Nil(t, lobby.GetTeam("unknown"))
5252
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"github.com/argus-labs/world-engine/pkg/cardinal"
77
"github.com/argus-labs/world-engine/pkg/cardinal/snapshot"
8-
"github.com/argus-labs/world-engine/pkg/lobby"
8+
"github.com/argus-labs/world-engine/pkg/plugin/lobby"
99
"github.com/stretchr/testify/require"
1010
)
1111

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ package lobby
3030

3131
import (
3232
"github.com/argus-labs/world-engine/pkg/cardinal"
33-
"github.com/argus-labs/world-engine/pkg/lobby/component"
34-
"github.com/argus-labs/world-engine/pkg/lobby/system"
33+
"github.com/argus-labs/world-engine/pkg/plugin/lobby/component"
34+
"github.com/argus-labs/world-engine/pkg/plugin/lobby/system"
3535
)
3636

3737
// Re-export types for easier user access.
@@ -63,6 +63,7 @@ type (
6363
UpdatePlayerPassthroughCommand = system.UpdatePlayerPassthroughCommand
6464
GetPlayerCommand = system.GetPlayerCommand
6565
GetAllPlayersCommand = system.GetAllPlayersCommand
66+
GetLobbyCommand = system.GetLobbyCommand
6667

6768
// Events (Broadcast).
6869
CreatedEvent = system.LobbyCreatedEvent
@@ -95,6 +96,7 @@ type (
9596
UpdatePlayerPassthroughResult = system.UpdatePlayerPassthroughResult
9697
GetPlayerResult = system.GetPlayerResult
9798
GetAllPlayersResult = system.GetAllPlayersResult
99+
GetLobbyResult = system.GetLobbyResult
98100

99101
// Cross-Shard Commands.
100102
NotifySessionStartCommand = system.NotifySessionStartCommand
@@ -139,6 +141,14 @@ type Config struct {
139141
// Clients should send heartbeats more frequently than this (e.g., every timeout/3 seconds).
140142
// Default: 30 seconds.
141143
HeartbeatTimeout int64
144+
145+
// LobbyPresets is the server-owned registry of team configurations
146+
// that clients can reference by label in CreateLobbyCommand.Preset.
147+
// The map key is the preset label; the value is the ordered list of
148+
// teams that a lobby created with that preset will contain. Server
149+
// operators are the source of truth for team caps; clients cannot
150+
// supply arbitrary team configurations.
151+
LobbyPresets map[string][]TeamConfig
142152
}
143153

144154
// Plugin implements cardinal.Plugin for the lobby system.
@@ -160,6 +170,7 @@ func (p *Plugin) Register(world *cardinal.World) {
160170
HeartbeatTimeout: p.config.HeartbeatTimeout,
161171
AssignmentAuthority: p.config.AssignmentAuthority,
162172
MaxAllocationTimeout: p.config.MaxAllocationTimeout,
173+
LobbyPresets: p.config.LobbyPresets,
163174
})
164175

165176
// Store provider

0 commit comments

Comments
 (0)