2018-10-26 23:42:53 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.conf import settings
|
2019-02-06 20:54:50 +00:00
|
|
|
from django.urls import reverse
|
2018-11-22 02:09:30 +00:00
|
|
|
from sys import maxsize
|
2018-10-26 23:42:53 +00:00
|
|
|
|
2019-02-02 22:31:02 +00:00
|
|
|
from GeoffreyApp.util import create_token, objects_list_to_json
|
2018-12-30 01:41:18 +00:00
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
# Create your models here.
|
|
|
|
|
2018-12-02 00:25:53 +00:00
|
|
|
|
2018-12-30 01:41:18 +00:00
|
|
|
class APIToken(models.Model):
|
|
|
|
key = models.CharField(default=create_token, max_length=25, unique=True)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Key used to access the Geoffrey API
|
|
|
|
'''
|
|
|
|
|
2018-12-30 01:41:18 +00:00
|
|
|
name = models.CharField(max_length=50, blank=True)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Name of the key
|
|
|
|
'''
|
|
|
|
|
2018-12-30 01:41:18 +00:00
|
|
|
commands_perm = models.BooleanField(default=False)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Permission to use the command api
|
|
|
|
'''
|
2018-12-30 01:41:18 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if len(self.name):
|
|
|
|
return "{}: {}".format(self.name, self.key)
|
|
|
|
else:
|
|
|
|
return self.key
|
|
|
|
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
class Player(models.Model):
|
2018-11-22 02:09:30 +00:00
|
|
|
name = models.CharField(max_length=30, unique=True)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Player username
|
|
|
|
'''
|
|
|
|
|
2018-11-22 02:09:30 +00:00
|
|
|
mc_uuid = models.CharField(max_length=36, unique=True)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Minecraft UUID
|
|
|
|
'''
|
|
|
|
|
2018-11-22 02:09:30 +00:00
|
|
|
discord_uuid = models.CharField(max_length=50, unique=True)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Discord UUID
|
|
|
|
'''
|
2018-11-22 02:09:30 +00:00
|
|
|
|
2019-01-12 21:06:06 +00:00
|
|
|
@property
|
|
|
|
def loc_count(self):
|
|
|
|
return Location.objects.filter(owner=self).count()
|
|
|
|
|
2018-11-22 02:09:30 +00:00
|
|
|
@property
|
|
|
|
def json(self):
|
2019-01-06 21:24:47 +00:00
|
|
|
return {"name": self.name,
|
|
|
|
"mc_uuid": self.mc_uuid,
|
|
|
|
"discord_uuid": self.discord_uuid
|
2019-01-04 17:20:24 +00:00
|
|
|
}
|
2018-10-26 23:42:53 +00:00
|
|
|
|
2018-12-14 01:58:59 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
|
|
|
|
class Location(models.Model):
|
|
|
|
DIMENSIONS = (
|
|
|
|
('O', 'Overworld'),
|
|
|
|
('N', 'Nether'),
|
|
|
|
('E', 'The End')
|
|
|
|
)
|
|
|
|
|
|
|
|
name = models.CharField(max_length=128, unique=True)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Name of the location
|
|
|
|
'''
|
2018-10-26 23:42:53 +00:00
|
|
|
|
|
|
|
x_coord = models.IntegerField()
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
X Position
|
|
|
|
'''
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
z_coord = models.IntegerField()
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Z Position
|
|
|
|
'''
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
dimension = models.CharField(max_length=1, choices=DIMENSIONS)
|
|
|
|
|
2019-01-31 20:07:25 +00:00
|
|
|
owner = models.ManyToManyField(Player)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Owner of Location
|
|
|
|
'''
|
2018-10-26 23:42:53 +00:00
|
|
|
|
2019-01-12 21:06:06 +00:00
|
|
|
@property
|
|
|
|
def location(self):
|
2019-01-14 01:27:48 +00:00
|
|
|
return "(x={}, z={})".format(self.x_coord, self.z_coord)
|
2019-01-12 21:06:06 +00:00
|
|
|
|
2019-01-14 20:22:18 +00:00
|
|
|
@property
|
|
|
|
def tunnel(self):
|
|
|
|
try:
|
|
|
|
tunnel = Tunnel.objects.get(location=self)
|
|
|
|
except Tunnel.DoesNotExist:
|
|
|
|
tunnel = None
|
|
|
|
|
|
|
|
return tunnel
|
|
|
|
|
2019-01-31 20:07:25 +00:00
|
|
|
@property
|
|
|
|
def get_owners(self):
|
|
|
|
owner_list = []
|
|
|
|
for owner in self.owner.all():
|
|
|
|
owner_list.append(owner.json)
|
|
|
|
|
|
|
|
return owner_list
|
|
|
|
|
2018-11-22 02:09:30 +00:00
|
|
|
@property
|
|
|
|
def json(self):
|
2019-01-06 21:24:47 +00:00
|
|
|
return {"type": self.__class__.__name__,
|
|
|
|
"name": self.name,
|
2019-01-04 17:20:24 +00:00
|
|
|
"x_coord": self.x_coord,
|
|
|
|
"z_coord": self.z_coord,
|
|
|
|
"dimension": self.dimension,
|
2019-01-31 20:07:25 +00:00
|
|
|
"owner": self.get_owners,
|
2019-01-14 20:22:18 +00:00
|
|
|
"location": self.location,
|
2019-02-06 20:54:50 +00:00
|
|
|
"tunnel": None if self.tunnel is None else self.tunnel.tunnel_str,
|
|
|
|
"link": self.link
|
2019-01-04 17:20:24 +00:00
|
|
|
}
|
2018-11-22 02:09:30 +00:00
|
|
|
|
2019-01-14 01:27:48 +00:00
|
|
|
@property
|
|
|
|
def loc_type(self):
|
2019-02-06 20:54:50 +00:00
|
|
|
str = self.loc_child_obj.__class__.__name__
|
|
|
|
|
|
|
|
return str
|
|
|
|
|
|
|
|
@property
|
|
|
|
def link(self):
|
2019-03-02 17:48:28 +00:00
|
|
|
child = self.loc_child_obj
|
|
|
|
|
|
|
|
if child != self:
|
|
|
|
return self.loc_child_obj.link
|
2019-02-06 20:54:50 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def loc_child_obj(self):
|
2019-01-14 01:27:48 +00:00
|
|
|
if hasattr(self, "shop"):
|
2019-02-06 20:54:50 +00:00
|
|
|
return self.shop
|
2019-01-14 01:27:48 +00:00
|
|
|
elif hasattr(self, "base"):
|
2019-02-06 20:54:50 +00:00
|
|
|
return self.base
|
2019-02-02 22:31:02 +00:00
|
|
|
elif hasattr(self, "town"):
|
2019-02-06 20:54:50 +00:00
|
|
|
return self.town
|
2019-03-02 17:48:28 +00:00
|
|
|
elif hasattr(self, "publicfarm"):
|
|
|
|
return self.publicfarm
|
2019-01-14 01:27:48 +00:00
|
|
|
else:
|
2019-02-06 20:54:50 +00:00
|
|
|
return self
|
2019-01-14 01:27:48 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def dynmap_url(self):
|
|
|
|
base_url = getattr(settings, "GEOFFREY_DYNMAP_BASE_URL")
|
|
|
|
world_name = getattr(settings, "GEOFFREY_DYNMAP_WORLD_NAME")
|
|
|
|
if base_url is not None:
|
|
|
|
url = base_url + "/?worldname={}&mapname=surface&zoom=4&x={}&y=65&z={}".format(world_name, self.x_coord,
|
|
|
|
self.z_coord)
|
|
|
|
return url
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-12-30 01:41:18 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
|
|
|
|
class Shop(Location):
|
|
|
|
def __str__(self):
|
|
|
|
return "Shop: %s" % self.name
|
|
|
|
|
2019-02-06 20:54:50 +00:00
|
|
|
@property
|
|
|
|
def link(self):
|
|
|
|
return reverse("GeoffreyShopInfo", kwargs={"id": self.id})
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
|
|
|
|
class Base(Location):
|
|
|
|
def __str__(self):
|
|
|
|
return "Base: %s" % self.name
|
|
|
|
|
2019-02-06 20:54:50 +00:00
|
|
|
@property
|
|
|
|
def link(self):
|
|
|
|
return reverse("GeoffreyBaseInfo", kwargs={"id": self.id})
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
|
2019-02-02 22:31:02 +00:00
|
|
|
class Town(Location):
|
|
|
|
residents = models.ManyToManyField(Player)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Town: %s" % self.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def get_residents(self):
|
|
|
|
residents = self.residents.all()
|
|
|
|
|
|
|
|
return objects_list_to_json(residents)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def json(self):
|
|
|
|
json = super().json
|
|
|
|
|
2019-02-06 20:54:50 +00:00
|
|
|
json["residents"] = self.get_residents
|
2019-02-02 22:31:02 +00:00
|
|
|
|
|
|
|
return json
|
|
|
|
|
2019-02-06 20:54:50 +00:00
|
|
|
@property
|
|
|
|
def link(self):
|
|
|
|
return reverse("GeoffreyTownInfo", kwargs={"id": self.id})
|
|
|
|
|
2019-02-02 22:31:02 +00:00
|
|
|
|
2019-03-02 17:48:28 +00:00
|
|
|
class PublicFarm(Location):
|
|
|
|
@property
|
|
|
|
def link(self):
|
|
|
|
return reverse("GeoffreyPublicFarmInfo", kwargs={"id": self.id})
|
|
|
|
|
|
|
|
|
|
|
|
class Resource(models.Model):
|
|
|
|
farm = models.ForeignKey(PublicFarm, related_name="resource", on_delete=models.CASCADE)
|
|
|
|
resource_name = models.CharField(max_length=128)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def json(self):
|
|
|
|
return {"name": self.resource_name,
|
|
|
|
"farm_id": self.farm_id
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
class ItemListing(models.Model):
|
|
|
|
item_name = models.CharField(max_length=128)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Name of the item
|
|
|
|
'''
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
price = models.IntegerField()
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Number of diamonds per amount of items
|
|
|
|
'''
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
amount = models.IntegerField()
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Number of items
|
|
|
|
'''
|
|
|
|
|
2019-01-04 20:39:53 +00:00
|
|
|
date_restocked = models.DateTimeField(auto_now=True)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Datetime the item was last restocked
|
|
|
|
'''
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
shop = models.ForeignKey(Shop, related_name="shop_selling", on_delete=models.CASCADE)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Shop the item is sold at
|
|
|
|
'''
|
2018-10-26 23:42:53 +00:00
|
|
|
|
2018-11-22 02:09:30 +00:00
|
|
|
@property
|
|
|
|
def normalized_price(self):
|
|
|
|
if self.amount == 0:
|
|
|
|
return maxsize
|
|
|
|
else:
|
|
|
|
return self.price/self.amount
|
|
|
|
|
2019-01-04 17:20:24 +00:00
|
|
|
@property
|
|
|
|
def json(self):
|
|
|
|
return {"item_name": self.item_name,
|
|
|
|
"price": self.price,
|
|
|
|
"amount": self.amount,
|
2019-01-11 22:40:12 +00:00
|
|
|
"date_restocked": self.date_restocked,
|
2019-01-04 17:20:24 +00:00
|
|
|
"normalized_price": self.normalized_price,
|
|
|
|
"shop": self.shop.json,
|
|
|
|
}
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "Item: %d %s for %d" % (self.amount, self.item_name, self.amount)
|
|
|
|
|
|
|
|
|
|
|
|
class Tunnel(models.Model):
|
|
|
|
TUNNEL_NAMES = (
|
2019-01-11 22:40:12 +00:00
|
|
|
('N', getattr(settings, 'GEOFFREY_NORTH_TUNNEL', '')),
|
|
|
|
('E', getattr(settings, 'GEOFFREY_EAST_TUNNEL', '')),
|
|
|
|
('S', getattr(settings, 'GEOFFREY_SOUTH_TUNNEL', '')),
|
|
|
|
('W', getattr(settings, 'GEOFFREY_WEST_TUNNEL', ''))
|
2018-10-26 23:42:53 +00:00
|
|
|
)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Tunnel Direction
|
|
|
|
'''
|
2018-10-26 23:42:53 +00:00
|
|
|
|
|
|
|
tunnel_number = models.IntegerField()
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Tunnel coordinate
|
|
|
|
'''
|
|
|
|
|
2018-10-26 23:42:53 +00:00
|
|
|
tunnel_direction = models.CharField(max_length=1, choices=TUNNEL_NAMES)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Tunnel Direction
|
|
|
|
'''
|
2018-10-26 23:42:53 +00:00
|
|
|
|
|
|
|
location = models.ForeignKey(Location, related_name="tunnel_location", on_delete=models.CASCADE)
|
2019-01-11 22:40:12 +00:00
|
|
|
'''
|
|
|
|
Location that the tunnel is for
|
|
|
|
'''
|
2018-10-26 23:42:53 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-01-14 20:22:18 +00:00
|
|
|
return "Tunnel: %s %d" % (self.get_tunnel_direction_display(), self.tunnel_number)
|
2018-12-18 18:13:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def json(self):
|
2019-01-06 21:24:47 +00:00
|
|
|
return {"location_name": self.location.name,
|
2019-01-14 20:22:18 +00:00
|
|
|
"tunnel_direction": self.get_tunnel_direction_display(),
|
2019-01-04 17:20:24 +00:00
|
|
|
"tunnel_number": self.tunnel_number
|
|
|
|
}
|
2019-01-14 20:22:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def tunnel_str(self):
|
|
|
|
return "{} {}".format(self.get_tunnel_direction_display(), self.tunnel_number)
|