-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclock.go
70 lines (61 loc) · 1.65 KB
/
clock.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
package wrapper
import (
"context"
"math"
"time"
)
const (
// MarketOpenTick is the mc time at which a villagers begin their workday,
// hence are open for item trading.
MarketOpenTick int64 = 2000
// MarketCloseTick is the mc time at which villagers end their workday
// and begin socializing, trading is not available at this point.
MarketCloseTick int64 = 9000
// GameTickPerSecond is the minecraft game server tick runs at a fixed
// rate of 20 ticks per second.
GameTickPerSecond int = 20
// ClockSyncInterval is the interval where the wrapper clock will sync with the
// game tick rate. The wrapper and game tick can be skewed when the game lags.
ClockSyncInterval time.Duration = 30 * time.Second
)
// clock represents an internal wrapper clock, meant to be always in sync
// with the running game server clock (sync clock.Tick and game server tick).
type clock struct {
ticker *time.Ticker
syncTicker *time.Ticker
LastSync time.Time
Tick int
}
func newClock() *clock {
return &clock{
ticker: time.NewTicker(1 * time.Second),
syncTicker: time.NewTicker(ClockSyncInterval),
}
}
func (c *clock) start(ctx context.Context) {
go func() {
for {
select {
case <-ctx.Done():
return
case <-c.ticker.C:
c.Tick += GameTickPerSecond
}
}
}()
}
func (c *clock) stop() {
c.ticker.Stop()
}
func (c *clock) requestSync() <-chan time.Time {
return c.syncTicker.C
}
func (c *clock) resetLastSync() {
c.LastSync = time.Now()
}
func (c *clock) syncTick(t int) {
delay := time.Since(c.LastSync).Seconds()
delayRoundUp := int(math.Floor(delay))
tickOffset := delayRoundUp * GameTickPerSecond
c.Tick = t + tickOffset
}