item.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package genPool
  2. import (
  3. "f1-game/internal/data"
  4. "f1-game/internal/enum"
  5. ctime "github.com/cherry-game/cherry/extend/time"
  6. )
  7. const (
  8. CountKey = "count"
  9. PeriodEndTimeKey = "periodEndTime"
  10. )
  11. type GenPoolItem struct {
  12. GenPoolID int32 // 产出池ID
  13. ItemID int32 // 道具ID
  14. Count int64 // 已产出数量
  15. PeriodEndTime int64 // 周期结束时间
  16. }
  17. func NewGenPoolItem(GenPoolID, itemID int32) *GenPoolItem {
  18. return &GenPoolItem{
  19. GenPoolID: GenPoolID,
  20. ItemID: itemID,
  21. Count: 0,
  22. }
  23. }
  24. // 检查是否可以重置
  25. func (p *GenPoolItem) CheckReset(nowTime ctime.CherryTime) bool {
  26. // 未到到重置时间
  27. if nowTime.ToMillisecond() > p.PeriodEndTime {
  28. return true
  29. }
  30. return false
  31. }
  32. // 根据时间类型,计算周期结束时间
  33. func (p *GenPoolItem) CalcPeriodEndTime(nowTime ctime.CherryTime) int64 {
  34. genPoolRow, _ := data.GenPool.GetByIDItemID(p.GenPoolID, p.ItemID)
  35. switch genPoolRow.TimeType {
  36. case enum.GenPoolTimeType_Hour: // 每小时
  37. return nowTime.EndOfHour().ToMillisecond()
  38. case enum.GenPoolTimeType_Day: // 每天
  39. return nowTime.EndOfDay().ToMillisecond()
  40. case enum.GenPoolTimeType_Week: // 每周
  41. return nowTime.EndOfWeek().ToMillisecond()
  42. case enum.GenPoolTimeType_Month: // 每月
  43. return nowTime.EndOfMonth().ToMillisecond()
  44. default:
  45. return 0
  46. }
  47. }
  48. // 重置产出数量并刷新周期结束时间
  49. func (p *GenPoolItem) ResetCount(nowTime ctime.CherryTime) {
  50. p.Count = 0
  51. p.PeriodEndTime = p.CalcPeriodEndTime(nowTime)
  52. }
  53. // 增加产出数量
  54. func (p *GenPoolItem) AddCount(count int64) {
  55. p.Count += count
  56. }
  57. // 获取可产出数量
  58. func (p *GenPoolItem) GetCanCount(add int64) int64 {
  59. genPoolRow, _ := data.GenPool.GetByIDItemID(p.GenPoolID, p.ItemID)
  60. // 剩余可产出数量
  61. remaining := genPoolRow.Num - p.Count
  62. return min(remaining, add)
  63. }