initTable.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package codeutil
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/os/gcache"
  5. "github.com/gogf/gf/os/gfile"
  6. "github.com/gogf/gf/util/gconv"
  7. "github.com/jszwec/csvutil"
  8. "strings"
  9. )
  10. // 删除零宽度无分隔符空间
  11. func DelPrefix(data []byte) []byte {
  12. if data == nil || len(data) < 4 {
  13. return nil
  14. }
  15. UTF8_BOM := "\uFEFF"
  16. if strings.HasPrefix(string(data), UTF8_BOM) {
  17. data = data[3:]
  18. }
  19. return data
  20. }
  21. // Item
  22. func GetItemConfig() *gcache.Cache {
  23. var c = gcache.New()
  24. csvInput := gfile.GetBytes("./config/server/Item.csv")
  25. csvInput = DelPrefix(csvInput)
  26. var itemConfigs []ItemConfig
  27. if err := csvutil.Unmarshal(csvInput, &itemConfigs); err != nil {
  28. fmt.Println("error:", err)
  29. }
  30. for _, itemConfig := range itemConfigs {
  31. c.Set(gconv.String(itemConfig.Id), itemConfig.Note, 0)
  32. }
  33. return c
  34. }
  35. // RechargeCommodityConfig
  36. func GetRechargeCommodityConfig() *gcache.Cache {
  37. var c = gcache.New()
  38. csvInput := gfile.GetBytes("./config/server/RechargeCommodityConfig.csv")
  39. csvInput = DelPrefix(csvInput)
  40. var rechargeCommodityConfigs []RechargeCommodityConfig
  41. if err := csvutil.Unmarshal(csvInput, &rechargeCommodityConfigs); err != nil {
  42. fmt.Println("error:", err)
  43. }
  44. for _, rechargeCommodityConfig := range rechargeCommodityConfigs {
  45. c.Set(gconv.String(rechargeCommodityConfig.Id), rechargeCommodityConfig, 0)
  46. }
  47. return c
  48. }
  49. // Reason
  50. func GetReasonConfig() *gcache.Cache {
  51. var c = gcache.New()
  52. csvInput := gfile.GetBytes("./config/server/Reason.csv")
  53. csvInput = DelPrefix(csvInput)
  54. var reasonConfigs []ReasonConfig
  55. if err := csvutil.Unmarshal(csvInput, &reasonConfigs); err != nil {
  56. fmt.Println("error:", err)
  57. }
  58. for _, reasonConfig := range reasonConfigs {
  59. c.Set(gconv.String(reasonConfig.Id), reasonConfig, 0)
  60. }
  61. return c
  62. }
  63. type ReasonConfig struct {
  64. Id string `csv:"Id"`
  65. Name string `csv:"Name"`
  66. Notes string `csv:"Notes"`
  67. }
  68. type RechargeCommodityConfig struct {
  69. Id string `csv:"Id"`
  70. Price string `csv:"Price"`
  71. BaseReward string `csv:"BaseReward"`
  72. PriceWeight string `csv:"PriceWeight"`
  73. }
  74. type ItemConfig struct {
  75. Id string `csv:"Id"`
  76. Note string `csv:"Note"`
  77. }