Archived
1

Squad members from DataCache

This commit is contained in:
Vladimir Hodakov 2018-02-17 17:19:50 +04:00
parent 20b66480ef
commit 2617375f46
4 changed files with 17 additions and 53 deletions

View File

@ -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

View File

@ -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{}

View File

@ -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 {

View File

@ -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 {