From aec0b77d29612be5844271face0bde6d8e3440e4 Mon Sep 17 00:00:00 2001 From: Alfred Melch Date: Fri, 3 Jan 2020 15:04:17 +0100 Subject: [PATCH] Add models --- srv/Makefile | 5 +++- srv/ebermergen/__init__.py | 12 ++++++-- srv/ebermergen/lib/__init__.py | 3 +- srv/ebermergen/models/__init__.py | 0 srv/ebermergen/models/auto_id.py | 14 ++++++++++ srv/ebermergen/models/entities.py | 26 +++++++++++++++++ srv/ebermergen/models/game.py | 46 +++++++++++++++++++++++++++++++ srv/ebermergen/models/map.py | 15 ++++++++++ srv/ebermergen/models/player.py | 7 +++++ srv/tests/__init__.py | 0 srv/tests/test_auto_id.py | 35 +++++++++++++++++++++++ srv/tests/test_game.py | 13 +++++++++ srv/tests/test_serialize.py | 10 +++++++ 13 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 srv/ebermergen/models/__init__.py create mode 100644 srv/ebermergen/models/auto_id.py create mode 100644 srv/ebermergen/models/entities.py create mode 100644 srv/ebermergen/models/game.py create mode 100644 srv/ebermergen/models/map.py create mode 100644 srv/ebermergen/models/player.py create mode 100644 srv/tests/__init__.py create mode 100644 srv/tests/test_auto_id.py create mode 100644 srv/tests/test_game.py create mode 100644 srv/tests/test_serialize.py diff --git a/srv/Makefile b/srv/Makefile index d1858ee..7272f31 100644 --- a/srv/Makefile +++ b/srv/Makefile @@ -6,4 +6,7 @@ install: ./venv/bin/pip install -r requirements.txt run: - ./venv/bin/flask run --host 0.0.0.0 \ No newline at end of file + ./venv/bin/flask run --host 0.0.0.0 + +test: + ./venv/bin/python -m unittest \ No newline at end of file diff --git a/srv/ebermergen/__init__.py b/srv/ebermergen/__init__.py index fda81e6..3868d91 100644 --- a/srv/ebermergen/__init__.py +++ b/srv/ebermergen/__init__.py @@ -2,6 +2,7 @@ import json from flask import Flask, jsonify from flask_cors import CORS +from ebermergen.models.game import Game from .lib import generate_map app = Flask(__name__) @@ -15,9 +16,16 @@ def index(): @app.route('/generate') def generate(): - return jsonify(generate_map(10, 8)) + return jsonify(generate_map((10, 8))) @app.route('/generate//') def generate_var(x, y): - return jsonify(generate_map(x, y)) + return jsonify(generate_map((x, y))) + + +@app.route('/state') +def state(): + a = Game() + a.seed_map() + return jsonify(a.serialize()) diff --git a/srv/ebermergen/lib/__init__.py b/srv/ebermergen/lib/__init__.py index 747a879..b9f8717 100644 --- a/srv/ebermergen/lib/__init__.py +++ b/srv/ebermergen/lib/__init__.py @@ -3,5 +3,6 @@ import random TERRAIN_TYPES = [0, 1, 2, 3] -def generate_map(x_len, y_len): +def generate_map(dimensions): + x_len, y_len = dimensions return [[random.choice(TERRAIN_TYPES) for y in range(y_len)] for x in range(x_len)] diff --git a/srv/ebermergen/models/__init__.py b/srv/ebermergen/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/srv/ebermergen/models/auto_id.py b/srv/ebermergen/models/auto_id.py new file mode 100644 index 0000000..c287c95 --- /dev/null +++ b/srv/ebermergen/models/auto_id.py @@ -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 diff --git a/srv/ebermergen/models/entities.py b/srv/ebermergen/models/entities.py new file mode 100644 index 0000000..427555c --- /dev/null +++ b/srv/ebermergen/models/entities.py @@ -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 diff --git a/srv/ebermergen/models/game.py b/srv/ebermergen/models/game.py new file mode 100644 index 0000000..a327800 --- /dev/null +++ b/srv/ebermergen/models/game.py @@ -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 diff --git a/srv/ebermergen/models/map.py b/srv/ebermergen/models/map.py new file mode 100644 index 0000000..94f726f --- /dev/null +++ b/srv/ebermergen/models/map.py @@ -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 diff --git a/srv/ebermergen/models/player.py b/srv/ebermergen/models/player.py new file mode 100644 index 0000000..b7a244e --- /dev/null +++ b/srv/ebermergen/models/player.py @@ -0,0 +1,7 @@ +from .auto_id import AutoID + + +class Player(AutoID): + def __init__(self, name): + super().__init__() + self.name = name diff --git a/srv/tests/__init__.py b/srv/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/srv/tests/test_auto_id.py b/srv/tests/test_auto_id.py new file mode 100644 index 0000000..61d12d5 --- /dev/null +++ b/srv/tests/test_auto_id.py @@ -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) diff --git a/srv/tests/test_game.py b/srv/tests/test_game.py new file mode 100644 index 0000000..25e525d --- /dev/null +++ b/srv/tests/test_game.py @@ -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() diff --git a/srv/tests/test_serialize.py b/srv/tests/test_serialize.py new file mode 100644 index 0000000..09ee2ac --- /dev/null +++ b/srv/tests/test_serialize.py @@ -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()