Loggest thy stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.4 KiB

3 years ago
3 years ago
  1. package models
  2. type StatEntry struct {
  3. ID int `json:"id"`
  4. Name string `json:"name"`
  5. Weight float64 `json:"weight"`
  6. }
  7. type StatProgressEntry struct {
  8. StatEntry
  9. Acquired int `json:"acquired"`
  10. Required int `json:"required"`
  11. }
  12. type Stat struct {
  13. StatEntry
  14. Description string `json:"description"`
  15. AllowedAmounts map[string]int `json:"allowedAmounts"`
  16. }
  17. func (stat *Stat) Update(update StatUpdate) {
  18. if update.Name != nil {
  19. stat.Name = *update.Name
  20. }
  21. if update.Description != nil {
  22. stat.Description = *update.Description
  23. }
  24. if update.Weight != nil {
  25. stat.Weight = *update.Weight
  26. }
  27. for key, value := range update.AllowedAmounts {
  28. if stat.AllowedAmounts == nil {
  29. stat.AllowedAmounts = make(map[string]int, len(update.AllowedAmounts))
  30. }
  31. if value <= 0 {
  32. delete(stat.AllowedAmounts, key)
  33. } else {
  34. stat.AllowedAmounts[key] = value
  35. }
  36. }
  37. if len(stat.AllowedAmounts) == 0 {
  38. stat.AllowedAmounts = nil
  39. }
  40. }
  41. type StatUpdate struct {
  42. Name *string `json:"name"`
  43. Description *string `json:"description"`
  44. Weight *float64 `json:"weight"`
  45. AllowedAmounts map[string]int `json:"allowedAmounts"`
  46. }
  47. func (stat *Stat) AllowsAmount(amount int) bool {
  48. if stat == nil {
  49. return false
  50. }
  51. if stat.AllowedAmounts == nil || len(stat.AllowedAmounts) == 0 {
  52. return true
  53. }
  54. for _, v := range stat.AllowedAmounts {
  55. if v == amount {
  56. return true
  57. }
  58. }
  59. return false
  60. }