@@ -45,27 +45,30 @@ type Player struct {
4545// Init parses command line arguments with pflag and calls either CreateAndJoinGame, CreateAndSpectateGame, JoinGame, SpectateGame or ReconnectGame depending on the provided commands:
4646//
4747// 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.
5151// Flags:
5252// --public Make the created game public.
53+ // --protected Make the created game protected.
5354// --spectate Spectate the created/joined game. The username is not neccessary if this flag is set.
5455//
5556// 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.
5657func Init(config GameConfig) (*Game, error) {
5758 pflag.Usage = func() {
5859 fmt.Fprintf(os.Stderr, "USAGE: %s [OPTIONS] <operation>\n", os.Args[0])
5960 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")
6364 fmt.Fprintf(os.Stderr, "\nFlags:\n")
6465 pflag.PrintDefaults()
6566 }
6667
6768 var public bool
6869 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.")
6972 var spectate bool
7073 pflag.BoolVarP(&spectate, "spectate", "s", false, "Spectate the created/joined game. The username is not neccessary if this flag is set.")
7174 pflag.Parse()
@@ -86,10 +89,15 @@ func Init(config GameConfig) (*Game, error) {
8689 var err error
8790 switch operation {
8891 case "create":
92+ var joinSecret string
8993 if spectate {
90- game, err = CreateAndSpectateGame(public, config)
94+ game, joinSecret, err = CreateAndSpectateGame(public, protected , config)
9195 } 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)
93101 }
94102 case "join":
95103 if (!spectate && pflag.NArg() < 3) || (spectate && pflag.NArg() < 2) {
@@ -100,7 +108,11 @@ func Init(config GameConfig) (*Game, error) {
100108 if spectate {
101109 game, err = SpectateGame(pflag.Arg(1))
102110 } 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)
104116 }
105117 case "reconnect":
106118 game, err = ReconnectGame(pflag.Arg(1))
@@ -117,59 +129,60 @@ func Init(config GameConfig) (*Game, error) {
117129}
118130
119131// 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) {
121133 socket, err := cg.NewSocket(URL)
122134 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)
124136 }
125137
126- gameId, err := socket.Create (public, config)
138+ gameId, joinSecret, err := socket.CreateGame (public, protected , config)
127139 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)
129141 }
130142
131- err = socket.Join(gameId, username)
143+ err = socket.Join(gameId, username, joinSecret )
132144 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)
134146 }
135147
136148 return &Game{
137149 Id: gameId,
138150 socket: socket,
139- }, nil
151+ }, joinSecret, nil
140152}
141153
142154// 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) {
144156 socket, err := cg.NewSocket(URL)
145157 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)
147159 }
148160
149- gameId, err := socket.Create (public, config)
161+ gameId, joinSecret, err := socket.CreateGame (public, protected , config)
150162 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)
152164 }
153165
154166 err = socket.Spectate(gameId)
155167 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)
157169 }
158170
159171 return &Game{
160172 Id: gameId,
161173 socket: socket,
162- }, nil
174+ }, joinSecret, nil
163175}
164176
165177// 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) {
167180 socket, err := cg.NewSocket(URL)
168181 if err != nil {
169182 return nil, fmt.Errorf("failed to connect to server: %s", err)
170183 }
171184
172- err = socket.Join(gameId, username)
185+ err = socket.Join(gameId, username, joinSecret )
173186 if err != nil {
174187 return nil, fmt.Errorf("failed to join game: %s", err)
175188 }
@@ -230,9 +243,9 @@ func (g *Game) Update() error {
230243 return err
231244}
232245
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)
236249}
237250
238251// Session returns details of the current session.
@@ -244,8 +257,3 @@ func (g *Game) Session() cg.Session {
244257func (g *Game) Disconnect() error {
245258 return g.socket.Close()
246259}
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