-
Notifications
You must be signed in to change notification settings - Fork 7
/
model.go
182 lines (159 loc) · 4.79 KB
/
model.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
170
171
172
173
174
175
176
177
178
179
180
181
182
package gamedb
import (
"fmt"
"time"
"github.com/ardanlabs/liarsdice/business/core/game"
"github.com/ardanlabs/liarsdice/business/data/sqldb/dbarray"
"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
)
type dbState struct {
ID uuid.UUID `db:"game_id"`
Name string `db:"name"`
DateCreated time.Time `db:"date_created"`
Round int `db:"round"`
Status string `db:"status"`
PlayerLastOut string `db:"player_last_out"`
PlayerLastWin string `db:"player_last_win"`
PlayerTurn string `db:"player_turn"`
ExistingPlayers dbarray.String `db:"existing_players"`
Cups []dbCup
Bets []dbBet
Balances []dbBalance
}
type dbCup struct {
ID uuid.UUID `db:"game_id"`
Round int `db:"round"`
Player string `db:"player"`
OrderIdx int `db:"order_idx"`
Outs int `db:"outs"`
Dice dbarray.Int64 `db:"dice"`
}
type dbBet struct {
ID uuid.UUID `db:"game_id"`
Round int `db:"round"`
BetOrder int `db:"bet_order"`
Player string `db:"player"`
Number int `db:"number"`
Suit int `db:"suit"`
}
type dbBalance struct {
ID uuid.UUID `db:"game_id"`
Round int `db:"round"`
Player string `db:"player"`
Amount string `db:"amount"`
}
func toDBState(state game.State) dbState {
cups := make([]dbCup, 0, len(state.Cups))
for _, cup := range state.Cups {
dice := make([]int64, len(cup.Dice))
for i, d := range cup.Dice {
dice[i] = int64(d)
}
cups = append(cups, dbCup{
ID: state.GameID,
Round: state.Round,
Player: cup.Player.String(),
OrderIdx: cup.OrderIdx,
Outs: cup.Outs,
Dice: dice,
})
}
bets := make([]dbBet, len(state.Bets))
for i, bet := range state.Bets {
bets[i] = dbBet{
ID: state.GameID,
Round: state.Round,
BetOrder: i,
Player: bet.Player.String(),
Number: bet.Number,
Suit: bet.Suit,
}
}
balances := make([]dbBalance, len(state.Balances))
for i, balance := range state.Balances {
balances[i] = dbBalance{
ID: state.GameID,
Round: state.Round,
Player: balance.Player.String(),
Amount: balance.Amount,
}
}
existingPlayers := make([]string, len(state.ExistingPlayers))
for i, ep := range state.ExistingPlayers {
existingPlayers[i] = ep.String()
}
return dbState{
ID: state.GameID,
Name: state.GameID.String(),
DateCreated: state.DateCreated,
Round: state.Round,
Status: state.Status,
PlayerLastOut: state.PlayerLastOut.String(),
PlayerLastWin: state.PlayerLastWin.String(),
PlayerTurn: state.PlayerTurn.String(),
ExistingPlayers: existingPlayers,
Cups: cups,
Bets: bets,
Balances: balances,
}
}
func toCoreState(dbState dbState) (game.State, error) {
if !common.IsHexAddress(dbState.PlayerLastOut) {
return game.State{}, fmt.Errorf("player last out is not a common address: %q", dbState.PlayerLastOut)
}
if !common.IsHexAddress(dbState.PlayerLastWin) {
return game.State{}, fmt.Errorf("player last win is not a common address: %q", dbState.PlayerLastWin)
}
if !common.IsHexAddress(dbState.PlayerTurn) {
return game.State{}, fmt.Errorf("player turn is not a common address: %q", dbState.PlayerTurn)
}
existingPlayers := make([]common.Address, len(dbState.ExistingPlayers))
for i, player := range dbState.ExistingPlayers {
existingPlayers[i] = common.HexToAddress(player)
}
cups := make(map[common.Address]game.Cup)
for _, cup := range dbState.Cups {
player := common.HexToAddress(cup.Player)
dice := make([]int, len(cup.Dice))
for i, d := range cup.Dice {
dice[i] = int(d)
}
cups[player] = game.Cup{
Player: player,
OrderIdx: cup.OrderIdx,
Outs: cup.Outs,
Dice: dice,
}
}
bets := make([]game.Bet, len(dbState.Bets))
for i, bet := range dbState.Bets {
bets[i] = game.Bet{
Player: common.HexToAddress(bet.Player),
Number: bet.Number,
Suit: bet.Suit,
}
}
balances := make([]game.BalanceFmt, len(dbState.Balances))
for i, balance := range dbState.Balances {
balances[i] = game.BalanceFmt{
Player: common.HexToAddress(balance.Player),
Amount: balance.Amount,
}
}
state := game.State{
GameID: dbState.ID,
GameName: dbState.Name,
DateCreated: dbState.DateCreated,
Round: dbState.Round,
Status: dbState.Status,
PlayerLastOut: common.HexToAddress(dbState.PlayerLastOut),
PlayerLastWin: common.HexToAddress(dbState.PlayerLastWin),
PlayerTurn: common.HexToAddress(dbState.PlayerTurn),
ExistingPlayers: existingPlayers,
Cups: cups,
Bets: bets,
Balances: balances,
}
return state, nil
}