-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
169 lines (134 loc) · 3.46 KB
/
utils.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gatiserv
import (
"log/slog"
"os"
"github.com/bytedance/sonic"
goutils "github.com/zhs007/goutils"
sgc7game "github.com/zhs007/slotsgamecore7/game"
)
// BuildIPlayerState - PlayerState => sgc7game.IPlayerState
func BuildIPlayerState(ips sgc7game.IPlayerState, ps *PlayerState) error {
err := ips.SetPublic(ps.Public)
if err != nil {
return err
}
err = ips.SetPrivate(ps.Private)
if err != nil {
return err
}
return nil
}
// BuildPlayerStateString - sgc7game.IPlayerState => string
func BuildPlayerStateString(ps sgc7game.IPlayerState) (string, error) {
if ps == nil {
return "{\"playerStatePublic\":{},\"playerStatePrivate\":{}}", nil
}
dps, err := BuildPlayerState(ps)
if err != nil {
goutils.Warn("gatiserv.BuildPlayerStateString:BuildPlayerState",
goutils.Err(err))
return "", err
}
psfb, err := sonic.Marshal(dps)
if err != nil {
goutils.Warn("gatiserv.BuildPlayerStateString:Marshal PlayerState",
goutils.Err(err))
return "", err
}
return string(psfb), nil
}
// BuildPlayerState - sgc7game.IPlayerState => PlayerState
func BuildPlayerState(ips sgc7game.IPlayerState) (*PlayerState, error) {
if ips == nil {
return nil, nil
}
return &PlayerState{
Public: ips.GetPublic(),
Private: ips.GetPrivate(),
}, nil
}
// ParsePlayerState - json => PlayerState
func ParsePlayerState(str string, ps *PlayerState) error {
err := sonic.Unmarshal([]byte(str), ps)
if err != nil {
goutils.Error("gatiserv.ParsePlayerState:JSON",
slog.String("str", str),
goutils.Err(err))
return err
}
return nil
}
// BuildStake - PlayerState => sgc7game.IPlayerState
func BuildStake(stake Stake) *sgc7game.Stake {
return &sgc7game.Stake{
CoinBet: int64(stake.CoinBet * 100),
CashBet: int64(stake.CashBet * 100),
Currency: stake.Currency,
}
}
// AddWinResult - add sgc7game.PlayResult
func AddWinResult(pr *PlayResult, stake Stake, playResult *sgc7game.PlayResult) error {
r := &Result{
CoinWin: playResult.CoinWin,
ClientData: playResult,
}
r.CashWin = float64(playResult.CashWin) / 100.0
pr.Results = append(pr.Results, r)
return nil
}
// AddPlayResult - []*sgc7game.PlayResult => *PlayResult
func AddPlayResult(pr *PlayResult, stake Stake, results []*sgc7game.PlayResult) {
for _, v := range results {
AddWinResult(pr, stake, v)
}
}
// ParsePlayParams - string => *PlayParams
func ParsePlayParams(str string, ps *PlayerState) (*PlayParams, error) {
pp := &PlayParams{
PlayerState: ps,
}
err := sonic.Unmarshal([]byte(str), pp)
if err != nil {
goutils.Error("gatiserv.ParsePlayParams:JSON",
slog.String("str", str),
goutils.Err(err))
return nil, err
}
return pp, nil
}
// ParsePlayResult - string => *PlayResult
func ParsePlayResult(str string) (*PlayResult, error) {
pr := &PlayResult{}
err := sonic.Unmarshal([]byte(str), pr)
if err != nil {
goutils.Error("gatiserv.ParsePlayResult:JSON",
slog.String("str", str),
goutils.Err(err))
return nil, err
}
return pr, nil
}
// NewGATIGameInfo -
func NewGATIGameInfo() *GATIGameInfo {
return &GATIGameInfo{
Components: make(map[int]*GATICriticalComponent),
}
}
// LoadGATIGameConfig - load
func LoadGATIGameConfig(fn string) (*GATIGameConfig, error) {
if fn == "" {
return &GATIGameConfig{}, nil
}
data, err := os.ReadFile(fn)
if err != nil {
return nil, err
}
ccs := &GATIGameConfig{}
err = sonic.Unmarshal(data, ccs)
if err != nil {
goutils.Warn("gatiserv.LoadGATIGameConfig",
goutils.Err(err))
return nil, err
}
return ccs, nil
}