playcards.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. Hand int
  25. }
  26. // Adjust date to 2:00 AM
  27. func NormalizeDate(date *time.Time) {
  28. offset := time.Duration(date.Second())*time.Second +
  29. time.Duration(date.Minute())*time.Minute +
  30. time.Duration(date.Hour()-2)*time.Hour +
  31. time.Duration(date.Nanosecond())*time.Nanosecond
  32. *date = date.Add(-offset)
  33. }
  34. func (pc *PlayCards) Init() {
  35. // init_values()
  36. hands := pc.DB.GetSetting("hands_per_day", "3")
  37. pc.Total_hands, _ = strconv.Atoi(hands)
  38. pc.Play_card = 28
  39. pc.Current_streak = 0
  40. pc.Best_streak = 0
  41. best := pc.DB.GetSetting("best_streak", "0")
  42. pc.Best_streak, _ = strconv.Atoi(best)
  43. pc.Select_card = 23
  44. pc.Score = 0
  45. // PlayCards::PlayCards()
  46. pc.Play_day = time.Now()
  47. NormalizeDate(&pc.Play_day)
  48. pc.Play_day_t = pc.Play_day.Unix()
  49. var last_played int64
  50. last := pc.DB.GetSetting("last_played", "0")
  51. last_played, _ = strconv.ParseInt(last, 10, 64)
  52. days := pc.DB.GetSetting("days_played", "0")
  53. pc.Days_played, _ = strconv.Atoi(days)
  54. if last_played != pc.Play_day_t {
  55. pc.DB.SetSetting("last_played", strconv.FormatInt(pc.Play_day_t, 10))
  56. pc.DB.SetSetting("days_played", "0")
  57. pc.Days_played = 0
  58. }
  59. pc.Seeds = make([]int32, 0)
  60. // config _seed
  61. parts := strings.Split((*pc.Config)["_seed"], ",")
  62. for _, seed := range parts {
  63. i, _ := strconv.ParseInt(seed, 10, 32)
  64. pc.Seeds = append(pc.Seeds, int32(i))
  65. }
  66. pc.Hand = 0
  67. pc.Total_hands = 0
  68. // Panels ?
  69. }