package thirteen

import (
	"mongo.games.com/game/common"
	protoplayer "mongo.games.com/game/protocol/player"
	"mongo.games.com/game/protocol/thirteen"
	"mongo.games.com/game/robot/base"
	"mongo.games.com/goserver/core/netlib"
)

type ThirteenWaterScene struct {
	base.BaseScene
	*thirteen.SCThirteenRoomInfo
	seats   [8]*ThirteenWaterPlayer
	players map[int32]*ThirteenWaterPlayer
}

func NewThirteenWaterScene(info *thirteen.SCThirteenRoomInfo) *ThirteenWaterScene {
	s := &ThirteenWaterScene{
		SCThirteenRoomInfo: info,
		players:            make(map[int32]*ThirteenWaterPlayer),
	}
	s.Init()
	return s
}

func (s *ThirteenWaterScene) Init() {
	for _, mpd := range s.GetPlayers() {
		p := NewThirteenWaterPlayer(mpd)
		if p != nil {
			s.AddPlayer(p)
		}
	}
}
func (s *ThirteenWaterScene) Clear() {
	for _, player := range s.players {
		player.Clear()
	}
}

func (s *ThirteenWaterScene) AddPlayer(p base.IPlayer) {
	if mp, ok := p.(*ThirteenWaterPlayer); ok {
		s.players[p.GetSnId()] = mp
		s.seats[p.GetPos()] = mp
	}
}

func (s *ThirteenWaterScene) DelPlayer(snid int32) {
	if p, exist := s.players[snid]; exist && p != nil {
		delete(s.players, snid)
		s.seats[p.GetPos()] = nil
	}
}

func (s *ThirteenWaterScene) GetPlayerByPos(pos int32) base.IPlayer {
	if pos >= 0 && pos < 8 {
		return s.seats[pos]
	}
	return nil
}

func (s *ThirteenWaterScene) GetPlayerBySnid(snid int32) base.IPlayer {
	if p, exist := s.players[snid]; exist {
		return p
	}
	return nil
}

func (this *ThirteenWaterScene) GetMe(s *netlib.Session) base.IPlayer {
	if user, ok := s.GetAttribute(base.SessionAttributeUser).(*protoplayer.SCPlayerData); ok {
		player := this.GetPlayerBySnid(user.GetData().GetSnId())
		return player
	}
	return nil
}

func (s *ThirteenWaterScene) IsFull() bool {
	return len(s.players) >= 4
}

func (s *ThirteenWaterScene) IsMatchScene() bool {
	return s.GetRoomId() >= common.MatchSceneStartId
}

func (s *ThirteenWaterScene) Update(ts int64) {

}

/////////////////////////////////////////////////////////////////////////////////////////////////////
