wopr.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package door
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. "time"
  7. )
  8. /*
  9. WOPR
  10. [ GAME ]
  11. [ TIME ELAPSED ]
  12. [ XX HRS ]
  13. [ XX MIN XX SEC ]
  14. [■ ] ->
  15. [ GAME ]
  16. [ TIME REMAINING ]
  17. [ XX HRS ]
  18. [ XX MIN XX SEC ]
  19. [ ■] <-
  20. Border SINGLE
  21. Ok, Width = 16
  22. 1000 / 16 = 62.5 ms = 62500 us
  23. Width 16 (at pos 1, update time/sec increment)
  24. Black on Cyan
  25. */
  26. type WOPR struct {
  27. ElapsedPanel Panel
  28. Elapsed time.Time
  29. ElapsedD time.Duration
  30. RemainingPanel Panel
  31. Remaining time.Time
  32. RemainingD time.Duration
  33. Color string
  34. Index int
  35. Ticker *time.Ticker
  36. }
  37. // Initialize, Set X, Y on Panels, Animate()
  38. // Initialize, and create panels
  39. func (w *WOPR) Init(elapsed time.Time, remaining time.Time, color string) {
  40. if color == "" {
  41. color = ColorText("BLACK ON CYAN")
  42. }
  43. w.Elapsed = elapsed
  44. w.Remaining = remaining
  45. w.Index = 0
  46. w.ElapsedPanel = Panel{Width: 16,
  47. BorderColor: color,
  48. Style: SINGLE}
  49. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, Line{Text: " GAME "})
  50. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, Line{Text: " TIME ELAPSED "})
  51. var ehour Line = Line{}
  52. ehour.UpdateF = func() string {
  53. var hours int = int(w.ElapsedD.Hours())
  54. return fmt.Sprintf(" %02d HRS ", hours%100)
  55. }
  56. ehour.Text = ehour.UpdateF()
  57. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, ehour)
  58. var eminsec Line = Line{}
  59. eminsec.UpdateF = func() string {
  60. var mins int = int(w.ElapsedD.Minutes()) % 60
  61. var secs int = int(w.ElapsedD.Seconds()) % 60
  62. return fmt.Sprintf(" %02d MIN %02d SEC ", mins, secs)
  63. }
  64. eminsec.Text = eminsec.UpdateF()
  65. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, eminsec)
  66. var eanimate Line = Line{}
  67. eanimate.UpdateF = func() string {
  68. // Left to Right
  69. if Unicode {
  70. var buffer []rune = []rune(strings.Repeat(" ", 16))
  71. buffer[w.Index] = '\u25a0'
  72. return string(buffer)
  73. } else {
  74. var buffer []byte = bytes.Repeat([]byte(" "), 16)
  75. buffer[w.Index] = '\xfe'
  76. return string(buffer)
  77. }
  78. }
  79. eanimate.Text = eanimate.UpdateF()
  80. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, eanimate)
  81. w.RemainingPanel = Panel{Width: 16,
  82. BorderColor: color,
  83. Style: SINGLE}
  84. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, Line{Text: " GAME "})
  85. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, Line{Text: " TIME REMAINING "})
  86. var rhour Line = Line{}
  87. rhour.UpdateF = func() string {
  88. var hours int = int(w.RemainingD.Hours())
  89. return fmt.Sprintf(" %02d HRS ", hours%100)
  90. }
  91. rhour.Text = rhour.UpdateF()
  92. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, rhour)
  93. var rminsec Line = Line{}
  94. rminsec.UpdateF = func() string {
  95. var mins int = int(w.RemainingD.Minutes()) % 60
  96. var secs int = int(w.RemainingD.Seconds()) % 60
  97. return fmt.Sprintf(" %02d MIN %02d SEC ", mins, secs)
  98. }
  99. rminsec.Text = rminsec.UpdateF()
  100. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, rminsec)
  101. var ranimate Line = Line{}
  102. ranimate.UpdateF = func() string {
  103. // Left to Right
  104. if Unicode {
  105. var buffer []rune = []rune(strings.Repeat(" ", 16))
  106. buffer[15-w.Index] = '\u25a0'
  107. return string(buffer)
  108. } else {
  109. var buffer []byte = bytes.Repeat([]byte(" "), 16)
  110. buffer[15-w.Index] = '\xfe'
  111. return string(buffer)
  112. }
  113. }
  114. ranimate.Text = ranimate.UpdateF()
  115. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, ranimate)
  116. }
  117. func (w *WOPR) Inc() {
  118. w.Index++
  119. if w.Index == 16 {
  120. w.Index = 0
  121. w.ElapsedPanel.Update()
  122. w.RemainingPanel.Update()
  123. w.RemainingD = time.Duration(w.RemainingD.Seconds()-1) * time.Second
  124. w.ElapsedD = time.Duration(w.ElapsedD.Seconds()+1) * time.Second
  125. }
  126. }
  127. func (w *WOPR) Animate(d *Door) {
  128. // til := time.Now().UnixNano() % int64(time.Second)
  129. // w.Index = int((til / int64(time.Microsecond)) / 62500)
  130. // either put the sync sleep in the go routine, or sleep and sync Index.
  131. // time.Sleep(time.Duration(int64(time.Second) - til)) // time.Now().UnixMilli()%1000) * time.Millisecond)
  132. // time.Second / 16
  133. // w.Ticker = time.NewTicker(time.Microsecond * time.Duration(62500))
  134. // w.Index = 0
  135. // Convert time.Time to time.Duration
  136. // This gives us consistency when resuming.
  137. w.ElapsedD = time.Since(w.Elapsed)
  138. w.RemainingD = time.Until(w.Remaining)
  139. go func(d *Door) {
  140. // til := time.Now().UnixNano() % int64(time.Second)
  141. sec16 := int64(time.Second) / 16
  142. // tilms := til % sec16
  143. w.Index = 0 // int(til / sec16)
  144. // log.Printf("til: %d, sec: %d, w.Index: %d\n", til, sec16, w.Index)
  145. // either put the sync sleep in the go routine, or sleep and sync Index.
  146. // time.Sleep(time.Second - time.Duration(til)) // sec16 - (til % sec16))) //int64(time.Second) - til)) // time.Now().UnixMilli()%1000) * time.Millisecond)
  147. // time.Second / 16
  148. w.Ticker = time.NewTicker(time.Duration(sec16)) // time.Microsecond * time.Duration(62500))
  149. var output string
  150. for range w.Ticker.C {
  151. w.ElapsedPanel.Update()
  152. w.RemainingPanel.Update()
  153. output = SavePos + w.ElapsedPanel.Output() + w.RemainingPanel.Output() + RestorePos
  154. if !d.Disconnect() {
  155. d.Write(output)
  156. } else {
  157. w.Ticker.Stop()
  158. return
  159. }
  160. w.Inc()
  161. }
  162. }(d)
  163. }
  164. func (w *WOPR) Stop() {
  165. w.Ticker.Stop()
  166. }