Skip to content

Commit 9899b75

Browse files
committed
Pass config object into create methods
1 parent 9ecee5d commit 9899b75

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

templates/new/client/game.go.tmpl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"strings"
1515

1616
"github.com/code-game-project/go-client/cg"
17-
"github.com/Bananenpro/pflag"
17+
"github.com/spf13/pflag"
1818
)
1919

2020
var URL string
@@ -53,7 +53,7 @@ type Player struct {
5353
// --spectate Spectate the created/joined game. The username is not neccessary if this flag is set.
5454
//
5555
// 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-
func Init() (*Game, error) {
56+
func Init(config GameConfig) (*Game, error) {
5757
pflag.Usage = func() {
5858
fmt.Fprintf(os.Stderr, "USAGE: %s [OPTIONS] <operation>\n", os.Args[0])
5959
fmt.Fprintf(os.Stderr, "\nOperations:\n")
@@ -87,9 +87,9 @@ func Init() (*Game, error) {
8787
switch operation {
8888
case "create":
8989
if spectate {
90-
game, err = CreateAndSpectateGame(public)
90+
game, err = CreateAndSpectateGame(public, config)
9191
} else {
92-
game, err = CreateAndJoinGame(public, pflag.Arg(1))
92+
game, err = CreateAndJoinGame(public, pflag.Arg(1), config)
9393
}
9494
case "join":
9595
if (!spectate && pflag.NArg() < 3) || (spectate && pflag.NArg() < 2) {
@@ -117,13 +117,13 @@ func Init() (*Game, error) {
117117
}
118118

119119
// CreateAndJoinGame creates a new game and joins it immediately after.
120-
func CreateAndJoinGame(public bool, username string) (*Game, error) {
120+
func CreateAndJoinGame(public bool, username string, config GameConfig) (*Game, error) {
121121
socket, err := cg.NewSocket(URL)
122122
if err != nil {
123123
return nil, fmt.Errorf("failed to connect to server: %s", err)
124124
}
125125

126-
gameId, err := socket.Create(public)
126+
gameId, err := socket.Create(public, config)
127127
if err != nil {
128128
return nil, fmt.Errorf("failed to create game: %s", err)
129129
}
@@ -140,13 +140,13 @@ func CreateAndJoinGame(public bool, username string) (*Game, error) {
140140
}
141141

142142
// CreateAndSpectateGame creates a new game and spectates it immediately after.
143-
func CreateAndSpectateGame(public bool) (*Game, error) {
143+
func CreateAndSpectateGame(public bool, config GameConfig) (*Game, error) {
144144
socket, err := cg.NewSocket(URL)
145145
if err != nil {
146146
return nil, fmt.Errorf("failed to connect to server: %s", err)
147147
}
148148

149-
gameId, err := socket.Create(public)
149+
gameId, err := socket.Create(public, config)
150150
if err != nil {
151151
return nil, fmt.Errorf("failed to create game: %s", err)
152152
}

templates/new/server/main.go.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"os"
56
"strconv"
67
"time"
@@ -37,7 +38,7 @@ func main() {
3738
KickInactivePlayerDelay: 24 * time.Hour,
3839
})
3940

40-
server.Run(func(cgGame *cg.Game) {
41+
server.Run(func(cgGame *cg.Game, config json.RawMessage) {
4142
{{.PackageName}}.NewGame(cgGame).Run()
4243
})
4344
}

0 commit comments

Comments
 (0)