123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package door
- import (
- "bytes"
- "fmt"
- "strings"
- "time"
- )
- type WOPR struct {
- ElapsedPanel Panel
- Elapsed time.Time
- ElapsedD time.Duration
- RemainingPanel Panel
- Remaining time.Time
- RemainingD time.Duration
- Color string
- Index int
- Ticker *time.Ticker
- StopIt chan bool
- }
- func (w *WOPR) Clear() string {
- return w.ElapsedPanel.Clear() + w.RemainingPanel.Clear()
- }
- func (w *WOPR) Init(elapsed time.Time, remaining time.Time, color string) {
- if color == "" {
- color = ColorText("BLACK ON CYAN")
- }
- w.Elapsed = elapsed
- w.Remaining = remaining
- w.Index = 0
- w.ElapsedPanel = Panel{Width: 16,
- BorderColor: color,
- Style: SINGLE}
- w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, Line{Text: " GAME "})
- w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, Line{Text: " TIME ELAPSED "})
- var ehour Line = Line{}
- ehour.UpdateF = func() string {
- var hours int = int(w.ElapsedD.Hours())
- return fmt.Sprintf(" %02d HRS ", hours%100)
- }
- ehour.Text = ehour.UpdateF()
- w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, ehour)
- var eminsec Line = Line{}
- eminsec.UpdateF = func() string {
- var mins int = int(w.ElapsedD.Minutes()) % 60
- var secs int = int(w.ElapsedD.Seconds()) % 60
- return fmt.Sprintf(" %02d MIN %02d SEC ", mins, secs)
- }
- eminsec.Text = eminsec.UpdateF()
- w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, eminsec)
- var eanimate Line = Line{}
- eanimate.UpdateF = func() string {
-
- if Unicode {
- var buffer []rune = []rune(strings.Repeat(" ", 16))
- buffer[w.Index] = '\u25a0'
- return string(buffer)
- } else {
- var buffer []byte = bytes.Repeat([]byte(" "), 16)
- buffer[w.Index] = '\xfe'
- return string(buffer)
- }
- }
- eanimate.Text = eanimate.UpdateF()
- w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, eanimate)
- w.RemainingPanel = Panel{Width: 16,
- BorderColor: color,
- Style: SINGLE}
- w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, Line{Text: " GAME "})
- w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, Line{Text: " TIME REMAINING "})
- var rhour Line = Line{}
- rhour.UpdateF = func() string {
- var hours int = int(w.RemainingD.Hours())
- return fmt.Sprintf(" %02d HRS ", hours%100)
- }
- rhour.Text = rhour.UpdateF()
- w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, rhour)
- var rminsec Line = Line{}
- rminsec.UpdateF = func() string {
- var mins int = int(w.RemainingD.Minutes()) % 60
- var secs int = int(w.RemainingD.Seconds()) % 60
- return fmt.Sprintf(" %02d MIN %02d SEC ", mins, secs)
- }
- rminsec.Text = rminsec.UpdateF()
- w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, rminsec)
- var ranimate Line = Line{}
- ranimate.UpdateF = func() string {
-
- if Unicode {
- var buffer []rune = []rune(strings.Repeat(" ", 16))
- buffer[15-w.Index] = '\u25a0'
- return string(buffer)
- } else {
- var buffer []byte = bytes.Repeat([]byte(" "), 16)
- buffer[15-w.Index] = '\xfe'
- return string(buffer)
- }
- }
- ranimate.Text = ranimate.UpdateF()
- w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, ranimate)
- }
- func (w *WOPR) Inc() {
- w.Index++
- if w.Index == 16 {
- w.Index = 0
- w.ElapsedPanel.Update()
- w.RemainingPanel.Update()
- w.RemainingD = time.Duration(w.RemainingD.Seconds()-1) * time.Second
- w.ElapsedD = time.Duration(w.ElapsedD.Seconds()+1) * time.Second
- }
- }
- func (w *WOPR) Animate(d *Door) {
-
-
-
-
-
-
-
-
-
- w.ElapsedD = time.Since(w.Elapsed)
- w.RemainingD = time.Until(w.Remaining)
- w.StopIt = make(chan bool)
- go func(d *Door) {
-
- sec16 := int64(time.Second) / 16
-
- w.Index = 0
-
-
-
-
- w.Ticker = time.NewTicker(time.Duration(sec16))
- var output string
- for {
- select {
- case <-w.StopIt:
- return
- case <-w.Ticker.C:
- w.ElapsedPanel.Update()
- w.RemainingPanel.Update()
- output = SavePos + w.ElapsedPanel.Output() + w.RemainingPanel.Output() + RestorePos
- if !d.Disconnect() {
- d.Write(output)
- } else {
- w.Ticker.Stop()
- return
- }
- w.Inc()
- }
- }
- }(d)
- }
- func (w *WOPR) Stop() {
- w.Ticker.Stop()
- w.StopIt <- true
- }
|