match.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/app/interface/main/activity/model/like"
  7. "go-common/app/job/main/activity/model/match"
  8. "go-common/app/service/main/coin/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _upMatchUserLimit = 100
  14. _reason = "竞猜奖励"
  15. )
  16. func (s *Service) upMatchUser(c context.Context, newMsg, oldMsg json.RawMessage) {
  17. var (
  18. err error
  19. newMatchObj = new(match.ActMatchObj)
  20. oldMatchObj = new(match.ActMatchObj)
  21. )
  22. if err = json.Unmarshal(newMsg, newMatchObj); err != nil {
  23. log.Error("upMatchUser json.Unmarshal(%s) error(%+v)", newMsg, err)
  24. return
  25. }
  26. if err = json.Unmarshal(oldMsg, oldMatchObj); err != nil {
  27. log.Error("upMatchUser json.Unmarshal(%s) error(%+v)", oldMsg, err)
  28. return
  29. }
  30. if oldMatchObj.Result == match.ResultNo {
  31. if newMatchObj.Result == match.ResultNo {
  32. return
  33. }
  34. s.upUsers(c, newMatchObj)
  35. }
  36. }
  37. // FinishMatch finish match.
  38. func (s *Service) FinishMatch(c context.Context, moID int64) (err error) {
  39. var matchObj *match.ActMatchObj
  40. if matchObj, err = s.dao.MatchObjInfo(c, moID); err != nil || matchObj == nil {
  41. return
  42. }
  43. if matchObj.Result == match.ResultNo {
  44. log.Error("FinishMatch moID(%d) result error", moID)
  45. err = ecode.RequestErr
  46. return
  47. }
  48. go s.upUsers(context.Background(), matchObj)
  49. return
  50. }
  51. func (s *Service) upUsers(c context.Context, matchObj *match.ActMatchObj) {
  52. var (
  53. matchs []*like.Match
  54. list []*match.ActMatchUser
  55. stake int64
  56. err error
  57. )
  58. if matchs, err = s.actRPC.Matchs(c, &like.ArgMatch{Sid: matchObj.SID}); err != nil {
  59. log.Error("upMatchUser s.actRPC.Matchs(%d) error(%v)", matchObj.SID, err)
  60. return
  61. }
  62. for _, v := range matchs {
  63. if v.ID == matchObj.MatchID {
  64. stake = v.Stake
  65. }
  66. }
  67. if stake == 0 {
  68. log.Error("upMatchUser match_id(%d) not found", matchObj.MatchID)
  69. return
  70. }
  71. for {
  72. if list, err = s.dao.UnDoMatchUsers(context.Background(), matchObj.ID, _upMatchUserLimit); err != nil {
  73. time.Sleep(time.Duration(s.c.Interval.QueryInterval))
  74. continue
  75. } else if len(list) == 0 {
  76. log.Info("upMatchUser finish m_o_id(%d)", matchObj.ID)
  77. return
  78. }
  79. var resultMids []int64
  80. for _, v := range list {
  81. if v.Result == matchObj.Result {
  82. count := float64(v.Stake * stake)
  83. if _, err = s.coinRPC.ModifyCoin(context.Background(), &model.ArgModifyCoin{Mid: v.Mid, Count: count, Reason: _reason}); err != nil {
  84. log.Error("upMatchUser coin error s.coinRPC.ModifyCoin mid(%d) coin(%v) error(%v)", v.Mid, count, err)
  85. continue
  86. }
  87. log.Info("upMatchUser s.coinRPC.ModifyCoin mid(%d) coin(%v)", v.Mid, count)
  88. resultMids = append(resultMids, v.Mid)
  89. time.Sleep(time.Duration(s.c.Interval.CoinInterval))
  90. } else {
  91. resultMids = append(resultMids, v.Mid)
  92. }
  93. }
  94. if err = s.dao.UpMatchUserResult(context.Background(), matchObj.ID, resultMids); err != nil {
  95. continue
  96. }
  97. log.Info("upMatchUser s.dao.UpMatchUserResult matchID(%d) mids(%+v)", matchObj.ID, resultMids)
  98. time.Sleep(time.Duration(s.c.Interval.QueryInterval))
  99. }
  100. }