gamedefs.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package gamelogic
  2. import "bet24.com/log"
  3. const (
  4. GAMEID = 42
  5. GAME_NAME = "spinplay"
  6. CHAIR_COUNT = 4
  7. )
  8. const (
  9. CMD_ROOMINFO = "CMD_ROOMINFO"
  10. CMD_TABLECHAT = "CMD_TABLECHAT"
  11. CMD_ACTION = "CMD_ACTION"
  12. CMD_ACTION_FAILED = "CMD_ACTION_FAILED"
  13. CMD_CANCLE_AUTO = "CMD_CANCLE_AUTO"
  14. CMD_AUTO = "CMD_AUTO"
  15. )
  16. const (
  17. ScoreType_Bet = 0
  18. ScoreType_End = 1
  19. ScoreType_Return = 2
  20. ScoreType_Flee = 3
  21. )
  22. // 定时器时间
  23. const (
  24. SEC_READY = 60 * 1000 //准备计时
  25. SEC_AUTO = 800
  26. SEC_DELAY = 1500
  27. SEC_START_ACTION = 2000
  28. SEC_ROBBANKER_TO_ROUND_END = 1000
  29. SEC_ACTION_TO_ROUND_END = 1000
  30. SEC_COMPARE_TO_ROUND_END = 4000
  31. SEC_KICK_USER = 60000
  32. )
  33. const (
  34. TIMER_READY_0 = iota
  35. TIMER_READY_1
  36. TIMER_READY_2
  37. TIMER_READY_3
  38. TIMER_ADD_ROBOT
  39. TIMER_REMOVE_ROBOT
  40. TIMER_ROBOT_0
  41. TIMER_ROBOT_1
  42. TIMER_ROBOT_2
  43. TIMER_ROBOT_3
  44. TIMER_AUTO_ACTION_0
  45. TIMER_AUTO_ACTION_1
  46. TIMER_AUTO_ACTION_2
  47. TIMER_AUTO_ACTION_3
  48. TIMER_GAME
  49. TIMER_START_GAME
  50. TIMER_START_ACTION
  51. TIMER_ROUND_END
  52. TIMER_KICK_USER_0
  53. TIMER_KICK_USER_1
  54. TIMER_KICK_USER_2
  55. TIMER_KICK_USER_3
  56. )
  57. const (
  58. Phase_Free = iota // 空闲阶段
  59. Phase_JackPot // 开奖阶段
  60. Phase_SendCard // 发牌阶段
  61. Phase_RobBanker // 抢庄阶段
  62. Phase_ConfirmBanker // 确认庄家阶段
  63. Phase_Action // 庄闲动作
  64. Phase_CompareCard // 比牌阶段
  65. Phase_RoundEnd // 小结算
  66. Phase_End // 大结算
  67. )
  68. const (
  69. ActionPhase_Banker_GiveMoney = iota // 庄闲动作之庄家给钱
  70. ActionPhase_Player_Reply // 庄闲动作之闲家回应
  71. ActionPhase_Banker_Reply // 庄闲动作之庄家回应
  72. )
  73. const (
  74. Action_RobBanker = iota
  75. Action_Banker_GiveMoney // 庄家给钱
  76. Action_Banker_CompareCardDirect // 庄家直接比牌
  77. Action_Player_CompareCard // 玩家不要钱直接比牌
  78. Action_Player_Agree // 玩家同意给的钱
  79. Action_Player_AddMoney // 玩家加钱
  80. Action_Banker_Agree // 庄家同意加钱
  81. Action_Banker_DisAgree // 庄家不同意加钱,要比牌
  82. )
  83. const (
  84. Player_Status_Free = iota
  85. Player_Status_Play
  86. Player_Status_GiveUp
  87. Player_Status_AllIn
  88. Player_Status_CompareCard
  89. )
  90. func isValidChair(chairId int) bool {
  91. return chairId >= 0 && chairId < CHAIR_COUNT
  92. }
  93. func getPreviousChair(chair int) int {
  94. return (chair + CHAIR_COUNT - 1) % CHAIR_COUNT
  95. }
  96. type CmdAction struct {
  97. Action int
  98. Param int
  99. }
  100. type userAction struct {
  101. action int
  102. data string
  103. }
  104. func (u *userAction) dump() {
  105. log.Release("action[%d] data[%s]", u.action, u.data)
  106. }
  107. // 玩家动作失败
  108. type CmdActionFailed struct {
  109. CmdAction
  110. ErrMsg string
  111. }
  112. type cardStruct struct {
  113. cardType int
  114. cards []int
  115. fourValue int
  116. threeValue int
  117. pair1Value int
  118. pair2Value int
  119. }
  120. func (cs *cardStruct) analyzeStruct() {
  121. if len(cs.cards) != HOLD_CARD_COUNT {
  122. return
  123. }
  124. if isFour(cs.cards) {
  125. cs.cardType = CardType_Four
  126. cs.fourValue = getCardValue(cs.cards[0])
  127. return
  128. }
  129. if value, ret := isThree(cs.cards); ret {
  130. cs.cardType = CardType_Three
  131. cs.threeValue = value
  132. return
  133. }
  134. if value1, value2, ret := isDoublePair(cs.cards); ret {
  135. cs.cardType = CardType_DoublePair
  136. cs.pair1Value = value1
  137. cs.pair2Value = value2
  138. return
  139. }
  140. if value, ret := isPair(cs.cards); ret {
  141. cs.cardType = CardType_Pair
  142. cs.pair1Value = value
  143. return
  144. }
  145. cs.cardType = CardType_SanPai
  146. }
  147. func getSortedCards(cards []int) [][]int {
  148. sortedCards := make([][]int, 4)
  149. for i := 0; i < len(cards); i++ {
  150. v := getCardValue(cards[i])
  151. sortedCards[v] = append(sortedCards[v], cards[i])
  152. }
  153. return sortedCards
  154. }
  155. func isFour(cards []int) bool {
  156. cardList := getSortedCards(cards)
  157. for i := 0; i < 4; i++ {
  158. if len(cardList[i]) == 4 {
  159. return true
  160. }
  161. }
  162. return false
  163. }
  164. func isThree(cards []int) (int, bool) {
  165. cardList := getSortedCards(cards)
  166. for i := 0; i < 4; i++ {
  167. if len(cardList[i]) == 3 {
  168. return i, true
  169. }
  170. }
  171. return 0, false
  172. }
  173. func isDoublePair(cards []int) (int, int, bool) {
  174. cardList := getSortedCards(cards)
  175. pairCount := 0
  176. for i := 0; i < 4; i++ {
  177. if len(cardList[i]) == 2 {
  178. pairCount++
  179. }
  180. }
  181. if pairCount != 2 {
  182. return 0, 0, false
  183. }
  184. v1, v2 := -1, -1
  185. for i := 0; i < 4; i++ {
  186. if len(cardList[i]) == 2 {
  187. if v1 == -1 {
  188. v1 = i
  189. }
  190. v2 = i
  191. }
  192. }
  193. return v2, v1, true
  194. }
  195. func isPair(cards []int) (int, bool) {
  196. cardList := getSortedCards(cards)
  197. for i := 0; i < 4; i++ {
  198. if len(cardList[i]) == 2 {
  199. return i, true
  200. }
  201. }
  202. return 0, false
  203. }
  204. func getHandCardType(cards []int) int {
  205. if len(cards) != HOLD_CARD_COUNT {
  206. return -1
  207. }
  208. if isFour(cards) {
  209. return CardType_Four
  210. }
  211. if _, ret := isThree(cards); ret {
  212. return CardType_Three
  213. }
  214. if _, _, ret := isDoublePair(cards); ret {
  215. return CardType_DoublePair
  216. }
  217. if _, ret := isPair(cards); ret {
  218. return CardType_Pair
  219. }
  220. return CardType_SanPai
  221. }
  222. func getCardStruct(cards []int) *cardStruct {
  223. var c cardStruct
  224. c.cards = append(c.cards, cards...)
  225. c.analyzeStruct()
  226. return &c
  227. }
  228. const (
  229. CardType_SanPai = iota
  230. CardType_Pair
  231. CardType_DoublePair
  232. CardType_Three
  233. CardType_Four
  234. )
  235. func isBigger(cards1, cards2 []int) bool {
  236. c1 := getCardStruct(cards1)
  237. c2 := getCardStruct(cards2)
  238. if c1.cardType > c2.cardType {
  239. return true
  240. }
  241. if c1.cardType == c2.cardType {
  242. switch c1.cardType {
  243. case CardType_Four:
  244. return c1.fourValue > c2.fourValue
  245. case CardType_Three:
  246. return c1.threeValue > c2.threeValue
  247. case CardType_DoublePair:
  248. if c1.pair1Value > c2.pair1Value {
  249. return true
  250. } else if c1.pair1Value == c2.pair1Value {
  251. return c1.pair2Value > c2.pair2Value
  252. }
  253. case CardType_Pair:
  254. if c1.pair1Value > c2.pair1Value {
  255. return true
  256. } else if c1.pair1Value == c2.pair1Value {
  257. cardList1 := []int{}
  258. cardList2 := []int{}
  259. for i := 0; i < len(cards1); i++ {
  260. if getCardValue(cards1[i]) != c1.pair1Value {
  261. cardList1 = append(cardList1, cards1[i])
  262. }
  263. }
  264. for i := 0; i < len(cards2); i++ {
  265. if getCardValue(cards2[i]) != c1.pair1Value {
  266. cardList2 = append(cardList2, cards2[i])
  267. }
  268. }
  269. for i := 0; i < len(cardList1); i++ {
  270. if getCardValue(cardList1[i]) > getCardValue(cardList2[i]) {
  271. return true
  272. }
  273. }
  274. }
  275. }
  276. }
  277. return false
  278. }
  279. func isEqual(cards1, cards2 []int) bool {
  280. for i := 0; i < len(cards1); i++ {
  281. if getCardValue(cards1[i]) != getCardValue(cards2[i]) {
  282. return false
  283. }
  284. }
  285. return true
  286. }