-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (54 loc) · 1.55 KB
/
main.go
File metadata and controls
65 lines (54 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
"math/rand"
"os"
"strconv"
"time"
"github.com/code-game-project/go-server/cg"
"github.com/spf13/pflag"
"github.com/code-game-project/number-guessing/numberguessing"
)
func main() {
rand.Seed(time.Now().UnixMilli())
var port int
pflag.IntVarP(&port, "port", "p", 0, "The network port of the game server.")
var frontend string
pflag.StringVar(&frontend, "frontend", "", "The root directory of the frontend")
pflag.Parse()
if port == 0 {
portStr, ok := os.LookupEnv("CG_PORT")
if ok {
port, _ = strconv.Atoi(portStr)
}
}
if port == 0 {
port = 8080
}
server := cg.NewServer("number-guessing", cg.ServerConfig{
DisplayName: "Number Guessing",
MaxPlayersPerGame: 1,
DeleteInactiveGameDelay: 30 * time.Second,
KickInactivePlayerDelay: 5 * time.Minute,
Version: "0.2",
Description: "A number guessing game.",
RepositoryURL: "https://github.com/code-game-project/number-guessing",
Port: port,
CGEFilepath: "events.cge",
WebRoot: frontend,
})
server.Run(func(cgGame *cg.Game, config json.RawMessage) {
var gameConfig numberguessing.GameConfig
err := json.Unmarshal(config, &gameConfig)
if err != nil || gameConfig.Min > gameConfig.Max {
cgGame.Log.ErrorData(config, "invalid game config")
gameConfig.Min = 0
gameConfig.Max = 100
}
if gameConfig.Max == 0 {
gameConfig.Max = 100
}
cgGame.SetConfig(gameConfig)
numberguessing.NewGame(cgGame, gameConfig).Run()
})
}