forked from guozanhua/waiter
/
game_timing.go
73 lines (57 loc) · 1.45 KB
/
game_timing.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
package main
import (
"time"
"github.com/sauerbraten/waiter/enet"
)
var (
// channel to pause the game
pauseChannel = make(chan bool)
// channel to interrupt the game (for example when a master changes mode or map mid-game)
interruptChannel = make(chan bool)
)
const (
TEN_MINUTES int32 = 180000 // 3 minutes for testing and debugging purposes
)
func countDown() {
endTimer := time.NewTimer(time.Duration(state.TimeLeft) * time.Millisecond)
gameTicker := time.NewTicker(1 * time.Millisecond)
paused := false
for {
select {
case <-gameTicker.C:
state.TimeLeft--
case shouldPause := <-pauseChannel:
if shouldPause && !paused {
endTimer.Stop()
gameTicker.Stop()
paused = true
} else if !shouldPause && paused {
endTimer.Reset(time.Duration(state.TimeLeft) * time.Millisecond)
gameTicker = time.NewTicker(1 * time.Millisecond)
paused = false
}
case <-interruptChannel:
endTimer.Stop()
gameTicker.Stop()
case <-endTimer.C:
endTimer.Stop()
gameTicker.Stop()
go intermission()
return
}
}
}
func intermission() {
// notify all clients
clients.send(enet.PACKET_FLAG_RELIABLE, 1, NewPacket(N_TIMELEFT, 0))
// start 5 second timer
end := time.After(5 * time.Second)
// TODO: send server messages with some top stats
// wait for timer to finish
<-end
// start new 10 minutes timer
state.TimeLeft = TEN_MINUTES
go countDown()
// change map
state.changeMap(mapRotation.nextMap(state.Map))
}