From 16d9c4a2efdcbfb9dee51c092fb627ec6dd71744 Mon Sep 17 00:00:00 2001 From: roman Date: Sun, 3 Nov 2024 09:51:34 +0200 Subject: [PATCH 1/4] first --- db/migrations/0001_initial.py | 52 ++++++++++++++++++++++ db/models.py | 25 +++++++++++ main.py | 82 ++++++++++++++++++++++++++++++++++- 3 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 db/migrations/0001_initial.py diff --git a/db/migrations/0001_initial.py b/db/migrations/0001_initial.py new file mode 100644 index 00000000..f231eeb4 --- /dev/null +++ b/db/migrations/0001_initial.py @@ -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')), + ], + ), + 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')), + ('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')), + ], + ), + ] diff --git a/db/models.py b/db/models.py index 137941ff..fad11f9c 100644 --- a/db/models.py +++ b/db/models.py @@ -1 +1,26 @@ from django.db import models + + +class Race(models.Model): + name = models.CharField(max_length=255, unique=True) + 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) diff --git a/main.py b/main.py index e71f55d8..1276bc82 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,90 @@ +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") + + # print(players) + # show_schem(players) + + 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, skill_created = Skill.objects.get_or_create( + name=skill_name, bonus=skill_bonus, race=race + ) + + 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( + nickname=player, + email=email_info, + bio=bio_info, + race=race, + guild=guild + ) + + +def file_data(path): + file_path = os.path.join(path) + with open(file_path) as f: + players = json.load(f) + + return players + + +def show_schem(players): + for player, info in players.items(): + print(player) + print("email: ", info.get("email")) + print("bio: ", info.get("bio")) + + if race_info := info.get("race"): + print("race") + print("name:", race_info.get("name")) + print("description:", race_info.get("description")) + + if skills_info := race_info.get("skills"): + print("skills") + for skill in skills_info: + print("name:", skill.get("name")) + print("bonus:", skill.get("bonus")) + + if guild_info := info.get("guild"): + print("guild") + print("name:", guild_info.get("name")) + print("description:", guild_info.get("description")) + + print("-" * 30) if __name__ == "__main__": From 848fe7e89126c1b86af84325b8a2c8e05d5428d2 Mon Sep 17 00:00:00 2001 From: roman Date: Sun, 3 Nov 2024 10:57:36 +0200 Subject: [PATCH 2/4] second --- main.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 1276bc82..0c691a28 100644 --- a/main.py +++ b/main.py @@ -8,10 +8,10 @@ def main() -> None: players = file_data("players.json") + create_db(players) - # print(players) - # show_schem(players) +def create_db(players: dict) -> None: for player, info in players.items(): email_info = info.get("email") bio_info = info.get("bio") @@ -30,9 +30,11 @@ def main() -> None: skill_name = skill.get("name") skill_bonus = skill.get("bonus") - skill, skill_created = Skill.objects.get_or_create( + 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") @@ -54,7 +56,7 @@ def main() -> None: ) -def file_data(path): +def file_data(path: str) -> dict: file_path = os.path.join(path) with open(file_path) as f: players = json.load(f) From 8e678b2ffa2966f2cf57382139d5b0cf938c34e9 Mon Sep 17 00:00:00 2001 From: roman Date: Sun, 3 Nov 2024 11:04:08 +0200 Subject: [PATCH 3/4] Solution-py-game-models v1.0 --- main.py | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/main.py b/main.py index 0c691a28..dc0ba74f 100644 --- a/main.py +++ b/main.py @@ -64,30 +64,5 @@ def file_data(path: str) -> dict: return players -def show_schem(players): - for player, info in players.items(): - print(player) - print("email: ", info.get("email")) - print("bio: ", info.get("bio")) - - if race_info := info.get("race"): - print("race") - print("name:", race_info.get("name")) - print("description:", race_info.get("description")) - - if skills_info := race_info.get("skills"): - print("skills") - for skill in skills_info: - print("name:", skill.get("name")) - print("bonus:", skill.get("bonus")) - - if guild_info := info.get("guild"): - print("guild") - print("name:", guild_info.get("name")) - print("description:", guild_info.get("description")) - - print("-" * 30) - - if __name__ == "__main__": main() From 9191281eeb0135506c58f5a52ec14c19815aa95a Mon Sep 17 00:00:00 2001 From: roman Date: Sun, 3 Nov 2024 12:26:18 +0200 Subject: [PATCH 4/4] Solution-py-game-models v1.1 --- db/models.py | 8 +++++++- main.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/db/models.py b/db/models.py index fad11f9c..c884c09b 100644 --- a/db/models.py +++ b/db/models.py @@ -2,7 +2,13 @@ class Race(models.Model): - name = models.CharField(max_length=255, unique=True) + RACE_TYPE = ( + ("Elf", "elf race"), + ("Dwarf", "dwarf race"), + ("Human", "human race"), + ("Ork", "ork race") + ) + name = models.CharField(max_length=255, unique=True, choices=RACE_TYPE) description = models.TextField(blank=True) diff --git a/main.py b/main.py index dc0ba74f..42ef8c6b 100644 --- a/main.py +++ b/main.py @@ -47,7 +47,7 @@ def create_db(players: dict) -> None: else: guild = None - Player.objects.create( + Player.objects.get_or_create( nickname=player, email=email_info, bio=bio_info,