| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054 |
- package gamelogic
- import (
- "encoding/json"
- "math/rand"
- "sort"
- "time"
- robotmanager "bet24.com/servers/insecureframe/robot"
- waterpool "bet24.com/servers/micros/waterpool/proto"
- "bet24.com/servers/user"
- )
- func (ts *tablesink) checkAndStartGame() {
- if ts.gameScene.Phase != Phase_Free &&
- ts.gameScene.Phase != Phase_End {
- return
- }
- // 查看是否所有人都ready
- readyCount := 0
- for i := 0; i < CHAIR_COUNT; i++ {
- usr := ts.table.GetUserByChair(i)
- if usr == nil {
- continue
- }
- userStatus := usr.GetUserStatus()
- // 旁观不判断
- if userStatus <= user.UserStatus_Free ||
- userStatus == user.UserStatus_Watch {
- continue
- }
- if usr.GetUserStatus() != user.UserStatus_Ready && userStatus != user.UserStatus_Offline {
- ts.table.LogWithTableId("-----checkAndStartGame: chair[%d]: %d, status: %d", i, usr.GetUserId(), usr.GetUserStatus())
- return
- }
- readyCount++
- }
- ts.table.LogWithTableId("-----checkAndStartGame : %d ", readyCount)
- if readyCount >= CHAIR_COUNT {
- ts.startGame()
- }
- }
- func (ts *tablesink) checkIsNeedRobot() {
- if ts.gameScene.Phase != Phase_Free && ts.gameScene.Phase != Phase_End {
- return
- }
- if ts.roomInfo.RobotCount == 0 {
- return
- }
- if ts.table.IsPrivate() {
- return
- }
- count := 0
- for i := 0; i < CHAIR_COUNT; i++ {
- usr := ts.table.GetUserByChair(i)
- if usr == nil {
- continue
- }
- if usr.GetUserStatus() <= user.UserStatus_Free {
- continue
- }
- count++
- }
- chairCount := CHAIR_COUNT
- if count >= chairCount {
- return
- }
- robotCount := chairCount - count
- ts.table.LogWithTableId("-------checkIsNeedRobot. need: %d ------", robotCount)
- if robotCount > 0 {
- time.AfterFunc(1000*time.Millisecond, func() {
- if ts.gameScene.Phase != Phase_Free && ts.gameScene.Phase != Phase_End {
- return
- }
- tableID := ts.table.GetTableID()
- robotmanager.GetOneRobotEnterTable(tableID, ts.roomInfo.MinGold, ts.roomInfo.MaxGold)
- })
- }
- }
- func (ts *tablesink) removeOneRobot() {
- if ts.gameScene.Phase != Phase_End && ts.gameScene.Phase != Phase_Free {
- return
- }
- for i := 0; i < CHAIR_COUNT; i++ {
- usr := ts.table.GetUserByChair(i)
- if usr == nil {
- continue
- }
- if !usr.IsRobot() {
- continue
- }
- ts.table.KickUserByChair(i, false)
- }
- }
- func (ts *tablesink) checkUserReadyStatus(chair int) {
- if ts.gameScene.Phase != Phase_Free && ts.gameScene.Phase != Phase_End {
- ts.table.LogWithTableId("tablesink.checkUserReadyStatus status not right %d", ts.gameScene.Phase)
- return
- }
- usr := ts.table.GetUserByChair(chair)
- if usr == nil {
- ts.table.LogWithTableId("tablesink.checkUserReadyStatus user not exist %d", chair)
- return
- }
- if ts.gameScene.Phase == Phase_End {
- ts.table.SetUserReadyStatus(usr.GetUserIndex(), true)
- return
- }
- if !usr.IsRobot() {
- ts.table.LogWithTableId("tablesink.checkUserReadyStatus kicking [%d]: %d ", chair, usr.GetUserId())
- ts.table.UserWatch(usr.GetUserIndex())
- return
- }
- //桌上还有没有真人,有就准备,没有就踢走
- count := 0
- for i := 0; i < CHAIR_COUNT; i++ {
- u := ts.table.GetUserByChair(i)
- if u == nil {
- continue
- }
- if u.IsRobot() {
- continue
- }
- if u.GetUserStatus() <= user.UserStatus_Free {
- continue
- }
- count++
- }
- if count >= 1 {
- ts.table.SetUserReadyStatus(usr.GetUserIndex(), true)
- } else {
- ts.table.LogWithTableId("tablesink.checkUserReadyStatus: %d ", usr.GetUserId())
- if usr.IsRobot() {
- ts.table.KickUser(usr.GetUserIndex(), false)
- } else {
- ts.table.UserWatch(usr.GetUserIndex())
- }
- }
- }
- func (ts *tablesink) getSecond(phase int) int {
- ret := 0
- switch phase {
- case Phase_Free:
- ret = ts.roomInfo.SecFree
- case Phase_JackPot:
- ret = ts.roomInfo.SecJackPot
- case Phase_SendCard:
- ret = ts.roomInfo.SecSendCard
- case Phase_RobBanker:
- ret = ts.roomInfo.SecRobBanker
- case Phase_ConfirmBanker:
- ret = ts.roomInfo.SecConfirmBanker
- case Phase_Action:
- ret = ts.roomInfo.SecAction
- case Phase_CompareCard:
- ret = ts.roomInfo.SecCompareCard
- case Phase_RoundEnd:
- ret = ts.roomInfo.SecRoundEnd
- case Phase_End:
- ret = ts.roomInfo.SecEnd
- }
- if ret < 100 {
- ret *= 1000
- }
- return ret
- }
- func (ts *tablesink) recvCancleAutoMsg(userIndex int32, data string) {
- usr := ts.table.GetPlayer(userIndex)
- if usr == nil {
- ts.table.LogWithTableId("tablesink.recvCancleAutoMsg,user[%d] not exist", userIndex)
- return
- }
- chairId := usr.GetUserChairId()
- if chairId == -1 {
- return
- }
- if !ts.gameScene.Players[chairId].AutoOut {
- return
- }
- ts.gameScene.Players[chairId].AutoOut = false
- // 重启倒计时
- if ts.gameScene.WhoseTurn != chairId {
- return
- }
- if ts.gameScene.Phase == Phase_RobBanker || ts.gameScene.Phase == Phase_Action {
- ts.table.SetTimer(TIMER_GAME, ts.getSecond(ts.gameScene.Phase))
- }
- }
- func (ts *tablesink) recvAutoMsg(userIndex int32, data string) {
- usr := ts.table.GetPlayer(userIndex)
- if usr == nil {
- ts.table.LogWithTableId("tablesink.recvAutoMsg,user[%d] not exist", userIndex)
- return
- }
- chairId := usr.GetUserChairId()
- if chairId == -1 {
- return
- }
- ts.gameScene.Players[chairId].AutoOut = true
- // 重启倒计时
- if ts.gameScene.WhoseTurn != chairId {
- return
- }
- if ts.gameScene.Phase == Phase_RobBanker || ts.gameScene.Phase == Phase_Action {
- ts.table.SetTimer(TIMER_GAME, SEC_AUTO)
- }
- }
- func (ts *tablesink) killAutoTimer() {
- for i := 0; i < CHAIR_COUNT; i++ {
- ts.table.KillTimer(TIMER_AUTO_ACTION_0 + i)
- }
- }
- func (ts *tablesink) canStartGame(needCost bool) bool {
- count := 0
- for i := 0; i < CHAIR_COUNT; i++ {
- usr := ts.table.GetUserByChair(i)
- if usr == nil {
- break
- }
- userStatus := usr.GetUserStatus()
- if userStatus != user.UserStatus_Ready && userStatus != user.UserStatus_Offline {
- ts.table.LogWithTableId("ts.startGame chair[%d] userStatue = %d", i, userStatus)
- break
- }
- if !needCost {
- count++
- continue
- }
- userGold := ts.table.GetUserChipOrGoldByUser(usr)
- if userGold < ts.roomInfo.MinGold || (ts.roomInfo.MaxGold > 0 && userGold > ts.roomInfo.MaxGold) {
- ts.table.LogWithTableId("tablesink.startGame %d 金币不足,站起", usr.GetUserId())
- if usr.IsRobot() {
- go ts.table.KickUser(usr.GetUserIndex(), false)
- } else {
- go ts.table.UserWatch(usr.GetUserIndex())
- }
- break
- }
- // 先投注
- ok := ts.writeScore(usr.GetUserId(), -ts.roomInfo.BaseScore, 0, 0, ScoreType_Bet, ts.roomInfo.RoomName, usr.IsRobot())
- if !ok {
- break
- }
- ts.gameScene.Players[i].initData(usr.GetUserId(), ts.roomInfo.InitScore)
- ts.gameScene.Players[i].IsValid = true
- ts.gameScene.Players[i].userID = usr.GetUserId()
- ts.gameScene.Players[i].isRobot = usr.IsRobot()
- count++
- }
- if count < CHAIR_COUNT {
- ts.table.LogWithTableId("游戏开始,用户不足")
- if !needCost {
- return false
- }
- ts.gameScene.Phase = Phase_End
- ts.gameScene.PhaseIndex = 0
- // 分数还原
- for i := 0; i < CHAIR_COUNT; i++ {
- if !ts.gameScene.Players[i].IsValid {
- continue
- }
- ts.writeScore(ts.gameScene.Players[i].userID, ts.roomInfo.BaseScore, 0, 0,
- ScoreType_Return, ts.roomInfo.RoomName, ts.gameScene.Players[i].isRobot)
- }
- if ts.table.IsPrivate() {
- ts.table.PrivateRoomSetWinners([]int{})
- }
- ts.endGame()
- ts.gameScene.initData(ts.table.GetTableID())
- return false
- }
- return true
- }
- func (ts *tablesink) startGame() {
- ts.gameScene.initData(ts.table.GetTableID())
- if ts.canStartGame(true) {
- ts.table.SetTimer(TIMER_START_GAME, SEC_DELAY)
- }
- }
- func (ts *tablesink) checkStartGame() {
- if !ts.canStartGame(false) {
- return
- }
- ts.startJackPot()
- ts.table.StartGame()
- ts.table.LogWithTableId("tablesink.gameStart[%d]", ts.table.GetTableID())
- }
- func (ts *tablesink) startJackPot() {
- value := waterpool.GetInventoryValue(GAMEID, waterpool.RoomType_Normal, ts.roomInfo.RoomName)
- ts.gameScene.Phase = Phase_JackPot
- ts.gameScene.JackPotAmount = ts.roomInfo.GetJackpotAmount(value)
- ts.gameScene.dump()
- ts.resetGameTimer()
- }
- func (ts *tablesink) gameStart() {
- // 发牌
- ts.logic.shuffle()
- for i := 0; i < CHAIR_COUNT; i++ {
- usr := ts.table.GetUserByChair(i)
- if usr == nil {
- continue
- }
- userStatus := usr.GetUserStatus()
- if userStatus <= user.UserStatus_Free ||
- userStatus == user.UserStatus_Watch {
- continue
- }
- if !ts.gameScene.Players[i].IsValid || ts.gameScene.Players[i].Score <= 0 {
- continue
- }
- ts.gameScene.Players[i].Status = Player_Status_Play
- ts.gameScene.Players[i].HandCards = ts.logic.getHandCards()
- sort.Slice(ts.gameScene.Players[i].HandCards, func(m, n int) bool {
- if getCardValue(ts.gameScene.Players[i].HandCards[m]) > getCardValue(ts.gameScene.Players[i].HandCards[n]) {
- return true
- } else if getCardValue(ts.gameScene.Players[i].HandCards[m]) == getCardValue(ts.gameScene.Players[i].HandCards[n]) {
- return getCardType(ts.gameScene.Players[i].HandCards[m]) > getCardType(ts.gameScene.Players[i].HandCards[n])
- }
- return false
- })
- ts.gameScene.Players[i].CardType = getHandCardType(ts.gameScene.Players[i].HandCards)
- }
- ts.gameScene.Phase = Phase_SendCard
- ts.gameScene.PhaseIndex = 0
- ts.gameScene.LastTurn = -1
- ts.gameScene.Index++
- ts.LastOpraTime = time.Now()
- ts.table.NotifySceneChanged(-1)
- ts.resetGameTimer()
- }
- func (ts *tablesink) dealGamePlay() {
- ts.table.LogWithTableId("------dealGamePlay -----")
- ts.gameScene.Phase = Phase_RobBanker
- ts.gameScene.PhaseIndex = 0
- ts.gameScene.WhoseTurn = ts.gameScene.FirstActionPlayer
- ts.gameScene.CurrentMinRobScore = ts.roomInfo.MinRobBankerScore
- ts.gameScene.Index++
- ts.LastOpraTime = time.Now()
- ts.table.NotifySceneChanged(-1)
- ts.resetGameTimer()
- }
- func (ts *tablesink) dealConfirmBanker() {
- ts.table.LogWithTableId("------dealConfirmBanker -----")
- ts.gameScene.PhaseIndex = 0
- ts.gameScene.confirmBanker()
- ts.gameScene.Index++
- ts.LastOpraTime = time.Now()
- ts.table.NotifySceneChanged(-1)
- ts.table.SetTimer(TIMER_START_ACTION, SEC_START_ACTION)
- }
- func (ts *tablesink) dealStartAction() {
- ts.gameScene.Phase = Phase_Action
- ts.gameScene.PhaseIndex = 0
- ts.gameScene.ActionPhase = ActionPhase_Banker_GiveMoney
- ts.gameScene.CurrentActionPlayer = -1
- ts.gameScene.LastTurn = -1
- ts.gameScene.Index++
- ts.LastOpraTime = time.Now()
- ts.table.NotifySceneChanged(-1)
- ts.resetGameTimer()
- }
- func (ts *tablesink) dealCompareCard() {
- ts.gameScene.PhaseIndex = 0
- ts.gameScene.Index++
- ts.gameScene.compareCard()
- ts.gameScene.roundEnd()
- ts.gameScene.isNeedCalc = false
- ts.LastOpraTime = time.Now()
- ts.table.NotifySceneChanged(-1)
- ts.table.SetTimer(TIMER_ROUND_END, SEC_COMPARE_TO_ROUND_END)
- }
- func (ts *tablesink) dealRoundEnd() {
- ts.gameScene.Phase = Phase_RoundEnd
- if ts.gameScene.isNeedCalc {
- ts.gameScene.roundEnd()
- }
- ts.gameScene.PhaseIndex = 0
- ts.gameScene.Index++
- ts.LastOpraTime = time.Now()
- ts.table.NotifySceneChanged(-1)
- ts.resetGameTimer()
- for i := 0; i < CHAIR_COUNT; i++ {
- if !ts.gameScene.Players[i].IsValid {
- continue
- }
- if ts.gameScene.Players[i].ChangeScore > 0 && ts.roomInfo.LevelParam >= 100 {
- value := ts.roomInfo.LevelParam * ts.gameScene.Players[i].ChangeScore / 10000
- value = ts.table.AddExperience(ts.gameScene.Players[i].userID, value)
- ts.gameScene.Players[i].Experience += value
- }
- if ts.gameScene.Players[i].Score == 0 {
- usr := ts.table.GetPlayerByUserId(ts.gameScene.Players[i].userID)
- usr.SetUserStatus(Player_Status_Free)
- ts.table.SetTimer(TIMER_KICK_USER_0+i, SEC_KICK_USER)
- }
- }
- }
- func (ts *tablesink) dealGameTimeOut() {
- if ts.gameScene.Phase == Phase_End {
- return
- }
- switch ts.gameScene.Phase {
- case Phase_JackPot:
- first := rand.Intn(CHAIR_COUNT)
- ts.gameScene.gameInit(first)
- ts.gameStart()
- return
- case Phase_SendCard:
- ts.dealGamePlay()
- return
- case Phase_RobBanker:
- if !isValidChair(ts.gameScene.WhoseTurn) {
- ts.table.LogWithTableId("tablesink.dealGameTimeOut invalid whoseturn")
- return
- }
- if !ts.gameScene.Players[ts.gameScene.WhoseTurn].isRobot {
- ts.gameScene.Players[ts.gameScene.WhoseTurn].AutoOut = true
- }
- ts.dealRobBanker(ts.gameScene.WhoseTurn, 0)
- return
- case Phase_ConfirmBanker:
- ts.dealConfirmBanker()
- return
- case Phase_Action:
- if !isValidChair(ts.gameScene.WhoseTurn) {
- ts.table.LogWithTableId("tablesink.dealGameTimeOut invalid whoseturn")
- return
- }
- if !ts.gameScene.Players[ts.gameScene.WhoseTurn].isRobot {
- ts.gameScene.Players[ts.gameScene.WhoseTurn].AutoOut = true
- }
- action := 0
- switch ts.gameScene.ActionPhase {
- case ActionPhase_Banker_GiveMoney:
- action = Action_Banker_CompareCardDirect
- case ActionPhase_Player_Reply:
- action = Action_Player_CompareCard
- case ActionPhase_Banker_Reply:
- action = Action_Banker_DisAgree
- }
- ts.dealAction(ts.gameScene.WhoseTurn, action, 0)
- return
- case Phase_CompareCard:
- ts.dealCompareCard()
- return
- case Phase_RoundEnd:
- if ts.gameScene.nextGame(ts.roomInfo.WinScore) {
- if len(ts.gameScene.RoundWinner) > 0 {
- ts.gameScene.gameInit(ts.gameScene.RoundWinner[0])
- } else {
- if ts.gameScene.FirstActionPlayer == -1 {
- first := rand.Intn(CHAIR_COUNT)
- ts.gameScene.gameInit(first)
- } else {
- ts.gameScene.gameInit(getPreviousChair(ts.gameScene.FirstActionPlayer))
- }
- }
- ts.gameStart()
- } else {
- ts.enterEndPhase()
- }
- return
- }
- ts.table.LogWithTableId("tablesink.dealGameTimeOut unreachable")
- }
- func (ts *tablesink) resetGameTimer() {
- ts.killAutoTimer()
- switch ts.gameScene.Phase {
- case Phase_JackPot:
- fallthrough
- case Phase_SendCard:
- fallthrough
- case Phase_ConfirmBanker:
- fallthrough
- case Phase_CompareCard:
- fallthrough
- case Phase_RoundEnd:
- ts.table.SetTimer(TIMER_GAME, ts.getSecond(ts.gameScene.Phase))
- default:
- if !isValidChair(ts.gameScene.WhoseTurn) {
- return
- }
- chairId := ts.gameScene.WhoseTurn
- usr := ts.table.GetUserByChair(chairId)
- if usr == nil {
- ts.table.LogWithTableId("resetGameTimer Phase:%d ChairId:%d nnot exist", ts.gameScene.Phase, chairId)
- ts.gameScene.Phase = Phase_End
- ts.gameScene.PhaseIndex = 0
- // 分数还原
- for i := 0; i < CHAIR_COUNT; i++ {
- if !ts.gameScene.Players[i].IsValid {
- continue
- }
- ts.writeScore(ts.gameScene.Players[i].userID, ts.roomInfo.BaseScore, 0, 0,
- ScoreType_Return, ts.roomInfo.RoomName, ts.gameScene.Players[i].isRobot)
- }
- if ts.table.IsPrivate() {
- ts.table.PrivateRoomSetWinners([]int{})
- }
- ts.endGame()
- ts.gameScene.initData(ts.table.GetTableID())
- return
- }
- if (ts.gameScene.Players[chairId].AutoOut || usr.GetUserStatus() == user.UserStatus_Offline) && !usr.IsRobot() {
- ts.table.SetTimer(TIMER_GAME, SEC_AUTO)
- } else {
- ts.table.SetTimer(TIMER_GAME, ts.getSecond(ts.gameScene.Phase))
- }
- }
- ts.checkRobotAction()
- }
- func (ts *tablesink) recvAction(userIndex int32, data string) bool {
- usr := ts.table.GetPlayer(userIndex)
- if usr == nil {
- ts.table.LogWithTableId("tablesink.recvAction,user[%d] not exist", userIndex)
- return false
- }
- chairId := usr.GetUserChairId()
- if !isValidChair(chairId) {
- ts.table.LogWithTableId("tablesink.recvAction invalid chair %d", chairId)
- return false
- }
- ts.gameScene.Players[chairId].AutoOut = false
- if !ts.gameScene.Players[chairId].IsValid {
- ts.table.LogWithTableId("tablesink.recvAction invalid player [%d]", chairId)
- return false
- }
- var cmd CmdAction
- e := json.Unmarshal([]byte(data), &cmd)
- if e != nil {
- ts.table.LogWithTableId("tablesink.recvAction Unmarshal failed %s", data)
- return false
- }
- ts.table.KillTimer(TIMER_GAME)
- ret := false
- errMsg := ""
- switch cmd.Action {
- case Action_RobBanker:
- ret, errMsg = ts.dealRobBanker(chairId, cmd.Param)
- case Action_Banker_GiveMoney:
- fallthrough
- case Action_Banker_CompareCardDirect:
- fallthrough
- case Action_Player_CompareCard:
- fallthrough
- case Action_Player_Agree:
- fallthrough
- case Action_Player_AddMoney:
- fallthrough
- case Action_Banker_Agree:
- fallthrough
- case Action_Banker_DisAgree:
- ret, errMsg = ts.dealAction(chairId, cmd.Action, cmd.Param)
- }
- if !ret {
- var cmdFailed CmdActionFailed
- cmdFailed.CmdAction = cmd
- cmdFailed.ErrMsg = errMsg
- d, _ := json.Marshal(cmdFailed)
- ts.table.SendGameData(userIndex, CMD_ACTION_FAILED, string(d))
- }
- return ret
- }
- func (ts *tablesink) dealRobBanker(chairId, amount int) (bool, string) {
- if chairId != ts.gameScene.WhoseTurn {
- return false, "Not WhoseTurn"
- }
- usr := ts.table.GetUserByChair(chairId)
- if usr == nil {
- return false, "User Not Exist"
- }
- p := ts.gameScene.Players[chairId]
- if !p.IsValid || p.Status != Player_Status_Play {
- return false, "Invaild Player"
- }
- if amount > 0 && amount < ts.gameScene.CurrentMinRobScore &&
- p.Score > ts.gameScene.CurrentMinRobScore {
- return false, "Invaild Amount"
- }
- if amount > ts.roomInfo.InitScore {
- return false, "Invaild Amount"
- }
- if amount >= ts.gameScene.CurrentMinRobScore {
- ts.gameScene.CurrentMinRobScore = amount
- ts.gameScene.CurrentMinRobScore += ts.roomInfo.MinRobBankerScore
- if ts.gameScene.CurrentMinRobScore > ts.roomInfo.InitScore {
- ts.gameScene.CurrentMinRobScore = ts.roomInfo.InitScore
- }
- }
- ts.gameScene.Players[chairId].RobScore = amount
- ts.gameScene.Players[chairId].Score -= amount
- if amount == 0 {
- ts.gameScene.Players[chairId].Status = Player_Status_GiveUp
- }
- if ts.gameScene.Players[chairId].Score == 0 || amount == ts.roomInfo.InitScore {
- ts.gameScene.Players[chairId].Status = Player_Status_AllIn
- }
- ts.gameScene.PhaseIndex++
- ts.gameScene.Index++
- ts.gameScene.addUserAction(chairId, Action_RobBanker, amount)
- if getPreviousChair(ts.gameScene.WhoseTurn) != ts.gameScene.FirstActionPlayer {
- ts.gameScene.nextChair()
- } else {
- ts.gameScene.LastTurn = ts.gameScene.WhoseTurn
- ts.gameScene.WhoseTurn = -1
- }
- ts.gameScene.Players[chairId].setLastData(Action_RobBanker, amount)
- ts.LastOpraTime = time.Now()
- ts.table.NotifySceneChanged(-1)
- if ts.gameScene.isBelowOneRobBanker() {
- ts.table.SetTimer(TIMER_ROUND_END, SEC_ROBBANKER_TO_ROUND_END)
- return true, ""
- }
- if ts.gameScene.isAllDoRobBanker() {
- ts.gameScene.Phase = Phase_ConfirmBanker
- }
- ts.resetGameTimer()
- return true, ""
- }
- func (ts *tablesink) dealAction(chairId int, action, param int) (bool, string) {
- if chairId != ts.gameScene.WhoseTurn {
- return false, "Not WhoseTurn"
- }
- usr := ts.table.GetUserByChair(chairId)
- if usr == nil {
- return false, "User Not Exist"
- }
- p := ts.gameScene.Players[chairId]
- if !p.IsValid || (p.Status != Player_Status_Play && p.Status != Player_Status_AllIn) {
- return false, "Invaild Player"
- }
- isActionOver := false
- switch ts.gameScene.ActionPhase {
- case ActionPhase_Banker_GiveMoney:
- if param != 0 {
- if param < ts.roomInfo.MinBankerGiveScore || param > ts.gameScene.Players[chairId].RobScore {
- return false, "Over RobScore"
- }
- }
- if ts.gameScene.Players[ts.gameScene.Banker].RobScore == 0 {
- return false, "Score=0"
- }
- ts.gameScene.CurrentTradeAmount = param
- ts.gameScene.nextActionChair()
- if param == 0 {
- ts.gameScene.Players[ts.gameScene.CurrentActionPlayer].Status = Player_Status_CompareCard
- ts.gameScene.ActionPhase = ActionPhase_Banker_GiveMoney
- if ts.gameScene.isActionOver() {
- isActionOver = true
- } else {
- ts.gameScene.WhoseTurn = ts.gameScene.Banker
- }
- } else {
- ts.gameScene.ActionPhase = ActionPhase_Player_Reply
- }
- case ActionPhase_Player_Reply:
- if ts.gameScene.Players[ts.gameScene.Banker].RobScore == 0 {
- return false, "Score=0"
- }
- if param == 1 {
- ts.gameScene.Players[chairId].Score += ts.gameScene.Players[chairId].RobScore + ts.gameScene.CurrentTradeAmount
- ts.gameScene.Players[ts.gameScene.Banker].RobScore -= ts.gameScene.CurrentTradeAmount
- ts.gameScene.CurrentTradeAmount = 0
- ts.gameScene.Players[chairId].RobScore = 0
- ts.gameScene.ActionPhase = ActionPhase_Banker_GiveMoney
- ts.gameScene.Players[chairId].Status = Player_Status_GiveUp
- if ts.gameScene.isActionOver() {
- isActionOver = true
- }
- } else if param == 0 {
- ts.gameScene.CurrentTradeAmount = 0
- ts.gameScene.ActionPhase = ActionPhase_Banker_GiveMoney
- ts.gameScene.Players[chairId].Status = Player_Status_CompareCard
- if ts.gameScene.isActionOver() {
- isActionOver = true
- }
- } else {
- if param < ts.gameScene.CurrentTradeAmount ||
- param > (ts.gameScene.Players[ts.gameScene.Banker].RobScore+ts.gameScene.CurrentTradeAmount) {
- return false, "Over RobScore"
- }
- ts.gameScene.CurrentTradeAmount = param
- ts.gameScene.ActionPhase = ActionPhase_Banker_Reply
- }
- ts.gameScene.LastTurn = chairId
- ts.gameScene.WhoseTurn = ts.gameScene.Banker
- case ActionPhase_Banker_Reply:
- if param == 1 {
- if ts.gameScene.Players[ts.gameScene.Banker].RobScore == 0 {
- return false, "Score=0"
- }
- chair := ts.gameScene.CurrentActionPlayer
- ts.gameScene.Players[chair].Score += ts.gameScene.Players[chair].RobScore + ts.gameScene.CurrentTradeAmount
- ts.gameScene.Players[ts.gameScene.Banker].RobScore -= ts.gameScene.CurrentTradeAmount
- ts.gameScene.CurrentTradeAmount = 0
- ts.gameScene.Players[chair].RobScore = 0
- ts.gameScene.Players[chair].Status = Player_Status_GiveUp
- } else {
- ts.gameScene.CurrentTradeAmount = 0
- ts.gameScene.Players[ts.gameScene.CurrentActionPlayer].Status = Player_Status_CompareCard
- }
- if ts.gameScene.isActionOver() {
- isActionOver = true
- } else {
- ts.gameScene.ActionPhase = ActionPhase_Banker_GiveMoney
- }
- ts.gameScene.LastTurn = chairId
- ts.gameScene.WhoseTurn = ts.gameScene.Banker
- }
- if isActionOver {
- ts.gameScene.WhoseTurn = -1
- }
- ts.gameScene.PhaseIndex++
- ts.gameScene.Index++
- ts.gameScene.addUserAction(chairId, action, param)
- ts.gameScene.Players[chairId].setLastData(action, param)
- ts.LastOpraTime = time.Now()
- ts.table.NotifySceneChanged(-1)
- if isActionOver {
- if ts.gameScene.isNoNeedCompare() {
- ts.table.SetTimer(TIMER_ROUND_END, SEC_ACTION_TO_ROUND_END)
- return true, ""
- } else {
- ts.gameScene.Phase = Phase_CompareCard
- }
- }
- ts.resetGameTimer()
- return true, ""
- }
- func (ts *tablesink) enterEndPhase() {
- // 结算
- winner := ts.gameScene.getWinner()
- ts.table.LogWithTableId("tablesink.enterEndPhase getWinners:%d", winner)
- // 写分
- for i := 0; i < CHAIR_COUNT; i++ {
- p := &ts.gameScene.Players[i]
- usr := ts.table.GetUserByChair(i)
- if !p.IsValid || usr == nil {
- continue
- }
- p.EndScore = p.Score
- score := 0
- if i == winner {
- score = ts.gameScene.JackPotAmount
- ts.writeScore(p.userID, ts.gameScene.JackPotAmount, 0, 0, ScoreType_End, ts.roomInfo.RoomName, p.isRobot)
- }
- if !p.isRobot {
- waterpool.ReduceInventoryValue(GAMEID, ts.roomInfo.RoomName, score-ts.roomInfo.BaseScore, waterpool.RoomType_Normal)
- }
- // 写记录
- if ts.table.IsPrivate() {
- } else {
- go ts.table.WriteBetRecordWithSetcount(p.userID, ts.roomInfo.InitScore, score, 1.0, "normal", "",
- ts.roomInfo.RoomName, ts.gameScene.GameIndex)
- }
- }
- ts.gameScene.Phase = Phase_End
- ts.LastOpraTime = time.Now()
- ts.table.NotifySceneChanged(-1)
- if ts.table.IsPrivate() {
- list := []int{}
- list = append(list, winner)
- ts.table.PrivateRoomSetWinners(list)
- }
- ts.endGame()
- ts.gameScene.initData(ts.table.GetTableID())
- }
- func (ts *tablesink) endGame() {
- ts.table.LogWithTableId("--------endGame-----------")
- ts.gameScene.dump()
- ts.table.KillAllTimer()
- ts.table.EndGame()
- robotCount := 0
- for i := 0; i < CHAIR_COUNT; i++ {
- usr := ts.table.GetUserByChair(i)
- if usr == nil {
- continue
- }
- if !usr.IsRobot() {
- ts.table.SetTimer(TIMER_READY_0+i, SEC_READY)
- ts.gameScene.Players[i].isEndToStart = true
- } else {
- robotCount++
- }
- }
- if robotCount > 0 {
- ts.table.SetTimer(TIMER_REMOVE_ROBOT, 1000+rand.Intn(1000))
- }
- }
- // 聊天
- func (ts *tablesink) recvChatMsg(userIndex int32, data string) {
- usr := ts.table.GetPlayer(userIndex)
- if usr == nil {
- ts.table.LogWithTableId("tablesink.recvChatMsg,usr[%d] not exist", userIndex)
- return
- }
- ts.table.SendGameData(-1, CMD_TABLECHAT, data)
- }
- func (ts *tablesink) writeScore(userId int, amount, tax int, status, scoreType int, sourceName string, isRobot bool) bool {
- ok, _ := ts.table.WriteUserMoney(userId, amount, tax, status, scoreType, sourceName)
- if ok && !isRobot {
- waterpool.ReduceInventoryValue(GAMEID, ts.roomInfo.RoomName, amount, waterpool.RoomType_Normal)
- }
- return ok
- }
- func (ts *tablesink) checkRobotAction() {
- if ts.gameScene.Phase != Phase_Action && ts.gameScene.Phase != Phase_RobBanker {
- return
- }
- if !ts.gameScene.Players[ts.gameScene.WhoseTurn].isRobot {
- return
- }
- sec := ts.roomInfo.MinActionSec + rand.Intn(ts.roomInfo.MaxActionSec-ts.roomInfo.MinActionSec)
- ts.table.SetTimer(TIMER_ROBOT_0+ts.gameScene.WhoseTurn, sec)
- }
- func (ts *tablesink) getEventType(chairId int) int {
- if chairId == ts.gameScene.FirstActionPlayer {
- return 1
- }
- score := 0
- isFisrtAction := false
- for i := 0; i < CHAIR_COUNT; i++ {
- chair := (ts.gameScene.FirstActionPlayer + CHAIR_COUNT - i) % CHAIR_COUNT
- if chair == chairId && score == 0 {
- isFisrtAction = true
- break
- }
- if ts.gameScene.Players[chair].Status == Player_Status_AllIn {
- return 2
- }
- if ts.gameScene.Players[chair].RobScore > 0 {
- if score < ts.gameScene.Players[chair].RobScore {
- score = ts.gameScene.Players[chair].RobScore
- }
- }
- }
- if isFisrtAction {
- return 1
- }
- if score < ts.roomInfo.InitScore {
- return 0
- }
- return -1
- }
- func (ts *tablesink) onTimerRobotAction(chairId int) {
- if chairId != ts.gameScene.WhoseTurn {
- return
- }
- if ts.gameScene.Phase == Phase_RobBanker {
- score := 0
- limit := ts.gameScene.Players[chairId].Score
- eventType := ts.getEventType(chairId)
- if ts.gameScene.Players[chairId].CardType <= CardType_DoublePair {
- if ts.gameScene.Players[chairId].robotType == 1 {
- if eventType == 1 {
- if limit > 2100 {
- time := (limit - 2000) / ts.roomInfo.MinRobBankerDifferScore
- score = 2000 + rand.Intn(time)*ts.roomInfo.MinRobBankerDifferScore
- } else {
- score = limit
- }
- } else if eventType == 0 {
- if limit <= ts.gameScene.CurrentMinRobScore {
- score = limit
- } else {
- score = ts.gameScene.CurrentMinRobScore
- }
- }
- } else {
- if eventType == 1 {
- if limit > ts.roomInfo.MinRobBankerScore {
- score = ts.roomInfo.MinRobBankerScore
- } else {
- score = limit
- }
- }
- }
- } else {
- if ts.gameScene.Players[chairId].robotType == 1 {
- if eventType == 1 {
- if limit > 2100 {
- time := (limit - 2000) / ts.roomInfo.MinRobBankerDifferScore
- score = 2000 + rand.Intn(time)*ts.roomInfo.MinRobBankerDifferScore
- } else {
- score = limit
- }
- } else if eventType == 0 || eventType == 2 {
- if limit <= ts.gameScene.CurrentMinRobScore {
- score = limit
- } else {
- score = ts.gameScene.CurrentMinRobScore
- }
- }
- } else {
- if eventType == 1 {
- score = limit
- } else if eventType == 0 {
- if limit <= ts.gameScene.CurrentMinRobScore {
- score = limit
- } else {
- score = ts.gameScene.CurrentMinRobScore
- }
- }
- }
- }
- if score > ts.roomInfo.InitScore {
- score = ts.roomInfo.InitScore
- }
- ts.dealRobBanker(chairId, score)
- } else {
- data := 0
- action := 0
- switch ts.gameScene.ActionPhase {
- case ActionPhase_Banker_GiveMoney:
- data = ts.roomInfo.MinBankerGiveScore
- action = Action_Banker_GiveMoney
- case ActionPhase_Player_Reply:
- if ts.gameScene.Players[chairId].CardType <= CardType_DoublePair {
- action = Action_Player_Agree
- data = 1
- } else {
- if ts.gameScene.Players[chairId].robotType == 1 {
- action = Action_Player_CompareCard
- data = 0
- } else {
- if ts.gameScene.CurrentTradeAmount >= ts.gameScene.Players[ts.gameScene.Banker].RobScore/2 {
- action = Action_Player_CompareCard
- data = 0
- } else {
- c := ts.gameScene.Players[ts.gameScene.Banker].RobScore / 2 / ts.roomInfo.BetLevel
- if c <= 1 {
- data = (c + 1) * ts.roomInfo.BetLevel
- action = Action_Player_AddMoney
- } else {
- r := rand.Intn(c)
- data = (ts.gameScene.Players[ts.gameScene.Banker].RobScore/2/ts.roomInfo.BetLevel + r) * ts.roomInfo.BetLevel
- action = Action_Player_AddMoney
- }
- }
- }
- }
- case ActionPhase_Banker_Reply:
- if ts.gameScene.Players[chairId].robotType == 1 {
- action = Action_Banker_DisAgree
- data = 0
- } else {
- if ts.gameScene.CurrentTradeAmount <= ts.gameScene.Players[ts.gameScene.Banker].RobScore/2 {
- data = 1
- action = Action_Banker_Agree
- } else {
- action = Action_Banker_DisAgree
- data = 0
- }
- }
- }
- ts.dealAction(chairId, action, data)
- }
- }
|