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
Show file tree
Hide file tree
Changes from all 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
18 changes: 12 additions & 6 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 RconClient) (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])
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 RconClient) {
_, _ = 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
40 changes: 40 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,43 @@ func Test_sendMessageToFactorio(t *testing.T) {
})
}
}

func Test_updatePlayerCount(t *testing.T) {
log = logrus.New()
log.Out = io.Discard

type args struct {
playersOnline string
expected string
}
tests := []struct {
name string
args args
}{
{"0 players online", args{playersOnline: "Online players (0):", expected: "the world burn"}},
{"1 players online", args{playersOnline: "Online players (1):", expected: "the factory grow"}},
{"123 players online", args{playersOnline: "Online players (123):", expected: "the factory grow"}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
discordActivities = make(chan discordgo.Activity, 1)
mockRconClient := new(MockRconClient)
// Return the amount of players online when Execute is called
mockRconClient.On("Execute", "/players online count").Return(tt.args.playersOnline, nil)
// Execute the funtion tested
updatePlayerCount(mockRconClient)
// Make sure we did a call to the RCON client
mockRconClient.AssertCalled(t, "Execute", "/players online count")
// Read back the activity from the channel, and make sure it is what we expect
activity := <-discordActivities
if activity.Type != discordgo.ActivityTypeWatching {
t.Errorf("updatePlayerCount() = '%v', want '%v'", activity.Type, discordgo.ActivityTypeWatching)
}
if activity.Name != tt.args.expected {
t.Errorf("updatePlayerCount() = '%v', want '%v'", activity.Name, tt.args.expected)
}
close(discordActivities)
})
}
}
Loading