tablePokerTables.go (3666B)
1 package database 2 3 import ( 4 "dkforest/pkg/config" 5 "fmt" 6 "github.com/dustin/go-humanize" 7 "github.com/sirupsen/logrus" 8 ) 9 10 type PokerTable struct { 11 ID int64 12 IDX int64 13 Slug string 14 Name string 15 MinBuyIn PokerChip 16 MaxBuyIn PokerChip 17 MinBet PokerChip 18 IsTest bool 19 } 20 21 func (d *DkfDB) GetPokerTables() (out []PokerTable, err error) { 22 err = d.db.Order("idx ASC, id").Find(&out).Error 23 return 24 } 25 26 func (d *DkfDB) GetPokerTableBySlug(slug string) (out PokerTable, err error) { 27 err = d.db.First(&out, "slug = ?", slug).Error 28 return 29 } 30 31 // Piconero the smallest unit of Monero is 1 piconero (0.000000000001 XMR) also known as the atomic unit 32 // https://www.getmonero.org/resources/moneropedia/denominations.html 33 type Piconero uint64 34 35 func (p Piconero) ToPokerChip() PokerChip { 36 return PokerChip(p / 10_000_000) 37 } 38 39 func (p Piconero) XmrStr() string { 40 return fmt.Sprintf("%.12f", float64(p)/1_000_000_000_000) 41 } 42 43 func (p Piconero) UsdStr() string { 44 return fmt.Sprintf("$%.2f", float64(p)/1_000_000_000_000*config.MoneroPrice.Load()) 45 } 46 47 func (p Piconero) RawString() string { return fmt.Sprintf("%d", p) } 48 49 func (p Piconero) String() string { return humanize.Comma(int64(p)) } 50 51 type PokerChip uint64 52 53 func (p PokerChip) ToPiconero() Piconero { 54 return Piconero(p * 10_000_000) 55 } 56 57 func (p PokerChip) String() string { return humanize.Comma(int64(p)) } 58 59 func (p PokerChip) Raw() uint64 { return uint64(p) } 60 61 type PokerTableAccount struct { 62 ID int64 63 UserID UserID 64 PokerTableID int64 65 Amount PokerChip 66 AmountBet PokerChip 67 } 68 69 func (a *PokerTableAccount) Save(db *DkfDB) error { 70 return db.db.Save(a).Error 71 } 72 73 func (a *PokerTableAccount) DoSave(db *DkfDB) { 74 if err := a.Save(db); err != nil { 75 logrus.Error(err) 76 } 77 } 78 79 func (d *DkfDB) GetPositivePokerTableAccounts() (out []PokerTableAccount, err error) { 80 err = d.db.Find(&out, "amount > 0 OR amount_bet > 0").Error 81 return 82 } 83 84 func (d *DkfDB) GetPokerTableAccount(userID UserID, pokerTableID int64) (out PokerTableAccount, err error) { 85 if err = d.db.First(&out, "user_id = ? AND poker_table_id = ?", userID, pokerTableID).Error; err != nil { 86 out = PokerTableAccount{UserID: userID, PokerTableID: pokerTableID} 87 err = d.db.Create(&out).Error 88 } 89 return 90 } 91 92 func (d *DkfDB) GetPokerTableAccounts(userID UserID) (out []PokerTableAccount, err error) { 93 err = d.db.Find(&out, "user_id = ?", userID).Error 94 return 95 } 96 97 func (d *DkfDB) GetPokerTableAccountSums() (sumAmounts, sumBets PokerChip, err error) { 98 var tmp struct{ SumAmounts, SumBets PokerChip } 99 err = d.db.Raw(`SELECT SUM(amount) AS sum_amounts, SUM(amount_bet) AS sum_bets FROM poker_table_accounts INNER JOIN poker_tables t ON t.id= poker_table_id WHERE t.is_test = 0`).Scan(&tmp).Error 100 return tmp.SumAmounts, tmp.SumBets, err 101 } 102 103 func (d *DkfDB) PokerTableAccountBet(userID UserID, pokerTableID int64, bet PokerChip) (err error) { 104 err = d.db.Exec(`UPDATE poker_table_accounts SET amount = amount - ?, amount_bet = amount_bet + ? WHERE user_id = ? AND poker_table_id = ?`, 105 bet, bet, userID, pokerTableID).Error 106 return 107 } 108 109 func (d *DkfDB) PokerTableAccountRefundPartialBet(userID UserID, pokerTableID int64, diff PokerChip) (err error) { 110 err = d.db.Exec(`UPDATE poker_table_accounts SET amount = amount + ?, amount_bet = amount_bet - ? WHERE user_id = ? AND poker_table_id = ?`, 111 diff, diff, userID, pokerTableID).Error 112 return 113 } 114 115 func (d *DkfDB) PokerTableAccountGain(userID UserID, pokerTableID int64, gain PokerChip) (err error) { 116 err = d.db.Exec(`UPDATE poker_table_accounts SET amount = amount + ?, amount_bet = 0 WHERE user_id = ? AND poker_table_id = ?`, 117 gain, userID, pokerTableID).Error 118 return 119 }