Squads and chats in DataCache, squads rework
Work in progress, bugs may vary
This commit is contained in:
170
lib/datacache/chats.go
Normal file
170
lib/datacache/chats.go
Normal file
@@ -0,0 +1,170 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package datacache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (dc *DataCache) initChats() {
|
||||
c.Log.Info("Initializing Chats storage...")
|
||||
dc.chats = make(map[int]*dbmapping.Chat)
|
||||
}
|
||||
|
||||
func (dc *DataCache) loadChats() {
|
||||
c.Log.Info("Load current Chats data from database to DataCache...")
|
||||
chats := []dbmapping.Chat{}
|
||||
err := c.Db.Select(&chats, "SELECT * FROM chats")
|
||||
if err != nil {
|
||||
// This is critical error and we need to stop immediately!
|
||||
c.Log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
dc.chatsMutex.Lock()
|
||||
for i := range chats {
|
||||
dc.chats[chats[i].ID] = &chats[i]
|
||||
}
|
||||
c.Log.Info("Loaded chats in DataCache: " + strconv.Itoa(len(dc.chats)))
|
||||
dc.chatsMutex.Unlock()
|
||||
}
|
||||
|
||||
// External function
|
||||
|
||||
// GetAllGroupChats returns all bot's group chats
|
||||
func (dc *DataCache) GetAllGroupChats() []dbmapping.Chat {
|
||||
chats := []dbmapping.Chat{}
|
||||
|
||||
dc.chatsMutex.Lock()
|
||||
for i := range dc.chats {
|
||||
if dc.chats[i].ChatType == "group" || dc.chats[i].ChatType == "supergroup" {
|
||||
chats = append(chats, *dc.chats[i])
|
||||
}
|
||||
}
|
||||
dc.chatsMutex.Unlock()
|
||||
|
||||
return chats
|
||||
}
|
||||
|
||||
// GetAllPrivateChats returns all bot's private chats
|
||||
func (dc *DataCache) GetAllPrivateChats() []dbmapping.Chat {
|
||||
chats := []dbmapping.Chat{}
|
||||
|
||||
dc.chatsMutex.Lock()
|
||||
for i := range dc.chats {
|
||||
if dc.chats[i].ChatType == "private" {
|
||||
chats = append(chats, *dc.chats[i])
|
||||
}
|
||||
}
|
||||
dc.chatsMutex.Unlock()
|
||||
|
||||
return chats
|
||||
}
|
||||
|
||||
// GetChatByID returns Chat by it's ID
|
||||
func (dc *DataCache) GetChatByID(chatID int) (*dbmapping.Chat, error) {
|
||||
if dc.chats[chatID] != nil {
|
||||
return dc.chats[chatID], nil
|
||||
}
|
||||
|
||||
return nil, errors.New("There is no chat with ID=" + strconv.Itoa(chatID))
|
||||
}
|
||||
|
||||
// GetGroupChatsByIDs returns bot's group chats with given IDs
|
||||
func (dc *DataCache) GetGroupChatsByIDs(chatIDs []int) []dbmapping.Chat {
|
||||
chats := []dbmapping.Chat{}
|
||||
|
||||
dc.chatsMutex.Lock()
|
||||
for i := range dc.chats {
|
||||
if dc.chats[i].ChatType == "group" || dc.chats[i].ChatType == "supergroup" {
|
||||
for j := range chatIDs {
|
||||
if dc.chats[i].ID == j {
|
||||
chats = append(chats, *dc.chats[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dc.chatsMutex.Unlock()
|
||||
|
||||
return chats
|
||||
}
|
||||
|
||||
// GetLeaguePrivateChats returns private chats for all league members
|
||||
func (dc *DataCache) GetLeaguePrivateChats() []dbmapping.Chat {
|
||||
dc.playersMutex.Lock()
|
||||
dc.chatsMutex.Lock()
|
||||
|
||||
chats := []dbmapping.Chat{}
|
||||
|
||||
for i := range dc.players {
|
||||
if dc.players[i].Status != "banned" && dc.players[i].Status != "spy" && dc.players[i].Status != "league_changed" && dc.players[i].LeagueID == 1 {
|
||||
if dc.chats[dc.players[i].TelegramID] != nil {
|
||||
chats = append(chats, *dc.chats[dc.players[i].TelegramID])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dc.playersMutex.Unlock()
|
||||
dc.chatsMutex.Unlock()
|
||||
|
||||
return chats
|
||||
}
|
||||
|
||||
// GetOrCreateChat returns current or new Chat object by Telegram update
|
||||
func (dc *DataCache) GetOrCreateChat(update *tgbotapi.Update) (*dbmapping.Chat, error) {
|
||||
telegramID := update.Message.From.ID
|
||||
chatRaw := dbmapping.Chat{}
|
||||
c.Log.Info("DataCache: Getting chat with Telegram ID=", telegramID)
|
||||
|
||||
dc.chatsMutex.Lock()
|
||||
for i := range dc.chats {
|
||||
if dc.chats[i].TelegramID == int64(telegramID) {
|
||||
dc.chatsMutex.Unlock()
|
||||
return dc.chats[i], nil
|
||||
}
|
||||
}
|
||||
dc.chatsMutex.Unlock()
|
||||
|
||||
// If we're here: there is no chat with given Telegram ID
|
||||
c.Log.Error("Chat stream not found in DataCache. Adding chat...")
|
||||
|
||||
nameOfChat := ""
|
||||
if update.Message.Chat.FirstName != "" {
|
||||
nameOfChat += update.Message.Chat.FirstName
|
||||
}
|
||||
if update.Message.Chat.LastName != "" {
|
||||
nameOfChat += " " + update.Message.Chat.LastName
|
||||
}
|
||||
if update.Message.Chat.Title != "" {
|
||||
if nameOfChat != "" {
|
||||
nameOfChat += " [" + update.Message.Chat.Title + "]"
|
||||
} else {
|
||||
nameOfChat = update.Message.Chat.Title
|
||||
}
|
||||
}
|
||||
|
||||
chatRaw.Name = nameOfChat
|
||||
chatRaw.ChatType = update.Message.Chat.Type
|
||||
chatRaw.TelegramID = update.Message.Chat.ID
|
||||
chatRaw.CreatedAt = time.Now().UTC()
|
||||
_, err := c.Db.NamedExec("INSERT INTO chats VALUES(NULL, :name, :chat_type, :telegram_id, :created_at)", &chatRaw)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
err = c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=? AND chat_type=?"), chatRaw.TelegramID, chatRaw.ChatType)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dc.chatsMutex.Lock()
|
||||
dc.chats[chatRaw.ID] = &chatRaw
|
||||
dc.chatsMutex.Unlock()
|
||||
|
||||
return &chatRaw, nil
|
||||
}
|
@@ -4,6 +4,7 @@
|
||||
package datacacheinterface
|
||||
|
||||
import (
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
||||
)
|
||||
|
||||
@@ -11,6 +12,24 @@ import (
|
||||
type DataCacheInterface interface {
|
||||
Init()
|
||||
|
||||
GetAllGroupChats() []dbmapping.Chat
|
||||
GetAllPrivateChats() []dbmapping.Chat
|
||||
GetChatByID(chatID int) (*dbmapping.Chat, error)
|
||||
GetOrCreateChat(update *tgbotapi.Update) (*dbmapping.Chat, error)
|
||||
GetGroupChatsByIDs(chatIDs []int) []dbmapping.Chat
|
||||
GetLeaguePrivateChats() []dbmapping.Chat
|
||||
|
||||
AddPlayerToSquad(relation *dbmapping.SquadPlayer) (int, error)
|
||||
GetAllSquadsChats() []dbmapping.Chat
|
||||
GetAllSquadsWithChats() []dbmapping.SquadChat
|
||||
GetAvailableSquadsChatsForUser(userID int) []dbmapping.Chat
|
||||
GetCommandersForSquad(squadID int) []dbmapping.Player
|
||||
GetSquadByID(squadID int) (*dbmapping.SquadChat, error)
|
||||
GetSquadByChatID(chatID int) (*dbmapping.Squad, error)
|
||||
GetSquadsChatsBySquadsIDs(squadsIDs []int) []dbmapping.Chat
|
||||
GetUserRoleInSquad(squadID int, playerID int) string
|
||||
GetUserRolesInSquads(userID int) []dbmapping.SquadPlayerFull
|
||||
|
||||
AddPlayer(player *dbmapping.Player) (int, error)
|
||||
GetOrCreatePlayerByTelegramID(telegramID int) (*dbmapping.Player, error)
|
||||
GetPlayerByID(playerID int) (*dbmapping.Player, error)
|
||||
|
@@ -34,6 +34,16 @@ type DataCache struct {
|
||||
fullPokememes map[int]*dbmapping.PokememeFull
|
||||
fullPokememesMutex sync.Mutex
|
||||
|
||||
// Chats
|
||||
chats map[int]*dbmapping.Chat
|
||||
chatsMutex sync.Mutex
|
||||
// Squads
|
||||
squads map[int]*dbmapping.Squad
|
||||
squadsWithChats map[int]*dbmapping.SquadChat
|
||||
squadPlayersRelations map[int]*dbmapping.SquadPlayer
|
||||
squadPlayers map[int]map[int]*dbmapping.SquadPlayerFull
|
||||
squadsMutex sync.Mutex
|
||||
|
||||
// Elements
|
||||
elements map[int]*dbmapping.Element
|
||||
elementsMutex sync.Mutex
|
||||
@@ -73,4 +83,8 @@ func (dc *DataCache) Init() {
|
||||
dc.loadPlayers()
|
||||
dc.initProfiles()
|
||||
dc.loadProfiles()
|
||||
dc.initChats()
|
||||
dc.loadChats()
|
||||
dc.initSquads()
|
||||
dc.loadSquads()
|
||||
}
|
||||
|
248
lib/datacache/squads.go
Normal file
248
lib/datacache/squads.go
Normal file
@@ -0,0 +1,248 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package datacache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (dc *DataCache) initSquads() {
|
||||
c.Log.Info("Initializing Squads storage...")
|
||||
dc.squads = make(map[int]*dbmapping.Squad)
|
||||
dc.squadsWithChats = make(map[int]*dbmapping.SquadChat)
|
||||
dc.squadPlayers = make(map[int]map[int]*dbmapping.SquadPlayerFull)
|
||||
dc.squadPlayersRelations = make(map[int]*dbmapping.SquadPlayer)
|
||||
}
|
||||
|
||||
func (dc *DataCache) loadSquads() {
|
||||
c.Log.Info("Load current Squads data from database to DataCache...")
|
||||
squads := []dbmapping.Squad{}
|
||||
err := c.Db.Select(&squads, "SELECT * FROM squads")
|
||||
if err != nil {
|
||||
// This is critical error and we need to stop immediately!
|
||||
c.Log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
squadsPlayersRelations := []dbmapping.SquadPlayer{}
|
||||
err = c.Db.Select(&squadsPlayersRelations, "SELECT * FROM squads_players")
|
||||
if err != nil {
|
||||
c.Log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
dc.squadsMutex.Lock()
|
||||
for i := range squads {
|
||||
squadWithChat := dbmapping.SquadChat{}
|
||||
squadWithChat.Squad = squads[i]
|
||||
sChat := dc.chats[squads[i].ChatID]
|
||||
if sChat != nil {
|
||||
squadWithChat.Chat = *sChat
|
||||
|
||||
dc.squads[squads[i].ID] = &squads[i]
|
||||
dc.squadsWithChats[squads[i].ID] = &squadWithChat
|
||||
}
|
||||
}
|
||||
|
||||
for i := range squadsPlayersRelations {
|
||||
sPlayer := dc.players[squadsPlayersRelations[i].PlayerID]
|
||||
sProfile := dc.currentProfiles[squadsPlayersRelations[i].PlayerID]
|
||||
sSquad := dc.squadsWithChats[squadsPlayersRelations[i].PlayerID]
|
||||
if sPlayer != nil && sProfile != nil && sSquad != nil {
|
||||
dc.squadPlayersRelations[squadsPlayersRelations[i].ID] = &squadsPlayersRelations[i]
|
||||
|
||||
squadPlayer := dbmapping.SquadPlayerFull{}
|
||||
squadPlayer.Player = *sPlayer
|
||||
squadPlayer.Profile = *sProfile
|
||||
squadPlayer.Squad = *sSquad
|
||||
squadPlayer.UserRole = squadsPlayersRelations[i].UserType
|
||||
|
||||
if dc.squadPlayers[sSquad.Squad.ID] == nil {
|
||||
dc.squadPlayers[sSquad.Squad.ID] = make(map[int](*dbmapping.SquadPlayerFull))
|
||||
}
|
||||
|
||||
dc.squadPlayers[sSquad.Squad.ID][sPlayer.ID] = &squadPlayer
|
||||
}
|
||||
}
|
||||
c.Log.Info("Loaded squads in DataCache: " + strconv.Itoa(len(dc.squads)))
|
||||
c.Log.Info("Loaded players relations to squads in DataCache: " + strconv.Itoa(len(dc.squadPlayers)))
|
||||
dc.squadsMutex.Unlock()
|
||||
}
|
||||
|
||||
// External functions
|
||||
|
||||
// AddPlayerToSquad creates relation between player and squad
|
||||
func (dc *DataCache) AddPlayerToSquad(relation *dbmapping.SquadPlayer) (int, error) {
|
||||
sPlayer, err := c.DataCache.GetPlayerByID(relation.PlayerID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
sProfile, err := c.DataCache.GetProfileByPlayerID(relation.PlayerID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
sSquad, err := c.DataCache.GetSquadByID(relation.SquadID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
dc.squadsMutex.Lock()
|
||||
for i := range dc.squadPlayersRelations {
|
||||
if dc.squadPlayersRelations[i].SquadID == relation.SquadID && dc.squadPlayersRelations[i].PlayerID == relation.PlayerID {
|
||||
dc.squadsMutex.Unlock()
|
||||
return 0, errors.New("There is already such a player-squad relation")
|
||||
}
|
||||
}
|
||||
dc.squadsMutex.Unlock()
|
||||
|
||||
_, err = c.Db.NamedExec("INSERT INTO squads_players VALUES(NULL, :squad_id, :player_id, :user_type, :author_id, :created_at)", &relation)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
insertedRelation := dbmapping.SquadPlayer{}
|
||||
err = c.Db.Get(&insertedRelation, "SELECT * FROM squads_players WHERE squad_id=? AND player_id=?", relation.SquadID, relation.PlayerID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
dc.squadsMutex.Lock()
|
||||
dc.squadPlayersRelations[insertedRelation.ID] = &insertedRelation
|
||||
squadPlayerFull := dbmapping.SquadPlayerFull{}
|
||||
squadPlayerFull.Player = *sPlayer
|
||||
squadPlayerFull.Profile = *sProfile
|
||||
squadPlayerFull.Squad = *sSquad
|
||||
squadPlayerFull.UserRole = insertedRelation.UserType
|
||||
if dc.squadPlayers[sSquad.Squad.ID] == nil {
|
||||
dc.squadPlayers[sSquad.Squad.ID] = make(map[int]*dbmapping.SquadPlayerFull)
|
||||
}
|
||||
dc.squadPlayers[sSquad.Squad.ID][sPlayer.ID] = &squadPlayerFull
|
||||
dc.squadsMutex.Unlock()
|
||||
|
||||
return insertedRelation.ID, nil
|
||||
}
|
||||
|
||||
// GetAllSquadsChats returns all chats belonging to squads
|
||||
func (dc *DataCache) GetAllSquadsChats() []dbmapping.Chat {
|
||||
chats := []dbmapping.Chat{}
|
||||
|
||||
dc.squadsMutex.Lock()
|
||||
for i := range dc.squadsWithChats {
|
||||
chats = append(chats, dc.squadsWithChats[i].Chat)
|
||||
}
|
||||
dc.squadsMutex.Unlock()
|
||||
|
||||
return chats
|
||||
}
|
||||
|
||||
// GetAllSquadsWithChats returns all squads with chats
|
||||
func (dc *DataCache) GetAllSquadsWithChats() []dbmapping.SquadChat {
|
||||
squadsWithChats := []dbmapping.SquadChat{}
|
||||
|
||||
dc.squadsMutex.Lock()
|
||||
for i := range dc.squadsWithChats {
|
||||
squadsWithChats = append(squadsWithChats, *dc.squadsWithChats[i])
|
||||
}
|
||||
dc.squadsMutex.Unlock()
|
||||
|
||||
return squadsWithChats
|
||||
}
|
||||
|
||||
// GetAvailableSquadsChatsForUser returns all squads chats accessible by user with given ID
|
||||
func (dc *DataCache) GetAvailableSquadsChatsForUser(userID int) []dbmapping.Chat {
|
||||
chats := []dbmapping.Chat{}
|
||||
|
||||
dc.squadsMutex.Lock()
|
||||
for i := range dc.squadPlayers {
|
||||
for j := range dc.squadPlayers[i] {
|
||||
if dc.squadPlayers[i][j].Player.ID == userID {
|
||||
chats = append(chats, dc.squadPlayers[i][j].Squad.Chat)
|
||||
}
|
||||
}
|
||||
}
|
||||
dc.squadsMutex.Unlock()
|
||||
|
||||
return chats
|
||||
}
|
||||
|
||||
// GetCommandersForSquad returns all players which are commanders of squad with given ID
|
||||
func (dc *DataCache) GetCommandersForSquad(squadID int) []dbmapping.Player {
|
||||
commanders := []dbmapping.Player{}
|
||||
|
||||
dc.squadsMutex.Lock()
|
||||
for i := range dc.squadPlayers[squadID] {
|
||||
if dc.squadPlayers[squadID][i].Squad.Squad.ID == squadID {
|
||||
if dc.squadPlayers[squadID][i].UserRole == "commander" {
|
||||
commanders = append(commanders, dc.squadPlayers[squadID][i].Player)
|
||||
}
|
||||
}
|
||||
}
|
||||
dc.squadsMutex.Unlock()
|
||||
|
||||
return commanders
|
||||
}
|
||||
|
||||
// GetSquadByID returns squad with given ID
|
||||
func (dc *DataCache) GetSquadByID(squadID int) (*dbmapping.SquadChat, error) {
|
||||
if dc.squadsWithChats[squadID] != nil {
|
||||
return dc.squadsWithChats[squadID], nil
|
||||
}
|
||||
|
||||
return nil, errors.New("There is no squad with ID=" + strconv.Itoa(squadID))
|
||||
}
|
||||
|
||||
// GetSquadByChatID returns squad with given chat ID
|
||||
func (dc *DataCache) GetSquadByChatID(chatID int) (*dbmapping.Squad, error) {
|
||||
dc.squadsMutex.Lock()
|
||||
for i := range dc.squadsWithChats {
|
||||
if dc.squadsWithChats[i].Chat.ID == chatID {
|
||||
dc.squadsMutex.Unlock()
|
||||
return dc.squads[i], nil
|
||||
}
|
||||
}
|
||||
dc.squadsMutex.Unlock()
|
||||
return nil, errors.New("There is no squad with chat ID=" + strconv.Itoa(chatID))
|
||||
}
|
||||
|
||||
// GetSquadsChatsBySquadsIDs returns chats for given squad IDs
|
||||
func (dc *DataCache) GetSquadsChatsBySquadsIDs(squadsIDs []int) []dbmapping.Chat {
|
||||
chats := []dbmapping.Chat{}
|
||||
|
||||
dc.squadsMutex.Lock()
|
||||
for i := range dc.squadsWithChats {
|
||||
for j := range squadsIDs {
|
||||
if dc.squadsWithChats[i].Squad.ID == j {
|
||||
chats = append(chats, dc.squadsWithChats[i].Chat)
|
||||
}
|
||||
}
|
||||
}
|
||||
dc.squadsMutex.Unlock()
|
||||
|
||||
return chats
|
||||
}
|
||||
|
||||
// GetUserRolesInSquads returns all user roles for given user ID
|
||||
func (dc *DataCache) GetUserRolesInSquads(userID int) []dbmapping.SquadPlayerFull {
|
||||
userRoles := []dbmapping.SquadPlayerFull{}
|
||||
|
||||
dc.squadsMutex.Lock()
|
||||
for i := range dc.squadPlayers {
|
||||
for j := range dc.squadPlayers[i] {
|
||||
if dc.squadPlayers[i][j].Player.ID == userID {
|
||||
userRoles = append(userRoles, *dc.squadPlayers[i][j])
|
||||
}
|
||||
}
|
||||
}
|
||||
dc.squadsMutex.Unlock()
|
||||
|
||||
return userRoles
|
||||
}
|
||||
|
||||
// GetUserRoleInSquad returns user role in specified squad
|
||||
func (dc *DataCache) GetUserRoleInSquad(squadID int, playerID int) string {
|
||||
if dc.squadPlayers[squadID][playerID] != nil {
|
||||
return dc.squadPlayers[squadID][playerID].UserRole
|
||||
}
|
||||
|
||||
return "none"
|
||||
}
|
Reference in New Issue
Block a user