So i have a class that holds the moves of each player, 1 offensive and 1 defensive. I also allow the site owner to customize the moves. When a player starts a match he pick’s 6 offensive move and 6 defensive moves. The opponent is then notified and he does the same. In this case they would pick from the customized move set:
rom django.db import models
from characters.models import Character
from django.core.validators import MaxValueValidator, MinValueValidator
from django.core.exceptions import ValidationError
# Create your models here.
class MatchIndex(models.Model):
Open = 0
Completed = 1
Cancelled = 2
Error = 3
MATCH_STATUS = (
(0, "Open"),
(1, "Completed"),
(2, "Cancelled"),
(3, "Error"),
)
challenger = models.ForeignKey(Character,
help_text="The match's challenger",
related_name="MatchIndex_challenger",
on_delete=models.CASCADE)
opponent = models.ForeignKey(Character,
help_text="The match's opponent",
related_name="MatchIndex_opponent",
on_delete=models.CASCADE)
status = models.PositiveSmallIntegerField(
choices=MATCH_STATUS,
default=0,
help_text="The match status")
class Meta:
verbose_name_plural = "Match Indexes"
class Move(models.Model):
Offensive = 0
Defensive = 1
MOVE_TYPE = (
(0, "Offensive"),
(1, "Defensive"),
)
name = models.CharField(
max_length=16,
help_text="The move's name")
value = models.PositiveSmallIntegerField(
validators=[MinValueValidator(0), MaxValueValidator(99)],
help_text="Value used to map moves and order by on form")
move_type = models.PositiveSmallIntegerField(
choices=MOVE_TYPE,
default=0,
help_text="The move type offensive or defensive"
)
class MatchMoves(models.Model):
match = models.ForeignKey(MatchIndex,
help_text="Match ID",
on_delete=models.CASCADE)
challenger_offense = models.ManyToManyField(Move,
help_text="The challenger's offensive moves for the match",
blank=True,
related_name="MatchMoves_challenger_offense")
challenger_defense = models.ManyToManyField(Move,
help_text="The challenger's defensive moves for the match",
blank=True,
related_name="MatchMoves_challenger_defense")
opponent_offense = models.ManyToManyField(Move,
help_text="The opponent's offensive moves for the match",
blank=True,
related_name="MatchMoves_opponent_offense")
opponent_defense = models.ManyToManyField(Move,
help_text="The opponent's defensive moves for the match",
blank=True,
related_name="MatchMoves_opponent_defense")
class Meta:
verbose_name_plural = "Match Moves"
def clean(self):
if self.challenger_offense.count() > 6:
raise ValidationError("Challenger has too many offensive moves")
for move in self.challenger_offense.objects.all():
if move.move_type != Move.Offensive:
raise ValidationError("incorrect move used for challenger offense")
if self.challenger_defense.count() > 6:
raise ValidationError("challenger has too many defensive moves")
for move in self.challenger_defense.objects.all():
if move.move_type != Move.Defensive:
raise ValidationError("incorrect move used for challenger defense")
if self.opponent_offense.count() > 6:
raise ValidationError("Opponent has too many offensive moves")
for move in self.opponent_offense.objects.all():
if move.move_type != Move.Offensive:
raise ValidationError("incorrect move used for opponent offense")
if self.opponent_defense.count() > 6:
raise ValidationError("opponent has too many defensive moves")
for move in self.opponent_defense.objects.all():
if move.move_type != Move.Defensive:
raise ValidationError("incorrect move used for opponent defense")
How would i make it so for example Challenger Offense could have multiples of the same move?