Add models
parent
9f14b74575
commit
aec0b77d29
@ -0,0 +1,14 @@
|
||||
class AutoID:
|
||||
last_id = 0
|
||||
|
||||
def __init__(self):
|
||||
self.increment_id()
|
||||
self.id = self.last_id
|
||||
|
||||
@classmethod
|
||||
def increment_id(cls):
|
||||
cls.last_id += 1
|
||||
|
||||
@classmethod
|
||||
def reset_id(cls):
|
||||
cls.last_id = 0
|
@ -0,0 +1,26 @@
|
||||
|
||||
from .auto_id import AutoID
|
||||
|
||||
|
||||
class Town(AutoID):
|
||||
def __init__(self, player, coords):
|
||||
super().__init__()
|
||||
self.player = player
|
||||
self.coords = coords
|
||||
self.level = 1
|
||||
|
||||
|
||||
class Tower(AutoID):
|
||||
def __init__(self, player, coords):
|
||||
super().__init__()
|
||||
self.player = player
|
||||
self.coords = coords
|
||||
self.level = 1
|
||||
|
||||
|
||||
class Mob(AutoID):
|
||||
def __init__(self, player, coords):
|
||||
super().__init__()
|
||||
self.player = player
|
||||
self.coords = coords
|
||||
self.level = 1
|
@ -0,0 +1,46 @@
|
||||
import random
|
||||
import json
|
||||
|
||||
from .player import Player
|
||||
from .map import Map
|
||||
from .entities import Town
|
||||
|
||||
|
||||
class Game:
|
||||
def __init__(self):
|
||||
self.players = [Player('P1'), Player('P2')]
|
||||
self.map = Map((20, 16))
|
||||
self.towns = []
|
||||
self.towers = []
|
||||
self.mobs = []
|
||||
|
||||
def get_free_positions(self):
|
||||
positions = self.map.get_positions()
|
||||
positions = positions.difference([el.coords for el in self.towns])
|
||||
positions = positions.difference([el.coords for el in self.towers])
|
||||
return positions
|
||||
|
||||
def get_random_position(self):
|
||||
return random.choice(tuple(self.get_free_positions()))
|
||||
|
||||
def seed_map(self):
|
||||
self.towns.append(Town(self.players[0], self.get_random_position()))
|
||||
self.towns.append(Town(self.players[1], self.get_random_position()))
|
||||
self.towns.append(Town(self.players[0], self.get_random_position()))
|
||||
self.towns.append(Town(self.players[1], self.get_random_position()))
|
||||
|
||||
def serialize(self):
|
||||
naive = json.loads(json.dumps(self, default=lambda o: o.__dict__))
|
||||
|
||||
def reduce_player(entities):
|
||||
for obj in entities:
|
||||
obj['player'] = obj['player']['id']
|
||||
|
||||
def make_id_based(entities):
|
||||
return {obj['id']: obj for obj in entities}
|
||||
|
||||
for entity_type in ['towns', 'mobs', 'towers']:
|
||||
reduce_player(naive[entity_type])
|
||||
naive[entity_type] = make_id_based(naive[entity_type])
|
||||
naive['players'] = make_id_based(naive['players'])
|
||||
return naive
|
@ -0,0 +1,15 @@
|
||||
|
||||
from ebermergen.lib import generate_map
|
||||
|
||||
|
||||
class Map:
|
||||
def __init__(self, dimensions):
|
||||
self.dimensions = dimensions
|
||||
self.terrain = generate_map(dimensions)
|
||||
|
||||
def get_positions(self):
|
||||
positions = set()
|
||||
for x in range(self.dimensions[0]):
|
||||
for y in range(self.dimensions[1]):
|
||||
positions.add((x, y))
|
||||
return positions
|
@ -0,0 +1,7 @@
|
||||
from .auto_id import AutoID
|
||||
|
||||
|
||||
class Player(AutoID):
|
||||
def __init__(self, name):
|
||||
super().__init__()
|
||||
self.name = name
|
@ -0,0 +1,35 @@
|
||||
import unittest
|
||||
|
||||
from ebermergen.models.auto_id import AutoID
|
||||
|
||||
|
||||
class TestAutoID(unittest.TestCase):
|
||||
def setUp(self):
|
||||
AutoID.reset_id()
|
||||
|
||||
def test_first_instance(self):
|
||||
a = AutoID()
|
||||
self.assertEqual(a.id, 1)
|
||||
|
||||
def test_increment(self):
|
||||
a, b = AutoID(), AutoID()
|
||||
self.assertEqual(a.id, 1)
|
||||
self.assertEqual(b.id, 2)
|
||||
|
||||
def test_inheritance(self):
|
||||
class ClassA(AutoID):
|
||||
pass
|
||||
|
||||
class ClassB(AutoID):
|
||||
pass
|
||||
|
||||
a = ClassA()
|
||||
b = ClassB()
|
||||
self.assertEqual(a.id, 1)
|
||||
self.assertEqual(b.id, 1)
|
||||
a2 = ClassA()
|
||||
b2 = ClassB()
|
||||
self.assertEqual(a.id, 1)
|
||||
self.assertEqual(b.id, 1)
|
||||
self.assertEqual(a2.id, 2)
|
||||
self.assertEqual(b2.id, 2)
|
@ -0,0 +1,13 @@
|
||||
import unittest
|
||||
|
||||
from ebermergen.models.game import Game
|
||||
|
||||
|
||||
class TestSerializer(unittest.TestCase):
|
||||
def test_instantiation(self):
|
||||
a = Game()
|
||||
self.assertEqual(2, len(a.players))
|
||||
|
||||
def test_seeding(self):
|
||||
a = Game()
|
||||
a.seed_map()
|
@ -0,0 +1,10 @@
|
||||
import unittest
|
||||
|
||||
from ebermergen.models.game import Game
|
||||
|
||||
|
||||
class TestSerializer(unittest.TestCase):
|
||||
def test_serialize(self):
|
||||
a = Game()
|
||||
a.seed_map()
|
||||
a.serialize()
|
Loading…
Reference in New Issue