Skip to content

Commit

Permalink
Merge pull request #48 from Quaver/clan-chat-channels
Browse files Browse the repository at this point in the history
Implement clan chat channels
  • Loading branch information
Swan authored Oct 14, 2024
2 parents e8298ed + 1ad811e commit 7475a51
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 5 deletions.
1 change: 1 addition & 0 deletions chat/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
ChannelNormal ChannelType = iota
ChannelTypeMultiplayer
ChannelTypeSpectator
ChannelTypeClan
)

// NewChannel Creates a new chat channel instance
Expand Down
27 changes: 26 additions & 1 deletion chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ func Initialize() {
addChannel(NewChannel(ChannelNormal, channel.Name, channel.Description, channel.AdminOnly, channel.AutoJoin, channel.LimitedChat, channel.DiscordWebhook))
}

clans, err := db.GetAllClans()

if err != nil {
panic(err)
}

for _, clan := range clans {
AddClanChannel(clan.Id)
}

_ = sessions.AddUser(Bot)
addBotChatHandlers()
addSpectatorHandlers()
Expand All @@ -47,7 +57,10 @@ func GetAvailableChannels(userGroups common.UserGroups) []*Channel {
var availableChannels []*Channel

for _, channel := range channels {
if (channel.Type != ChannelTypeMultiplayer && channel.Type != ChannelTypeSpectator && !channel.AdminOnly) || (channel.AdminOnly && isChatModerator(userGroups)) {
if (channel.Type != ChannelTypeClan &&
channel.Type != ChannelTypeMultiplayer &&
channel.Type != ChannelTypeSpectator && !channel.AdminOnly) ||
(channel.AdminOnly && isChatModerator(userGroups)) {
availableChannels = append(availableChannels, channel)
}
}
Expand Down Expand Up @@ -243,6 +256,18 @@ func addChannel(channel *Channel) {
log.Printf("Initialized chat channel: %v\n", channel.Name)
}

// AddClanChannel Adds a new clan channel
func AddClanChannel(clanId int) *Channel {
name := fmt.Sprintf("#clan_%v", clanId)

channel := NewChannel(ChannelTypeClan, name, "Communicate with your fellow clan members",
false, false, false, "")

addChannel(channel)

return channel
}

// Removes a channel from channels
func removeChannel(channel *Channel) {
chatMutex.Lock()
Expand Down
24 changes: 24 additions & 0 deletions db/clans.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package db

import "database/sql"

type Clan struct {
Id int `db:"id"`
OwnerId int `db:"owner_id"`
Name string `db:"name"`
Tag string `db:"tag"`
Customizable bool `db:"customizable"`
}

// GetAllClans Retrieves all of the clans in the db
func GetAllClans() ([]*Clan, error) {
result := make([]*Clan, 0)

err := SQL.Select(&result, "SELECT id, owner_id, name, tag, customizable FROM clans")

if err != nil && err != sql.ErrNoRows {
return nil, err
}

return result, nil
}
3 changes: 2 additions & 1 deletion db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
RedisChannelTwitchConnection = "quaver:twitch_connection"
RedisChannelMultiplayerMapShares = "quaver:multiplayer_map_shares"
RedisChannelFirstPlaceScores = "quaver:first_place_scores"
RedisChannelRankedClanMap = "quaver:ranked_clan_map"
)

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

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

go func() {
for {
Expand Down
7 changes: 4 additions & 3 deletions db/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type User struct {
Country string `db:"country"`
AvatarUrl sql.NullString `db:"avatar_url"`
TwitchUsername sql.NullString `db:"twitch_username"`
ClanId sql.NullInt32 `db:"clan_id"`
}

// GetProfileUrl Returns the full profile url for the user
Expand All @@ -30,7 +31,7 @@ func (u *User) GetProfileUrl() string {

// GetUserById Retrieves a user from the database by their id
func GetUserById(id int) (*User, error) {
query := "SELECT id, steam_id, username, allowed, privileges, usergroups, mute_endtime, country, avatar_url, twitch_username FROM users WHERE id = ? LIMIT 1"
query := "SELECT id, steam_id, username, allowed, privileges, usergroups, mute_endtime, country, avatar_url, twitch_username, clan_id FROM users WHERE id = ? LIMIT 1"

var user User
err := SQL.Get(&user, query, id)
Expand All @@ -44,7 +45,7 @@ func GetUserById(id int) (*User, error) {

// GetUserBySteamId Retrieves a user from the database by their Steam id
func GetUserBySteamId(steamId string) (*User, error) {
query := "SELECT id, steam_id, username, allowed, privileges, usergroups, mute_endtime, country, avatar_url, twitch_username FROM users WHERE steam_id = ? LIMIT 1"
query := "SELECT id, steam_id, username, allowed, privileges, usergroups, mute_endtime, country, avatar_url, twitch_username, clan_id FROM users WHERE steam_id = ? LIMIT 1"

var user User
err := SQL.Get(&user, query, steamId)
Expand All @@ -58,7 +59,7 @@ func GetUserBySteamId(steamId string) (*User, error) {

// GetUserByUsername Rerieves a user from the database by their username
func GetUserByUsername(username string) (*User, error) {
query := "SELECT id, steam_id, username, allowed, privileges, usergroups, mute_endtime, country, avatar_url, twitch_username FROM users WHERE username = ? LIMIT 1"
query := "SELECT id, steam_id, username, allowed, privileges, usergroups, mute_endtime, country, avatar_url, twitch_username, clan_id FROM users WHERE username = ? LIMIT 1"

var user User
err := SQL.Get(&user, query, username)
Expand Down
43 changes: 43 additions & 0 deletions handlers/RedisSubHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func AddRedisHandlers() {
db.AddRedisSubscriberHandler(db.RedisChannelTwitchConnection, HandleTwitchConnection)
db.AddRedisSubscriberHandler(db.RedisChannelMultiplayerMapShares, HandleMultiplayerMapShares)
db.AddRedisSubscriberHandler(db.RedisChannelFirstPlaceScores, HandleFirstPlaceScores)
db.AddRedisSubscriberHandler(db.RedisChannelRankedClanMap, HandleRankedClanMap)
}

func HandleTwitchSongRequest(msg *redis.Message) {
Expand Down Expand Up @@ -153,3 +154,45 @@ func HandleFirstPlaceScores(msg *redis.Message) {
chat.SendMessage(chat.Bot, "#first-places", fmt.Sprintf("%v has just achieved first place on %v - %v [%v]",
parsed.User.Username, parsed.Map.Artist, parsed.Map.Title, parsed.Map.DifficultyName))
}

func HandleRankedClanMap(msg *redis.Message) {
type payload struct {
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
}

clans, err := db.GetAllClans()

if err != nil {
panic(err)
}

for _, clan := range clans {
channel := chat.GetChannelByName(fmt.Sprintf("#clan_%v", clan.Id))

if channel == nil {
continue
}

msg := 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)
}
}
15 changes: 15 additions & 0 deletions handlers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,21 @@ func joinChatChannels(user *sessions.User) {
channel.AddUser(user)
}
}

// Add user to clan channel (creates the new clan channel if necessary)
if user.Info.ClanId.Valid {
name := fmt.Sprintf("#clan_%v", user.Info.ClanId.Int32)

channel := chat.GetChannelByName(name)

if channel != nil {
channel.AddUser(user)
return
}

channel = chat.AddClanChannel(int(user.Info.ClanId.Int32))
channel.AddUser(user)
}
}

// Logs a generic login failure
Expand Down

0 comments on commit 7475a51

Please sign in to comment.