game.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. "go-common/app/interface/main/space/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. )
  12. const (
  13. _lastPlayGameURI = "/user/games.mid"
  14. _appPlayedGameURI = "/game/recent/play"
  15. _platformAndroid = "android"
  16. _platformIOS = "ios"
  17. _platTypeAndroid = 1
  18. _platTypeIOS = 2
  19. )
  20. // LastPlayGame get last play game.
  21. func (d *Dao) LastPlayGame(c context.Context, mid int64) (data []*model.Game, err error) {
  22. var (
  23. params = url.Values{}
  24. ip = metadata.String(c, metadata.RemoteIP)
  25. )
  26. params.Set("mid", strconv.FormatInt(mid, 10))
  27. var res struct {
  28. Code int `json:"code"`
  29. Data []*model.Game `json:"games"`
  30. }
  31. if err = d.httpR.Get(c, d.lastPlayGameURL, ip, params, &res); err != nil {
  32. log.Error("d.httpR.Get(%s,%d) error(%v)", d.lastPlayGameURL, mid, err)
  33. return
  34. }
  35. if res.Code != ecode.OK.Code() {
  36. log.Error("d.httpR.Get(%s,%d) code error(%d)", d.lastPlayGameURL, mid, res.Code)
  37. err = ecode.Int(res.Code)
  38. return
  39. }
  40. data = res.Data
  41. return
  42. }
  43. // AppPlayedGame get app player games.
  44. func (d *Dao) AppPlayedGame(c context.Context, mid int64, platform string, pn, ps int) (data []*model.AppGame, count int, err error) {
  45. var platformType int
  46. switch platform {
  47. case _platformAndroid:
  48. platformType = _platTypeAndroid
  49. case _platformIOS:
  50. platformType = _platTypeIOS
  51. }
  52. params := url.Values{}
  53. params.Set("uid", strconv.FormatInt(mid, 10))
  54. params.Set("platform_type", strconv.Itoa(platformType))
  55. params.Set("page_num", strconv.Itoa(pn))
  56. params.Set("page_size", strconv.Itoa(ps))
  57. params.Set("ts", strconv.FormatInt(time.Now().Unix()*1000, 10))
  58. var res struct {
  59. Code int `json:"code"`
  60. Data struct {
  61. List []*model.AppGame `json:"list"`
  62. TotalCount int `json:"total_count"`
  63. }
  64. }
  65. if err = d.httpGame.Get(c, d.appPlayedGameURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  66. log.Error("AppPlayedGame d.httpR.Get(%s,%d) error(%v)", d.appPlayedGameURL+params.Encode(), mid, err)
  67. return
  68. }
  69. if res.Code != ecode.OK.Code() {
  70. log.Error("AppPlayedGame d.httpR.Get(%s,%d) code error(%d)", d.appPlayedGameURL+params.Encode(), mid, res.Code)
  71. err = ecode.Int(res.Code)
  72. return
  73. }
  74. data = res.Data.List
  75. count = res.Data.TotalCount
  76. return
  77. }