Geoffrey-Django/models.py

82 lines
2.3 KiB
Python

from django.db import models
from django.conf import settings
from sys import maxsize
# Create your models here.
class Player(models.Model):
name = models.CharField(max_length=30, unique=True)
mc_uuid = models.CharField(max_length=36, unique=True)
discord_uuid = models.CharField(max_length=50, unique=True)
@property
def json(self):
return {"Name": self.name, "MC UUID": self.mc_uuid, "Discord UUID": self.discord_uuid}
class Location(models.Model):
DIMENSIONS = (
('O', 'Overworld'),
('N', 'Nether'),
('E', 'The End')
)
name = models.CharField(max_length=128, unique=True)
x_coord = models.IntegerField()
z_coord = models.IntegerField()
dimension = models.CharField(max_length=1, choices=DIMENSIONS)
owner = models.ForeignKey(Player, related_name='owner_player', on_delete=models.CASCADE)
@property
def json(self):
return {"Type": str(type(self)), "Name": self.name, "x_coord": self.x_coord, "z_coord": self.z_coord,
"dimension": self.dimension, "Owner": self.owner.json}
class Shop(Location):
def __str__(self):
return "Shop: %s" % self.name
class Base(Location):
def __str__(self):
return "Base: %s" % self.name
class ItemListing(models.Model):
item_name = models.CharField(max_length=128)
price = models.IntegerField()
amount = models.IntegerField()
shop = models.ForeignKey(Shop, related_name="shop_selling", on_delete=models.CASCADE)
@property
def normalized_price(self):
if self.amount == 0:
return maxsize
else:
return self.price/self.amount
def __str__(self):
return "Item: %d %s for %d" % (self.amount, self.item_name, self.amount)
class Tunnel(models.Model):
TUNNEL_NAMES = (
('N', getattr(settings, 'NORTH', '')),
('E', getattr(settings, 'EAST', '')),
('S', getattr(settings, 'SOUTH', '')),
('W', getattr(settings, 'WEST', ''))
)
tunnel_number = models.IntegerField()
tunnel_direction = models.CharField(max_length=1, choices=TUNNEL_NAMES)
location = models.ForeignKey(Location, related_name="tunnel_location", on_delete=models.CASCADE)
def __str__(self):
return "Tunnel: %s %d" % (self.tunnel_direction, self.tunnel_number)