2017-11-24 00:16:22 +04:00
// i2_bot – Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package squader
import (
2018-01-21 23:28:53 +04:00
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
2017-11-26 15:28:55 +04:00
"strconv"
"strings"
2017-11-24 00:16:22 +04:00
)
2017-12-23 17:03:26 +04:00
// 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
}
2017-11-24 00:16:22 +04:00
// 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
}
2017-11-26 06:48:13 +04:00
// GetAvailableSquadChatsForUser returns squad chats which user can join
func ( s * Squader ) GetAvailableSquadChatsForUser ( playerRaw * dbmapping . Player ) ( [ ] dbmapping . Chat , bool ) {
groupChats := [ ] dbmapping . Chat { }
2017-12-23 17:03:26 +04:00
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
}
2017-11-26 06:48:13 +04:00
}
return groupChats , true
}
2017-11-24 13:24:36 +04:00
// 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
}
2017-11-26 06:48:13 +04:00
// 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
}
2017-11-26 15:28:55 +04:00
// 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
}
2017-11-24 00:16:22 +04:00
// 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 , profileOk := c . Users . GetProfile ( playerRaw . ID )
userRoleFull . Profile = userProfile
userRoleFull . UserRole = userRolesRaw [ i ] . UserType
squad , squadOk := s . GetSquadByID ( userRolesRaw [ i ] . SquadID )
userRoleFull . Squad = squad
if profileOk && squadOk {
userRoles = append ( userRoles , userRoleFull )
}
}
return userRoles , true
}
2017-11-26 06:48:13 +04:00
// 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"
}