wopr.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. StopIt chan bool
  37. }
  38. func (w *WOPR) Clear() string {
  39. return w.ElapsedPanel.Clear() + w.RemainingPanel.Clear()
  40. }
  41. // Initialize, Set X, Y on Panels, Animate()
  42. // Initialize, and create panels
  43. func (w *WOPR) Init(elapsed time.Time, remaining time.Time, color string) {
  44. if color == "" {
  45. color = ColorText("BLACK ON CYAN")
  46. }
  47. w.Elapsed = elapsed
  48. w.Remaining = remaining
  49. w.Index = 0
  50. w.ElapsedPanel = Panel{Width: 16,
  51. BorderColor: color,
  52. Style: SINGLE}
  53. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, Line{Text: " GAME "})
  54. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, Line{Text: " TIME ELAPSED "})
  55. var ehour Line = Line{}
  56. ehour.UpdateF = func() string {
  57. var hours int = int(w.ElapsedD.Hours())
  58. return fmt.Sprintf(" %02d HRS ", hours%100)
  59. }
  60. ehour.Text = ehour.UpdateF()
  61. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, ehour)
  62. var eminsec Line = Line{}
  63. eminsec.UpdateF = func() string {
  64. var mins int = int(w.ElapsedD.Minutes()) % 60
  65. var secs int = int(w.ElapsedD.Seconds()) % 60
  66. return fmt.Sprintf(" %02d MIN %02d SEC ", mins, secs)
  67. }
  68. eminsec.Text = eminsec.UpdateF()
  69. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, eminsec)
  70. var eanimate Line = Line{}
  71. eanimate.UpdateF = func() string {
  72. // Left to Right
  73. if Unicode {
  74. var buffer []rune = []rune(strings.Repeat(" ", 16))
  75. buffer[w.Index] = '\u25a0'
  76. return string(buffer)
  77. } else {
  78. var buffer []byte = bytes.Repeat([]byte(" "), 16)
  79. buffer[w.Index] = '\xfe'
  80. return string(buffer)
  81. }
  82. }
  83. eanimate.Text = eanimate.UpdateF()
  84. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, eanimate)
  85. w.RemainingPanel = Panel{Width: 16,
  86. BorderColor: color,
  87. Style: SINGLE}
  88. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, Line{Text: " GAME "})
  89. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, Line{Text: " TIME REMAINING "})
  90. var rhour Line = Line{}
  91. rhour.UpdateF = func() string {
  92. var hours int = int(w.RemainingD.Hours())
  93. return fmt.Sprintf(" %02d HRS ", hours%100)
  94. }
  95. rhour.Text = rhour.UpdateF()
  96. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, rhour)
  97. var rminsec Line = Line{}
  98. rminsec.UpdateF = func() string {
  99. var mins int = int(w.RemainingD.Minutes()) % 60
  100. var secs int = int(w.RemainingD.Seconds()) % 60
  101. return fmt.Sprintf(" %02d MIN %02d SEC ", mins, secs)
  102. }
  103. rminsec.Text = rminsec.UpdateF()
  104. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, rminsec)
  105. var ranimate Line = Line{}
  106. ranimate.UpdateF = func() string {
  107. // Left to Right
  108. if Unicode {
  109. var buffer []rune = []rune(strings.Repeat(" ", 16))
  110. buffer[15-w.Index] = '\u25a0'
  111. return string(buffer)
  112. } else {
  113. var buffer []byte = bytes.Repeat([]byte(" "), 16)
  114. buffer[15-w.Index] = '\xfe'
  115. return string(buffer)
  116. }
  117. }
  118. ranimate.Text = ranimate.UpdateF()
  119. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, ranimate)
  120. }
  121. func (w *WOPR) Inc() {
  122. w.Index++
  123. if w.Index == 16 {
  124. w.Index = 0
  125. w.ElapsedPanel.Update()
  126. w.RemainingPanel.Update()
  127. w.RemainingD = time.Duration(w.RemainingD.Seconds()-1) * time.Second
  128. w.ElapsedD = time.Duration(w.ElapsedD.Seconds()+1) * time.Second
  129. }
  130. }
  131. func (w *WOPR) Animate(d *Door) {
  132. // til := time.Now().UnixNano() % int64(time.Second)
  133. // w.Index = int((til / int64(time.Microsecond)) / 62500)
  134. // either put the sync sleep in the go routine, or sleep and sync Index.
  135. // time.Sleep(time.Duration(int64(time.Second) - til)) // time.Now().UnixMilli()%1000) * time.Millisecond)
  136. // time.Second / 16
  137. // w.Ticker = time.NewTicker(time.Microsecond * time.Duration(62500))
  138. // w.Index = 0
  139. // Convert time.Time to time.Duration
  140. // This gives us consistency when resuming.
  141. w.ElapsedD = time.Since(w.Elapsed)
  142. w.RemainingD = time.Until(w.Remaining)
  143. w.StopIt = make(chan bool)
  144. go func(d *Door) {
  145. // til := time.Now().UnixNano() % int64(time.Second)
  146. sec16 := int64(time.Second) / 16
  147. // tilms := til % sec16
  148. w.Index = 0 // int(til / sec16)
  149. // log.Printf("til: %d, sec: %d, w.Index: %d\n", til, sec16, w.Index)
  150. // either put the sync sleep in the go routine, or sleep and sync Index.
  151. // time.Sleep(time.Second - time.Duration(til)) // sec16 - (til % sec16))) //int64(time.Second) - til)) // time.Now().UnixMilli()%1000) * time.Millisecond)
  152. // time.Second / 16
  153. w.Ticker = time.NewTicker(time.Duration(sec16)) // time.Microsecond * time.Duration(62500))
  154. var output string
  155. for {
  156. select {
  157. case <-w.StopIt:
  158. return
  159. case <-w.Ticker.C:
  160. w.ElapsedPanel.Update()
  161. w.RemainingPanel.Update()
  162. output = SavePos + w.ElapsedPanel.Output() + w.RemainingPanel.Output() + RestorePos
  163. if !d.Disconnect() {
  164. d.Write(output)
  165. } else {
  166. w.Ticker.Stop()
  167. return
  168. }
  169. w.Inc()
  170. }
  171. }
  172. }(d)
  173. }
  174. func (w *WOPR) Stop() {
  175. w.Ticker.Stop()
  176. w.StopIt <- true
  177. }