Squads and chats in DataCache, squads rework
Work in progress, bugs may vary
This commit is contained in:
@@ -1,185 +0,0 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package squader
|
||||
|
||||
import (
|
||||
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetCommandersForSquadViaChat gets commanders for selected chat
|
||||
func (s *Squader) GetCommandersForSquadViaChat(chatRaw *dbmapping.Chat) ([]dbmapping.Player, bool) {
|
||||
commanders := []dbmapping.Player{}
|
||||
err := c.Db.Select(&commanders, c.Db.Rebind("SELECT p.* FROM players p, squads_players sp, squads s WHERE (s.chat_id=? OR s.flood_chat_id=?) AND sp.squad_id = s.id AND sp.user_type = 'commander' AND sp.player_id = p.id"), chatRaw.ID, chatRaw.ID)
|
||||
if err != nil {
|
||||
c.Log.Debug(err.Error())
|
||||
return commanders, false
|
||||
}
|
||||
|
||||
return commanders, true
|
||||
}
|
||||
|
||||
// GetSquadByID returns squad will all support information
|
||||
func (s *Squader) GetSquadByID(squadID int) (dbmapping.SquadChat, bool) {
|
||||
squadFull := dbmapping.SquadChat{}
|
||||
squad := dbmapping.Squad{}
|
||||
chat := dbmapping.Chat{}
|
||||
floodChat := dbmapping.Chat{}
|
||||
|
||||
err := c.Db.Get(&squad, c.Db.Rebind("SELECT * FROM squads WHERE id=?"), squadID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squadFull, false
|
||||
}
|
||||
|
||||
err = c.Db.Get(&chat, c.Db.Rebind("SELECT * FROM chats WHERE id=?"), squad.ChatID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squadFull, false
|
||||
}
|
||||
err = c.Db.Get(&floodChat, c.Db.Rebind("SELECT * FROM chats WHERE id=?"), squad.FloodChatID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squadFull, false
|
||||
}
|
||||
|
||||
squadFull.Squad = squad
|
||||
squadFull.Chat = chat
|
||||
squadFull.FloodChat = floodChat
|
||||
|
||||
return squadFull, true
|
||||
}
|
||||
|
||||
// GetAvailableSquadChatsForUser returns squad chats which user can join
|
||||
func (s *Squader) GetAvailableSquadChatsForUser(playerRaw *dbmapping.Player) ([]dbmapping.Chat, bool) {
|
||||
groupChats := []dbmapping.Chat{}
|
||||
|
||||
if playerRaw.LeagueID == 1 && playerRaw.Status != "spy" && playerRaw.Status != "league_changed" {
|
||||
err := c.Db.Select(&groupChats, c.Db.Rebind("SELECT ch.* FROM chats ch, squads s, squads_players sp WHERE (s.chat_id=ch.id OR s.flood_chat_id=ch.id) AND sp.player_id = ? AND s.id = sp.squad_id"), playerRaw.ID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return groupChats, false
|
||||
}
|
||||
}
|
||||
|
||||
return groupChats, true
|
||||
}
|
||||
|
||||
// GetAllSquadChats returns all main squad chats
|
||||
func (s *Squader) GetAllSquadChats() ([]dbmapping.Chat, bool) {
|
||||
groupChats := []dbmapping.Chat{}
|
||||
|
||||
err := c.Db.Select(&groupChats, "SELECT ch.* FROM chats ch, squads s WHERE s.chat_id=ch.id")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return groupChats, false
|
||||
}
|
||||
|
||||
return groupChats, true
|
||||
}
|
||||
|
||||
// GetAllSquadFloodChats returns all flood squad chats
|
||||
func (s *Squader) GetAllSquadFloodChats() ([]dbmapping.Chat, bool) {
|
||||
groupChats := []dbmapping.Chat{}
|
||||
|
||||
err := c.Db.Select(&groupChats, "SELECT ch.* FROM chats ch, squads s WHERE s.flood_chat_id=ch.id")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return groupChats, false
|
||||
}
|
||||
|
||||
return groupChats, true
|
||||
}
|
||||
|
||||
// GetSquadChatsBySquadsIDs returns main squad chats for given squads IDs
|
||||
func (s *Squader) GetSquadChatsBySquadsIDs(squadsIDs string) ([]dbmapping.Chat, bool) {
|
||||
groupChats := []dbmapping.Chat{}
|
||||
|
||||
squadsIDsArray := strings.Split(squadsIDs, ",")
|
||||
if len(squadsIDsArray) < 1 {
|
||||
return groupChats, false
|
||||
}
|
||||
|
||||
sIDs := make([]int, 0)
|
||||
for i := range squadsIDsArray {
|
||||
sID, _ := strconv.Atoi(squadsIDsArray[i])
|
||||
if sID != 0 {
|
||||
sIDs = append(sIDs, sID)
|
||||
}
|
||||
}
|
||||
if len(sIDs) < 1 {
|
||||
return groupChats, false
|
||||
}
|
||||
|
||||
queryLine := ""
|
||||
for i := range sIDs {
|
||||
queryLine += strconv.Itoa(sIDs[i])
|
||||
if i < len(sIDs)-1 {
|
||||
queryLine += ","
|
||||
}
|
||||
}
|
||||
|
||||
err := c.Db.Select(&groupChats, "SELECT ch.* FROM chats ch, squads s WHERE s.chat_id=ch.id AND s.id IN ("+queryLine+")")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return groupChats, false
|
||||
}
|
||||
|
||||
return groupChats, true
|
||||
}
|
||||
|
||||
// GetUserRolesInSquads lists all user roles
|
||||
func (s *Squader) GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping.SquadPlayerFull, bool) {
|
||||
userRoles := []dbmapping.SquadPlayerFull{}
|
||||
userRolesRaw := []dbmapping.SquadPlayer{}
|
||||
|
||||
err := c.Db.Select(&userRolesRaw, c.Db.Rebind("SELECT * FROM squads_players WHERE player_id=?"), playerRaw.ID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return userRoles, false
|
||||
}
|
||||
|
||||
for i := range userRolesRaw {
|
||||
userRoleFull := dbmapping.SquadPlayerFull{}
|
||||
userRoleFull.Player = *playerRaw
|
||||
userProfile, profileError := c.DataCache.GetProfileByPlayerID(playerRaw.ID)
|
||||
userRoleFull.Profile = *userProfile
|
||||
userRoleFull.UserRole = userRolesRaw[i].UserType
|
||||
squad, squadOk := s.GetSquadByID(userRolesRaw[i].SquadID)
|
||||
userRoleFull.Squad = squad
|
||||
|
||||
if profileError == nil && squadOk {
|
||||
userRoles = append(userRoles, userRoleFull)
|
||||
}
|
||||
}
|
||||
|
||||
return userRoles, true
|
||||
}
|
||||
|
||||
// IsChatASquadEnabled checks group chat for restricting actions for squad
|
||||
func (s *Squader) IsChatASquadEnabled(chatRaw *dbmapping.Chat) string {
|
||||
mainChats, ok := s.GetAllSquadChats()
|
||||
if !ok {
|
||||
return "no"
|
||||
}
|
||||
floodChats, ok := s.GetAllSquadFloodChats()
|
||||
if !ok {
|
||||
return "no"
|
||||
}
|
||||
|
||||
for i := range mainChats {
|
||||
if *chatRaw == mainChats[i] {
|
||||
return "main"
|
||||
}
|
||||
}
|
||||
|
||||
for i := range floodChats {
|
||||
if *chatRaw == floodChats[i] {
|
||||
return "flood"
|
||||
}
|
||||
}
|
||||
|
||||
return "no"
|
||||
}
|
@@ -17,10 +17,7 @@ func (s *Squader) SquadsList(update *tgbotapi.Update, playerRaw *dbmapping.Playe
|
||||
return c.Talkers.AnyMessageUnauthorized(update)
|
||||
}
|
||||
}
|
||||
squads, ok := s.getAllSquadsWithChats()
|
||||
if !ok {
|
||||
return "fail"
|
||||
}
|
||||
squads := c.DataCache.GetAllSquadsWithChats()
|
||||
|
||||
message := "*Наши отряды:*\n"
|
||||
message += "---\n"
|
||||
@@ -33,7 +30,6 @@ func (s *Squader) SquadsList(update *tgbotapi.Update, playerRaw *dbmapping.Playe
|
||||
message += "[#" + strconv.Itoa(squads[i].Squad.ID) + "] _" + squads[i].Chat.Name
|
||||
message += "_ /show\\_squad" + strconv.Itoa(squads[i].Squad.ID) + "\n"
|
||||
message += "Telegram ID: " + strconv.FormatInt(squads[i].Chat.TelegramID, 10) + "\n"
|
||||
message += "Флудилка отряда: _" + squads[i].FloodChat.Name + "_\n"
|
||||
message += "Статистика отряда:\n"
|
||||
message += c.Statistics.SquadStatictics(squads[i].Squad.ID)
|
||||
}
|
||||
@@ -55,13 +51,14 @@ func (s *Squader) SquadInfo(update *tgbotapi.Update, playerRaw *dbmapping.Player
|
||||
}
|
||||
|
||||
if !c.Users.PlayerBetterThan(playerRaw, "admin") {
|
||||
if s.getUserRoleForSquad(squadID, playerRaw.ID) != "commander" {
|
||||
if c.DataCache.GetUserRoleInSquad(squadID, playerRaw.ID) != "commander" {
|
||||
return c.Talkers.AnyMessageUnauthorized(update)
|
||||
}
|
||||
}
|
||||
|
||||
squad, ok := s.GetSquadByID(squadID)
|
||||
if !ok {
|
||||
squad, err := c.DataCache.GetSquadByID(squadID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return c.Talkers.BotError(update)
|
||||
}
|
||||
|
||||
|
@@ -1,28 +0,0 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package squader
|
||||
|
||||
import (
|
||||
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
)
|
||||
|
||||
// CleanFlood will clean flood from squads
|
||||
func (s *Squader) CleanFlood(update *tgbotapi.Update, chatRaw *dbmapping.Chat) string {
|
||||
switch s.IsChatASquadEnabled(chatRaw) {
|
||||
case "main":
|
||||
talker, err := c.DataCache.GetPlayerByTelegramID(update.Message.From.ID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
s.deleteFloodMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
if (update.Message.From.UserName != "i2_bot") && (update.Message.From.UserName != "i2_bot_dev") && !s.isUserAnyCommander(talker.ID) {
|
||||
s.deleteFloodMessage(update)
|
||||
return "fail"
|
||||
}
|
||||
}
|
||||
|
||||
return "protection_passed"
|
||||
}
|
@@ -4,9 +4,9 @@
|
||||
package squader
|
||||
|
||||
import (
|
||||
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"regexp"
|
||||
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -17,12 +17,13 @@ func (s *Squader) getPlayersForSquad(squadID int) ([]dbmapping.SquadPlayerFull,
|
||||
playersRaw := []dbmapping.Player{}
|
||||
squadPlayers := []dbmapping.SquadPlayer{}
|
||||
|
||||
squad, ok := s.GetSquadByID(squadID)
|
||||
if !ok {
|
||||
squad, err := c.DataCache.GetSquadByID(squadID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return players, false
|
||||
}
|
||||
|
||||
err := c.Db.Select(&playersRaw, c.Db.Rebind("SELECT p.* FROM players p, squads_players sp WHERE p.id = sp.player_id AND sp.squad_id=?"), squad.Squad.ID)
|
||||
err = c.Db.Select(&playersRaw, c.Db.Rebind("SELECT p.* FROM players p, squads_players sp WHERE p.id = sp.player_id AND sp.squad_id=?"), squad.Squad.ID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return players, false
|
||||
@@ -38,13 +39,17 @@ func (s *Squader) getPlayersForSquad(squadID int) ([]dbmapping.SquadPlayerFull,
|
||||
for ii := range squadPlayers {
|
||||
if squadPlayers[ii].PlayerID == playersRaw[i].ID {
|
||||
playerWithProfile := dbmapping.SquadPlayerFull{}
|
||||
profile, _ := c.DataCache.GetProfileByPlayerID(playersRaw[i].ID)
|
||||
playerWithProfile.Profile = *profile
|
||||
playerWithProfile.Player = playersRaw[i]
|
||||
playerWithProfile.Squad = squad
|
||||
playerWithProfile.UserRole = squadPlayers[ii].UserType
|
||||
profile, err := c.DataCache.GetProfileByPlayerID(playersRaw[i].ID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
} else {
|
||||
playerWithProfile.Profile = *profile
|
||||
playerWithProfile.Player = playersRaw[i]
|
||||
playerWithProfile.Squad = *squad
|
||||
playerWithProfile.UserRole = squadPlayers[ii].UserType
|
||||
|
||||
players = append(players, playerWithProfile)
|
||||
players = append(players, playerWithProfile)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,140 +57,6 @@ func (s *Squader) getPlayersForSquad(squadID int) ([]dbmapping.SquadPlayerFull,
|
||||
return players, true
|
||||
}
|
||||
|
||||
func (s *Squader) getAllSquadsWithChats() ([]dbmapping.SquadChat, bool) {
|
||||
squadsWithChats := []dbmapping.SquadChat{}
|
||||
squads := []dbmapping.Squad{}
|
||||
|
||||
err := c.Db.Select(&squads, "SELECT * FROM squads")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squadsWithChats, false
|
||||
}
|
||||
|
||||
for i := range squads {
|
||||
chatSquad := dbmapping.SquadChat{}
|
||||
chat := dbmapping.Chat{}
|
||||
floodChat := dbmapping.Chat{}
|
||||
err = c.Db.Get(&chat, c.Db.Rebind("SELECT * FROM chats WHERE id=?"), squads[i].ChatID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squadsWithChats, false
|
||||
}
|
||||
err = c.Db.Get(&floodChat, c.Db.Rebind("SELECT * FROM chats WHERE id=?"), squads[i].FloodChatID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squadsWithChats, false
|
||||
}
|
||||
|
||||
chatSquad.Squad = squads[i]
|
||||
chatSquad.Chat = chat
|
||||
chatSquad.FloodChat = floodChat
|
||||
|
||||
squadsWithChats = append(squadsWithChats, chatSquad)
|
||||
}
|
||||
|
||||
return squadsWithChats, true
|
||||
}
|
||||
|
||||
func (s *Squader) createSquad(update *tgbotapi.Update, chatID int, floodChatID int) (dbmapping.Squad, string) {
|
||||
squad := dbmapping.Squad{}
|
||||
chat := dbmapping.Chat{}
|
||||
floodChat := dbmapping.Chat{}
|
||||
|
||||
// Checking if chats in database exist
|
||||
err := c.Db.Get(&chat, c.Db.Rebind("SELECT * FROM chats WHERE id=?"), chatID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squad, "fail"
|
||||
}
|
||||
err = c.Db.Get(&floodChat, c.Db.Rebind("SELECT * FROM chats WHERE id=?"), floodChatID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squad, "fail"
|
||||
}
|
||||
|
||||
err2 := c.Db.Get(&squad, c.Db.Rebind("SELECT * FROM squads WHERE chat_id IN (?, ?) OR flood_chat_id IN (?, ?)"), chat.ID, floodChat.ID, chat.ID, floodChat.ID)
|
||||
if err2 == nil {
|
||||
return squad, "dup"
|
||||
}
|
||||
c.Log.Debug(err2)
|
||||
|
||||
err = c.Db.Get(&squad, c.Db.Rebind("SELECT * FROM squads WHERE chat_id=? AND flood_chat_id=?"), chatID, floodChatID)
|
||||
if err != nil {
|
||||
c.Log.Debug(err)
|
||||
|
||||
playerRaw, err := c.DataCache.GetPlayerByTelegramID(update.Message.From.ID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return squad, "fail"
|
||||
}
|
||||
|
||||
squad.AuthorID = playerRaw.ID
|
||||
squad.ChatID = chatID
|
||||
squad.FloodChatID = floodChatID
|
||||
squad.CreatedAt = time.Now().UTC()
|
||||
|
||||
_, err = c.Db.NamedExec("INSERT INTO `squads` VALUES(NULL, :chat_id, :flood_chat_id, :author_id, :created_at)", &squad)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squad, "fail"
|
||||
}
|
||||
|
||||
err = c.Db.Get(&squad, c.Db.Rebind("SELECT * FROM squads WHERE chat_id=? AND flood_chat_id=?"), chatID, floodChatID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squad, "fail"
|
||||
}
|
||||
|
||||
return squad, "ok"
|
||||
}
|
||||
|
||||
return squad, "dup"
|
||||
}
|
||||
|
||||
func (s *Squader) getSquadByChatID(update *tgbotapi.Update, chatID int) (dbmapping.Squad, string) {
|
||||
squad := dbmapping.Squad{}
|
||||
chat := dbmapping.Chat{}
|
||||
|
||||
// Checking if chat in database exist
|
||||
err := c.Db.Get(&chat, c.Db.Rebind("SELECT * FROM chats WHERE id=?"), chatID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squad, "fail"
|
||||
}
|
||||
|
||||
err = c.Db.Get(&squad, c.Db.Rebind("SELECT * FROM squads WHERE chat_id=?"), chat.ID)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return squad, "fail"
|
||||
}
|
||||
|
||||
return squad, "ok"
|
||||
}
|
||||
|
||||
func (s *Squader) getUserRoleForSquad(squadID int, playerID int) string {
|
||||
squadPlayer := dbmapping.SquadPlayer{}
|
||||
err := c.Db.Get(&squadPlayer, c.Db.Rebind("SELECT * FROM squads_players WHERE squad_id=? AND player_id=?"), squadID, playerID)
|
||||
if err != nil {
|
||||
c.Log.Debug(err.Error())
|
||||
return "nobody"
|
||||
}
|
||||
|
||||
return squadPlayer.UserType
|
||||
}
|
||||
|
||||
func (s *Squader) deleteFloodMessage(update *tgbotapi.Update) {
|
||||
deleteMessageConfig := tgbotapi.DeleteMessageConfig{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
MessageID: update.Message.MessageID,
|
||||
}
|
||||
|
||||
_, err := c.Bot.DeleteMessage(deleteMessageConfig)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Squader) isUserAnyCommander(playerID int) bool {
|
||||
squadPlayers := []dbmapping.SquadPlayer{}
|
||||
err := c.Db.Select(&squadPlayers, c.Db.Rebind("SELECT * FROM squads_players WHERE player_id=? AND user_type='commander'"), playerID)
|
||||
@@ -200,42 +71,6 @@ func (s *Squader) isUserAnyCommander(playerID int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Squader) squadCreationDuplicate(update *tgbotapi.Update) string {
|
||||
message := "*Отряд уже существует*\n"
|
||||
message += "Проверьте, правильно ли вы ввели команду, и повторите попытку."
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
c.Bot.Send(msg)
|
||||
|
||||
return "fail"
|
||||
}
|
||||
|
||||
func (s *Squader) squadCreationFailure(update *tgbotapi.Update) string {
|
||||
message := "*Не удалось добавить отряд в базу*\n"
|
||||
message += "Проверьте, правильно ли вы ввели команду, и повторите попытку."
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
c.Bot.Send(msg)
|
||||
|
||||
return "fail"
|
||||
}
|
||||
|
||||
func (s *Squader) squadCreationSuccess(update *tgbotapi.Update) string {
|
||||
message := "*Отряд успешно добавлен в базу*\n"
|
||||
message += "Просмотреть список отрядов можно командой /squads."
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
c.Bot.Send(msg)
|
||||
|
||||
return "fail"
|
||||
}
|
||||
|
||||
func (s *Squader) squadUserAdditionFailure(update *tgbotapi.Update) string {
|
||||
message := "*Не удалось добавить игрока в отряд*\n"
|
||||
message += "Проверьте, правильно ли вы ввели команду, и повторите попытку. Кроме того, возможно, что у пользователя нет профиля в боте."
|
||||
@@ -293,8 +128,12 @@ func (s *Squader) AddUserToSquad(update *tgbotapi.Update, adderRaw *dbmapping.Pl
|
||||
c.Log.Error(err.Error())
|
||||
return s.squadUserAdditionFailure(update)
|
||||
}
|
||||
squadRaw := dbmapping.Squad{}
|
||||
err = c.Db.Get(&squadRaw, c.Db.Rebind("SELECT * FROM squads WHERE id=?"), squadID)
|
||||
profileRaw, err := c.DataCache.GetProfileByPlayerID(playerRaw.ID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return s.squadUserAdditionFailure(update)
|
||||
}
|
||||
squadRaw, err := c.DataCache.GetSquadByID(squadID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return s.squadUserAdditionFailure(update)
|
||||
@@ -313,7 +152,17 @@ func (s *Squader) AddUserToSquad(update *tgbotapi.Update, adderRaw *dbmapping.Pl
|
||||
return c.Talkers.AnyMessageUnauthorized(update)
|
||||
}
|
||||
|
||||
if s.getUserRoleForSquad(squadRaw.ID, adderRaw.ID) != "commander" {
|
||||
userRoles := c.DataCache.GetUserRolesInSquads(adderRaw.ID)
|
||||
isCommander := false
|
||||
for i := range userRoles {
|
||||
if userRoles[i].UserRole == "commander" {
|
||||
if userRoles[i].Squad.Squad.ID == squadRaw.Squad.ID {
|
||||
isCommander = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !isCommander {
|
||||
return c.Talkers.AnyMessageUnauthorized(update)
|
||||
}
|
||||
}
|
||||
@@ -321,18 +170,22 @@ func (s *Squader) AddUserToSquad(update *tgbotapi.Update, adderRaw *dbmapping.Pl
|
||||
if !c.Users.PlayerBetterThan(playerRaw, "admin") {
|
||||
if playerRaw.LeagueID != 1 {
|
||||
return s.squadUserAdditionFailure(update)
|
||||
} else if squadRaw.Squad.MinLevel > profileRaw.LevelID {
|
||||
return s.squadUserAdditionFailure(update)
|
||||
} else if squadRaw.Squad.MaxLevel-1 < profileRaw.LevelID {
|
||||
return s.squadUserAdditionFailure(update)
|
||||
}
|
||||
}
|
||||
|
||||
// All checks are passed here, creating new item in database
|
||||
playerSquad := dbmapping.SquadPlayer{}
|
||||
playerSquad.SquadID = squadRaw.ID
|
||||
playerSquad.SquadID = squadRaw.Squad.ID
|
||||
playerSquad.PlayerID = playerRaw.ID
|
||||
playerSquad.UserType = userType
|
||||
playerSquad.AuthorID = adderRaw.ID
|
||||
playerSquad.CreatedAt = time.Now().UTC()
|
||||
|
||||
_, err = c.Db.NamedExec("INSERT INTO squads_players VALUES(NULL, :squad_id, :player_id, :user_type, :author_id, :created_at)", &playerSquad)
|
||||
_, err = c.DataCache.AddPlayerToSquad(&playerSquad)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return s.squadUserAdditionFailure(update)
|
||||
@@ -340,35 +193,3 @@ func (s *Squader) AddUserToSquad(update *tgbotapi.Update, adderRaw *dbmapping.Pl
|
||||
|
||||
return s.squadUserAdditionSuccess(update)
|
||||
}
|
||||
|
||||
// CreateSquad creates new squad from chat if not already exist
|
||||
func (s *Squader) CreateSquad(update *tgbotapi.Update) string {
|
||||
commandArugments := update.Message.CommandArguments()
|
||||
argumentsRx := regexp.MustCompile(`(\d+)\s(\d+)`)
|
||||
|
||||
if !argumentsRx.MatchString(commandArugments) {
|
||||
return s.squadCreationFailure(update)
|
||||
}
|
||||
|
||||
chatNumbers := strings.Split(commandArugments, " ")
|
||||
if len(chatNumbers) < 2 {
|
||||
return s.squadCreationFailure(update)
|
||||
}
|
||||
chatID, _ := strconv.Atoi(chatNumbers[0])
|
||||
if chatID == 0 {
|
||||
return s.squadCreationFailure(update)
|
||||
}
|
||||
floodChatID, _ := strconv.Atoi(chatNumbers[1])
|
||||
if floodChatID == 0 {
|
||||
return s.squadCreationFailure(update)
|
||||
}
|
||||
|
||||
_, ok := s.createSquad(update, chatID, floodChatID)
|
||||
if ok == "fail" {
|
||||
return s.squadCreationFailure(update)
|
||||
} else if ok == "dup" {
|
||||
return s.squadCreationDuplicate(update)
|
||||
}
|
||||
|
||||
return s.squadCreationSuccess(update)
|
||||
}
|
||||
|
@@ -12,20 +12,8 @@ import (
|
||||
type SquaderInterface interface {
|
||||
Init()
|
||||
|
||||
GetAllSquadChats() ([]dbmapping.Chat, bool)
|
||||
GetAllSquadFloodChats() ([]dbmapping.Chat, bool)
|
||||
GetAvailableSquadChatsForUser(playerRaw *dbmapping.Player) ([]dbmapping.Chat, bool)
|
||||
GetCommandersForSquadViaChat(chatRaw *dbmapping.Chat) ([]dbmapping.Player, bool)
|
||||
GetSquadByID(squadID int) (dbmapping.SquadChat, bool)
|
||||
GetSquadChatsBySquadsIDs(squadsID string) ([]dbmapping.Chat, bool)
|
||||
GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping.SquadPlayerFull, bool)
|
||||
IsChatASquadEnabled(chatRaw *dbmapping.Chat) string
|
||||
|
||||
AddUserToSquad(update *tgbotapi.Update, adderRaw *dbmapping.Player) string
|
||||
CreateSquad(update *tgbotapi.Update) string
|
||||
|
||||
SquadInfo(update *tgbotapi.Update, playerRaw *dbmapping.Player) string
|
||||
SquadsList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string
|
||||
|
||||
CleanFlood(update *tgbotapi.Update, chatRaw *dbmapping.Chat) string
|
||||
}
|
||||
|
Reference in New Issue
Block a user