views:

25

answers:

1

I have the following (stripped down) code:

# games/models.py
side_choices = [('A', 'Attack'), ('D', 'Defense')]
position_choices = [(0, 'Commander'), (1, 'Knight'), (2, 'Mage'), (3, 'Healer')]
class Game(models.Model):
    users = models.ManyToManyField(User, through='GameParticipation)) // User is Django's user


class GameParticipation(models.Model):
    user = models.ForeignKey(User)
    game = models.ForeignKey(Game)
    side = models.CharField(choices=side_choices)
    position = models.PositiveSmallIntegerField(choices=position_choices)

# games/forms.py
class JoinForm(forms.Form):
    side = forms.ChoiceField(choices=side_choices)
    position = forms.ChoiceField(choices=position_choices)        

And I haven't a clue how I can write the code to get a list of free spaces in a game, and pass it to the JoinForm so that users don't see options for a taken space.

A: 

Need more info here, but assuming you know only spaces that are taken are in the database, can probably figure out which are free.

#assuming you know the game and user
game=1
user=2

#get all used spaces
usedSpaces=GameParticipation.objects.filter(game__get=game).filter(user__get=user)
#figure out from size of board, which spaces are empty

or if you know empty spaces have null values

emptySpaces=GameParticipation.objects.filter(game__get=game).filter(user__get=user).filter(position__exact=None)
JiminyCricket
Each space is taken by a different user.
Not Joe Bloggs
The information I have is: The game, and a user who wishes to join, and spaces that are taken.
Not Joe Bloggs
do you know how many spaces there are total? if you only know the spaces taken and not the size of the board then there is no way you can figure out the empty spaces
JiminyCricket
8 spaces. (And I didn't mention any board)
Not Joe Bloggs
well if there are 8 spaces, and the above query returns 5 spaces, then there are 3 free spaces. however i cant really visualize what sort of physical environment you are in. whats a space? is it a coordinate system? are all spaces next to one another? does each user have their own specific space? can they move around to other spaces? is their a finite number of knights, mages,healers? with all that info you can probably deduce where the empty stuff is. but again, you need to explain this a bit more. i was thinking this was some sort of board game prog problem so i assumed a coordinate board
JiminyCricket
There are 2 teams (Attackers and Defenders), each with 4 players. Each class is allowed to be used by only one player on each team.
Not Joe Bloggs
so then there are 8 players and 8 spaces....query all the players and get their spaces. any space that doesn't have a player is an empty space
JiminyCricket
And how do I pass this to the form as the possible choices?
Not Joe Bloggs
look at the django form documentation. fairly straightforward http://docs.djangoproject.com/en/dev/topics/forms/
JiminyCricket
if this answered your question, please hit the green checkmark
JiminyCricket