-
Notifications
You must be signed in to change notification settings - Fork 3
/
game.go
146 lines (136 loc) · 3.26 KB
/
game.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"os"
"time"
pubnub "github.com/pubnub/go"
"github.com/gosuri/uiprogress"
term "github.com/nsf/termbox-go"
)
func countdown(hostName string, guestName string) {
fmt.Println("")
fmt.Println("The game is about to start between " + hostName + " and " + guestName + "!")
fmt.Println("Alternate pressing SPACE and the RIGHT ARROW KEY to race!")
fmt.Println("")
time.Sleep(750 * time.Millisecond)
fmt.Print("3... ")
time.Sleep(1 * time.Second)
fmt.Print("2... ")
time.Sleep(1 * time.Second)
fmt.Print("1... ")
time.Sleep(1 * time.Second)
fmt.Print("Race!!")
fmt.Println("")
time.Sleep(500 * time.Millisecond)
}
func startGame(isHost bool, lobby string, hostName string, guestName string, pn *pubnub.PubNub) {
var (
spaced bool
progress int
winner string
)
data := make(map[string]interface{})
countdown(hostName, guestName) // Countdown before starting game.
uiprogress.Start()
hostBar := uiprogress.AddBar(100).AppendCompleted()
hostBar.AppendFunc(func(b *uiprogress.Bar) string {
if isHost {
return hostName + " (you)"
}
return hostName + " (host)"
})
guestBar := uiprogress.AddBar(100).AppendCompleted()
guestBar.AppendFunc(func(b *uiprogress.Bar) string {
if !isHost {
return guestName + " (you)"
}
return guestName + " (guest)"
})
gamelistener := pubnub.NewListener()
go func() {
for {
select {
case message := <-gamelistener.Message:
if msg, ok := message.Message.(map[string]interface{}); ok {
if val, ok := msg["guestProgress"]; ok {
guestBar.Set(int(val.(float64)))
}
if val, ok := msg["hostProgress"]; ok {
hostBar.Set(int(val.(float64)))
}
}
}
}
}()
pn.AddListener(gamelistener)
pn.Subscribe().
Channels([]string{lobby}).
Execute()
err := term.Init()
if err != nil {
panic(err)
}
defer term.Close()
keyPressListenerLoop:
for {
if hostBar.Current() == hostBar.Total { // Check for winner.
winner = hostName
break keyPressListenerLoop
} else if guestBar.Current() == guestBar.Total {
winner = guestName
break keyPressListenerLoop
}
event := term.PollEvent()
switch {
case event.Key == term.KeyEsc:
break keyPressListenerLoop
case event.Key == term.KeySpace:
if !spaced {
progress = progress + 1
if isHost {
if hostBar.Current() < hostBar.Total {
data["hostProgress"] = progress
pn.Publish().
Channel(lobby).
Message(data).
Execute()
}
} else {
if guestBar.Current() < guestBar.Total {
data["guestProgress"] = progress
pn.Publish().
Channel(lobby).
Message(data).
Execute()
}
}
spaced = true
}
case event.Key == term.KeyArrowRight: // Prevents the player from using keyrepeat to cheat.
spaced = false
}
}
uiprogress.Stop()
term.Close()
pn.Unsubscribe().
Channels([]string{lobby}).
Execute()
fmt.Println("")
if winner != "" {
fmt.Println(winner + " won the game!")
} else {
fmt.Println("You left the game.") // The other player wins if the player leaves the game.
if isHost {
data["guestProgress"] = guestBar.Total
} else {
data["hostProgress"] = hostBar.Total
}
pn.Publish().
Channel(lobby).
Message(data).
Execute()
}
fmt.Println("")
fmt.Println("Thanks for playing!")
os.Exit(0)
}