Skip to content

Commit

Permalink
Added logging of death count (with mod version >= 0.6.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattie112 committed Feb 26, 2022
1 parent c0308ee commit 6a0a72f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
29 changes: 25 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func parseAndFormatMessage(message string) string {

switch messageType[1] {
case "FactoriGOChatBot":
// Extracted to keep function small
// Extracted to keep this function small
return parseModLogEntries(message)
case "CHAT":
var re = regexp.MustCompile(`(?m)] (.*): (.*)`)
Expand Down Expand Up @@ -123,12 +123,33 @@ func parseModLogEntries(message string) string {
case "PLAYER_DIED":
var re = regexp.MustCompile(`(?m):([\w -]*)+`)
match := re.FindAllStringSubmatch(message, -1)
if len(match) == 3 {
return fmt.Sprintf(":skull: | Player died: `%s`, cause: `%s`", match[1][1], match[2][1])
}

// No cause
if len(match) == 2 {
return fmt.Sprintf(":skull: | Player died: `%s` (unknown cause)", match[1][1])
}

cause := match[2][1]
addText := ""

if cause == "locomotive" || cause == "cargo-wagon" || cause == "artillery-wagon" || cause == "fluid-wagon" {
addText = " (hahaha!)"
}

if cause == "cargo-wagon" || cause == "artillery-wagon" || cause == "fluid-wagon" {
addText = " (hahaha! how the hell did you do that?!?!)"
}

// Only cause (companion mod <= 0.5.0
if len(match) == 3 {
return fmt.Sprintf(":skull: | Player died: `%s`, cause: `%s`%s", match[1][1], cause, addText)
}

// Cause and death count (companion mod >= 0.6.0)
if len(match) == 5 {
return fmt.Sprintf(":skull: | Player died: `%s`, cause: `%s`%s (%s times out of %s deaths)", match[1][1], cause, addText, match[3][1], match[4][1])
}

return ""
default:
log.WithField("message", message).Debug("Could not parse message from mod, ignoring")
Expand Down
14 changes: 11 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ func Test_parseAndFormatMessage(t *testing.T) {
{"CHAT", args{message: "2022-02-01 15:31:30 [CHAT] Mattie: Some chat message"}, ":speech_left: | `Mattie`: Some chat message"},
// Messages below are generated by the companion mod, but I still want them to go through the normal flow!
{"PLAYER_DIED", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie]\""}, ":skull: | Player died: `Mattie` (unknown cause)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:locomotive]\""}, ":skull: | Player died: `Mattie`, cause: `locomotive`"},
{"PLAYER_DIED_CAUSE2", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:big worm]\""}, ":skull: | Player died: `Mattie`, cause: `big worm`"},
{"PLAYER_DIED_CAUSE2", args{message: "[[FactoriGOChatBot]: \"11083545 [PLAYER_DIED:Vance307:behemoth-spitter]\""}, ":skull: | Player died: `Vance307`, cause: `behemoth-spitter`"},
// Player died messages
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:locomotive]\""}, ":skull: | Player died: `Mattie`, cause: `locomotive` (hahaha!)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:cargo-wagon]\""}, ":skull: | Player died: `Mattie`, cause: `cargo-wagon` (hahaha! how the hell did you do that?!?!)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:big worm]\""}, ":skull: | Player died: `Mattie`, cause: `big worm`"},
{"PLAYER_DIED_CAUSE", args{message: "[[FactoriGOChatBot]: \"11083545 [PLAYER_DIED:Vance307:behemoth-spitter]\""}, ":skull: | Player died: `Vance307`, cause: `behemoth-spitter`"},
// Player died (with counts, companion mod > 0.6.0)
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:locomotive:10:50]\""}, ":skull: | Player died: `Mattie`, cause: `locomotive` (hahaha!) (10 times out of 50 deaths)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:cargo-wagon:10:50]\""}, ":skull: | Player died: `Mattie`, cause: `cargo-wagon` (hahaha! how the hell did you do that?!?!) (10 times out of 50 deaths)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Vance307:behemoth-spitter:1:1]\""}, ":skull: | Player died: `Vance307`, cause: `behemoth-spitter` (1 times out of 1 deaths)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:character:1:2]\""}, ":skull: | Player died: `Mattie`, cause: `character` (1 times out of 2 deaths)"},
// Research
{"RESEARCH_STARTED", args{message: "[FactoriGOChatBot]: \"3045105 [RESEARCH_STARTED:nuclear-power]\""}, ":microscope: | Research started: `nuclear-power`"},
{"RESEARCH_FINISHED", args{message: "[FactoriGOChatBot]: \"3229214 [RESEARCH_FINISHED:nuclear-power]\""}, ":microscope: | Research finished: `nuclear-power`"},
// Corrupted messages (as I don't know yet how to fix the file read, so it will have a single line guaranteed
Expand Down

0 comments on commit 6a0a72f

Please sign in to comment.