playcards.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "math/rand"
  4. "red-green/door"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. type PlayCards struct {
  10. Door *door.Door
  11. DB *DBData
  12. Config *map[string]string
  13. RNG *rand.Rand
  14. Seeds []int32
  15. Total_hands int
  16. Play_card int
  17. Current_streak int
  18. Best_streak int
  19. Select_card int
  20. Score int
  21. Play_day time.Time
  22. Play_day_t int64
  23. Days_played int
  24. }
  25. // Adjust date to 2:00 AM
  26. func NormalizeDate(date *time.Time) {
  27. offset := time.Duration(date.Second())*time.Second +
  28. time.Duration(date.Minute())*time.Minute +
  29. time.Duration(date.Hour()-2)*time.Hour +
  30. time.Duration(date.Nanosecond())*time.Nanosecond
  31. *date = date.Add(-offset)
  32. }
  33. func (pc *PlayCards) Init() {
  34. // init_values()
  35. hands := pc.DB.GetSetting("hands_per_day", "3")
  36. pc.Total_hands, _ = strconv.Atoi(hands)
  37. pc.Play_card = 28
  38. pc.Current_streak = 0
  39. pc.Best_streak = 0
  40. best := pc.DB.GetSetting("best_streak", "0")
  41. pc.Best_streak, _ = strconv.Atoi(best)
  42. pc.Select_card = 23
  43. pc.Score = 0
  44. // PlayCards::PlayCards()
  45. pc.Play_day = time.Now()
  46. NormalizeDate(&pc.Play_day)
  47. pc.Play_day_t = pc.Play_day.Unix()
  48. var last_played int64
  49. last := pc.DB.GetSetting("last_played", "0")
  50. last_played, _ = strconv.ParseInt(last, 10, 64)
  51. days := pc.DB.GetSetting("days_played", "0")
  52. pc.Days_played, _ = strconv.Atoi(days)
  53. if last_played != pc.Play_day_t {
  54. pc.DB.SetSetting("last_played", strconv.Itoa(int(pc.Play_day_t)))
  55. pc.DB.SetSetting("days_played", "0")
  56. pc.Days_played = 0
  57. }
  58. pc.Seeds = make([]int32, 0)
  59. // config _seed
  60. parts := strings.Split((*pc.Config)["_seed"], ",")
  61. for _, seed := range parts {
  62. i, _ := strconv.ParseInt(seed, 10, 32)
  63. pc.Seeds = append(pc.Seeds, int32(i))
  64. }
  65. }