Add profile icon support

This commit is contained in:
Trevor Slocum 2024-08-01 14:36:49 -07:00
parent a07da30d8b
commit 64b10515b0
5 changed files with 41 additions and 6 deletions

View file

@ -6,6 +6,9 @@ type account struct {
username []byte
password []byte
icon int
icons []byte
autoplay bool
highlight bool
pips bool

View file

@ -35,6 +35,8 @@ CREATE TABLE account (
email text NOT NULL,
username text NOT NULL,
password text NOT NULL,
icon integer NOT NULL DEFAULT 0,
icons text NOT NULL DEFAULT '',
casual_backgammon_single integer NOT NULL DEFAULT 150000,
casual_backgammon_multi integer NOT NULL DEFAULT 150000,
casual_acey_single integer NOT NULL DEFAULT 150000,
@ -351,7 +353,7 @@ func accountByID(id int) (*account, error) {
competitive: &clientRating{},
}
var autoplay, highlight, pips, moves, flip, traditional, advanced, muteJoinLeave, muteChat, muteRoll, muteMove, muteBearOff int
err = tx.QueryRow(context.Background(), "SELECT id, email, username, password, autoplay, highlight, pips, moves, flip, traditional, advanced, mutejoinleave, mutechat, muteroll, mutemove, mutebearoff, speed, casual_backgammon_single, casual_backgammon_multi, casual_acey_single, casual_acey_multi, casual_tabula_single, casual_tabula_multi, rated_backgammon_single, rated_backgammon_multi, rated_acey_single, rated_acey_multi, rated_tabula_single, rated_tabula_multi FROM account WHERE id = $1", id).Scan(&a.id, &a.email, &a.username, &a.password, &autoplay, &highlight, &pips, &moves, &flip, &traditional, &advanced, &a.muteJoinLeave, &a.muteChat, &a.muteRoll, &a.muteMove, &a.muteBearOff, &a.speed, &a.casual.backgammonSingle, &a.casual.backgammonMulti, &a.casual.aceySingle, &a.casual.aceyMulti, &a.casual.tabulaSingle, &a.casual.tabulaMulti, &a.competitive.backgammonSingle, &a.competitive.backgammonMulti, &a.competitive.aceySingle, &a.competitive.aceyMulti, &a.competitive.tabulaSingle, &a.competitive.tabulaMulti)
err = tx.QueryRow(context.Background(), "SELECT id, email, username, password, icon, autoplay, highlight, pips, moves, flip, traditional, advanced, mutejoinleave, mutechat, muteroll, mutemove, mutebearoff, speed, casual_backgammon_single, casual_backgammon_multi, casual_acey_single, casual_acey_multi, casual_tabula_single, casual_tabula_multi, rated_backgammon_single, rated_backgammon_multi, rated_acey_single, rated_acey_multi, rated_tabula_single, rated_tabula_multi FROM account WHERE id = $1", id).Scan(&a.id, &a.email, &a.username, &a.password, &a.icon, &autoplay, &highlight, &pips, &moves, &flip, &traditional, &advanced, &muteJoinLeave, &muteChat, &muteRoll, &muteMove, &muteBearOff, &a.speed, &a.casual.backgammonSingle, &a.casual.backgammonMulti, &a.casual.aceySingle, &a.casual.aceyMulti, &a.casual.tabulaSingle, &a.casual.tabulaMulti, &a.competitive.backgammonSingle, &a.competitive.backgammonMulti, &a.competitive.aceySingle, &a.competitive.aceyMulti, &a.competitive.tabulaSingle, &a.competitive.tabulaMulti)
if err != nil {
return nil, nil
}
@ -388,8 +390,8 @@ func accountByUsername(username string) (*account, error) {
casual: &clientRating{},
competitive: &clientRating{},
}
var autoplay, highlight, pips, moves, flip, advanced int
err = tx.QueryRow(context.Background(), "SELECT id, email, username, password, autoplay, highlight, pips, moves, flip, advanced, speed, casual_backgammon_single, casual_backgammon_multi, casual_acey_single, casual_acey_multi, casual_tabula_single, casual_tabula_multi, rated_backgammon_single, rated_backgammon_multi, rated_acey_single, rated_acey_multi, rated_tabula_single, rated_tabula_multi FROM account WHERE username = $1", strings.ToLower(username)).Scan(&a.id, &a.email, &a.username, &a.password, &autoplay, &highlight, &pips, &moves, &flip, &advanced, &a.speed, &a.casual.backgammonSingle, &a.casual.backgammonMulti, &a.casual.aceySingle, &a.casual.aceyMulti, &a.casual.tabulaSingle, &a.casual.tabulaMulti, &a.competitive.backgammonSingle, &a.competitive.backgammonMulti, &a.competitive.aceySingle, &a.competitive.aceyMulti, &a.competitive.tabulaSingle, &a.competitive.tabulaMulti)
var autoplay, highlight, pips, moves, flip, advanced, muteJoinLeave, muteChat, muteRoll, muteMove, muteBearOff int
err = tx.QueryRow(context.Background(), "SELECT id, email, username, password, icon, autoplay, highlight, pips, moves, flip, advanced, mutejoinleave, mutechat, muteroll, mutemove, mutebearoff, speed, casual_backgammon_single, casual_backgammon_multi, casual_acey_single, casual_acey_multi, casual_tabula_single, casual_tabula_multi, rated_backgammon_single, rated_backgammon_multi, rated_acey_single, rated_acey_multi, rated_tabula_single, rated_tabula_multi FROM account WHERE username = $1", strings.ToLower(username)).Scan(&a.id, &a.email, &a.username, &a.password, &a.icon, &autoplay, &highlight, &pips, &moves, &flip, &advanced, &muteJoinLeave, &muteChat, &muteRoll, &muteMove, &muteBearOff, &a.speed, &a.casual.backgammonSingle, &a.casual.backgammonMulti, &a.casual.aceySingle, &a.casual.aceyMulti, &a.casual.tabulaSingle, &a.casual.tabulaMulti, &a.competitive.backgammonSingle, &a.competitive.backgammonMulti, &a.competitive.aceySingle, &a.competitive.aceyMulti, &a.competitive.tabulaSingle, &a.competitive.tabulaMulti)
if err != nil {
return nil, nil
}
@ -399,6 +401,11 @@ func accountByUsername(username string) (*account, error) {
a.moves = moves == 1
a.flip = flip == 1
a.advanced = advanced == 1
a.muteJoinLeave = muteJoinLeave == 1
a.muteChat = muteChat == 1
a.muteRoll = muteRoll == 1
a.muteMove = muteMove == 1
a.muteBearOff = muteBearOff == 1
return a, nil
}
@ -424,8 +431,8 @@ func loginAccount(passwordSalt string, username []byte, password []byte) (*accou
casual: &clientRating{},
competitive: &clientRating{},
}
var autoplay, highlight, pips, moves, flip, advanced int
err = tx.QueryRow(context.Background(), "SELECT id, email, username, password, autoplay, highlight, pips, moves, flip, advanced, speed, casual_backgammon_single, casual_backgammon_multi, casual_acey_single, casual_acey_multi, casual_tabula_single, casual_tabula_multi, rated_backgammon_single, rated_backgammon_multi, rated_acey_single, rated_acey_multi, rated_tabula_single, rated_tabula_multi FROM account WHERE username = $1 OR email = $2", bytes.ToLower(bytes.TrimSpace(username)), bytes.ToLower(bytes.TrimSpace(username))).Scan(&a.id, &a.email, &a.username, &a.password, &autoplay, &highlight, &pips, &moves, &flip, &advanced, &a.speed, &a.casual.backgammonSingle, &a.casual.backgammonMulti, &a.casual.aceySingle, &a.casual.aceyMulti, &a.casual.tabulaSingle, &a.casual.tabulaMulti, &a.competitive.backgammonSingle, &a.competitive.backgammonMulti, &a.competitive.aceySingle, &a.competitive.aceyMulti, &a.competitive.tabulaSingle, &a.competitive.tabulaMulti)
var autoplay, highlight, pips, moves, flip, advanced, muteJoinLeave, muteChat, muteRoll, muteMove, muteBearOff int
err = tx.QueryRow(context.Background(), "SELECT id, email, username, password, icon, autoplay, highlight, pips, moves, flip, advanced, mutejoinleave, mutechat, muteroll, mutemove, mutebearoff, speed, casual_backgammon_single, casual_backgammon_multi, casual_acey_single, casual_acey_multi, casual_tabula_single, casual_tabula_multi, rated_backgammon_single, rated_backgammon_multi, rated_acey_single, rated_acey_multi, rated_tabula_single, rated_tabula_multi FROM account WHERE username = $1 OR email = $2", bytes.ToLower(bytes.TrimSpace(username)), bytes.ToLower(bytes.TrimSpace(username))).Scan(&a.id, &a.email, &a.username, &a.password, &a.icon, &autoplay, &highlight, &pips, &moves, &flip, &advanced, &muteJoinLeave, &muteChat, &muteRoll, &muteMove, &muteBearOff, &a.speed, &a.casual.backgammonSingle, &a.casual.backgammonMulti, &a.casual.aceySingle, &a.casual.aceyMulti, &a.casual.tabulaSingle, &a.casual.tabulaMulti, &a.competitive.backgammonSingle, &a.competitive.backgammonMulti, &a.competitive.aceySingle, &a.competitive.aceyMulti, &a.competitive.tabulaSingle, &a.competitive.tabulaMulti)
if err != nil {
return nil, nil
} else if len(a.password) == 0 {
@ -437,6 +444,11 @@ func loginAccount(passwordSalt string, username []byte, password []byte) (*accou
a.moves = moves == 1
a.flip = flip == 1
a.advanced = advanced == 1
a.muteJoinLeave = muteJoinLeave == 1
a.muteChat = muteChat == 1
a.muteRoll = muteRoll == 1
a.muteMove = muteMove == 1
a.muteBearOff = muteBearOff == 1
match, err := argon2id.ComparePasswordAndHash(string(password)+passwordSalt, string(a.password))
if err != nil {

View file

@ -353,20 +353,24 @@ func (g *serverGame) addClient(client *serverClient) (spectator bool) {
}
}()
var rating int
var icon int
if client.account != nil {
rating = client.account.casual.getRating(g.Variant, g.Points > 1) / 100
icon = client.account.icon
}
switch {
case g.client1 != nil:
g.client2 = client
g.Player2.Name = string(client.name)
g.Player2.Rating = rating
g.Player2.Icon = icon
client.playerNumber = 2
playerNumber = 2
case g.client2 != nil:
g.client1 = client
g.Player1.Name = string(client.name)
g.Player1.Rating = rating
g.Player1.Icon = icon
client.playerNumber = 1
playerNumber = 1
default:
@ -374,12 +378,14 @@ func (g *serverGame) addClient(client *serverClient) (spectator bool) {
g.client1 = client
g.Player1.Name = string(client.name)
g.Player1.Rating = rating
g.Player1.Icon = icon
client.playerNumber = 1
playerNumber = 1
} else {
g.client2 = client
g.Player2.Name = string(client.name)
g.Player2.Rating = rating
g.Player2.Icon = icon
client.playerNumber = 2
playerNumber = 2
}
@ -435,11 +441,13 @@ func (g *serverGame) removeClient(client *serverClient) {
g.client1 = nil
g.Player1.Name = ""
g.Player1.Rating = 0
g.Player1.Icon = 0
playerNumber = 1
case g.client2 == client:
g.client2 = nil
g.Player2.Name = ""
g.Player2.Rating = 0
g.Player2.Icon = 0
playerNumber = 2
default:
for i, spectator := range g.spectators {
@ -606,6 +614,7 @@ func (g *serverGame) handleWin() bool {
playerBar = bgammon.SpaceBarOpponent
}
// Check for backgammon win.
backgammon := bgammon.PlayerCheckers(g.Board[playerBar], opponent) != 0
if !backgammon {
homeStart, homeEnd := bgammon.HomeRange(g.Winner, g.Variant)
@ -616,6 +625,7 @@ func (g *serverGame) handleWin() bool {
})
}
// Calculate win type and point value.
var winPoints int8
switch g.Variant {
case bgammon.VariantAceyDeucey:
@ -637,8 +647,8 @@ func (g *serverGame) handleWin() bool {
}
}
// Finalize replay.
g.addReplayHeader()
r1, r2, r3 := g.Roll1, g.Roll2, g.Roll3
if r2 > r1 {
r1, r2 = r2, r1
@ -660,6 +670,7 @@ func (g *serverGame) handleWin() bool {
line = append(line, movesFormatted...)
g.replay = append(g.replay, line)
// Create win event.
winEvent := &bgammon.EventWin{
Points: winPoints * g.DoubleValue,
}
@ -682,6 +693,7 @@ func (g *serverGame) handleWin() bool {
}
}
// Record game.
winType := winPoints
if g.Variant != bgammon.VariantBackgammon {
winType = 1
@ -692,21 +704,26 @@ func (g *serverGame) handleWin() bool {
}
if !reset {
// Record match.
err := recordMatchResult(g, matchTypeCasual)
if err != nil {
log.Fatalf("failed to record match result: %s", err)
}
} else {
// Reset game and continue match.
g.Reset()
g.replay = g.replay[:0]
}
// Refresh cached ratings.
if g.client1 != nil && g.client1.account != nil {
g.Player1.Rating = g.client1.account.casual.getRating(g.Variant, g.Points > 1) / 100
}
if g.client2 != nil && g.client2.account != nil {
g.Player2.Rating = g.client2.account.casual.getRating(g.Variant, g.Points > 1) / 100
}
// Send board and win events.
g.eachClient(func(client *serverClient) {
g.sendBoard(client, false)
client.sendEvent(winEvent)

View file

@ -1062,6 +1062,8 @@ COMMANDS:
newGame.Player2.Name = clientGame.Player2.Name
newGame.Player1.Rating = clientGame.Player1.Rating
newGame.Player2.Rating = clientGame.Player2.Rating
newGame.Player1.Icon = clientGame.Player1.Icon
newGame.Player2.Icon = clientGame.Player2.Icon
newGame.allowed1 = clientGame.allowed1
newGame.allowed2 = clientGame.allowed2
s.games = append(s.games, newGame)

View file

@ -7,6 +7,7 @@ type Player struct {
Points int8
Entered bool // Whether all checkers have entered the board. (Acey-deucey)
Inactive int // Inactive time. (Seconds)
Icon int // Profile icon.
}
func NewPlayer(number int8) Player {