@@ -45,27 +45,30 @@ type Player struct {
45
45
// Init parses command line arguments with pflag and calls either CreateAndJoinGame, CreateAndSpectateGame, JoinGame, SpectateGame or ReconnectGame depending on the provided commands:
46
46
//
47
47
// Operations:\n")
48
- // create <username> Create a new game.
49
- // join <game_id> <username> Join an existing game.
50
- // reconnect <username> Reconnect to a previous session.
48
+ // create <username> Create a new game.
49
+ // join <game_id> <username> <join_secret?> Join an existing game.
50
+ // reconnect <username> Reconnect to a previous session.
51
51
// Flags:
52
52
// --public Make the created game public.
53
+ // --protected Make the created game protected.
53
54
// --spectate Spectate the created/joined game. The username is not neccessary if this flag is set.
54
55
//
55
56
// This function calls pflag.Parse() internally. If you want to add your own flags, you will need to register them with pflag *before* calling this function.
56
57
func Init(config GameConfig) (*Game, error) {
57
58
pflag.Usage = func() {
58
59
fmt.Fprintf(os.Stderr, "USAGE: %s [OPTIONS] <operation>\n", os.Args[0])
59
60
fmt.Fprintf(os.Stderr, "\nOperations:\n")
60
- fmt.Fprintf(os.Stderr, " create <username> Create a new game.\n")
61
- fmt.Fprintf(os.Stderr, " join <game_id> <username> Join an existing game.\n")
62
- fmt.Fprintf(os.Stderr, " reconnect <username> Reconnect to a previous session.\n")
61
+ fmt.Fprintf(os.Stderr, " create <username> Create a new game.\n")
62
+ fmt.Fprintf(os.Stderr, " join <game_id> <username> <join_secret?> Join an existing game.\n")
63
+ fmt.Fprintf(os.Stderr, " reconnect <username> Reconnect to a previous session.\n")
63
64
fmt.Fprintf(os.Stderr, "\nFlags:\n")
64
65
pflag.PrintDefaults()
65
66
}
66
67
67
68
var public bool
68
69
pflag.BoolVarP(&public, "public", "p", false, "Make the created game public.")
70
+ var protected bool
71
+ pflag.BoolVarP(&protected, "protected", "p", false, "Make the created game protected.")
69
72
var spectate bool
70
73
pflag.BoolVarP(&spectate, "spectate", "s", false, "Spectate the created/joined game. The username is not neccessary if this flag is set.")
71
74
pflag.Parse()
@@ -86,10 +89,15 @@ func Init(config GameConfig) (*Game, error) {
86
89
var err error
87
90
switch operation {
88
91
case "create":
92
+ var joinSecret string
89
93
if spectate {
90
- game, err = CreateAndSpectateGame(public, config)
94
+ game, joinSecret, err = CreateAndSpectateGame(public, protected , config)
91
95
} else {
92
- game, err = CreateAndJoinGame(public, pflag.Arg(1), config)
96
+ game, joinSecret, err = CreateAndJoinGame(public, protected, pflag.Arg(1), config)
97
+ }
98
+ fmt.Println("Game ID:", game.Id)
99
+ if joinSecret != "" {
100
+ fmt.Println("Join secret:", joinSecret)
93
101
}
94
102
case "join":
95
103
if (!spectate && pflag.NArg() < 3) || (spectate && pflag.NArg() < 2) {
@@ -100,7 +108,11 @@ func Init(config GameConfig) (*Game, error) {
100
108
if spectate {
101
109
game, err = SpectateGame(pflag.Arg(1))
102
110
} else {
103
- game, err = JoinGame(pflag.Arg(1), pflag.Arg(2))
111
+ var joinSecret string
112
+ if pflag.NArg() > 3 {
113
+ joinSecret = pflag.Arg(3)
114
+ }
115
+ game, err = JoinGame(pflag.Arg(1), pflag.Arg(2), joinSecret)
104
116
}
105
117
case "reconnect":
106
118
game, err = ReconnectGame(pflag.Arg(1))
@@ -117,59 +129,60 @@ func Init(config GameConfig) (*Game, error) {
117
129
}
118
130
119
131
// CreateAndJoinGame creates a new game and joins it immediately after.
120
- func CreateAndJoinGame(public bool, username string, config GameConfig) (*Game, error) {
132
+ func CreateAndJoinGame(public, protected bool, username string, config GameConfig) (*Game, string , error) {
121
133
socket, err := cg.NewSocket(URL)
122
134
if err != nil {
123
- return nil, fmt.Errorf("failed to connect to server: %s", err)
135
+ return nil, "", fmt.Errorf("failed to connect to server: %s", err)
124
136
}
125
137
126
- gameId, err := socket.Create (public, config)
138
+ gameId, joinSecret, err := socket.CreateGame (public, protected , config)
127
139
if err != nil {
128
- return nil, fmt.Errorf("failed to create game: %s", err)
140
+ return nil, "", fmt.Errorf("failed to create game: %s", err)
129
141
}
130
142
131
- err = socket.Join(gameId, username)
143
+ err = socket.Join(gameId, username, joinSecret )
132
144
if err != nil {
133
- return nil, fmt.Errorf("failed to join game: %s", err)
145
+ return nil, "", fmt.Errorf("failed to join game: %s", err)
134
146
}
135
147
136
148
return &Game{
137
149
Id: gameId,
138
150
socket: socket,
139
- }, nil
151
+ }, joinSecret, nil
140
152
}
141
153
142
154
// CreateAndSpectateGame creates a new game and spectates it immediately after.
143
- func CreateAndSpectateGame(public bool, config GameConfig) (*Game, error) {
155
+ func CreateAndSpectateGame(public, protected bool, config GameConfig) (*Game, string , error) {
144
156
socket, err := cg.NewSocket(URL)
145
157
if err != nil {
146
- return nil, fmt.Errorf("failed to connect to server: %s", err)
158
+ return nil, "", fmt.Errorf("failed to connect to server: %s", err)
147
159
}
148
160
149
- gameId, err := socket.Create (public, config)
161
+ gameId, joinSecret, err := socket.CreateGame (public, protected , config)
150
162
if err != nil {
151
- return nil, fmt.Errorf("failed to create game: %s", err)
163
+ return nil, "", fmt.Errorf("failed to create game: %s", err)
152
164
}
153
165
154
166
err = socket.Spectate(gameId)
155
167
if err != nil {
156
- return nil, fmt.Errorf("failed to spectate game: %s", err)
168
+ return nil, "", fmt.Errorf("failed to spectate game: %s", err)
157
169
}
158
170
159
171
return &Game{
160
172
Id: gameId,
161
173
socket: socket,
162
- }, nil
174
+ }, joinSecret, nil
163
175
}
164
176
165
177
// JoinGame joins the game with the specified id.
166
- func JoinGame(gameId, username string) (*Game, error) {
178
+ // Leave joinSecret empty if the game is not protected.
179
+ func JoinGame(gameId, username, joinSecret string) (*Game, error) {
167
180
socket, err := cg.NewSocket(URL)
168
181
if err != nil {
169
182
return nil, fmt.Errorf("failed to connect to server: %s", err)
170
183
}
171
184
172
- err = socket.Join(gameId, username)
185
+ err = socket.Join(gameId, username, joinSecret )
173
186
if err != nil {
174
187
return nil, fmt.Errorf("failed to join game: %s", err)
175
188
}
@@ -230,9 +243,9 @@ func (g *Game) Update() error {
230
243
return err
231
244
}
232
245
233
- // ResolveUsername returns the username associated with playerId.
234
- func (g *Game) ResolveUsername (playerId string) string {
235
- return g.socket.ResolveUsername (playerId)
246
+ // Username returns the username associated with playerId.
247
+ func (g *Game) Username (playerId string) string {
248
+ return g.socket.Username (playerId)
236
249
}
237
250
238
251
// Session returns details of the current session.
@@ -244,8 +257,3 @@ func (g *Game) Session() cg.Session {
244
257
func (g *Game) Disconnect() error {
245
258
return g.socket.Close()
246
259
}
247
-
248
- // Leave leaves the game and removes the session from disk.
249
- func (g *Game) Leave() error {
250
- return g.socket.Leave()
251
- }
0 commit comments