sisyphus: config-ize timing

This commit is contained in:
Chris Sexton 2017-11-06 14:32:49 -05:00
parent 827879b72d
commit df956513c2
3 changed files with 24 additions and 11 deletions

View File

@ -103,6 +103,10 @@ type Config struct {
Inventory struct { Inventory struct {
Max int Max int
} }
Sisyphus struct {
MinDecrement int
MinPush int
}
} }
func init() { func init() {

View File

@ -105,5 +105,14 @@ config = {
}, },
DBPath = "stats.db" DBPath = "stats.db"
}, },
HttpAddr = "127.0.0.1:1337" HttpAddr = "127.0.0.1:1337",
Inventory = {
Max = 5
},
Sisyphus = {
MinDecrement = 10,
MinPush = 1
}
}
} }

View File

@ -29,7 +29,6 @@ type game struct {
who string who string
start time.Time start time.Time
size int size int
hardness int
current int current int
nextPush time.Time nextPush time.Time
nextDec time.Time nextDec time.Time
@ -41,13 +40,12 @@ type game struct {
func NewRandomGame(bot bot.Bot, channel, who string) *game { func NewRandomGame(bot bot.Bot, channel, who string) *game {
size := rand.Intn(9) + 2 size := rand.Intn(9) + 2
g := game{ g := game{
channel: channel, channel: channel,
bot: bot, bot: bot,
who: who, who: who,
start: time.Now(), start: time.Now(),
size: size, size: size,
hardness: rand.Intn(9) + 1, current: size / 2,
current: size / 2,
} }
g.id = bot.SendMessage(channel, g.toMessageString()) g.id = bot.SendMessage(channel, g.toMessageString())
@ -61,7 +59,8 @@ func (g *game) scheduleDecrement() {
if g.timers[0] != nil { if g.timers[0] != nil {
g.timers[0].Stop() g.timers[0].Stop()
} }
g.nextDec = time.Now().Add(time.Duration((60 + rand.Intn(60*5))) * time.Second) minDec := g.bot.Config().Sisyphus.MinDecrement
g.nextDec = time.Now().Add(time.Duration((60 + rand.Intn(60*minDec))) * time.Minute)
go func() { go func() {
t := time.NewTimer(g.nextDec.Sub(time.Now())) t := time.NewTimer(g.nextDec.Sub(time.Now()))
g.timers[0] = t g.timers[0] = t
@ -76,7 +75,8 @@ func (g *game) schedulePush() {
if g.timers[1] != nil { if g.timers[1] != nil {
g.timers[1].Stop() g.timers[1].Stop()
} }
g.nextPush = time.Now().Add(time.Duration(rand.Intn(60)+1) * time.Minute) minPush := g.bot.Config().Sisyphus.MinPush
g.nextPush = time.Now().Add(time.Duration(rand.Intn(60)+minPush) * time.Minute)
go func() { go func() {
t := time.NewTimer(g.nextPush.Sub(time.Now())) t := time.NewTimer(g.nextPush.Sub(time.Now()))
g.timers[1] = t g.timers[1] = t