327 lines
7.2 KiB
Go
327 lines
7.2 KiB
Go
package player
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/tomas-qstarrs/boost/dogfish"
|
|
"github.com/tomas-qstarrs/boost/timex"
|
|
"mongo.games.com/game/gamesrv/slotspkg/internal/generic/errors"
|
|
"mongo.games.com/game/gamesrv/slotspkg/internal/generic/global"
|
|
"mongo.games.com/game/gamesrv/slotspkg/internal/generic/key"
|
|
"mongo.games.com/game/gamesrv/slotspkg/internal/module/session"
|
|
"mongo.games.com/game/gamesrv/slotspkg/internal/module/shared"
|
|
"mongo.games.com/goserver/core/logger"
|
|
)
|
|
|
|
// Get gets player from session
|
|
func Get(s *session.Session) *Player {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
v := s.Value(key.SessionPlayer)
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
return v.(*Player)
|
|
}
|
|
|
|
func Set(s *session.Session, p *Player) {
|
|
s.Set(key.SessionPlayer, p)
|
|
}
|
|
func GetOldPlayer(s *session.Session) *Player {
|
|
p := Get(s)
|
|
if p == nil {
|
|
hash := db.PullPlayer(s.UID())
|
|
if hash == nil {
|
|
return nil
|
|
}
|
|
}
|
|
return PullPlayer(s)
|
|
}
|
|
func PullPlayer(s *session.Session) *Player {
|
|
p := Get(s)
|
|
if p == nil {
|
|
hash := db.PullPlayer(s.UID())
|
|
|
|
// Create a new player from hash strings
|
|
p = &Player{}
|
|
err := p.Load(p, hash)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
Set(s, p)
|
|
|
|
return p
|
|
}
|
|
|
|
if s.Bool(key.SessionReloadPlayer) {
|
|
s.Set(key.SessionReloadPlayer, false)
|
|
hash := db.PullPlayer(s.UID())
|
|
|
|
// Reload old player from hash strings
|
|
err := p.Load(p, hash)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return p
|
|
}
|
|
|
|
return p
|
|
}
|
|
|
|
func PushPlayer(s *session.Session) {
|
|
p := Get(s)
|
|
|
|
hash, err := p.Dump()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
db.PushPlayer(s.UID(), hash)
|
|
}
|
|
|
|
func Init(s *session.Session) {
|
|
p := Get(s)
|
|
if p == nil {
|
|
panic(errors.UIDInvalid.Error())
|
|
}
|
|
|
|
p.Init(s)
|
|
}
|
|
|
|
func (p *Player) Init(s *session.Session) {
|
|
if p.UID.Get() == 0 {
|
|
p.handleCreate(s)
|
|
}
|
|
|
|
p.handleLogin(s)
|
|
|
|
CoinCenter.Sync(s)
|
|
|
|
}
|
|
|
|
func (p *Player) handleCreate(s *session.Session) {
|
|
uid := s.UID()
|
|
if uid == 0 {
|
|
panic(errors.UIDInvalid.Error())
|
|
}
|
|
|
|
p.UID.Set(int64(uid))
|
|
p.Char.CreateTime.Set(timex.Now().Unix())
|
|
}
|
|
func (p *Player) MarkBlack(flag int64) {
|
|
f := p.Cli.Black.Get()
|
|
f |= flag
|
|
p.Cli.Black.Set(f)
|
|
}
|
|
func (p *Player) UnmarkBlack(flag int64) {
|
|
f := p.Cli.Black.Get()
|
|
f &= ^flag
|
|
p.Cli.Black.Set(f)
|
|
}
|
|
func (p *Player) IsMarkBlack(flag int64) bool {
|
|
if (p.Cli.Black.Get() & flag) != 0 {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
func (p *Player) CheckBlack() bool {
|
|
for i := key.BlackMin; i <= key.BlackMax; i <<= 1 {
|
|
if p.IsMarkBlack(i) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
func (p *Player) handleLogin(s *session.Session) {
|
|
p.updateCli(s)
|
|
p.updateChar(s)
|
|
p.updateOthers(s)
|
|
}
|
|
func UpdateToken(s *session.Session, token string) {
|
|
if token == "" {
|
|
return
|
|
}
|
|
p := Get(s)
|
|
if p == nil {
|
|
panic(errors.UIDInvalid.Error())
|
|
}
|
|
logger.Logger.Infof("player UpdateToken %v-%v", s.UID(), token)
|
|
p.Cli.Token.Set(token)
|
|
}
|
|
func GetToken(s *session.Session) string {
|
|
p := Get(s)
|
|
if p == nil {
|
|
panic(errors.UIDInvalid.Error())
|
|
}
|
|
return p.Cli.Token.Get()
|
|
}
|
|
func GetOps(s *session.Session) string {
|
|
p := Get(s)
|
|
if p == nil {
|
|
panic(errors.UIDInvalid.Error())
|
|
}
|
|
return p.Cli.Ops.Get()
|
|
}
|
|
func GetIp(s *session.Session) string {
|
|
p := Get(s)
|
|
if p == nil {
|
|
panic(errors.UIDInvalid.Error())
|
|
}
|
|
return p.Cli.Ip.Get()
|
|
}
|
|
func (p *Player) updateCli(s *session.Session) {
|
|
sessionContext := s.Value(key.SessionContext).(*shared.SessionContext)
|
|
// Cli
|
|
p.Cli.ThirdName.Set(sessionContext.ThirdName)
|
|
p.Cli.Language.Set(sessionContext.Language)
|
|
p.Cli.Ops.Set(sessionContext.Ops)
|
|
p.Cli.IsSimulator.Set(sessionContext.IsSimulator)
|
|
p.Cli.IsSimulator.Set(sessionContext.IsSimulator)
|
|
p.Cli.Ip.Set(sessionContext.Ip)
|
|
|
|
p.Cli.Currency.Set(sessionContext.Currency)
|
|
p.Cli.TokenThird.Set(sessionContext.TokenThird)
|
|
p.Cli.PlayerName.Set(sessionContext.PlayerName)
|
|
p.Cli.Lobby.Set(sessionContext.Lobby)
|
|
p.Cli.Game.Set(sessionContext.Game)
|
|
}
|
|
|
|
func (p *Player) updateChar(s *session.Session) {
|
|
DataSet(s).Update()
|
|
|
|
p.Char.LastLoginTime.Set(p.Char.LoginTime.Get())
|
|
p.Char.LoginTime.Set(timex.Now().Unix())
|
|
p.Char.LastProcessTime.Set(p.Char.ProcessTime.Get())
|
|
p.Char.ProcessTime.Set(global.ProcessTime)
|
|
if !timex.IsSameDay(p.Char.LoginTime.Get(), p.Char.LastLoginTime.Get()) {
|
|
p.Char.ActiveDays.Set(p.Char.ActiveDays.Get() + 1)
|
|
}
|
|
|
|
var category string
|
|
if p.Char.Group.Get() == "" {
|
|
category = p.Char.BranchName.Get()
|
|
} else {
|
|
category = fmt.Sprintf("%s_%s", p.Char.BranchName.Get(), p.Char.Group.Get())
|
|
}
|
|
p.Char.Category.Set(category)
|
|
DataSet(s).Update()
|
|
}
|
|
|
|
func (p *Player) updateOthers(s *session.Session) {
|
|
if !timex.IsSameDay(p.Char.LoginTime.Get(), p.Char.LastLoginTime.Get()) {
|
|
p.Agg.SlotsDailyBet.Set(0)
|
|
p.Agg.SlotsDailyWin.Set(0)
|
|
p.Agg.SlotsDailySpins.Set(0)
|
|
}
|
|
}
|
|
|
|
func (p *Player) GetTheme(theme string) *dogfish.JSON {
|
|
return (*dogfish.JSON)(p.Field("Slots", theme))
|
|
}
|
|
|
|
func (p *Player) GetCli() *shared.Cli {
|
|
return &shared.Cli{
|
|
ThirdName: p.Cli.ThirdName.Get(),
|
|
Language: p.Cli.Language.Get(),
|
|
}
|
|
}
|
|
|
|
func GetChar(s *session.Session) *shared.Char {
|
|
p := Get(s)
|
|
if p == nil {
|
|
panic(errors.UIDInvalid.Error())
|
|
}
|
|
|
|
return p.GetChar()
|
|
}
|
|
|
|
func (p *Player) GetChar() *shared.Char {
|
|
return &shared.Char{
|
|
Category: p.Char.Category.Get(),
|
|
Group: p.Char.Group.Get(),
|
|
GroupBatch: p.Char.GroupBatch.Get(),
|
|
CreateTime: p.Char.CreateTime.Get(),
|
|
LoginTime: p.Char.LoginTime.Get(),
|
|
LastLoginTime: p.Char.LastLoginTime.Get(),
|
|
ProcessTime: p.Char.ProcessTime.Get(),
|
|
LastProcessTime: p.Char.LastProcessTime.Get(),
|
|
ActiveDays: p.Char.ActiveDays.Get(),
|
|
Branch: p.Char.Branch.Get(),
|
|
BranchName: p.Char.BranchName.Get(),
|
|
}
|
|
}
|
|
|
|
func GetBook(s *session.Session) *shared.Book {
|
|
p := Get(s)
|
|
if p == nil {
|
|
panic(errors.UIDInvalid.Error())
|
|
}
|
|
|
|
return p.GetBook()
|
|
}
|
|
|
|
func (p *Player) GetBook() *shared.Book {
|
|
return &shared.Book{
|
|
Coin: p.Book.Coin.Get(),
|
|
}
|
|
}
|
|
|
|
func (p *Player) GetAgg() *shared.Agg {
|
|
return &shared.Agg{
|
|
SlotsDailySpins: p.Agg.SlotsDailySpins.Get(),
|
|
SlotsDailyWin: p.Agg.SlotsDailyWin.Get(),
|
|
SlotsDailyBet: p.Agg.SlotsDailyBet.Get(),
|
|
PersonalPool: p.Agg.PersonalPool.Get(),
|
|
}
|
|
}
|
|
|
|
func (p *Player) Coin() int64 {
|
|
return p.Book.Coin.Get()
|
|
}
|
|
|
|
func (p *Player) QueryPermission(keyType string, info ...any) bool {
|
|
return db.QueryPermission(keyType, info...)
|
|
}
|
|
|
|
func CategoryName(s *session.Session) string {
|
|
p := Get(s)
|
|
if p == nil {
|
|
return key.Base
|
|
}
|
|
return p.CategoryName()
|
|
}
|
|
|
|
func (p *Player) CategoryName() string {
|
|
return p.Char.Category.Get()
|
|
}
|
|
|
|
func BetWin(s *session.Session, betCoin DecCoin, winCoin IncCoin) {
|
|
p := Get(s)
|
|
if p == nil {
|
|
panic(errors.UIDInvalid.Error())
|
|
}
|
|
p.BetWin(s, betCoin, winCoin)
|
|
}
|
|
|
|
func (p *Player) BetWin(s *session.Session, betCoin DecCoin, winCoin IncCoin) {
|
|
if betCoin.GetCoin() > 0 {
|
|
//总统计
|
|
p.Agg.BetSum.Set(p.Agg.BetSum.Get() + betCoin.GetCoin())
|
|
p.Agg.BetTimes.Set(p.Agg.BetTimes.Get() + 1)
|
|
//p.Agg.PersonalPool.Set(p.Agg.PersonalPool.Get() + int64(float64(betCoin.GetCoin())*0.96))
|
|
|
|
//每日
|
|
p.Agg.BetTimesDaily.Set(p.Agg.BetTimesDaily.Get() + 1)
|
|
p.Agg.BetSumDaily.Set(p.Agg.BetSumDaily.Get() + betCoin.GetCoin())
|
|
}
|
|
if winCoin.GetCoin() > 0 {
|
|
//总统计
|
|
p.Agg.WinSum.Set(p.Agg.WinSum.Get() + winCoin.GetCoin())
|
|
//p.Agg.PersonalPool.Set(p.Agg.PersonalPool.Get() - winCoin.GetCoin())
|
|
//每日
|
|
p.Agg.WinSumDaily.Set(p.Agg.WinSumDaily.Get() + winCoin.GetCoin())
|
|
}
|
|
}
|