DelayNoMore/battle_srv/models/player.go

172 lines
4.2 KiB
Go
Raw Normal View History

2022-09-20 15:50:01 +00:00
package models
import (
2022-11-21 09:27:32 +00:00
. "battle_srv/protos"
"battle_srv/storage"
. "dnmshared"
2022-09-20 15:50:01 +00:00
"fmt"
sq "github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx"
2022-11-21 09:27:32 +00:00
"go.uber.org/zap"
2023-01-29 16:20:43 +00:00
"net"
2022-09-20 15:50:01 +00:00
)
type PlayerBattleState struct {
ADDED_PENDING_BATTLE_COLLIDER_ACK int32
READDED_PENDING_BATTLE_COLLIDER_ACK int32
2022-12-05 04:55:17 +00:00
READDED_BATTLE_COLLIDER_ACKED int32
2022-09-20 15:50:01 +00:00
ACTIVE int32
DISCONNECTED int32
LOST int32
EXPELLED_DURING_GAME int32
EXPELLED_IN_DISMISSAL int32
}
var PlayerBattleStateIns PlayerBattleState
func InitPlayerBattleStateIns() {
PlayerBattleStateIns = PlayerBattleState{
ADDED_PENDING_BATTLE_COLLIDER_ACK: 0,
READDED_PENDING_BATTLE_COLLIDER_ACK: 1,
2022-12-05 04:55:17 +00:00
READDED_BATTLE_COLLIDER_ACKED: 2,
ACTIVE: 3,
DISCONNECTED: 4,
LOST: 5,
EXPELLED_DURING_GAME: 6,
EXPELLED_IN_DISMISSAL: 7,
2022-09-20 15:50:01 +00:00
}
}
type Player struct {
2022-11-21 09:27:32 +00:00
PlayerDownsync
2022-09-20 15:50:01 +00:00
2022-11-09 04:19:29 +00:00
// DB only fields
CreatedAt int64 `db:"created_at"`
UpdatedAt int64 `db:"updated_at"`
DeletedAt NullInt64 `db:"deleted_at"`
TutorialStage int `db:"tutorial_stage"`
2022-11-21 09:27:32 +00:00
// other in-battle info fields
LastConsecutiveRecvInputFrameId int32
LastSentInputFrameId int32
AckingFrameId int32
AckingInputFrameId int32
UdpAddr *PeerUdpAddr
2023-01-29 16:20:43 +00:00
BattleUdpTunnelAddr *net.UDPAddr // This addr is used by backend only, not visible to frontend
BattleUdpTunnelAuthKey int32
2022-09-20 15:50:01 +00:00
}
func ExistPlayerByName(name string) (bool, error) {
return exist("player", sq.Eq{"name": name, "deleted_at": nil})
}
func GetPlayerByName(name string) (*Player, error) {
return getPlayer(sq.Eq{"name": name, "deleted_at": nil})
}
func GetPlayerById(id int) (*Player, error) {
return getPlayer(sq.Eq{"id": id, "deleted_at": nil})
}
func getPlayer(cond sq.Eq) (*Player, error) {
2022-11-21 09:27:32 +00:00
p := Player{}
pd := PlayerDownsync{}
2022-11-21 09:27:32 +00:00
query, args, err := sq.Select("*").From("player").Where(cond).Limit(1).ToSql()
if err != nil {
return nil, err
}
rows, err := storage.MySQLManagerIns.Queryx(query, args...)
2023-02-26 15:33:47 +00:00
if nil != err {
2022-11-21 09:27:32 +00:00
return nil, err
}
cols, err := rows.Columns()
if nil != err {
panic(err)
2022-09-20 15:50:01 +00:00
}
2023-02-26 15:33:47 +00:00
cnt := 0
2022-11-21 09:27:32 +00:00
for rows.Next() {
// TODO: Do it more elegantly, but by now I don't have time to learn reflection of Golang
vals := rowValues(rows, cols)
for i, col := range cols {
val := *vals[i].(*interface{})
if "id" == col {
pd.Id = int32(val.(int64))
}
if "name" == col {
switch v := val.(type) {
case []byte:
pd.Name = string(v)
default:
pd.Name = fmt.Sprintf("%v", v)
}
}
if "created_at" == col {
p.CreatedAt = int64(val.(int64))
}
}
Logger.Debug("Queried player from db", zap.Any("cond", cond), zap.Any("p", p), zap.Any("pd", pd), zap.Any("cols", cols), zap.Any("rowValues", vals))
2023-02-26 15:33:47 +00:00
cnt++
}
if 0 < cnt {
p.PlayerDownsync = pd
return &p, nil
} else {
return nil, nil
2022-09-20 15:50:01 +00:00
}
}
func (p *Player) Insert(tx *sqlx.Tx) error {
result, err := txInsert(tx, "player", []string{"name", "display_name", "created_at", "updated_at", "avatar"},
[]interface{}{p.Name, p.DisplayName, p.CreatedAt, p.UpdatedAt, p.Avatar})
if err != nil {
return err
}
id, err := result.LastInsertId()
if err != nil {
return err
}
p.Id = int32(id)
return nil
}
func Update(tx *sqlx.Tx, id int32, p *Player) (bool, error) {
query, args, err := sq.Update("player").
Set("display_name", p.DisplayName).
Set("avatar", p.Avatar).
Where(sq.Eq{"id": id}).ToSql()
fmt.Println(query)
if err != nil {
return false, err
}
result, err := tx.Exec(query, args...)
if err != nil {
return false, err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return false, err
}
return rowsAffected >= 1, nil
}
func UpdatePlayerTutorialStage(tx *sqlx.Tx, id int) (bool, error) {
query, args, err := sq.Update("player").
Set("tutorial_stage", 1).
Where(sq.Eq{"tutorial_stage": 0, "id": id}).ToSql()
if err != nil {
return false, err
}
result, err := tx.Exec(query, args...)
if err != nil {
return false, err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return false, err
}
return rowsAffected >= 1, nil
}