Lock database

This commit is contained in:
Trevor Slocum 2023-12-18 15:34:02 -08:00
parent c9af675820
commit aef7db8309
3 changed files with 57 additions and 20 deletions

View file

@ -60,44 +60,44 @@ When no moves are possible, only the roll is specified.
bgammon-replay 00000024
i 1702738670 Guest_385 BOT_tabula 1 0 0 1 1 0
1 r 4-1 13/9 9/8
2 r 1-6 1/7 1/2
1 r 5-6 13/7 7/2
2 r 6-1 1/7 1/2
1 r 6-5 13/7 7/2
2 r 3-3 bar/3 bar/3 12/15 12/15
1 r 6-2 8/2 8/6
2 r 5-2 12/14 14/19
1 r 4-4 24/20 20/16 24/20 20/16
2 r 4-5 3/7 7/12
1 r 3-5 16/13 13/8
2 r 1-5 3/4 4/9
2 r 5-4 3/7 7/12
1 r 5-3 16/13 13/8
2 r 5-1 3/4 4/9
1 r 2-1 16/14 14/13
2 r 5-1 9/10 10/15
1 r 1-1 6/5 6/5 8/7 8/7
2 r 6-2 19/21 15/21
1 r 4-4 13/9 13/9 13/9 13/9
2 r 1-5 15/20 12/13
1 r 4-6 9/3 7/3
2 r 2-5 17/19 15/20
2 r 5-1 15/20 12/13
1 r 6-4 9/3 7/3
2 r 5-2 17/19 15/20
1 r 6-2 7/1 6/4
2 r 4-5 17/22 17/21
2 r 5-4 17/22 17/21
1 r 4-3 8/5 5/1
2 r 3-1 12/15 12/13
1 r 6-3 9/3 9/6
2 r 2-4 15/19 13/15
2 r 4-2 15/19 13/15
1 r 3-1 9/8 8/5
2 r 2-5 15/20 13/15
2 r 5-2 15/20 13/15
1 r 6-5 5/off 6/off
2 r 3-1 15/16 16/19
1 r 5-1 5/off 1/off
2 r 3-1 22/off 19/20
1 r 6-5 6/off 5/off
2 r 4-4 21/off 21/off 21/off 19/23
1 r 1-5 6/5 5/off
2 r 5-6 20/off 19/off
1 r 5-1 6/5 5/off
2 r 6-5 20/off 19/off
1 r 6-3 6/off 3/off
2 r 3-2 23/off 19/22
1 r 5-4 4/off 3/off
2 r 2-4 19/23 23/off
2 r 4-2 19/23 23/off
1 r 6-3 3/off 2/off
2 r 5-1 20/off 19/20
1 r 1-2 1/off 2/off
1 r 2-1 1/off 2/off
```

View file

@ -15,6 +15,7 @@ import (
"net/textproto"
"strconv"
"strings"
"sync"
"time"
"code.rocket9labs.com/tslocum/bgammon"
@ -53,7 +54,10 @@ CREATE TABLE game (
);
`
var db *pgx.Conn
var (
db *pgx.Conn
dbLock = &sync.Mutex{}
)
var passwordArgon2id = &argon2id.Params{
Memory: 128 * 1024,
@ -110,6 +114,9 @@ func initDB() {
}
func registerAccount(passwordSalt string, a *account) error {
dbLock.Lock()
defer dbLock.Unlock()
if db == nil {
return nil
} else if len(bytes.TrimSpace(a.username)) == 0 {
@ -158,6 +165,9 @@ func registerAccount(passwordSalt string, a *account) error {
}
func resetAccount(mailServer string, resetSalt string, email []byte) error {
dbLock.Lock()
defer dbLock.Unlock()
if db == nil {
return nil
} else if len(bytes.TrimSpace(email)) == 0 {
@ -249,6 +259,9 @@ func resetAccount(mailServer string, resetSalt string, email []byte) error {
}
func confirmResetAccount(resetSalt string, passwordSalt string, id int, key string) (string, error) {
dbLock.Lock()
defer dbLock.Unlock()
if db == nil {
return "", nil
} else if id == 0 {
@ -296,6 +309,9 @@ func confirmResetAccount(resetSalt string, passwordSalt string, id int, key stri
}
func loginAccount(passwordSalt string, username []byte, password []byte) (*account, error) {
dbLock.Lock()
defer dbLock.Unlock()
if db == nil {
return nil, nil
} else if len(bytes.TrimSpace(username)) == 0 {
@ -332,6 +348,9 @@ func loginAccount(passwordSalt string, username []byte, password []byte) (*accou
}
func setAccountPassword(passwordSalt string, id int, password string) error {
dbLock.Lock()
defer dbLock.Unlock()
if db == nil {
return nil
} else if id <= 0 {
@ -364,6 +383,9 @@ func setAccountPassword(passwordSalt string, id int, password string) error {
}
func setAccountSetting(id int, name string, value int) error {
dbLock.Lock()
defer dbLock.Unlock()
if db == nil {
return nil
} else if name == "" {
@ -389,6 +411,9 @@ func setAccountSetting(id int, name string, value int) error {
}
func recordGameResult(g *bgammon.Game, winType int, account1 int, account2 int, replay [][]byte) error {
dbLock.Lock()
defer dbLock.Unlock()
if db == nil || g.Started.IsZero() || g.Winner == 0 {
return nil
}
@ -413,6 +438,9 @@ func recordGameResult(g *bgammon.Game, winType int, account1 int, account2 int,
}
func replayByID(id int) ([]byte, error) {
dbLock.Lock()
defer dbLock.Unlock()
if db == nil {
return nil, nil
} else if id <= 0 {
@ -434,6 +462,9 @@ func replayByID(id int) ([]byte, error) {
}
func dailyStats(tz *time.Location) (*serverStatsResult, error) {
dbLock.Lock()
defer dbLock.Unlock()
tx, err := begin()
if err != nil {
return nil, err
@ -489,6 +520,9 @@ func dailyStats(tz *time.Location) (*serverStatsResult, error) {
}
func cumulativeStats(tz *time.Location) (*serverStatsResult, error) {
dbLock.Lock()
defer dbLock.Unlock()
tx, err := begin()
if err != nil {
return nil, err
@ -547,6 +581,9 @@ func cumulativeStats(tz *time.Location) (*serverStatsResult, error) {
}
func botStats(name string, tz *time.Location) (*botStatsResult, error) {
dbLock.Lock()
defer dbLock.Unlock()
tx, err := begin()
if err != nil {
return nil, err

View file

@ -1703,10 +1703,10 @@ COMMANDS:
continue
}
clientGame.Turn = 2
clientGame.Roll1 = 4
clientGame.Roll2 = 4
clientGame.Board = []int{1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -1, 0, 0}
clientGame.Turn = 1
clientGame.Roll1 = 6
clientGame.Roll2 = 6
clientGame.Board = []int{1, 0, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, -1, 1, -1}
clientGame.eachClient(func(client *serverClient) {
clientGame.sendBoard(client)