forked from wcp1231/mahjong-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
79 lines (66 loc) · 1.45 KB
/
config.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
package main
import (
"io/ioutil"
"encoding/json"
"bytes"
"os"
)
const (
configFile = "config.json"
)
type gameConfig struct {
MajsoulAccountIDs []int `json:"majsoul_account_ids"`
currentActiveMajsoulAccountID int `json:"-"`
currentActiveTenhouUsername string `json:"-"`
}
var gameConf = &gameConfig{
MajsoulAccountIDs: []int{},
currentActiveMajsoulAccountID: -1,
}
func init() {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return
}
data, err := ioutil.ReadFile(configFile)
if err != nil {
if debugMode {
panic(err)
}
return
}
if err := json.NewDecoder(bytes.NewReader(data)).Decode(gameConf); err != nil {
if debugMode {
panic(err)
}
return
}
//fmt.Println(*gameConf)
}
func (c *gameConfig) saveConfigToFile() error {
data, err := json.Marshal(c)
if err != nil {
return err
}
if err := ioutil.WriteFile(configFile, data, os.ModePerm); err != nil {
return err
}
return nil
}
func (c *gameConfig) isIDExist(majsoulAccountID int) bool {
for _, id := range c.MajsoulAccountIDs {
if id == majsoulAccountID {
return true
}
}
return false
}
func (c *gameConfig) addMajsoulAccountID(majsoulAccountID int) error {
if c.isIDExist(majsoulAccountID) {
return nil
}
gameConf.MajsoulAccountIDs = append(gameConf.MajsoulAccountIDs, majsoulAccountID)
return c.saveConfigToFile()
}
func (c *gameConfig) setMajsoulAccountID(majsoulAccountID int) {
c.currentActiveMajsoulAccountID = majsoulAccountID
}