Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution-py-game-models #1020

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions db/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 4.0.2 on 2024-11-03 01:48

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Guild',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, unique=True)),
('description', models.TextField(null=True)),
],
),
migrations.CreateModel(
name='Race',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, unique=True)),
('description', models.TextField(blank=True)),
],
),
migrations.CreateModel(
name='Skill',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, unique=True)),
('bonus', models.CharField(max_length=255)),
('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'to' parameter in the ForeignKey should match the correct app label. Ensure that 'db.race' is the correct reference. If 'race' is in the same app, it should be 'Race' instead of 'db.race'.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ForeignKey reference 'to='db.race'' should be 'to='app_name.Race'' where 'app_name' is the name of the Django app containing the models. Ensure that the app name is correctly specified to avoid import errors.

],
),
migrations.CreateModel(
name='Player',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('nickname', models.CharField(max_length=255, unique=True)),
('email', models.EmailField(max_length=255)),
('bio', models.CharField(max_length=255)),
('created_at', models.DateTimeField(auto_now_add=True)),
('guild', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='db.guild')),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'to' parameter in the ForeignKey should match the correct app label. Ensure that 'db.guild' is the correct reference. If 'guild' is in the same app, it should be 'Guild' instead of 'db.guild'.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ForeignKey reference 'to='db.guild'' should be 'to='app_name.Guild'' where 'app_name' is the name of the Django app containing the models. Ensure that the app name is correctly specified to avoid import errors.

('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'to' parameter in the ForeignKey should match the correct app label. Ensure that 'db.race' is the correct reference. If 'race' is in the same app, it should be 'Race' instead of 'db.race'.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ForeignKey reference 'to='db.race'' should be 'to='app_name.Race'' where 'app_name' is the name of the Django app containing the models. Ensure that the app name is correctly specified to avoid import errors.

],
),
]
25 changes: 25 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
from django.db import models


class Race(models.Model):
name = models.CharField(max_length=255, unique=True)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each player should choose a race to play, such as Elf, Dwarf, Human, or Ork.

Your Race model currently doesn't include predefined choices like Elf, Dwarf, Human, or Ork, as mentioned in the task. Consider adding a choices parameter to ensure players can select from these specific options.

description = models.TextField(blank=True)


class Skill(models.Model):
name = models.CharField(max_length=255, unique=True)
bonus = models.CharField(max_length=255)
race = models.ForeignKey(Race, on_delete=models.CASCADE)


class Guild(models.Model):
name = models.CharField(max_length=255, unique=True)
description = models.TextField(null=True)


class Player(models.Model):
nickname = models.CharField(max_length=255, unique=True)
email = models.EmailField(max_length=255)
bio = models.CharField(max_length=255)
race = models.ForeignKey(Race, on_delete=models.CASCADE)
guild = models.ForeignKey(Guild, on_delete=models.SET_NULL, null=True)
created_at = models.DateTimeField(auto_now_add=True)
59 changes: 58 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,67 @@
import os
import json

import init_django_orm # noqa: F401

from db.models import Race, Skill, Player, Guild


def main() -> None:
pass
players = file_data("players.json")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the 'players.json' file exists in the correct directory, or handle the potential FileNotFoundError exception in the 'file_data' function.

create_db(players)


def create_db(players: dict) -> None:
for player, info in players.items():
email_info = info.get("email")
bio_info = info.get("bio")

if race_info := info.get("race"):
race_name = race_info.get("name")
race_description = race_info.get("description")

race, race_created = Race.objects.get_or_create(
name=race_name,
description=race_description
)

if skills_info := race_info.get("skills"):
for skill in skills_info:
skill_name = skill.get("name")
skill_bonus = skill.get("bonus")

Skill.objects.get_or_create(
name=skill_name, bonus=skill_bonus, race=race
)
else:
race = None

if guild_info := info.get("guild"):
guild_name = guild_info.get("name")
guild_description = guild_info.get("description")

guild, guild_created = Guild.objects.get_or_create(
name=guild_name,
description=guild_description
)
else:
guild = None

Player.objects.create(
Copy link

@vladyslav-tmf vladyslav-tmf Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use Player.objects.get_or_create(...) instead of Player.objects.create(...) to avoid IntegrityError if you try to create a player with an existing nickname, since the nickname field is unique (unique=True).

nickname=player,
email=email_info,
bio=bio_info,
race=race,
guild=guild
)


def file_data(path: str) -> dict:
file_path = os.path.join(path)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of os.path.join(path) is unnecessary if path is already a complete file path. Consider verifying if path needs to be joined with another directory path.

with open(file_path) as f:
players = json.load(f)

return players


if __name__ == "__main__":
Expand Down
Loading