exchange.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package slotscore
  2. import (
  3. "encoding/json"
  4. "os"
  5. "sync"
  6. "time"
  7. "bet24.com/log"
  8. )
  9. const refresh_config_sec = 600
  10. type exchangeItem struct {
  11. ProductId int // 产品ID
  12. Score int // 所需积分
  13. Chip int // 兑换元宝数量
  14. }
  15. func newSlotExchange() *slotexchange {
  16. ret := new(slotexchange)
  17. ret.lock = &sync.RWMutex{}
  18. ret.initData()
  19. return ret
  20. }
  21. type slotexchange struct {
  22. items []exchangeItem
  23. lock *sync.RWMutex
  24. isValid bool
  25. }
  26. func (se *slotexchange) initData() {
  27. defer func() {
  28. time.AfterFunc(refresh_config_sec*time.Second, se.initData)
  29. }()
  30. data, err := os.ReadFile("fishconf/slotexchange.json")
  31. if err != nil {
  32. log.Release("slotexchange.loadData read slotexchange failed")
  33. return
  34. }
  35. se.lock.Lock()
  36. defer se.lock.Unlock()
  37. err = json.Unmarshal(data, &se.items)
  38. if err != nil {
  39. log.Release("slotexchange.loadData Unmarshal slotexchange failed err:%v", err)
  40. return
  41. }
  42. se.isValid = true
  43. }
  44. func (se *slotexchange) isExchangeVaid() bool {
  45. return se.isValid
  46. }
  47. func (se *slotexchange) getItem(productId int) *exchangeItem {
  48. se.lock.RLock()
  49. defer se.lock.RUnlock()
  50. for _, v := range se.items {
  51. if v.ProductId == productId {
  52. return &v
  53. }
  54. }
  55. return nil
  56. }
  57. func (se *slotexchange) getList() string {
  58. se.lock.RLock()
  59. d, _ := json.Marshal(se.items)
  60. se.lock.RUnlock()
  61. return string(d)
  62. }