# Path: srs/models.py
# class Allyteam(models.Model):
#     numallies = models.IntegerField()
#     startrectbottom = models.FloatField(blank=True, null=True)
#     startrectleft = models.FloatField(blank=True, null=True)
#     startrectright = models.FloatField(blank=True, null=True)
#     startrecttop = models.FloatField(blank=True, null=True)
#     winner = models.BooleanField(default=False)
#     replay = models.ForeignKey(Replay, on_delete=CASCADE)
#     num = models.SmallIntegerField()
# 
#     def __unicode__(self):
#         return "Allyteam({}) win:{}".format(self.id, self.winner)
# 
# class Player(models.Model):
#     account = models.ForeignKey(PlayerAccount, blank=True, null=True, on_delete=CASCADE)
#     name = models.CharField(max_length=128, db_index=True)
#     rank = models.SmallIntegerField()
#     skill = models.CharField(max_length=16, blank=True)
#     skilluncertainty = models.SmallIntegerField(default=-1, blank=True)
#     spectator = models.BooleanField(default=False)
#     team = models.ForeignKey("Team", blank=True, null=True, on_delete=CASCADE)
#     replay = models.ForeignKey(Replay, on_delete=CASCADE)
#     startposx = models.FloatField(blank=True, null=True)
#     startposy = models.FloatField(blank=True, null=True)
#     startposz = models.FloatField(blank=True, null=True)
# 
#     def __unicode__(self):
#         return "Player({}, {})".format(self.id, self.name)
# 
#     def get_absolute_url(self):
#         return reverse("srs/player", args=[self.account.accountid])
# 
#     class Meta:
#         ordering = ["name"]


import json
import logging
import pprint
import random
import sys
import urllib.parse
import urllib.request
import requests
from os.path import exists as path_exists, join as path_join
from shutil import copyfile
from django.conf import settings
from PIL import Image, ImageColor, ImageDraw, ImageFont
from .models import Allyteam, Player
# along with this program.  If not, see <http://www.gnu.org/licenses/>.




logger = logging.getLogger(__name__)


def fetch_map_info(map_name):
    params = {
        "category": "map",
        "springname": map_name,
        "torrent": False,
        "metadata": True,
        "nosensitive": True,
        "images": True,
    }

    url = "https://springfiles.springrts.com/json.php?" + urllib.parse.urlencode(params)
    map_info = json.loads(urllib.request.urlopen(url).read())
    return map_info


class SpringMaps:
    def __init__(self, mapname):
        self.map_name = mapname
        self.full_image_filename = "{}.jpg".format(self.map_name)
        self.map_info = None
        self.thumb = None


# Path: srs/models.py
# class Allyteam(models.Model):
#     numallies = models.IntegerField()
#     startrectbottom = models.FloatField(blank=True, null=True)
#     startrectleft = models.FloatField(blank=True, null=True)
#     startrectright = models.FloatField(blank=True, null=True)
#     startrecttop = models.FloatField(blank=True, null=True)
#     winner = models.BooleanField(default=False)
#     replay = models.ForeignKey(Replay, on_delete=CASCADE)
#     num = models.SmallIntegerField()
# 
#     def __unicode__(self):
#         return "Allyteam({}) win:{}".format(self.id, self.winner)
# 
# class Player(models.Model):
#     account = models.ForeignKey(PlayerAccount, blank=True, null=True, on_delete=CASCADE)
#     name = models.CharField(max_length=128, db_index=True)
#     rank = models.SmallIntegerField()
#     skill = models.CharField(max_length=16, blank=True)
#     skilluncertainty = models.SmallIntegerField(default=-1, blank=True)
#     spectator = models.BooleanField(default=False)
#     team = models.ForeignKey("Team", blank=True, null=True, on_delete=CASCADE)
#     replay = models.ForeignKey(Replay, on_delete=CASCADE)
#     startposx = models.FloatField(blank=True, null=True)
#     startposy = models.FloatField(blank=True, null=True)
#     startposz = models.FloatField(blank=True, null=True)
# 
#     def __unicode__(self):
#         return "Player({}, {})".format(self.id, self.name)
# 
#     def get_absolute_url(self):
#         return reverse("srs/player", args=[self.account.accountid])
# 
#     class Meta:
#         ordering = ["name"]


# Path: srs/springmaps.py
import json
import logging
import pprint
import random
import sys
import urllib.parse
import urllib.request
import requests
from os.path import exists as path_exists, join as path_join
from shutil import copyfile
from django.conf import settings
from PIL import Image, ImageColor, ImageDraw, ImageFont
from .models import Allyteam, Player

# along with this program.  If not, see <http://www.gnu.org/licenses/>.




logger = logging.getLogger(__name__)


def fetch_map_info(map_name):
    params = {
        "category": "map",
        "springname": map_name,
        "torrent": False,
        "metadata": True,
        "nosensitive": True,
        "images": True,
    }

    url = "https://springfiles.springrts.com/json.php?" + urllib.parse.urlencode(params)
    map_info = json.loads(urllib.request.urlopen(url).read())
    return map_info


class SpringMaps:
    def __init__(self, mapname):
        self.map_name = mapname
        self.full_image_filename = "{}.jpg".format(self.map_name)
        self.map_info = None
        self.thumb = None


    @property