Skip to content

Commit

Permalink
Merge pull request #50 from Quaver/clan-first-place-msg
Browse files Browse the repository at this point in the history
Send first place won/lost msg to clan chat
  • Loading branch information
Swan authored Oct 17, 2024
2 parents 4907265 + e9d1733 commit f3c578c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
4 changes: 3 additions & 1 deletion db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
RedisChannelMultiplayerMapShares = "quaver:multiplayer_map_shares"
RedisChannelFirstPlaceScores = "quaver:first_place_scores"
RedisChannelRankedClanMap = "quaver:ranked_clan_map"
RedisChannelClanFirstPlace = "quaver:clan_first_place"
)

// InitializeRedis Initializes a Redis client
Expand All @@ -40,7 +41,8 @@ func InitializeRedis() {
log.Fatalln(result.Err())
}

sub := Redis.Subscribe(RedisCtx, RedisChannelSongRequests, RedisChannelTwitchConnection, RedisChannelMultiplayerMapShares, RedisChannelFirstPlaceScores, RedisChannelRankedClanMap)
sub := Redis.Subscribe(RedisCtx, RedisChannelSongRequests, RedisChannelTwitchConnection, RedisChannelMultiplayerMapShares, RedisChannelFirstPlaceScores, RedisChannelRankedClanMap,
RedisChannelClanFirstPlace)

go func() {
for {
Expand Down
47 changes: 45 additions & 2 deletions handlers/RedisSubHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func AddRedisHandlers() {
db.AddRedisSubscriberHandler(db.RedisChannelMultiplayerMapShares, HandleMultiplayerMapShares)
db.AddRedisSubscriberHandler(db.RedisChannelFirstPlaceScores, HandleFirstPlaceScores)
db.AddRedisSubscriberHandler(db.RedisChannelRankedClanMap, HandleRankedClanMap)
db.AddRedisSubscriberHandler(db.RedisChannelClanFirstPlace, HandleClanFirstPlace)
}

func HandleTwitchSongRequest(msg *redis.Message) {
Expand Down Expand Up @@ -189,10 +190,52 @@ func HandleRankedClanMap(msg *redis.Message) {
continue
}

msg := fmt.Sprintf("New %v Clan Ranked Map: %v - %v [%v] by %v (#%v).",
message := fmt.Sprintf("New %v Clan Ranked Map: %v - %v [%v] by %v (#%v).",
parsed.Map.Mode, parsed.Map.Artist, parsed.Map.Title, parsed.Map.DifficultyName, parsed.Map.CreatorName,
parsed.Map.Id)

chat.SendMessage(chat.Bot, channel.Name, msg)
chat.SendMessage(chat.Bot, channel.Name, message)
}
}

func HandleClanFirstPlace(msg *redis.Message) {
type payload struct {
ClanId int `json:"clan_id"`
Won bool `json:"won"`
Map struct {
Id int `json:"id"`
Artist string `json:"artist"`
Title string `json:"title"`
DifficultyName string `json:"difficulty_name"`
CreatorName string `json:"creator_name"`
Mode string `json:"mode"`
} `json:"map"`
}

var parsed payload

err := json.Unmarshal([]byte(msg.Payload), &parsed)

if err != nil {
log.Printf("Failed to parse ranked clan map - %v - %v\n", msg.Payload, err)
return
}

channel := chat.GetChannelByName(fmt.Sprintf("#clan_%v", parsed.ClanId))

if channel == nil {
return
}

winStatus := "won"

if !parsed.Won {
winStatus = "lost"
}

message := fmt.Sprintf("You have %v first place on: %v - %v [%v] by %v (#%v).",
winStatus, parsed.Map.Artist, parsed.Map.Title, parsed.Map.DifficultyName, parsed.Map.CreatorName,
parsed.Map.Id)

chat.SendMessage(chat.Bot, channel.Name, message)
}

0 comments on commit f3c578c

Please sign in to comment.