game_sync/robot/chess/chesstitiansscene.go

126 lines
3.2 KiB
Go

package chess
import (
rule "mongo.games.com/game/gamerule/chess"
proto_chesstitians "mongo.games.com/game/protocol/chesstitians"
"mongo.games.com/game/protocol/player"
"mongo.games.com/game/robot/base"
"mongo.games.com/goserver/core/netlib"
)
type ChesstitiansScene struct {
base.BaseScene
*proto_chesstitians.SCChesstitiansRoomInfo
players map[int32]*ChesstitiansPlayer
chess rule.Logic
}
type filterFunc func(*ChesstitiansScene, *ChesstitiansPlayer, []int32, []int32, []int32, bool) (bool, []int32)
var FilterMgr []filterFunc
func NewChesstitiansScene(info *proto_chesstitians.SCChesstitiansRoomInfo) *ChesstitiansScene {
s := &ChesstitiansScene{
SCChesstitiansRoomInfo: info,
players: make(map[int32]*ChesstitiansPlayer),
}
s.Init()
return s
}
func (s *ChesstitiansScene) Init() {
s.players = make(map[int32]*ChesstitiansPlayer)
for _, mpd := range s.GetPlayers() {
p := NewChesstitiansPlayer(mpd)
if p != nil {
s.AddPlayer(p)
}
}
s.chess = rule.NewChess(int(s.Variant))
s.chess.Init()
s.makeChessStruct(s.chess)
}
func (s *ChesstitiansScene) GetIsAllAi() bool {
var i int
for _, p := range s.players {
if p.IsRobot() {
i++
}
}
return i == 4
}
func (s *ChesstitiansScene) Clear() {
for _, p := range s.players {
p.Clear()
}
}
func (s *ChesstitiansScene) AddPlayer(p base.IPlayer) {
if mp, ok := p.(*ChesstitiansPlayer); ok {
s.players[p.GetSnId()] = mp
}
}
func (s *ChesstitiansScene) DelPlayer(snid int32) {
if p, exist := s.players[snid]; exist && p != nil {
delete(s.players, snid)
}
}
func (s *ChesstitiansScene) GetPlayerByPos(pos int32) base.IPlayer {
return nil
}
func (s *ChesstitiansScene) GetPlayerBySnid(snid int32) base.IPlayer {
if p, exist := s.players[snid]; exist {
return p
}
return nil
}
func (s *ChesstitiansScene) GetPlayerByNotSnid(snid int32) base.IPlayer {
for _, chesstitiansPlayer := range s.players {
if chesstitiansPlayer.GetSnId() != snid {
return chesstitiansPlayer
}
}
return nil
}
func (this *ChesstitiansScene) GetMe(s *netlib.Session) base.IPlayer {
if user, ok := s.GetAttribute(base.SessionAttributeUser).(*player.SCPlayerData); ok {
return this.GetPlayerBySnid(user.GetData().GetSnId())
}
return nil
}
func (this *ChesstitiansScene) GetOp(s *netlib.Session) base.IPlayer {
if user, ok := s.GetAttribute(base.SessionAttributeUser).(*player.SCPlayerData); ok {
return this.GetPlayerByNotSnid(user.GetData().GetSnId())
}
return nil
}
func (s *ChesstitiansScene) IsFull() bool {
return len(s.players) >= int(s.MaxPlayerNum)
}
func (s *ChesstitiansScene) IsMatchScene() bool {
return s.IsMatch == 1 || s.IsMatch == 2 //锦标赛和冠军赛
}
func (this *ChesstitiansScene) Update(ts int64) {
}
func (this *ChesstitiansScene) makeChessStruct(chess rule.Logic) {
chess.Set(this.GetChess(), nil, this.GetAct(), this.GetCastling(), this.GetCambodianMove(), int(this.GetEnpassant()), int(this.GetChessRound()))
}
func (this *ChesstitiansScene) PlayOp(fromIdx int, toIdx int) {
this.chess.Move(fromIdx, toIdx)
this.Chess[fromIdx] = string(this.chess.GetPiece(fromIdx))
this.Chess[toIdx] = string(this.chess.GetPiece(toIdx))
this.chess.NextAct()
this.Act = this.chess.GetAct()
this.Round = int32(this.chess.GetRound())
this.CambodianMove = this.chess.GetCambodianMove()
}