Geoffrey-Django/models.py

126 lines
3.4 KiB
Python

from django.db import models
from django.conf import settings
from sys import maxsize
from GeoffreyApp.util import create_token
# Create your models here.
class APIToken(models.Model):
key = models.CharField(default=create_token, max_length=25, unique=True)
name = models.CharField(max_length=50, blank=True)
commands_perm = models.BooleanField(default=False)
def __str__(self):
if len(self.name):
return "{}: {}".format(self.name, self.key)
else:
return self.key
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
}
def __str__(self):
return self.name
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": self.__class__.__name__,
"Name": self.name,
"x_coord": self.x_coord,
"z_coord": self.z_coord,
"dimension": self.dimension,
"Owner": self.owner.json
}
def __str__(self):
return self.name
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
@property
def json(self):
return {"item_name": self.item_name,
"price": self.price,
"amount": self.amount,
"normalized_price": self.normalized_price,
"shop": self.shop.json,
}
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)
@property
def json(self):
return {"Location_name": self.location.name,
"tunnel_direction": self.tunnel_direction,
"tunnel_number": self.tunnel_number
}