-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7518b09
commit 5398e7b
Showing
6 changed files
with
183 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,97 @@ | ||
package gamedb | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/ardanlabs/liarsdice/business/core/game" | ||
"github.com/ardanlabs/liarsdice/business/data/sqldb/dbarray" | ||
) | ||
|
||
type dbGame struct { | ||
ID string `db:"game_id"` | ||
Name string `db:"name"` | ||
ID string `db:"game_id"` | ||
Name string `db:"name"` | ||
CreatedDate time.Time `db:"created_date"` | ||
State dbState | ||
Cups []dbCup | ||
Bets []dbBet | ||
Balances []dbBalance | ||
} | ||
|
||
type dbGameStatus struct { | ||
ID string `db:"game_id"` | ||
Iteration int `db:"iteration"` | ||
Status string `db:"status"` | ||
PlayerLastOut string `db:"player_last_out"` | ||
PlayerLastWin string `db:"player_last_win"` | ||
PlayerTurn string `db:"player_turn"` | ||
Round int `db:"round"` | ||
type dbState struct { | ||
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 any `db:"existing_players"` | ||
} | ||
|
||
type dbGameCup struct { | ||
ID string `db:"game_id"` | ||
Iteration int `db:"iteration"` | ||
Player string `db:"player"` | ||
OrderIdx int `db:"order_idx"` | ||
Outs int `db:"outs"` | ||
type dbCup struct { | ||
Round int `db:"round"` | ||
Player string `db:"player"` | ||
OrderIdx int `db:"order_idx"` | ||
Outs int `db:"outs"` | ||
Dice any `db:"dice"` | ||
} | ||
|
||
type dbGameDice struct { | ||
ID string `db:"game_id"` | ||
Iteration int `db:"iteration"` | ||
Player string `db:"player"` | ||
Dice int `db:"dice"` | ||
type dbBet struct { | ||
Round int `db:"round"` | ||
Player string `db:"player"` | ||
Number int `db:"number"` | ||
Suit int `db:"suit"` | ||
} | ||
|
||
type dbGameExistingPlayers struct { | ||
ID string `db:"game_id"` | ||
Iteration int `db:"iteration"` | ||
Player string `db:"player"` | ||
type dbBalance struct { | ||
Round int `db:"round"` | ||
Player string `db:"player"` | ||
Amount string `db:"amount"` | ||
} | ||
|
||
type dbGameBets struct { | ||
ID string `db:"game_id"` | ||
Iteration int `db:"iteration"` | ||
Player string `db:"player"` | ||
Number int `db:"number"` | ||
Suit int `db:"suit"` | ||
} | ||
func toDBGame(g *game.Game, state game.State) dbGame { | ||
cups := make([]dbCup, len(state.Cups)) | ||
for player, cup := range state.Cups { | ||
cups = append(cups, dbCup{ | ||
Round: state.Round, | ||
Player: player.String(), | ||
OrderIdx: cup.OrderIdx, | ||
Outs: cup.Outs, | ||
Dice: dbarray.Array(cup.Dice), | ||
}) | ||
} | ||
|
||
type dbGameBalances struct { | ||
ID string `db:"game_id"` | ||
Iteration int `db:"iteration"` | ||
Player string `db:"player"` | ||
Balance string `db:"balance"` | ||
} | ||
bets := make([]dbBet, len(state.Bets)) | ||
for _, bet := range state.Bets { | ||
bets = append(bets, dbBet{ | ||
Round: state.Round, | ||
Player: bet.Player.String(), | ||
Number: bet.Number, | ||
Suit: bet.Suit, | ||
}) | ||
} | ||
|
||
balances := make([]dbBalance, len(state.Balances)) | ||
for _, balance := range state.Balances { | ||
balances = append(balances, dbBalance{ | ||
Round: state.Round, | ||
Player: balance.Player.String(), | ||
Amount: balance.Amount, | ||
}) | ||
} | ||
|
||
// func toDBGame(status game.State) | ||
return dbGame{ | ||
ID: state.GameID, | ||
Name: state.GameID, | ||
CreatedDate: g.CreatedDate(), | ||
State: dbState{ | ||
Round: state.Round, | ||
Status: state.Status, | ||
PlayerLastOut: state.PlayerLastOut.String(), | ||
PlayerLastWin: state.PlayerLastWin.String(), | ||
PlayerTurn: state.PlayerTurn.String(), | ||
ExistingPlayers: dbarray.Array(state.ExistingPlayers), | ||
}, | ||
Cups: cups, | ||
Bets: bets, | ||
Balances: balances, | ||
} | ||
} |
Oops, something went wrong.