Skip to content

Commit 33876c1

Browse files
committed
Add game subcommands
1 parent 8f5618d commit 33876c1

File tree

6 files changed

+238
-18
lines changed

6 files changed

+238
-18
lines changed

README.md

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,138 @@ The official [CodeGame](https://code-game.org) CLI.
88
### Docs and info
99

1010
View the CodeGame documentation:
11-
```sh
11+
```
1212
codegame docs
1313
```
1414

1515
View the documentation of a game:
16-
```sh
16+
```
1717
codegame docs <url>
1818
```
1919

2020
Get information about a game server:
21-
```sh
21+
```
2222
codegame info <url>
2323
```
2424

2525
### Project templates
2626

2727
Create a new project:
28-
```sh
28+
```
2929
codegame new
3030
```
3131

3232
Update event definitions, wrappers and libraries to match the latest game version:
33-
```sh
33+
```
3434
codegame update
3535
```
3636

3737
Permanently switch to a different game URL:
38-
```sh
38+
```
3939
codegame change-url <new_url>
4040
```
4141

4242
### Running and building
4343

4444
Run a project:
45-
```sh
45+
```
4646
codegame run
4747
```
4848

4949
Build a project:
50-
```sh
50+
```
5151
codegame build
5252
```
5353

54+
### Session management
55+
56+
List all sessions:
57+
```
58+
codegame session list
59+
```
60+
61+
Show session details:
62+
```
63+
codegame session show
64+
```
65+
66+
Remove a session:
67+
```
68+
codegame session remove
69+
```
70+
71+
Export a session to *share.code-game.org*:
72+
```
73+
codegame session export
74+
```
75+
76+
Import a session from *share.code-game.org*:
77+
```
78+
codegame session import <id>
79+
```
80+
81+
### Games
82+
83+
List all games on a server:
84+
```
85+
codegame game list <url>
86+
```
87+
88+
Create a new game on a server:
89+
```
90+
codegame game create <url>
91+
```
92+
93+
### Sharing
94+
95+
Share a game:
96+
```
97+
codegame share game
98+
```
99+
100+
Share a spectate link:
101+
```
102+
codegame share spectate
103+
```
104+
105+
Share a session:
106+
```
107+
codegame share session
108+
```
109+
110+
### cg-gen-events
111+
112+
Download and execute the correct version of [cg-gen-events](https://github.com/code-game-project/cg-gen-events):
113+
```
114+
codegame gen-events <input>
115+
```
116+
117+
### cg-debug
118+
119+
Download and execute the correct version of [cg-debug](https://github.com/code-game-project/cg-debug):
120+
```
121+
codegame debug <url>
122+
```
123+
124+
### Completion
125+
126+
Generate an autocompletion script for codegame-cli for the specified shell:
127+
```
128+
codegame completion <bash|zsh|fish|powershell>
129+
```
130+
54131
### Help
55132

56-
Display help:
57-
```sh
133+
Display general help:
134+
```
58135
codegame --help
59136
```
60137

138+
Display help about a specific command:
139+
```
140+
codegame help <cmd>
141+
```
142+
61143
## Installation
62144

63145
### Windows
@@ -95,13 +177,13 @@ To update, simply run the command again.
95177

96178
#### x86_64
97179

98-
```sh
180+
```
99181
curl -L https://github.com/code-game-project/codegame-cli/releases/latest/download/codegame-cli-darwin-amd64.tar.gz | tar -xz codegame && sudo mv codegame /usr/local/bin
100182
```
101183

102184
#### ARM64
103185

104-
```sh
186+
```
105187
curl -L https://github.com/code-game-project/codegame-cli/releases/latest/download/codegame-cli-darwin-arm64.tar.gz | tar -xz codegame && sudo mv codegame /usr/local/bin
106188
```
107189

@@ -113,13 +195,13 @@ To update, simply run the command again.
113195

114196
#### x86_64
115197

116-
```sh
198+
```
117199
curl -L https://github.com/code-game-project/codegame-cli/releases/latest/download/codegame-cli-linux-amd64.tar.gz | tar -xz codegame && sudo mv codegame /usr/local/bin
118200
```
119201

120202
#### ARM64
121203

122-
```sh
204+
```
123205
curl -L https://github.com/code-game-project/codegame-cli/releases/latest/download/codegame-cli-linux-arm64.tar.gz | tar -xz codegame && sudo mv codegame /usr/local/bin
124206
```
125207

@@ -133,7 +215,7 @@ You can download a prebuilt binary file for your operating system on the [releas
133215

134216
- [Go](https://go.dev/) 1.18+
135217

136-
```sh
218+
```
137219
git clone https://github.com/code-game-project/codegame-cli.git
138220
cd codegame-cli
139221
go build .

cmd/game.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// gameCmd represents the game command
8+
var gameCmd = &cobra.Command{
9+
Use: "game",
10+
Short: "Manage CodeGame games.",
11+
}
12+
13+
func init() {
14+
rootCmd.AddCommand(gameCmd)
15+
}

cmd/game_create.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
9+
"github.com/Bananenpro/cli"
10+
"github.com/code-game-project/go-utils/server"
11+
"github.com/mattn/go-colorable"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
// gameCreateCmd represents the game create command
16+
var gameCreateCmd = &cobra.Command{
17+
Use: "create",
18+
Short: "Create a new game on the a server.",
19+
Args: cobra.MaximumNArgs(1),
20+
Run: func(cmd *cobra.Command, args []string) {
21+
var gameURL string
22+
var err error
23+
if len(args) > 0 {
24+
gameURL = args[0]
25+
} else if gameURL = findGameURL(); gameURL == "" {
26+
gameURL, err = cli.Input("Game URL:")
27+
abort(err)
28+
}
29+
api, err := server.NewAPI(gameURL)
30+
abort(err)
31+
32+
type request struct {
33+
Public bool `json:"public"`
34+
Protected bool `json:"protected"`
35+
Config any `json:"config,omitempty"`
36+
}
37+
public, err := cmd.Flags().GetBool("public")
38+
abort(err)
39+
protected, err := cmd.Flags().GetBool("protected")
40+
abort(err)
41+
42+
data, err := json.Marshal(request{
43+
Public: public,
44+
Protected: protected,
45+
})
46+
abort(err)
47+
48+
body := bytes.NewBuffer(data)
49+
resp, err := http.Post(api.BaseURL()+"/games", "application/json", body)
50+
abort(err)
51+
defer resp.Body.Close()
52+
if resp.StatusCode != http.StatusCreated {
53+
abort(fmt.Errorf("invalid response code: expected: %d, got: %d", http.StatusCreated, resp.StatusCode))
54+
}
55+
56+
type response struct {
57+
GameId string `json:"game_id"`
58+
JoinSecret string `json:"join_secret"`
59+
}
60+
var r response
61+
err = json.NewDecoder(resp.Body).Decode(&r)
62+
63+
out := colorable.NewColorableStdout()
64+
fmt.Fprintf(out, "%sGame ID:%s %s\n", cli.Cyan, cli.Reset, r.GameId)
65+
if r.JoinSecret != "" {
66+
fmt.Fprintf(out, "%sJoin secret:%s %s\n", cli.Cyan, cli.Reset, r.JoinSecret)
67+
}
68+
},
69+
}
70+
71+
func init() {
72+
gameCmd.AddCommand(gameCreateCmd)
73+
gameCreateCmd.Flags().Bool("public", false, "The game is displayed on a public game list.")
74+
gameCreateCmd.Flags().Bool("protected", false, "You can only join the game with the returned join secret.")
75+
}

cmd/game_list.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/Bananenpro/cli"
7+
"github.com/code-game-project/go-utils/server"
8+
"github.com/mattn/go-colorable"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
// gameListCmd represents the game list command
13+
var gameListCmd = &cobra.Command{
14+
Use: "list",
15+
Short: "List all public games of a game server.",
16+
Args: cobra.MaximumNArgs(1),
17+
Run: func(cmd *cobra.Command, args []string) {
18+
var gameURL string
19+
var err error
20+
if len(args) > 0 {
21+
gameURL = args[0]
22+
} else if gameURL = findGameURL(); gameURL == "" {
23+
gameURL, err = cli.Input("Game URL:")
24+
abort(err)
25+
}
26+
27+
api, err := server.NewAPI(gameURL)
28+
abort(err)
29+
30+
private, public, err := api.ListGames()
31+
abort(err)
32+
33+
out := colorable.NewColorableStdout()
34+
fmt.Fprintf(out, "%sPrivate:%s %d\n", cli.Cyan, cli.Reset, private)
35+
if len(public) == 0 {
36+
fmt.Fprintf(out, "%sPublic:%s none\n", cli.Cyan, cli.Reset)
37+
} else {
38+
cli.PrintColor(cli.Cyan, "Public:")
39+
for _, g := range public {
40+
cli.Print("- %s (%d players)", g.Id, g.Players)
41+
}
42+
}
43+
},
44+
}
45+
46+
func init() {
47+
gameCmd.AddCommand(gameListCmd)
48+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
github.com/Bananenpro/cli v0.2.2
77
github.com/adrg/xdg v0.4.0
8-
github.com/code-game-project/go-utils v0.2.5
8+
github.com/code-game-project/go-utils v0.2.8
99
github.com/gomarkdown/markdown v0.0.0-20220731190611-dcdaee8e7a53
1010
github.com/google/uuid v1.3.0
1111
github.com/mattn/go-colorable v0.1.12

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63n
66
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
77
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
88
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
9-
github.com/code-game-project/go-utils v0.2.5 h1:f5IbAsUMu+e0eiDlbzbpJ5Hg2jJOCZLdcDvY3CpmGSg=
10-
github.com/code-game-project/go-utils v0.2.5/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
9+
github.com/code-game-project/go-utils v0.2.8 h1:f6ZvORWmlf8uZnCSAhLJ23MdA5dGvcAPkijTLfbv+B4=
10+
github.com/code-game-project/go-utils v0.2.8/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
1111
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1212
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
1313
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=

0 commit comments

Comments
 (0)