item.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * @Descripttion:
  3. * @version:
  4. * @Author: Neo,Huang
  5. * @Date: 2023-11-18 22:52:59
  6. * @LastEditors: Neo,Huang
  7. * @LastEditTime: 2023-11-19 16:04:30
  8. */
  9. package models
  10. import (
  11. "encoding/json"
  12. "log"
  13. "github.com/astaxie/beego"
  14. "box-gm/utils"
  15. )
  16. type ItemConfig struct {
  17. Id int `json:"id"` // 道具ID
  18. Name string `json:"name"` // 道具名称
  19. Type int `json:"ty"` // 道具类型
  20. Price int `json:"price"` // 道具价格
  21. }
  22. type ItemConfigs []*ItemConfig
  23. type RES_CONF_ITEMS struct {
  24. Code int `json:"code"` // 道具ID
  25. Items ItemConfigs `json:"items"` // 道具配置列表
  26. }
  27. var itemConfigMap map[int]*ItemConfig
  28. var itemConfigs ItemConfigs
  29. func init() {
  30. itemConfigMap = make(map[int]*ItemConfig)
  31. itemConfigMap[101] = &ItemConfig{
  32. Id: 101,
  33. Name: "金币",
  34. Type: 1,
  35. }
  36. itemConfigMap[102] = &ItemConfig{
  37. Id: 102,
  38. Name: "绑金",
  39. Type: 1,
  40. }
  41. itemConfigMap[103] = &ItemConfig{
  42. Id: 103,
  43. Name: "N1",
  44. Type: 1,
  45. }
  46. itemConfigs = append(itemConfigs, itemConfigMap[101])
  47. itemConfigs = append(itemConfigs, itemConfigMap[102])
  48. itemConfigs = append(itemConfigs, itemConfigMap[103])
  49. }
  50. func UpdateItemConfig() (int, int) {
  51. params := make(map[string]interface{})
  52. params["name"] = "gm_get_conf_items"
  53. body, _ := json.Marshal(params)
  54. host := utils.GetKeyConf("gameservers", "web")
  55. sign := utils.Strtomd5(string(body) + utils.GetKeyConf("app", "gameKey"))
  56. url := "http://" + host + "/json?sign=" + sign
  57. buff, err := utils.SendAndRecvHTTP("POST", url, string(body))
  58. log.Printf("UpdateItemConfig url[%s] body[%s] buff[%s] err[%v]", url, string(body), string(buff), err)
  59. if err != nil {
  60. return 0, 0
  61. }
  62. var res RES_CONF_ITEMS
  63. err = json.Unmarshal(buff, &res)
  64. if err != nil {
  65. return 0, 0
  66. }
  67. if res.Code != 200 {
  68. return 0, 0
  69. }
  70. oldCount := len(itemConfigs)
  71. itemConfigMap = make(map[int]*ItemConfig)
  72. itemConfigs = ItemConfigs{}
  73. for _, v := range res.Items {
  74. itemConfigMap[v.Id] = v
  75. itemConfigs = append(itemConfigs, v)
  76. }
  77. beego.Info("load item config, oldCount: ", oldCount, "newCount", len(itemConfigs))
  78. return oldCount, len(itemConfigs)
  79. }
  80. func GetItemName(id int) string {
  81. item, ok := itemConfigMap[id]
  82. if !ok {
  83. return ""
  84. }
  85. return item.Name
  86. }
  87. // 获取物品列表
  88. func GetItemInfoList() ItemConfigs {
  89. // InitItemConfig()
  90. return itemConfigs
  91. }