package room

import (
	"game/common/baseroom"
	"game/common/proto/pb"
	"time"
)

func (rm *ColorRoom) gameStart() {
	rm.setStatus(pb.ColorGameStatus_CGS_Start)
	rm.resetGameData()
	rm.notifyGameStart()
	rm.NewTimer(TtStartBetting, time.Duration(rm.timingCfg.Start)*time.Millisecond)
}

func (rm *ColorRoom) gameStartBetting() {
	rm.setStatus(pb.ColorGameStatus_CGS_Betting)
	rm.notifyBetting()
	rm.NewTimer(TtEndBet, time.Duration(rm.timingCfg.Betting)*time.Millisecond)
	rm.NewTimer(TtSecond, time.Second)
}

func (rm *ColorRoom) gameEndBetting() {
	rm.setStatus(pb.ColorGameStatus_CGS_BetEnd)
	rm.endBetPayoutUser()
	rm.updateEndBetAreaMul()
	rm.notifyEndBetting()
	rm.NewTimer(TtOpenThreeDices, time.Duration(rm.timingCfg.EndBetting)*time.Millisecond)
}

func (rm *ColorRoom) gameOpenThreeDices() {
	rm.setStatus(pb.ColorGameStatus_CGS_OpenThreeDice)
	rm.openDices()
	rm.notifyOpenThreeDice()
	rm.NewTimer(TtSettle, time.Duration(rm.timingCfg.OpenThreeDice)*time.Millisecond)
}

func (rm *ColorRoom) gameSettle() {
	rm.setStatus(pb.ColorGameStatus_CGS_Settle)
	rm.settle()
	rm.notifySettle()
	rm.updateGameTrend()
	rm.notifyTrend()
	rm.NewTimer(TtGameStart, time.Duration(rm.timingCfg.Settle)*time.Millisecond)
}

// 每秒定时器干的活
func (rm *ColorRoom) onSecond() {
	rm.notifyColorBetAreaInfo()
	if rm.status < pb.ColorGameStatus_CGS_BetEnd {
		rm.NewTimer(TtSecond, time.Second)
	}
}

// 踢出不投注玩家，清空座位及从玩家管理器中删除
func (rm *ColorRoom) kickoutUsers() {
	rm.RangePlayer(func(u baseroom.IPlayer) bool {
		user := GetPlayer(u)
		if user.notBetCount >= rm.roomCfg.NoBetCountMax {
			rm.leaveRoom(user)
			rm.notifyKickoutUser(user, pb.ErrCode_NotBetCount)
		}
		return true
	})
}
