123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- package door
- import (
- "bytes"
- "fmt"
- "log"
- "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
- output *bytes.Buffer
- }
- const DEBUG_WOPR bool = true
- func (w *WOPR) Clear() []byte {
- var output bytes.Buffer
- output.Write(w.ElapsedPanel.Clear())
- output.Write(w.RemainingPanel.Clear())
- return output.Bytes()
- }
- func (w *WOPR) Init(elapsed time.Time, remaining time.Time, color string) {
- if color == "" {
- color = ColorText("BLACK ON CYAN")
- }
- w.output = &bytes.Buffer{}
- 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, NewLine(" GAME "))
- w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, NewLine(" TIME ELAPSED "))
- var ehour Line = Line{}
- ehour.UpdateF = func(u *bytes.Buffer) {
- var hours int = int(w.ElapsedD.Hours())
- u.Reset()
-
- fmt.Fprintf(u, " %02d HRS ", hours%100)
- }
- ehour.Update()
-
- w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, ehour)
- var eminsec Line = Line{}
- eminsec.UpdateF = func(u *bytes.Buffer) {
- var mins int = int(w.ElapsedD.Minutes()) % 60
- var secs int = int(w.ElapsedD.Seconds()) % 60
- u.Reset()
- fmt.Fprintf(u, " %02d MIN %02d SEC ", mins, secs)
- }
- eminsec.Update()
- w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, eminsec)
- var eanimate Line = Line{}
- eanimate.UpdateF = func(u *bytes.Buffer) {
- u.Reset()
-
- if Unicode {
- for i := 0; i < 16; i++ {
- if i == w.Index {
- u.WriteRune('\u25a0')
- } else {
- u.WriteByte(' ')
- }
- }
-
- } else {
- for i := 0; i < 16; i++ {
- if i == w.Index {
- u.WriteRune('\xfe')
- } else {
- u.WriteByte(' ')
- }
- }
-
- }
- }
- eanimate.Update()
- w.ElapsedPanel.Lines = append(w.ElapsedPanel.Lines, eanimate)
- w.RemainingPanel = Panel{Width: 16,
- BorderColor: color,
- Style: SINGLE}
- w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, NewLine(" GAME "))
- w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, NewLine(" TIME REMAINING "))
- var rhour Line = Line{}
- rhour.UpdateF = func(u *bytes.Buffer) {
- var hours int = int(w.RemainingD.Hours())
- u.Reset()
-
- fmt.Fprintf(u, " %02d HRS ", hours%100)
- }
- rhour.Update()
- w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, rhour)
- var rminsec Line = Line{}
- rminsec.UpdateF = func(u *bytes.Buffer) {
- var mins int = int(w.RemainingD.Minutes()) % 60
- var secs int = int(w.RemainingD.Seconds()) % 60
- u.Reset()
- fmt.Fprintf(u, " %02d MIN %02d SEC ", mins, secs)
- }
- rminsec.Update()
- w.RemainingPanel.Lines = append(w.RemainingPanel.Lines, rminsec)
- var ranimate Line = Line{}
- ranimate.UpdateF = func(u *bytes.Buffer) {
- u.Reset()
-
- if Unicode {
-
- for i := 0; i < 16; i++ {
- if i == 15-w.Index {
- u.WriteRune('\u25a0')
- } else {
- u.WriteByte(' ')
- }
- }
-
- } else {
- for i := 0; i < 16; i++ {
- if i == 15-w.Index {
- u.WriteByte('\xfe')
- } else {
- u.WriteByte(' ')
- }
- }
-
-
-
-
-
- }
- }
- ranimate.Update()
- 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
- }
- }
- var woprBytesUsed int
- func (w *WOPR) Output() []byte {
- w.output.Reset()
- w.output.WriteString(SavePos)
- w.output.Write(w.ElapsedPanel.Output())
- w.output.Write(w.RemainingPanel.Output())
- w.output.WriteString(RestorePos)
- if DEBUG_WOPR {
- if w.output.Cap() > woprBytesUsed {
- woprBytesUsed = w.output.Cap()
- log.Printf("WOPR: now %d\n", woprBytesUsed)
- }
- }
- return w.output.Bytes()
- }
- 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))
-
- for {
- select {
- case <-w.StopIt:
- return
- case <-w.Ticker.C:
- w.ElapsedPanel.Update()
- w.RemainingPanel.Update()
-
- if !d.Writer.IsClosed() {
-
- d.Write(w.Output())
- } else {
- w.Ticker.Stop()
- return
- }
- w.Inc()
- }
- }
- }(d)
- }
- func (w *WOPR) Stop() {
- w.Ticker.Stop()
- w.StopIt <- true
- }
|