-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameconfig.go
115 lines (95 loc) · 2.54 KB
/
gameconfig.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gameconfig
import (
"encoding/base32"
"errors"
"fmt"
"github.com/gorilla/securecookie"
"github.com/kofuk/premises/common/entity/runner"
"golang.org/x/exp/slices"
)
type Config struct {
C runner.Config
}
func New() *Config {
result := new(Config)
result.C.World.LevelType = "default"
result.C.World.Difficulty = "normal"
return result
}
func (c *Config) SetServer(version, downloadURL string) {
c.C.Server.Version = version
c.C.Server.DownloadUrl = downloadURL
}
var (
ErrMemoryTooSmall = errors.New("memory too small")
)
func calculateMemSizeForGame(availableSizeMiB int) (int, error) {
if availableSizeMiB < 2048 {
return 0, ErrMemoryTooSmall
}
return availableSizeMiB - 1024, nil
}
func (c *Config) SetDetectServerVersion(detect bool) {
c.C.Server.PreferDetected = detect
}
func (c *Config) GenerateAuthKey() string {
encoder := base32.StdEncoding.WithPadding(base32.NoPadding)
result := encoder.EncodeToString(securecookie.GenerateRandomKey(30))
c.C.AuthKey = result
return result
}
func (c *Config) SetWorld(worldName string, generationId string) error {
if worldName == "" || generationId == "" {
return fmt.Errorf("either worldName or generationId is empty")
}
c.C.World.Name = worldName
c.C.World.GenerationId = generationId
return nil
}
func (c *Config) GenerateWorld(worldName, seed string) {
c.C.World.ShouldGenerate = true
c.C.World.Name = worldName
c.C.World.Seed = seed
}
func (c *Config) SetMotd(motd string) {
c.C.Motd = motd
}
func isValidLevelType(levelType string) bool {
return slices.Contains([]string{"default", "flat", "largeBiomes", "amplified", "buffet"}, levelType)
}
func (c *Config) SetLevelType(levelType string) error {
if !isValidLevelType(levelType) {
return errors.New("unknown level type")
}
c.C.World.LevelType = levelType
return nil
}
func isValidDifficulty(difficulty string) bool {
return slices.Contains([]string{"peaceful", "easy", "normal", "hard"}, difficulty)
}
func (c *Config) SetDifficulty(difficulty string) error {
if !isValidDifficulty(difficulty) {
return errors.New("unknown difficulty")
}
c.C.World.Difficulty = difficulty
return nil
}
func addToSlice[T comparable](to []T, elm T) []T {
for _, v := range to {
if v == elm {
return to
}
}
return append(to, elm)
}
func (c *Config) SetOperators(ops []string) {
for _, op := range ops {
c.C.Operators = addToSlice(c.C.Operators, op)
c.C.Whitelist = addToSlice(c.C.Whitelist, op)
}
}
func (c *Config) SetWhitelist(wlist []string) {
for _, wl := range wlist {
c.C.Whitelist = addToSlice(c.C.Whitelist, wl)
}
}