player.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package game
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/davy66666/poker-go/src/github.com/dolotech/leaf/gate"
  6. "github.com/gorilla/websocket"
  7. "microGame/app/usercenter/cmd/api/internal/algorithm"
  8. "microGame/app/usercenter/cmd/api/internal/svc"
  9. "microGame/app/usercenter/model/user"
  10. "sync"
  11. "time"
  12. )
  13. const (
  14. PlayerStatusInGame int32 = 3
  15. PlayerStatusOffline int32 = 1
  16. PlayerStatusObserve int32 = 2
  17. PlayerStatusSitDown int32 = 0
  18. )
  19. type Player struct {
  20. *user.User
  21. SvcCtx *svc.ServiceContext
  22. mu sync.Mutex
  23. Conn *websocket.Conn
  24. Send chan []byte
  25. cards algorithm.Cards
  26. Pos uint8 // 玩家座位号,从1开始
  27. status int32 // 1为离线状态
  28. Bet uint32 // 当前下注
  29. actions chan int32
  30. IsFolded bool // 是否弃牌
  31. IsAllIn bool // 是否全押
  32. waitAction bool
  33. chips uint32 // 带入的筹码
  34. HandValue uint32 //手里的牌
  35. }
  36. func (o *Player) SetAction(n int32) error {
  37. if o.waitAction {
  38. o.actions <- n
  39. return nil
  40. }
  41. return errors.New("not your action")
  42. }
  43. func (o *Player) GetAction(timeout time.Duration) int32 {
  44. timer := time.NewTimer(timeout)
  45. o.waitAction = true
  46. select {
  47. case n := <-o.actions:
  48. timer.Stop()
  49. o.waitAction = false
  50. return n
  51. case <-timer.C:
  52. o.waitAction = false
  53. timer.Stop()
  54. return -1 // 超时弃牌
  55. }
  56. }
  57. func (o *Player) SetPos(pos uint8) {
  58. o.Pos = pos
  59. }
  60. func (o *Player) GetPos() uint8 {
  61. return o.Pos
  62. }
  63. func (o *Player) GetUid() int64 {
  64. return o.Uid
  65. }
  66. func (o *Player) WriteMsg(msg interface{}) {
  67. if o.status != PlayerStatusOffline {
  68. data, err := json.Marshal(msg)
  69. if err != nil {
  70. return // 适当处理错误
  71. }
  72. o.Send <- data
  73. }
  74. }
  75. func (o *Player) SetObserve() {
  76. o.status = PlayerStatusObserve
  77. }
  78. func (o *Player) IsObserve() bool {
  79. return o.status == PlayerStatusObserve
  80. }
  81. func (o *Player) SetOffline() {
  82. o.status = PlayerStatusOffline
  83. }
  84. func (o *Player) IsOffline() bool {
  85. return o.status == PlayerStatusOffline
  86. }
  87. func (o *Player) SetSitDown() {
  88. o.status = PlayerStatusSitDown
  89. }
  90. func (o *Player) IsShutdown() bool {
  91. return o.status == PlayerStatusSitDown
  92. }
  93. func (o *Player) SetGaming() {
  94. o.status = PlayerStatusInGame
  95. }
  96. func (o *Player) IsGaming() bool {
  97. return o.status == PlayerStatusInGame
  98. }
  99. func (o *Player) Replace(value *Player) {
  100. o.Pos = value.Pos
  101. o.cards = value.cards
  102. }
  103. func NewPlayer(data *user.User, conn gate.Agent) *Player {
  104. o := &Player{
  105. //Agent: conn,
  106. actions: make(chan int32),
  107. }
  108. return o
  109. }
  110. //德州扑克分为以下几个阶段:
  111. //Pre-flop:玩家手牌阶段。
  112. //Flop:翻三张公共牌。玩法
  113. //Turn:翻第四张公共牌。
  114. //River:翻第五张公共牌。
  115. //结算:根据牌型确定胜负。
  116. //过牌(Check):在没有人下注的情况下,选择不下注。
  117. //下注(Bet):如果没有人下注,可以选择下注一定金额。
  118. //跟注(Call):匹配其他玩家的下注金额。
  119. //加注(Raise):在跟注的基础上再增加下注金额。
  120. //弃牌(Fold):放弃继续参与该手牌。
  121. // HandleMessage 玩家提交下注数据
  122. // 有四种下注方式,下注数分别对应为:
  123. // 弃牌: <0 (fold)
  124. // 跟注:等于单注额 (call)
  125. // 看注:= 0 表示看注 (check)
  126. // 加注:大于单注额 (raise)
  127. // // 全押:等于玩家手中所有筹码 (allin)
  128. //func (o *Player) handlePlayerAction(player *Player, action string, amount int, gameState *GameState) {
  129. // switch action {
  130. // case "bet":
  131. // player.CurrentBet += amount
  132. // player.Chips -= amount
  133. // gameState.Pot += amount
  134. // if player.CurrentBet > gameState.CurrentBet {
  135. // gameState.CurrentBet = player.CurrentBet
  136. // }
  137. // case "fold":
  138. // player.IsFolded = true
  139. // case "all-in":
  140. // player.IsAllIn = true
  141. // gameState.Pot += player.Chips
  142. // player.CurrentBet += player.Chips
  143. // player.Chips = 0
  144. // }
  145. //
  146. // if shouldDealCards(gameState) {
  147. // nextPhase(gameState)
  148. // }
  149. //}
  150. //func nextPhase(gameState *GameState) {
  151. // switch gameState.Phase {
  152. // case "pre-flop":
  153. // gameState.Phase = "flop"
  154. // dealCommunityCards(gameState, 3)
  155. // case "flop":
  156. // gameState.Phase = "turn"
  157. // dealCommunityCards(gameState, 1)
  158. // case "turn":
  159. // gameState.Phase = "river"
  160. // dealCommunityCards(gameState, 1)
  161. // case "river":
  162. // gameState.Phase = "showdown"
  163. // determineWinner(gameState)
  164. // }
  165. //}