Skip to content

Commit

Permalink
time restiction option added
Browse files Browse the repository at this point in the history
  • Loading branch information
Distortions81 committed Mar 24, 2024
1 parent ad30a69 commit 35bda89
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cfg/lcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type localOptions struct {
ResetDate int
ResetHour int
ResetPingRole string
PlayHourEnable bool
PlayStartHour int
PlayEndHour int

AutoStart bool
AutoUpdate bool
Expand Down
30 changes: 30 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,36 @@ var cmds = []Command{
Type: discordgo.ChatApplicationCommand,
},
Command: moderator.ConfigServer, ModeratorOnly: true},
{AppCmd: &discordgo.ApplicationCommand{
Name: "config-hours",
Description: "MOD ONLY: Set hours map can be played (GMT)",
Type: discordgo.ChatApplicationCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "start-hour",
Description: "hour to start server (GMT)",
Type: discordgo.ApplicationCommandOptionInteger,
MinValue: &valZero,
MaxValue: 23,
Required: false,
},
{
Name: "end-hour",
Description: "hour to stop server (GMT)",
Type: discordgo.ApplicationCommandOptionInteger,
MinValue: &valZero,
MaxValue: 23,
Required: false,
},
{
Name: "enabled",
Description: "hour limits enabled",
Type: discordgo.ApplicationCommandOptionBoolean,
Required: false,
},
},
},
Command: moderator.ConfigHours, ModeratorOnly: true},

{AppCmd: &discordgo.ApplicationCommand{
Name: "player-level",
Expand Down
44 changes: 44 additions & 0 deletions commands/moderator/hourLimits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package moderator

import (
"ChatWire/cfg"
"ChatWire/disc"
"fmt"
"strings"

"github.com/bwmarrin/discordgo"
)

func ConfigHours(s *discordgo.Session, i *discordgo.InteractionCreate) {
a := i.ApplicationCommandData()

buf := ""
for _, o := range a.Options {
lbuf := ""
if strings.EqualFold(o.Name, "enabled") {
cfg.Local.Options.PlayHourEnable = o.BoolValue()
lbuf = fmt.Sprintf("hour limits: %v", cfg.Local.Options.PlayHourEnable)
} else if strings.EqualFold(o.Name, "start-hour") {
arg := o.IntValue()
if arg > 0 && arg < 24 {
cfg.Local.Options.PlayStartHour = int(arg)
lbuf = fmt.Sprintf("start hour is (GMT): %v", arg)
}
} else if strings.EqualFold(o.Name, "end-hour") {
arg := o.IntValue()
if arg > 0 && arg < 24 {
cfg.Local.Options.PlayEndHour = int(arg)
lbuf = fmt.Sprintf("end hour is (GMT): %v", arg)
}
}
if lbuf != "" {
buf = buf + lbuf + "\n"
}
}
if buf != "" {
disc.EphemeralResponse(s, i, "Status:", buf)
cfg.WriteLCfg()
} else {
disc.EphemeralResponse(s, i, "Error:", "You didn't supply any options!")
}
}
4 changes: 4 additions & 0 deletions commands/user/newInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func Info(s *discordgo.Session, i *discordgo.InteractionCreate) {
}
}

if cfg.Local.Options.PlayHourEnable {
buf = buf + fmt.Sprintf("Time restrictions: %v - %v GMT.\n",
cfg.Local.Options.PlayStartHour, cfg.Local.Options.PlayEndHour)
}
if verbose {
buf = buf + fmt.Sprintf("%17v: %v\n", "Save name", fact.LastSaveName)
}
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ func main() {
/* Start Discord bot, don't wait for it.
* We want Factorio online even if Discord is down. */
go startbot()

if cfg.Local.Options.AutoStart {
fact.FactAutoStart = true
}
Expand Down
12 changes: 10 additions & 2 deletions support/mainLoops.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ func MainLoops() {
}

/* We aren't running, but should be! */
} else if !fact.FactIsRunning && fact.FactAutoStart && !fact.DoUpdateFactorio && !*glob.NoAutoLaunch {
} else if !fact.FactIsRunning &&
fact.FactAutoStart &&
!fact.DoUpdateFactorio &&
!*glob.NoAutoLaunch {
/* Don't relaunch if we are set to auto update */
launchFactorio()

if WithinHours() {
launchFactorio()
}
}
}
}()
Expand Down Expand Up @@ -785,4 +791,6 @@ func MainLoops() {
}
}()

go checkHours()

}
52 changes: 52 additions & 0 deletions support/supportUtil.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,68 @@
package support

import (
"fmt"
"os"
"strings"
"time"

"ChatWire/cfg"
"ChatWire/cwlog"
"ChatWire/disc"
"ChatWire/fact"
"ChatWire/glob"
"ChatWire/sclean"
)

func checkHours() {
for glob.ServerRunning {

if cfg.Local.Options.PlayHourEnable {

tn := time.Now().UTC()
shouldPlay := WithinHours()

if !shouldPlay && fact.FactIsRunning {
buf := fmt.Sprintf("It is now %v GMT, server will stop in 10 minutes.", tn.Hour())
fact.CMS(cfg.Local.Channel.ChatChannel, buf)

time.Sleep(time.Minute * 10)
fact.FactAutoStart = false
fact.QuitFactorio("Time is up...")
} else if shouldPlay && !fact.FactIsRunning {
buf := fmt.Sprintf("It is now %v GMT, server will now start.", tn.Hour())
fact.CMS(cfg.Local.Channel.ChatChannel, buf)
fact.FactAutoStart = true
}

}

time.Sleep(time.Minute)
}
}

func WithinHours() bool {

if cfg.Local.Options.PlayHourEnable {
curTime := time.Now().UTC().Hour()

if cfg.Local.Options.PlayStartHour > cfg.Local.Options.PlayEndHour {
if curTime <= cfg.Local.Options.PlayStartHour &&
curTime >= cfg.Local.Options.PlayEndHour {
return true
}
} else {
if curTime >= cfg.Local.Options.PlayStartHour &&
curTime <= cfg.Local.Options.PlayEndHour {
return true
}
}
return false
} else {
return true
}
}

/* Check if an idiot pasted their register code to a chat channel */
func ProtectIdiots(text string) bool {
idiotID := ""
Expand Down

0 comments on commit 35bda89

Please sign in to comment.