wopr.go 5.3 KB

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