-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (61 loc) · 1.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bufio"
"fmt"
"iqhater/tick_based_game_time/events"
"iqhater/tick_based_game_time/ui"
"log"
"os"
"strconv"
"time"
"github.com/fatih/color"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
ui.ShowUI()
// ticks store
var totalTicks time.Duration
var minEventID, maxEventID = 1, 6
for scanner.Scan() {
parsedNumber, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Printf("%v\n", err)
continue
}
if parsedNumber < minEventID || parsedNumber > maxEventID {
log.Printf("%v\n", "Unknown action number!")
continue
}
var action events.Ticker
action = events.NewEvent(parsedNumber)
ticker := time.NewTicker(action.Tick())
done := make(chan struct{})
go func() {
for {
// guarantee to be executed
select {
case <-done:
return
default:
}
select {
case <-done:
return
case t := <-ticker.C:
totalTicks += action.ElapsedTimes()
fmt.Printf("EVENT TYPE: %s ITEM ID: %d TICK: %v \n", action.EventTitle(), action.ItemID(), t.Round(time.Millisecond))
}
}
}()
time.Sleep(3 * time.Second)
ticker.Stop()
done <- struct{}{}
close(done)
totalTime := color.New(color.FgYellow).PrintfFunc()
totalTime("Total time: %s\n", totalTicks.Round(time.Millisecond))
ui.ShowUI()
}
if err := scanner.Err(); err != nil {
panic(err)
}
}