So this isn't EXACTLY my code but it is close enough to show what I have. Also the naming is only for this site so if its not really clear forgive me. Its much clearer in the actual code.
class Member(models.Model):
team = models.IntegerField(default=0)
match = models.ForeignKey(Match, null=True, blank=True)
def engage(self, match_instance, team_number):
self.match = match_instance
self.team = team_number
self.save()
class NPCGeneric(models.Model):
speed = models.IntegerField(default=1)
difficulty = models.IntegerField(default=0)
class NPCInstance(Member):
generic = models.ForiegnKey(NPCGeneric);
status = models.CharField(max_length=50)
def engage(self, match_instance, team_number):
new_instance = NPCInstance(generic=self)
new_instance.save()
return new_instance.engage(match_instance, team_number)
class Player(Member):
name = models.CharField(max_length=100)
class Match(models.Model):
event_members = []
def __init__(self, *args, **kwargs):
retval = super(Match, self).__init__(*args, **kwargs)
if(args[0] and hasattr(args[0], '__iter__')):
teams = args[0]
args = args[1:]
self.__init_new__(teams)
elif(self.id):
self.__init_existing__()
return retval
def __init_new__(self, teams):
new_teams = []
for team in teams:
new_team = []
new_teams.append(new_team)
team_number = new_teams.index(new_team)
for event_member in team:
team_member = event_member .engage(self, team_number)
team_member .save()
new_team.append(team_member)
self.event_members = new_teams
def __init_existing__(self):
new_teams = []
def add_list(list, member, team):
while(list.__len__() < (team + 1)):
list.append([])
list[team].append(member)
for event_member in self.member_set.all():
try:
member = event_member.npcinstance
except:
member = event_member.player
add_list(new_teams, member, member.team)
self.event_members= new_teams
So this is where I start thinking that there has to be a better cleaner way. Unfortunately my brain just keeps getting stuck on what I already have.
I'd love to know if anyone can suggest a cleaner/Pythonic way for holding the Members in the Match class. specifically:
- The initialising just seems very odd and clumsy. I'm afraid I will introduce errors this way.
- a better way of representing teams other than a list of lists
event_members
Match
initialization might be "odd and clumsy", but without knowing the intent of the code it's hard to tell if it is so and whether there are alternatives. It would be also useful if you comment the code for clarification (especially the "odd" places). \$\endgroup\$member_set
but i can't really access it per team. and if a team gets knocked out of the match then there might be a team missing (e.g. teams 1, 2, 4 and 5) might be still in the match but not team 3). The Intent of the code is to holdMembers
grouped by team in an object calledMatch
. \$\endgroup\$member_set
:member_set.filter(team=<some_team_number>)
. Also, why not add members to matches viaTeam
objects? You'll be able to access members on a match like that:team_set.get(number=<some_team_number>).member_set
. And if team gets knocked out, you can just set a flag onTeam
object (or clear itsForeignKey
, which will remove it from match'steam_set
). \$\endgroup\$Team
objects but I was hoping not to get too complex. i can't access the filtering in the templates either. \$\endgroup\$