Skip to content

Commit

Permalink
Unify status messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rkfg committed Jan 5, 2021
1 parent a0c60f6 commit 371c716
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 60 deletions.
32 changes: 18 additions & 14 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ type hive struct {
CommSkillOffset int `json:"comm_skill_offset"`
}

type message struct {
*discordgo.MessageSend
channelID string
}

var (
vanityRegex = regexp.MustCompile(`https://steamcommunity.com/id/([^/]*)/?`)
profileRegex = regexp.MustCompile(`https://steamcommunity.com/profiles/(\d*)/?`)
lowercasePath = makePath("discord", "users", "lowercase")
normalPath = makePath("discord", "users", "normal")
sendChan = make(chan message, 10)
)

func playerIDFromDiscordName(username string) (uint32, error) {
Expand Down Expand Up @@ -119,18 +125,13 @@ func getSkill(playerID uint32) (*discordgo.MessageSend, error) {
}}, nil
}

func parseFields(fields []string, author *discordgo.User) (response *discordgo.MessageSend, err error) {
func parseFields(fields []string, author *discordgo.User, channelID string) (response *discordgo.MessageSend, err error) {
var playerID uint32
switch fields[0] {
case "status":
response = &discordgo.MessageSend{Embed: &discordgo.MessageEmbed{}}
for i := range config.Servers {
srv := config.Servers[i]
response.Embed.Fields = append(response.Embed.Fields, &discordgo.MessageEmbedField{
Name: fmt.Sprintf("%s [%s]", srv.Name, srv.currentMap),
Value: fmt.Sprintf("skill: %d, players: %d", srv.avgSkill, len(srv.players)),
Inline: true,
})
msg := serverStatus(config.Servers[i])
sendChan <- message{msg, channelID}
}
case "skill":
if len(fields) == 1 {
Expand Down Expand Up @@ -207,20 +208,24 @@ func handleCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
msg = strings.TrimPrefix(msg, cmdPrefix)
fields := strings.Fields(msg)
if len(fields) > 0 {
response, err := parseFields(fields, m.Author)
response, err := parseFields(fields, m.Author, m.ChannelID)
if err != nil {
response = &discordgo.MessageSend{Content: "Error: " + err.Error()}
}
if response != nil {
s.ChannelMessageSendComplex(m.ChannelID, response)
sendChan <- message{response, m.ChannelID}
}
}
}

func sendMsg(c chan discordgo.MessageSend, s *discordgo.Session) {
func sendMsg(c chan message, s *discordgo.Session) {
for {
msg := <-c
s.ChannelMessageSendComplex(config.ChannelID, &msg)
channelID := msg.channelID
if channelID == "" {
channelID = config.ChannelID
}
s.ChannelMessageSendComplex(channelID, msg.MessageSend)
time.Sleep(time.Second)
}
}
Expand Down Expand Up @@ -252,12 +257,11 @@ func bot() (err error) {
}
defer dg.Close()
dg.AddHandler(handleCommand)
sendChan := make(chan discordgo.MessageSend, 10)
go sendMsg(sendChan, dg)
restartChan := make(chan bool)
for i := range config.Servers {
config.Servers[i].restartChan = restartChan
go query(config.Servers[i], sendChan)
go query(config.Servers[i])
}
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
Expand Down
94 changes: 52 additions & 42 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,50 @@ func (e queryError) Error() string {
return fmt.Sprintf(e.description, e.err)
}

func maybeNotify(srv *ns2server, sendChan chan discordgo.MessageSend) {
func serverStatus(srv *ns2server) *discordgo.MessageSend {
specSlots := srv.SpecSlots
playerSlots := srv.PlayerSlots - len(srv.players)
freeSlots := playerSlots + srv.SpecSlots
if freeSlots < specSlots {
specSlots = freeSlots
}
if playerSlots < 0 {
playerSlots = 0
}
msg := discordgo.MessageSend{Embed: &discordgo.MessageEmbed{
Title: fmt.Sprintf("%s [%s]", srv.Name, srv.currentMap),
Fields: []*discordgo.MessageEmbedField{
{
Name: "Players",
Value: fmt.Sprint(len(srv.players)),
Inline: true,
},
{
Name: "Player slots",
Value: fmt.Sprint(playerSlots),
Inline: true,
},
{
Name: "Spectator slots",
Value: fmt.Sprint(specSlots),
Inline: true,
},
},
Footer: &discordgo.MessageEmbedFooter{Text: fmt.Sprintf("Skill: %d", srv.avgSkill)},
},
}
playersCount := len(srv.players)
if playersCount < config.Seeding.AlmostFull {
msg.Embed.Color = 0x009900
} else if playersCount < srv.PlayerSlots {
msg.Embed.Color = 0xcc9900
} else if playersCount >= srv.PlayerSlots {
msg.Embed.Color = 0xff3300
}
return &msg
}

func maybeNotify(srv *ns2server) {
playersCount := len(srv.players)
newState := empty
if playersCount < config.Seeding.Seeding {
Expand All @@ -39,52 +82,19 @@ func maybeNotify(srv *ns2server, sendChan chan discordgo.MessageSend) {
srv.serverState = newState
if srv.lastStateAnnounced != newState {
srv.lastStateAnnounced = newState
specSlots := srv.SpecSlots
playerSlots := srv.PlayerSlots - len(srv.players)
freeSlots := playerSlots + srv.SpecSlots
if freeSlots < specSlots {
specSlots = freeSlots
}
if playerSlots < 0 {
playerSlots = 0
}
msg := discordgo.MessageSend{Embed: &discordgo.MessageEmbed{
Title: fmt.Sprintf("%s [%s]", srv.Name, srv.currentMap),
Fields: []*discordgo.MessageEmbedField{
{
Name: "Players",
Value: fmt.Sprint(len(srv.players)),
Inline: true,
},
{
Name: "Player slots",
Value: fmt.Sprint(playerSlots),
Inline: true,
},
{
Name: "Spectator slots",
Value: fmt.Sprint(specSlots),
Inline: true,
},
},
Footer: &discordgo.MessageEmbedFooter{Text: fmt.Sprintf("Skill: %d", srv.avgSkill)},
},
}
msg := serverStatus(srv)
switch newState {
case seedingstarted:
msg.Embed.Description = "Seeding started! Players on the server: " + srv.playersString()
msg.Embed.Color = 0x009900
srv.maxStateToMessage = specsonly
sendChan <- msg
sendChan <- message{MessageSend: msg}
case almostfull:
msg.Embed.Description = "Server is almost full!"
msg.Embed.Color = 0xcc9900
sendChan <- msg
sendChan <- message{MessageSend: msg}
case specsonly:
msg.Embed.Description = "Server is full but you can still make it!"
msg.Embed.Color = 0xff3300
srv.maxStateToMessage = seedingstarted
sendChan <- msg
sendChan <- message{MessageSend: msg}
}
}
} else {
Expand All @@ -98,7 +108,7 @@ func maybeNotify(srv *ns2server, sendChan chan discordgo.MessageSend) {
}
}

func queryServer(client *a2s.Client, srv *ns2server, sendChan chan discordgo.MessageSend) error {
func queryServer(client *a2s.Client, srv *ns2server) error {
info, err := client.QueryInfo()
if err != nil {
return queryError{"server info query: %s", err}
Expand All @@ -125,11 +135,11 @@ func queryServer(client *a2s.Client, srv *ns2server, sendChan chan discordgo.Mes
for _, p := range playersInfo.Players {
srv.players = append(srv.players, p.Name)
}
maybeNotify(srv, sendChan)
maybeNotify(srv)
return nil
}

func query(srv *ns2server, sendChan chan discordgo.MessageSend) {
func query(srv *ns2server) {
client, err := a2s.NewClient(srv.Address)
if err != nil {
log.Println("error creating client:", err)
Expand All @@ -142,7 +152,7 @@ func query(srv *ns2server, sendChan chan discordgo.MessageSend) {
srv.maxStateToMessage = full
srv.lastStateAnnounced = empty
for {
err = queryServer(client, srv, sendChan)
err = queryServer(client, srv)
if err != nil {
log.Printf("Error: %s", err)
}
Expand Down
5 changes: 1 addition & 4 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"strconv"
"testing"
"time"

"github.com/bwmarrin/discordgo"
)

func TestMain(t *testing.M) {
Expand All @@ -18,10 +16,9 @@ func TestMain(t *testing.M) {
}

func notif(t *testing.T, srv *ns2server, expected string) {
sendChan := make(chan discordgo.MessageSend)
q := make(chan bool)
go func() {
maybeNotify(srv, sendChan)
maybeNotify(srv)
close(q)
}()
select {
Expand Down

0 comments on commit 371c716

Please sign in to comment.