Skip to content

Commit

Permalink
Configurable messages (#34)
Browse files Browse the repository at this point in the history
* make hardcoded messages configurable

* flip Disconnect/Connect SimpleMessage example

* replace if statement with switch
  • Loading branch information
granny authored Apr 15, 2020
1 parent 848b19a commit 088dcc3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
20 changes: 20 additions & 0 deletions server/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,23 @@ Enabled = false

# The port to run the UI web server on
Port = 8080

[Messages]

# Format for all events if SimpleMessage is enabled
EventFormatSimple = "%event%: %data%"

# Map start format if SimpleMessage is enabled
EventFormatSimpleMapStart = "Map Start: %data%"

# Map end format if SimpleMessage is enabled
EventFormatSimpleMapEnd = "Map Ended: %data%"

# Player connect format if SimpleMessage is enabled
EventFormatSimplePlayerConnect = "Player Connected: %data%"

# Player disconnect format if SimpleMessage is enabled
EventFormatSimplePlayerDisconnect = "Player Disconnected: %data%"

# Player chat format if SimpleMessage is enabled
EventFormatSimplePlayerChat = "%username%: %message%"
10 changes: 10 additions & 0 deletions server/config/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,19 @@ type UIConfig struct {
Port int `toml:"Port"`
}

type MsgConfig struct {
EventFormatSimple string `toml:"EventFormatSimple"`
EventFormatSimpleMapStart string `toml:"EventFormatSimpleMapStart"`
EventFormatSimpleMapEnd string `toml:"EventFormatSimpleMapEnd"`
EventFormatSimplePlayerConnect string `toml:"EventFormatSimplePlayerConnect"`
EventFormatSimplePlayerDisconnect string `toml:"EventFormatSimplePlayerDisconnect"`
EventFormatSimplePlayerChat string `toml:"EventFormatSimplePlayerChat"`
}

type Config_t struct {
General GeneralConfig
Bot BotConfig
Database DatabaseConfig
UI UIConfig
Messages MsgConfig
}
4 changes: 3 additions & 1 deletion server/protocol/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"encoding/binary"
"fmt"
"time"
"strings"

"github.com/bwmarrin/discordgo"
"github.com/rumblefrog/source-chat-relay/server/packet"
"github.com/rumblefrog/source-chat-relay/server/config"
)

type IdentificationType uint8
Expand Down Expand Up @@ -83,7 +85,7 @@ func (m *ChatMessage) Marshal() []byte {
}

func (m *ChatMessage) Plain() string {
return m.Username + ": " + m.Message
return strings.ReplaceAll(strings.ReplaceAll(config.Config.Messages.EventFormatSimplePlayerChat, "%username%", m.Username), "%message%", m.Message)
}

func (m *ChatMessage) Embed() *discordgo.MessageEmbed {
Expand Down
17 changes: 16 additions & 1 deletion server/protocol/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package protocol

import (
"time"
"strings"

"github.com/bwmarrin/discordgo"
"github.com/rumblefrog/source-chat-relay/server/packet"
"github.com/rumblefrog/source-chat-relay/server/config"
)

type EventMessage struct {
Expand Down Expand Up @@ -58,7 +60,20 @@ func (m *EventMessage) Marshal() []byte {
}

func (m *EventMessage) Plain() string {
return m.Event + ": " + m.Data

switch m.Event {
case "Map Start":
return strings.ReplaceAll(config.Config.Messages.EventFormatSimpleMapStart, "%data%", m.Data)
case "Map Ended":
return strings.ReplaceAll(config.Config.Messages.EventFormatSimpleMapEnd, "%data%", m.Data)
case "Player Connected":
return strings.ReplaceAll(config.Config.Messages.EventFormatSimplePlayerConnect, "%data%", m.Data)
case "Player Disconnected":
return strings.ReplaceAll(config.Config.Messages.EventFormatSimplePlayerDisconnect, "%data%", m.Data)
default:
return strings.ReplaceAll(strings.ReplaceAll(config.Config.Messages.EventFormatSimple, "%data%", m.Data), "%event%", m.Event)
}

}

func (m *EventMessage) Embed() *discordgo.MessageEmbed {
Expand Down

0 comments on commit 088dcc3

Please sign in to comment.