wopr.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package door
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  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. output *bytes.Buffer
  38. }
  39. const DEBUG_WOPR bool = true
  40. func (w *WOPR) Clear() []byte {
  41. var output bytes.Buffer
  42. output.Write(w.ElapsedPanel.Clear())
  43. output.Write(w.RemainingPanel.Clear())
  44. return output.Bytes()
  45. }
  46. // Initialize, Set X, Y on Panels, Animate()
  47. // Initialize, and create panels
  48. func (w *WOPR) Init(elapsed time.Time, remaining time.Time, color []byte) {
  49. if len(color) == 0 {
  50. color = ColorText("BLACK ON CYAN")
  51. }
  52. w.output = &bytes.Buffer{}
  53. w.Elapsed = elapsed
  54. w.Remaining = remaining
  55. w.Index = 0
  56. w.ElapsedPanel = Panel{Width: 16,
  57. BorderColor: color,
  58. Style: SINGLE}
  59. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, NewLine(" GAME "))
  60. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, NewLine(" TIME ELAPSED "))
  61. var ehour *Line = &Line{}
  62. ehour.UpdateF = func(u *bytes.Buffer) {
  63. var hours int = int(w.ElapsedD.Hours())
  64. u.Reset()
  65. /*
  66. // This does not change anything in the benchmark.
  67. u.WriteString(" ")
  68. u.WriteByte(byte(int('0') + (hours%100)/10))
  69. u.WriteByte(byte(int('0') + (hours%100)%10))
  70. u.WriteString(" HRS ")
  71. */
  72. fmt.Fprintf(u, " %02d HRS ", hours%100)
  73. }
  74. ehour.Update()
  75. // ehour.Text.Write(ehour.update)
  76. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, ehour)
  77. var eminsec *Line = &Line{}
  78. eminsec.UpdateF = func(u *bytes.Buffer) {
  79. var mins int = int(w.ElapsedD.Minutes()) % 60
  80. var secs int = int(w.ElapsedD.Seconds()) % 60
  81. u.Reset()
  82. fmt.Fprintf(u, " %02d MIN %02d SEC ", mins, secs)
  83. }
  84. eminsec.Update() // Text.Write(eminsec.UpdateF())
  85. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, eminsec)
  86. var eanimate *Line = &Line{}
  87. eanimate.UpdateF = func(u *bytes.Buffer) {
  88. u.Reset()
  89. // Left to Right
  90. if Unicode {
  91. for i := 0; i < 16; i++ {
  92. if i == w.Index {
  93. u.WriteRune('\u25a0')
  94. } else {
  95. u.WriteByte(' ')
  96. }
  97. }
  98. /*
  99. var buffer []rune = []rune(strings.Repeat(" ", 16))
  100. buffer[w.Index] = '\u25a0'
  101. u.WriteString(string(buffer))
  102. */
  103. } else {
  104. for i := 0; i < 16; i++ {
  105. if i == w.Index {
  106. u.WriteByte('\xfe')
  107. } else {
  108. u.WriteByte(' ')
  109. }
  110. }
  111. /*
  112. var buffer []byte = bytes.Repeat([]byte(" "), 16)
  113. buffer[w.Index] = '\xfe'
  114. u.WriteString(string(buffer))
  115. */
  116. }
  117. }
  118. eanimate.Update() // Text.Write(eanimate.UpdateF())
  119. w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, eanimate)
  120. w.RemainingPanel = Panel{Width: 16,
  121. BorderColor: color,
  122. Style: SINGLE}
  123. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, NewLine(" GAME "))
  124. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, NewLine(" TIME REMAINING "))
  125. var rhour *Line = &Line{}
  126. rhour.UpdateF = func(u *bytes.Buffer) {
  127. var hours int = int(w.RemainingD.Hours())
  128. u.Reset()
  129. /*
  130. u.WriteString(" ")
  131. u.WriteByte(byte(int('0') + (hours%100)/10))
  132. u.WriteByte(byte(int('0') + (hours%100)%10))
  133. u.WriteString(" HRS ")
  134. */
  135. fmt.Fprintf(u, " %02d HRS ", hours%100)
  136. }
  137. rhour.Update() // Text.Write(rhour.UpdateF())
  138. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, rhour)
  139. var rminsec *Line = &Line{}
  140. rminsec.UpdateF = func(u *bytes.Buffer) {
  141. var mins int = int(w.RemainingD.Minutes()) % 60
  142. var secs int = int(w.RemainingD.Seconds()) % 60
  143. u.Reset()
  144. fmt.Fprintf(u, " %02d MIN %02d SEC ", mins, secs)
  145. }
  146. rminsec.Update() // Text.Write(rminsec.UpdateF())
  147. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, rminsec)
  148. var ranimate *Line = &Line{}
  149. ranimate.UpdateF = func(u *bytes.Buffer) {
  150. u.Reset()
  151. // Left to Right
  152. if Unicode {
  153. // Changing to for loop from buffers did 0.
  154. for i := 0; i < 16; i++ {
  155. if i == 15-w.Index {
  156. u.WriteRune('\u25a0')
  157. } else {
  158. u.WriteByte(' ')
  159. }
  160. }
  161. /*
  162. var buffer []rune = []rune(strings.Repeat(" ", 16))
  163. buffer[15-w.Index] = '\u25a0'
  164. u.WriteString(string(buffer))
  165. */
  166. } else {
  167. for i := 0; i < 16; i++ {
  168. if i == 15-w.Index {
  169. u.WriteByte('\xfe')
  170. } else {
  171. u.WriteByte(' ')
  172. }
  173. }
  174. // var buffer []byte = bytes.Repeat([]byte{' '}, 16)
  175. // No change here.
  176. /*
  177. var buffer [16]byte = [16]byte{' ', ' ', ' ', ' ',
  178. ' ', ' ', ' ', ' ',
  179. ' ', ' ', ' ', ' ',
  180. ' ', ' ', ' ', ' '}
  181. */
  182. // bytes.Repeat([]byte(" "), 16)
  183. /*
  184. buffer[15-w.Index] = '\xfe'
  185. u.Write(buffer[:])
  186. // u.WriteString(string(buffer))
  187. */
  188. }
  189. }
  190. ranimate.Update() // Text.Write(ranimate.UpdateF())
  191. w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, ranimate)
  192. }
  193. func (w *WOPR) Inc() {
  194. w.Index++
  195. if w.Index == 16 {
  196. w.Index = 0
  197. w.ElapsedPanel.Update()
  198. w.RemainingPanel.Update()
  199. w.RemainingD = time.Duration(w.RemainingD.Seconds()-1) * time.Second
  200. w.ElapsedD = time.Duration(w.ElapsedD.Seconds()+1) * time.Second
  201. }
  202. }
  203. var woprBytesUsed int
  204. func (w *WOPR) Output() []byte {
  205. w.output.Reset()
  206. w.output.WriteString(SavePos)
  207. w.output.Write(w.ElapsedPanel.Output())
  208. w.output.Write(w.RemainingPanel.Output())
  209. w.output.WriteString(RestorePos)
  210. if DEBUG_WOPR {
  211. if w.output.Cap() > woprBytesUsed {
  212. woprBytesUsed = w.output.Cap()
  213. log.Printf("WOPR: now %d\n", woprBytesUsed)
  214. }
  215. }
  216. return w.output.Bytes()
  217. }
  218. func (w *WOPR) Animate(d *Door) {
  219. // til := time.Now().UnixNano() % int64(time.Second)
  220. // w.Index = int((til / int64(time.Microsecond)) / 62500)
  221. // either put the sync sleep in the go routine, or sleep and sync Index.
  222. // time.Sleep(time.Duration(int64(time.Second) - til)) // time.Now().UnixMilli()%1000) * time.Millisecond)
  223. // time.Second / 16
  224. // w.Ticker = time.NewTicker(time.Microsecond * time.Duration(62500))
  225. // w.Index = 0
  226. // Convert time.Time to time.Duration
  227. // This gives us consistency when resuming.
  228. w.ElapsedD = time.Since(w.Elapsed)
  229. w.RemainingD = time.Until(w.Remaining)
  230. w.StopIt = make(chan bool)
  231. go func(d *Door) {
  232. // til := time.Now().UnixNano() % int64(time.Second)
  233. sec16 := int64(time.Second) / 16
  234. // tilms := til % sec16
  235. w.Index = 0 // int(til / sec16)
  236. // log.Printf("til: %d, sec: %d, w.Index: %d\n", til, sec16, w.Index)
  237. // either put the sync sleep in the go routine, or sleep and sync Index.
  238. // time.Sleep(time.Second - time.Duration(til)) // sec16 - (til % sec16))) //int64(time.Second) - til)) // time.Now().UnixMilli()%1000) * time.Millisecond)
  239. // time.Second / 16
  240. w.Ticker = time.NewTicker(time.Duration(sec16))
  241. // var output bytes.Buffer
  242. for {
  243. select {
  244. case <-w.StopIt:
  245. return
  246. case <-w.Ticker.C:
  247. w.ElapsedPanel.Update()
  248. w.RemainingPanel.Update()
  249. //var output []byte = w.Output()
  250. if !d.Writer.IsClosed() {
  251. // d.Write(output)
  252. d.Write(w.Output())
  253. } else {
  254. w.Ticker.Stop()
  255. return
  256. }
  257. w.Inc()
  258. }
  259. }
  260. }(d)
  261. }
  262. func (w *WOPR) Stop() {
  263. w.Ticker.Stop()
  264. w.StopIt <- true
  265. }