比赛与其玩家之间的数据库关系

发布于 2025-01-25 20:23:47 字数 1546 浏览 2 评论 0原文

我正在尝试设计一个可以在3V3游戏中节省匹配和玩家的数据库。

到目前为止,我的模型看起来像这样:

class Player(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
    player_tag = models.CharField(max_length=9, unique=True)
    player_name = models.CharField(max_length=50)
    trophy_count = models.IntegerField(default=0)
    club = models.ForeignKey('Club', on_delete=models.SET_NULL, null=True)
    total_club_war_trophy_count = models.IntegerField(default=0)


class Club(models.Model):
    club_name = models.CharField(max_length=50)
    club_tag = models.CharField(max_length=9, unique=True)


class Match(models.Model):
    # Matches are 3v3
    player_1 = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_2 = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_3 = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_4 = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_5 = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_6 = models.ForeignKey(Player, on_delete=models.CASCADE)
    # Brawlball, Gem grab, Knockout ...
    mode = models.CharField(max_length=20)
    # Power match or normal match
    battle_type = models.CharField(max_length=20)
    trophies_won = models.IntegerField(default=0)
    date = models.DateTimeField(auto_now_add=True)

但是“ player_< int ”的重复是在发痒,我认为这不是正确的方法。如果玩家数量在某个时候发生变化,或者我如何找到玩家参加比赛怎么办?我认为这充其量是一种笨拙的方法。

我怎么能更好地解决这个问题?我当时正在考虑一些玩家清单,但我不知道如何表征这种关系。

I'm trying to design a database that would save matches and players in a 3v3 game.

So far my models look like this :

class Player(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
    player_tag = models.CharField(max_length=9, unique=True)
    player_name = models.CharField(max_length=50)
    trophy_count = models.IntegerField(default=0)
    club = models.ForeignKey('Club', on_delete=models.SET_NULL, null=True)
    total_club_war_trophy_count = models.IntegerField(default=0)


class Club(models.Model):
    club_name = models.CharField(max_length=50)
    club_tag = models.CharField(max_length=9, unique=True)


class Match(models.Model):
    # Matches are 3v3
    player_1 = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_2 = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_3 = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_4 = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_5 = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_6 = models.ForeignKey(Player, on_delete=models.CASCADE)
    # Brawlball, Gem grab, Knockout ...
    mode = models.CharField(max_length=20)
    # Power match or normal match
    battle_type = models.CharField(max_length=20)
    trophies_won = models.IntegerField(default=0)
    date = models.DateTimeField(auto_now_add=True)

However the repetition of "player_<int>" is itching me, I don't think this is the proper way to do it. What if the number of player changes at some point, or how to I find if a player participated in a match ? I think it's a clunky approach at best.

How could I approach this better ? I was thinking about maybe a list of players, but I don't know how to characterize this kind of relationship.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2025-02-01 20:23:47

您可以使用Django的ManyTomanyField如下:

class Match(models.Model):
    # Matches are 3v3
    players = models.ManyToManyField(Player, blank = True)
    # Brawlball, Gem grab, Knockout ...
    mode = models.CharField(max_length=20)
    # Power match or normal match
    battle_type = models.CharField(max_length=20)
    trophies_won = models.IntegerField(default=0)
    date = models.DateTimeField(auto_now_add=True)

然后,您可以将玩家设置为匹配。

match = Match.objects.get(pk = 1)
# players with ids (1,2,3) are in the match with id of 1.
match.players.set([1,2,3])

You can use Django's ManyToManyField like the following:

class Match(models.Model):
    # Matches are 3v3
    players = models.ManyToManyField(Player, blank = True)
    # Brawlball, Gem grab, Knockout ...
    mode = models.CharField(max_length=20)
    # Power match or normal match
    battle_type = models.CharField(max_length=20)
    trophies_won = models.IntegerField(default=0)
    date = models.DateTimeField(auto_now_add=True)

Then you can set players to a match.

match = Match.objects.get(pk = 1)
# players with ids (1,2,3) are in the match with id of 1.
match.players.set([1,2,3])
安穩 2025-02-01 20:23:47

我认为我一直在寻找的是Manytomany的关系。
doc: https:// https://djangoproject.com/fr/4.0 /topics/db/xplass/many_to_many/

I think that what I was looking for was a ManyToMany Relationship.
Doc : https://docs.djangoproject.com/fr/4.0/topics/db/examples/many_to_many/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文