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

Implemented Game-models task #450

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 2023-08-18 16:09

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=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')),
],
),
]
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)
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=True)
41 changes: 40 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
import json

import init_django_orm # noqa: F401

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


def main() -> None:
pass
with open("players.json", "r") as file:
data = json.load(file)

for user_name, user_description in data.items():

race = user_description.get("race")
if not Race.objects.filter(name=race.get("name")).exists():
race_instance = Race.objects.create(
name=race.get("name"),
description=race.get("description")
)

skills = race.get("skills")
for skill in skills:
if not Skill.objects.filter(name=skill.get("name")).exists():
Skill.objects.create(
name=skill.get("name"),
bonus=skill.get("bonus"),
race=race_instance
)

if guild := user_description.get("guild"):
if not Guild.objects.filter(name=guild.get("name")).exists():
guild_instance = Guild.objects.create(
name=guild.get("name"),
description=guild.get("description")
)
else:
guild_instance = None

if not Player.objects.filter(nickname=user_name).exists():
Player.objects.create(
nickname=user_name,
email=user_description.get("email"),
bio=user_description.get("bio"),
race=race_instance,
guild=guild_instance
)


if __name__ == "__main__":
Expand Down
Loading