Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/players_online_count #39

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,13 @@ func parseAndFormatMessage(message string) string {

return fmt.Sprintf(":speech_left: | `%s`: %s", match[1], messageContent)
case "JOIN":
playersOnline += 1
if !config.sendJoinLeave {
return ""
}
var re = regexp.MustCompile(`(?m)] (\w*)`)
match := re.FindStringSubmatch(message)
return fmt.Sprintf(":green_circle: | `%s` joined the game!", match[1])
case "LEAVE":
playersOnline -= 1
if !config.sendJoinLeave {
return ""
}
Expand Down Expand Up @@ -398,17 +396,22 @@ func getSeedFromFactorio(rconClient *rcon.Client) string {
return seed
}

func updatePlayerCount(rconClient *rcon.Client) {
func getPlayersOnlineFromFactorio(rconClient *rcon.Client) (int, error) {
msg, err := rconClient.Execute("/players online count")
if err != nil {
log.WithFields(logrus.Fields{"err": err}).Error("Could not get player count from Factorio")
return
return 0, err
}
playersOnline, err = strconv.Atoi(strings.Split(strings.Split(msg, "(")[1], ")")[0])
playersOnline, err := strconv.Atoi(strings.Split(strings.Split(msg, "(")[1], ")")[0])
Mattie112 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.WithFields(logrus.Fields{"err": err}).Panic("Could not parse player count from Factorio")
return
return 0, err
}
return playersOnline, nil
}

func updatePlayerCount(rconClient *rcon.Client) {
getPlayersOnlineFromFactorio(rconClient)
if playersOnline > 0 {
updateDiscordStatus(discordgo.ActivityTypeWatching, "the factory grow")
} else {
Expand Down Expand Up @@ -446,7 +449,10 @@ func handleCommands(rconClient *rcon.Client) {
for command := range commands {
switch command {
case "!online":
messagesToDiscord <- strconv.Itoa(playersOnline) + " players online"
playersOnline, err := getPlayersOnlineFromFactorio(rconClient)
if err == nil {
messagesToDiscord <- strconv.Itoa(playersOnline) + " players online"
}
break
case "!seed":
messagesToDiscord <- getSeedFromFactorio(rconClient)
Expand Down
Loading