2017-11-19 22:16:11 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
|
|
|
|
package squader
|
|
|
|
|
|
|
|
|
|
import (
|
2018-01-29 23:50:25 +04:00
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2017-11-19 22:16:11 +04:00
|
|
|
|
"regexp"
|
2018-02-17 07:03:58 +04:00
|
|
|
|
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
2017-11-19 22:16:11 +04:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2017-11-24 00:16:22 +04:00
|
|
|
|
func (s *Squader) getPlayersForSquad(squadID int) ([]dbmapping.SquadPlayerFull, bool) {
|
|
|
|
|
players := []dbmapping.SquadPlayerFull{}
|
|
|
|
|
playersRaw := []dbmapping.Player{}
|
|
|
|
|
squadPlayers := []dbmapping.SquadPlayer{}
|
|
|
|
|
|
2018-02-17 07:03:58 +04:00
|
|
|
|
squad, err := c.DataCache.GetSquadByID(squadID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-11-24 00:16:22 +04:00
|
|
|
|
return players, false
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-17 07:03:58 +04:00
|
|
|
|
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)
|
2017-11-24 00:16:22 +04:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
return players, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = c.Db.Select(&squadPlayers, c.Db.Rebind("SELECT * FROM squads_players WHERE squad_id=?"), squad.Squad.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
return players, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := range playersRaw {
|
|
|
|
|
for ii := range squadPlayers {
|
|
|
|
|
if squadPlayers[ii].PlayerID == playersRaw[i].ID {
|
|
|
|
|
playerWithProfile := dbmapping.SquadPlayerFull{}
|
2018-02-17 07:03:58 +04:00
|
|
|
|
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)
|
|
|
|
|
}
|
2017-11-24 00:16:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return players, true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Squader) isUserAnyCommander(playerID int) bool {
|
|
|
|
|
squadPlayers := []dbmapping.SquadPlayer{}
|
2017-11-26 06:48:13 +04:00
|
|
|
|
err := c.Db.Select(&squadPlayers, c.Db.Rebind("SELECT * FROM squads_players WHERE player_id=? AND user_type='commander'"), playerID)
|
2017-11-24 00:16:22 +04:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Debug(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(squadPlayers) > 0 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Squader) squadUserAdditionFailure(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) squadUserAdditionSuccess(update *tgbotapi.Update) string {
|
|
|
|
|
message := "*Игрок добавлен в отряд*\n"
|
|
|
|
|
message += "Теперь вы можете дать ему ссылку для входа в чаты отряда."
|
|
|
|
|
|
|
|
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
|
|
|
|
msg.ParseMode = "Markdown"
|
|
|
|
|
|
|
|
|
|
c.Bot.Send(msg)
|
|
|
|
|
|
|
|
|
|
return "ok"
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 22:16:11 +04:00
|
|
|
|
// External functions
|
|
|
|
|
|
2017-11-24 00:16:22 +04:00
|
|
|
|
// AddUserToSquad adds user to squad
|
|
|
|
|
func (s *Squader) AddUserToSquad(update *tgbotapi.Update, adderRaw *dbmapping.Player) string {
|
|
|
|
|
command := update.Message.Command()
|
|
|
|
|
commandArugments := update.Message.CommandArguments()
|
|
|
|
|
userType := "user"
|
|
|
|
|
if command == "squad_add_commander" {
|
|
|
|
|
userType = "commander"
|
|
|
|
|
}
|
|
|
|
|
argumentsRx := regexp.MustCompile(`(\d+)\s(\d+)`)
|
|
|
|
|
if !argumentsRx.MatchString(commandArugments) {
|
|
|
|
|
return s.squadUserAdditionFailure(update)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
argumentNumbers := strings.Split(commandArugments, " ")
|
|
|
|
|
if len(argumentNumbers) < 2 {
|
|
|
|
|
return s.squadUserAdditionFailure(update)
|
|
|
|
|
}
|
|
|
|
|
squadID, _ := strconv.Atoi(argumentNumbers[0])
|
|
|
|
|
if squadID == 0 {
|
|
|
|
|
return s.squadUserAdditionFailure(update)
|
|
|
|
|
}
|
|
|
|
|
playerID, _ := strconv.Atoi(argumentNumbers[1])
|
|
|
|
|
if playerID == 0 {
|
|
|
|
|
return s.squadUserAdditionFailure(update)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
playerRaw, err := c.DataCache.GetPlayerByID(playerID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-11-24 00:16:22 +04:00
|
|
|
|
return s.squadUserAdditionFailure(update)
|
|
|
|
|
}
|
2018-02-17 07:03:58 +04:00
|
|
|
|
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)
|
2017-11-24 00:16:22 +04:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
return s.squadUserAdditionFailure(update)
|
|
|
|
|
}
|
2017-11-26 16:12:49 +04:00
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
if !c.Users.PlayerBetterThan(playerRaw, "admin") {
|
|
|
|
|
_, err = c.DataCache.GetProfileByPlayerID(playerRaw.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-11-26 16:12:49 +04:00
|
|
|
|
return s.squadUserAdditionFailure(update)
|
|
|
|
|
}
|
2017-11-24 00:16:22 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !c.Users.PlayerBetterThan(adderRaw, "admin") {
|
|
|
|
|
if userType == "commander" {
|
2017-11-25 03:00:34 +04:00
|
|
|
|
return c.Talkers.AnyMessageUnauthorized(update)
|
2017-11-24 00:16:22 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-17 07:03:58 +04:00
|
|
|
|
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 {
|
2017-11-25 03:00:34 +04:00
|
|
|
|
return c.Talkers.AnyMessageUnauthorized(update)
|
2017-11-24 00:16:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
if !c.Users.PlayerBetterThan(playerRaw, "admin") {
|
2017-11-24 00:16:22 +04:00
|
|
|
|
if playerRaw.LeagueID != 1 {
|
|
|
|
|
return s.squadUserAdditionFailure(update)
|
2018-02-17 07:03:58 +04:00
|
|
|
|
} else if squadRaw.Squad.MinLevel > profileRaw.LevelID {
|
|
|
|
|
return s.squadUserAdditionFailure(update)
|
|
|
|
|
} else if squadRaw.Squad.MaxLevel-1 < profileRaw.LevelID {
|
|
|
|
|
return s.squadUserAdditionFailure(update)
|
2017-11-24 00:16:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All checks are passed here, creating new item in database
|
|
|
|
|
playerSquad := dbmapping.SquadPlayer{}
|
2018-02-17 07:03:58 +04:00
|
|
|
|
playerSquad.SquadID = squadRaw.Squad.ID
|
2017-11-24 00:16:22 +04:00
|
|
|
|
playerSquad.PlayerID = playerRaw.ID
|
|
|
|
|
playerSquad.UserType = userType
|
|
|
|
|
playerSquad.AuthorID = adderRaw.ID
|
|
|
|
|
playerSquad.CreatedAt = time.Now().UTC()
|
|
|
|
|
|
2018-02-17 07:03:58 +04:00
|
|
|
|
_, err = c.DataCache.AddPlayerToSquad(&playerSquad)
|
2017-11-24 00:16:22 +04:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err.Error())
|
|
|
|
|
return s.squadUserAdditionFailure(update)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s.squadUserAdditionSuccess(update)
|
|
|
|
|
}
|