diff --git a/lib/datacache/datacacheinterface/datacacheinterface.go b/lib/datacache/datacacheinterface/datacacheinterface.go index aee154b..bbbc400 100644 --- a/lib/datacache/datacacheinterface/datacacheinterface.go +++ b/lib/datacache/datacacheinterface/datacacheinterface.go @@ -21,6 +21,7 @@ type DataCacheInterface interface { UpdateChatTitle(chatID int, newTitle string) (*dbmapping.Chat, error) AddPlayerToSquad(relation *dbmapping.SquadPlayer) (int, error) + GetAllSquadMembers(squadID int) []dbmapping.SquadPlayerFull GetAllSquadsChats() []dbmapping.Chat GetAllSquadsWithChats() []dbmapping.SquadChat GetAvailableSquadsChatsForUser(userID int) []dbmapping.Chat diff --git a/lib/datacache/squads.go b/lib/datacache/squads.go index d33e632..37f3f1a 100644 --- a/lib/datacache/squads.go +++ b/lib/datacache/squads.go @@ -122,6 +122,21 @@ func (dc *DataCache) AddPlayerToSquad(relation *dbmapping.SquadPlayer) (int, err return insertedRelation.ID, nil } +// GetAllSquadMembers returns all squad members by squad ID +func (dc *DataCache) GetAllSquadMembers(squadID int) []dbmapping.SquadPlayerFull { + players := []dbmapping.SquadPlayerFull{} + dc.squadsMutex.Lock() + for i := range dc.squadPlayers { + if i == squadID { + for j := range dc.squadPlayers[i] { + players = append(players, *dc.squadPlayers[i][j]) + } + } + } + dc.squadsMutex.Unlock() + return players +} + // GetAllSquadsChats returns all chats belonging to squads func (dc *DataCache) GetAllSquadsChats() []dbmapping.Chat { chats := []dbmapping.Chat{} diff --git a/lib/squader/responders.go b/lib/squader/responders.go index 0a85e80..ca1cb04 100644 --- a/lib/squader/responders.go +++ b/lib/squader/responders.go @@ -66,10 +66,7 @@ func (s *Squader) SquadInfo(update *tgbotapi.Update, playerRaw *dbmapping.Player message += c.Statistics.SquadStatictics(squad.Squad.ID) message += "\n" - squadMembers, ok := s.getPlayersForSquad(squad.Squad.ID) - if !ok { - return "fail" - } + squadMembers := c.DataCache.GetAllSquadMembers(squadID) if len(squadMembers) > 0 { message += "Участники отряда:\n" for i := range squadMembers { diff --git a/lib/squader/squader.go b/lib/squader/squader.go index 14eed31..ee753eb 100644 --- a/lib/squader/squader.go +++ b/lib/squader/squader.go @@ -12,51 +12,6 @@ import ( "time" ) -func (s *Squader) getPlayersForSquad(squadID int) ([]dbmapping.SquadPlayerFull, bool) { - players := []dbmapping.SquadPlayerFull{} - playersRaw := []dbmapping.Player{} - squadPlayers := []dbmapping.SquadPlayer{} - - 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) - 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{} - 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) - } - } - } - } - - return players, true -} - func (s *Squader) isUserAnyCommander(playerID int) bool { userRoles := c.DataCache.GetUserRolesInSquads(playerID) for i := range userRoles { @@ -146,10 +101,6 @@ func (s *Squader) AddUserToSquad(update *tgbotapi.Update, adderRaw *dbmapping.Pl } if !c.Users.PlayerBetterThan(adderRaw, "admin") { - if userType == "commander" { - return c.Talkers.AnyMessageUnauthorized(update) - } - userRoles := c.DataCache.GetUserRolesInSquads(adderRaw.ID) isCommander := false for i := range userRoles {