-
Notifications
You must be signed in to change notification settings - Fork 6
/
gamedb.go
223 lines (190 loc) · 5.28 KB
/
gamedb.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Package gamedb contains game related CRUD functionality.
package gamedb
import (
"context"
"errors"
"fmt"
"time"
"github.com/ardanlabs/liarsdice/business/core/game"
"github.com/ardanlabs/liarsdice/business/data/sqldb"
"github.com/ardanlabs/liarsdice/foundation/logger"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
// Store manages the set of APIs for user database access.
type Store struct {
log *logger.Logger
db sqlx.ExtContext
}
// NewStore constructs the api for data access.
func NewStore(log *logger.Logger, db *sqlx.DB) *Store {
return &Store{
log: log,
db: db,
}
}
// Create adds a game to the db.
func (s *Store) Create(ctx context.Context, g *game.Game) error {
// NEED A TRANSACTION HERE
d := struct {
ID uuid.UUID `db:"game_id"`
Name string `db:"name"`
DateCreated time.Time `db:"date_created"`
}{
ID: g.ID(),
Name: g.ID().String(),
DateCreated: g.DateCreated(),
}
q := `
INSERT INTO games
(game_id, name, date_created)
VALUES
(:game_id, :name, :date_created)`
if err := sqldb.NamedExecContext(ctx, s.log, s.db, q, d); err != nil {
return fmt.Errorf("namedexeccontext: %w", err)
}
q = `
INSERT INTO game_state
(game_id, round, status, player_last_out, player_last_win, player_turn, existing_players)
VALUES
(:game_id, :round, :status, :player_last_out, :player_last_win, :player_turn, :existing_players)`
if err := sqldb.NamedExecContext(ctx, s.log, s.db, q, toDBState(g.State())); err != nil {
return fmt.Errorf("namedexeccontext-state: %w", err)
}
return nil
}
// InsertRound adds a new set of state records for a given round to the db.
func (s *Store) InsertRound(ctx context.Context, state game.State) error {
dbState := toDBState(state)
// NEED A TRANSACTION HERE
q := `
INSERT INTO game_state
(game_id, round, status, player_last_out, player_last_win, player_turn, existing_players)
VALUES
(:game_id, :round, :status, :player_last_out, :player_last_win, :player_turn, :existing_players)`
if err := sqldb.NamedExecContext(ctx, s.log, s.db, q, dbState); err != nil {
return fmt.Errorf("namedexeccontext-state: %w", err)
}
q = `
INSERT INTO game_cups
(game_id, round, player, order_idx, outs, dice)
VALUES
(:game_id, :round, :player, :order_idx, :outs, :dice)`
for _, dbCup := range dbState.Cups {
if err := sqldb.NamedExecContext(ctx, s.log, s.db, q, dbCup); err != nil {
return fmt.Errorf("namedexeccontext-cups: %w", err)
}
}
q = `
INSERT INTO game_bets
(game_id, round, bet_order, player, number, suit)
VALUES
(:game_id, :round, :bet_order, :player, :number, :suit)`
for _, dbBet := range dbState.Bets {
if err := sqldb.NamedExecContext(ctx, s.log, s.db, q, dbBet); err != nil {
return fmt.Errorf("namedexeccontext-bets: %w", err)
}
}
q = `
INSERT INTO game_balances
(game_id, round, player, amount)
VALUES
(:game_id, :round, :player, :amount)`
for _, dbBalance := range dbState.Balances {
if err := sqldb.NamedExecContext(ctx, s.log, s.db, q, dbBalance); err != nil {
return fmt.Errorf("namedexeccontext-balances: %w", err)
}
}
return nil
}
// QueryStateByID gets the specified game state from the database.
func (s *Store) QueryStateByID(ctx context.Context, gameID uuid.UUID, round int) (game.State, error) {
data := struct {
ID string `db:"game_id"`
Round int `db:"round"`
}{
ID: gameID.String(),
Round: round,
}
q := `
SELECT
g.game_id,
g.name,
g.date_created,
s.round,
s.status,
s.player_last_out,
s.player_last_win,
s.player_turn,
s.existing_players
FROM
games AS g
JOIN
game_state AS s ON g.game_id = s.game_id
WHERE
g.game_id = :game_id AND
s.round = :round`
var dbState dbState
if err := sqldb.NamedQueryStruct(ctx, s.log, s.db, q, data, &dbState); err != nil {
if errors.Is(err, sqldb.ErrDBNotFound) {
return game.State{}, fmt.Errorf("namedquerystruct: %w", game.ErrNotFound)
}
return game.State{}, fmt.Errorf("namedquerystruct: %w", err)
}
q = `
SELECT
game_id,
round,
player,
order_idx,
outs,
dice
FROM
game_cups
WHERE
game_id = :game_id AND
round = :round`
if err := sqldb.NamedQuerySlice(ctx, s.log, s.db, q, data, &dbState.Cups); err != nil {
if errors.Is(err, sqldb.ErrDBNotFound) {
return game.State{}, fmt.Errorf("namedquerystruct: %w", game.ErrNotFound)
}
return game.State{}, fmt.Errorf("namedquerystruct: %w", err)
}
q = `
SELECT
game_id,
round,
bet_order,
player,
number,
suit
FROM
game_bets
WHERE
game_id = :game_id AND
round = :round`
if err := sqldb.NamedQuerySlice(ctx, s.log, s.db, q, data, &dbState.Bets); err != nil {
if errors.Is(err, sqldb.ErrDBNotFound) {
return game.State{}, fmt.Errorf("namedquerystruct: %w", game.ErrNotFound)
}
return game.State{}, fmt.Errorf("namedquerystruct: %w", err)
}
q = `
SELECT
game_id,
round,
player,
amount
FROM
game_balances
WHERE
game_id = :game_id AND
round = :round`
if err := sqldb.NamedQuerySlice(ctx, s.log, s.db, q, data, &dbState.Balances); err != nil {
if errors.Is(err, sqldb.ErrDBNotFound) {
return game.State{}, fmt.Errorf("namedquerystruct: %w", game.ErrNotFound)
}
return game.State{}, fmt.Errorf("namedquerystruct: %w", err)
}
return toCoreState(dbState)
}