Rematch5SavedTeams={}-- actual savedvar for the teams (don't reference this directly)
-- some teams are not a savedvar but can be used/referenced like one and serve a special role
localmetaTeams={
empty={teamID="empty",name="",pets={},tags={}},-- a team with empty values (for resetting sideline or temporary)
sideline={teamID="sideline",name="",pets={},tags={}},-- teams being created/manipulated without affecting a live team
loadonly={teamID="loadonly",name=BATTLE_PET_SLOTS,pets={},tags={}},-- teams loaded without being saved
temporary={teamID="temporary",name="",pets={},tags={}},-- temporary holding place for teams
original={teamID="original",name="",pets={},tags={}},-- for save dialog to compare original team against modifications
counter={teamID="counter",name=C.COUNTER_TEAM_NAME,pets={},tags={}},-- for random counter teams from target panel
}
-- indexed by petID, the count of teams that contain the petID (updated in updatePets())
localnumPetsByTeamID={}
-- indexed by team name, the teamID that has the name
localteamIDsByName={}
-- number of teams (updated during afterTeamsChange)
localnumTeams=0
-- ordered list of team events and the args to pass when they fire at the end of afterTeamsChanged
dispatch={}
--[[ local functions ]]
-- returns true if the given team is structured properly
localfunctionvalidateTeamStructure(team)
ifnotteamortype(team)~="table"then-- this isn't even an attempt at a team
returnfalse
elseifnotteam.teamIDthen-- all teams must have a teamID
returnfalse
elseifnotteam.namethen-- all teams must have a name
returnfalse
elseiftype(team.pets)~="table"then-- all teams must have a list of petIDs (even if empty)
returnfalse
elseiftype(team.tags)~="table"then-- all teams must have a list of petTags (even if empty)
returnfalse
end
returntrue-- gauntlet passed, this team is valid
end
-- copies a team from the source to the destination; all except teamID
localfunctioncopyTeam(source,dest)
assert(type(dest)=="table"anddest.teamID,"Attempt to copy to a malformed team: "..(source.teamIDor"nil").." to "..(type(dest)=="table"and"nil"or"non-tabnle"))
assert(validateTeamStructure(source),"Attempt to copy from a malformed team: "..(source.teamIDor"nil").." to "..(dest.teamIDor"nil"))
source.name=source.name:trim()
-- first wipe the destination of all but teamID
fork,vinpairs(dest)do
ifk=="teamID"then
-- do nothing
elseifk=="pets"ork=="tags"then
wipe(v)-- pets and tags tables are mandatory
else
dest[k]=nil-- but all other tables (and non-tables beside teamID) are optional
end
end
-- now copy contents of source to dest
fork,vinpairs(source)do
ifk=="teamID"then
-- never replace teamID
elseiftype(v)=="table"then--k=="pets" or k=="tags" or k=="targets" or k=="preferences" then
dest[k]=CopyTable(v)
else
dest[k]=v
end
end
-- if non-meta team doesn't have a group after copy, put it in group:none
ifnotdest.groupIDandnotmetaTeams[dest.teamID]then
dest.groupID="group:none"
end
end
-- returns an unused team:<number> for use as a unique teamID
localfunctiongetNewUniqueTeamID()
localteamID=1
whileRematch5SavedTeams["team:"..teamID]do
teamID=teamID+1
end
return"team:"..teamID
end
-- rematch.savedTeams[key] will get the savedvar unless it's a meta teamID
localfunctiongetter(self,key)
ifmetaTeams[key]then
returnmetaTeams[key]
else-- every other teamID should be a number
returnRematch5SavedTeams[key]-- otherwise returns the savedvar team of the given temID
end
end
-- rematch.savedTeams[key] = {team} will "copy" the contents of {team} to rematch.savedTeams[key]
-- if {team} is nil, it will empty metateams or nil savedvar teams
localfunctionsetter(self,key,value)
ifkey=="empty"then
return-- empty team should never be modified
elseifvalue==nilthen-- if nil'ing a team
ifmetaTeams[key]then-- if nil'ing a metateam, empty it but don't nil it
copyTeam(metaTeams.empty,metaTeams[key])
else-- otherwise nil the saved team
Rematch5SavedTeams[key]=nil
tinsert(dispatch,{"REMATCH_TEAM_DELETED",key})
rematch.savedTeams:TeamsChanged()
end
elseifvalidateTeamStructure(value)then-- if assigning another team to this team
value.name=value.name:trim()
ifmetaTeams[key]then-- if setting a metaTeam value, then copy the team
copyTeam(value,metaTeams[key])
elseifRematch5SavedTeams[key]then-- team already exists, replace contents
copyTeam(value,Rematch5SavedTeams[key])
tinsert(dispatch,{"REMATCH_TEAM_UPDATED",key})
rematch.savedTeams:TeamsChanged()
end
end
end
-- updates numPetsByTeamID with all petIDs in a team and how many teams they belong to
-- while here looping through teams, update numTeams to the number of teams