commit 65dd70c0f664115c0adc16cde7a106a888abb6af
parent c9eb12b8009c8c3815d59609c2f6cd628266ae23
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Tue, 12 Dec 2023 05:34:11 -0500
cleanup
Diffstat:
3 files changed, 8 insertions(+), 35 deletions(-)
diff --git a/cmd/dkf/migrations/147.sql b/cmd/dkf/migrations/147.sql
@@ -17,6 +17,7 @@ CREATE TABLE IF NOT EXISTS poker_table_accounts (
user_id INTEGER NOT NULL,
poker_table_id INTEGER NOT NULL,
amount INTEGER NOT NULL,
+ amount_bet INTEGER NOT NULL,
UNIQUE (user_id, poker_table_id),
CONSTRAINT poker_table_accounts_user_id_fk
FOREIGN KEY (user_id)
@@ -31,15 +32,6 @@ CREATE TABLE IF NOT EXISTS poker_table_accounts (
CREATE INDEX poker_table_accounts_poker_table_id_idx ON poker_table_accounts (poker_table_id);
CREATE INDEX poker_table_accounts_user_id_idx ON poker_table_accounts (user_id);
CREATE INDEX poker_table_accounts_amount_idx ON poker_table_accounts (amount);
-
-CREATE TABLE IF NOT EXISTS poker_table_bets (
- poker_table_account_id INTEGER NOT NULL PRIMARY KEY,
- amount INTEGER NOT NULL,
- CONSTRAINT poker_table_bets_poker_table_account_id_fk
- FOREIGN KEY (poker_table_account_id)
- REFERENCES poker_table_accounts (id)
- ON DELETE CASCADE
- ON UPDATE CASCADE);
-CREATE INDEX poker_table_bets_poker_table_account_id_idx ON poker_table_bets (poker_table_account_id);
+CREATE INDEX poker_table_accounts_amount_bet_idx ON poker_table_accounts (amount_bet);
-- +migrate Down
diff --git a/pkg/database/tablePokerTables.go b/pkg/database/tablePokerTables.go
@@ -24,6 +24,7 @@ type PokerTableAccount struct {
UserID UserID
PokerTableID int64
Amount int64
+ AmountBet int64
PokerTable PokerTable
}
@@ -38,7 +39,7 @@ func (a *PokerTableAccount) DoSave(db *DkfDB) {
}
func (d *DkfDB) GetPositivePokerTableAccounts() (out []PokerTableAccount, err error) {
- err = d.db.Preload("PokerTable").Find(&out, "amount > 0").Error
+ err = d.db.Preload("PokerTable").Find(&out, "amount > 0 OR amount_bet > 0").Error
return
}
@@ -49,20 +50,3 @@ func (d *DkfDB) GetPokerTableAccount(userID UserID, pokerTableID int64) (out Pok
}
return
}
-
-type PokerTableBet struct {
- PokerTableAccountID int64
- Amount int64
-}
-
-func (d *DkfDB) GetPokerTableBet(accountID int64) (out PokerTableBet, err error) {
- if err = d.db.First(&out, "poker_table_account_id = ?", accountID).Error; err != nil {
- out = PokerTableBet{PokerTableAccountID: accountID}
- err = d.db.Create(&out).Error
- }
- return
-}
-
-func (b *PokerTableBet) UpdateAmount(db *DkfDB, accountID, v int64) {
- db.db.Model(b).Where("poker_table_account_id = ?", accountID).Update("amount", v)
-}
diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go
@@ -136,9 +136,8 @@ func (p *PokerPlayer) doBet(db *database.DkfDB, roomID string, bet int) {
pokerTable, _ := db.GetPokerTableBySlug(roomID)
account, _ := db.GetPokerTableAccount(user.ID, pokerTable.ID)
account.Amount -= int64(bet)
+ account.AmountBet += int64(bet)
account.DoSave(db)
- tableBet, _ := db.GetPokerTableBet(account.ID)
- tableBet.UpdateAmount(db, account.ID, tableBet.Amount+int64(bet))
p.RoundTotalBet += bet
p.Bet += bet
p.Cash -= bet
@@ -893,9 +892,8 @@ END:
pokerTable, _ := db.GetPokerTableBySlug(roomID)
account, _ := db.GetPokerTableAccount(user.ID, pokerTable.ID)
account.Amount += int64(el.Gain)
+ account.AmountBet = 0
account.DoSave(db)
- tableBet, _ := db.GetPokerTableBet(account.ID)
- tableBet.UpdateAmount(db, account.ID, 0)
winnersStr += el.Player.Username + " "
el.Player.Cash += el.Gain
}
@@ -1172,15 +1170,14 @@ func Refund(db *database.DkfDB) {
accounts, _ := db.GetPositivePokerTableAccounts()
for _, account := range accounts {
if user, err := db.GetUserByID(account.UserID); err == nil {
- tableBet, _ := db.GetPokerTableBet(account.ID)
- account.Amount += tableBet.Amount
+ account.Amount += account.AmountBet
if account.PokerTable.IsTest {
user.ChipsTest += int(account.Amount)
}
user.DoSave(db)
account.Amount = 0
+ account.AmountBet = 0
account.DoSave(db)
- tableBet.UpdateAmount(db, account.ID, 0)
}
}
}