matchroom.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package simplematch
  2. import (
  3. "bet24.com/log"
  4. privateroom "bet24.com/servers/micros/privateroom/proto"
  5. "fmt"
  6. )
  7. type matchroom struct {
  8. RoomNo int
  9. Users []int
  10. Status int
  11. mi *matchinstance
  12. Winners []int
  13. }
  14. func newMatchRoom(roomNo int, mi *matchinstance) *matchroom {
  15. ret := new(matchroom)
  16. ret.RoomNo = roomNo
  17. ret.mi = mi
  18. return ret
  19. }
  20. func (mr *matchroom) addUser(userId int) {
  21. for i := 0; i < len(mr.Users); i++ {
  22. if mr.Users[i] == userId {
  23. log.Release("simplematch.matchroom[%d] addUser %d already exist", mr.RoomNo, userId)
  24. return
  25. }
  26. }
  27. mr.Users = append(mr.Users, userId)
  28. }
  29. func (mr *matchroom) removeUser(userId int) {
  30. for i := 0; i < len(mr.Users); i++ {
  31. if mr.Users[i] == userId {
  32. mr.Users = append(mr.Users[:i], mr.Users[i+1:]...)
  33. return
  34. }
  35. }
  36. log.Release("simplematch.matchroom[%d] removeUser %d not exist", mr.RoomNo, userId)
  37. }
  38. func (mr *matchroom) isEnded() bool {
  39. return mr.Status == privateroom.PrivateRoomStatus_Ended
  40. }
  41. func (mr *matchroom) isStarted() bool {
  42. return mr.Status >= privateroom.PrivateRoomStatus_Playing
  43. }
  44. func (mr *matchroom) dump() {
  45. if mr == nil {
  46. return
  47. }
  48. var users string
  49. for i := 0; i < len(mr.Users); i++ {
  50. users = fmt.Sprintf("%s%d:%d;", users, mr.Users[i], mr.mi.getUserScore(mr.Users[i]))
  51. }
  52. if len(mr.Winners) > 0 {
  53. users = fmt.Sprintf("%s winners:%v", users, mr.Winners)
  54. }
  55. log.Release(" RoomNo[%d] Status[%d] Users[%s]", mr.RoomNo, mr.Status, users)
  56. }
  57. func (mr *matchroom) isValidUsers(users []int) bool {
  58. for _, v := range users {
  59. if !mr.isUserExist(v) {
  60. log.Release("matchroom.isValidUsers %d not exist in", v)
  61. return false
  62. }
  63. }
  64. return true
  65. }
  66. func (mr *matchroom) isUserExist(userId int) bool {
  67. for _, v := range mr.Users {
  68. if v == userId {
  69. return true
  70. }
  71. }
  72. return false
  73. }
  74. func (mr *matchroom) setWinners(winners []int) {
  75. mr.Winners = winners
  76. }