Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Pickups changed to suit level1 minimum product
Browse files Browse the repository at this point in the history
test_pickup_decoder written for the new class.

Changes made as requested in PR.

holdingTote no longer used. Implemented Counter

Obsolete pickup file removed.
  • Loading branch information
OlafSzmidt committed Sep 25, 2017
1 parent bef017f commit a44d7c5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 104 deletions.
3 changes: 3 additions & 0 deletions aimmo-game/simulation/avatar/avatar_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
from collections import Counter

import requests

from simulation.action import ACTIONS, MoveAction, WaitAction
Expand All @@ -20,6 +22,7 @@ def __init__(self, player_id, initial_location, worker_url, avatar_appearance):
self.health = 5
self.score = 0
self.events = []
self.pickups = Counter() # Empty counter as avatar has not picked anything up yet.
self.avatar_appearance = avatar_appearance
self.worker_url = worker_url
self.effects = set()
Expand Down
13 changes: 3 additions & 10 deletions aimmo-game/simulation/custom_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

from simulation.location import Location
from simulation.game_state import GameState
from simulation.pickups import DeliveryPickup
from simulation.world_map import WorldMap
from simulation.world_map import world_map_static_spawn_decorator
from simulation.world_map import DEFAULT_LEVEL_SETTINGS

from simulation.pickups import HealthPickup
from simulation.pickups import InvulnerabilityPickup
from simulation.pickups import DamagePickup


class BaseGenerator(object):
__metaclass__ = abc.ABCMeta
Expand Down Expand Up @@ -75,12 +72,8 @@ def decode(self, json, world_map):
class PickupDecoder(Decoder):
def decode(self, json, world_map):
x, y = int(json["x"]), int(json["y"])
if json["type"] == "invulnerability":
world_map.get_cell(Location(x, y)).pickup = InvulnerabilityPickup(Location(x, y))
if json["type"] == "health":
world_map.get_cell(Location(x, y)).pickup = HealthPickup(Location(x, y), int(json["health_restored"]))
if json["type"] == "damage":
world_map.get_cell(Location(x, y)).pickup = DamagePickup(Location(x, y))
if json["type"] == "delivery":
world_map.get_cell(Location(x, y)).pickup = DeliveryPickup(Location(x, y))

################################################################################

Expand Down
71 changes: 18 additions & 53 deletions aimmo-game/simulation/pickups.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ def __str__(self):
return self.__class__.__name__

def delete(self):
'''
Deletes the pickup from the /CELL/.
'''
self.cell.pickup = None

def apply(self, avatar):
'''
Public method to apply the pickup to an avatar. This will vary from type of pickup
therefore implementation is done privately.
:param avatar: an Avatar object.
'''
self._apply(avatar)
self.delete()

Expand All @@ -27,69 +35,26 @@ def serialise(self):
raise NotImplementedError()


class HealthPickup(_Pickup):
def __init__(self, cell, health_restored=3):
super(HealthPickup, self).__init__(cell)
self.health_restored = health_restored
class DeliveryPickup(_Pickup):
'''
Inherits generic functionality from _Pickup and needs to implement abstract methods.
'''

def __repr__(self):
return 'HealthPickup(health_restored={})'.format(self.health_restored)

def serialise(self):
return {
'type': 'health',
'health_restored': self.health_restored,
}

def _apply(self, avatar):
avatar.health += self.health_restored


class _PickupEffect(_Pickup):
__metaclass__ = ABCMeta

def __init__(self, *args):
super(_PickupEffect, self).__init__(*args)
self.params = []

@abstractproperty
def EFFECT(self):
raise NotImplementedError()
def __init__(self, cell):
super(DeliveryPickup, self).__init__(cell)

def _apply(self, avatar):
self.params.append(avatar)
avatar.effects.add(self.EFFECT(*self.params))


class InvulnerabilityPickup(_PickupEffect):
EFFECT = effects.InvulnerabilityPickupEffect

def serialise(self):
return {
'type': 'invulnerability',
}


class DamagePickup(_PickupEffect):
EFFECT = effects.DamagePickupEffect

def __init__(self, *args):
super(DamagePickup, self).__init__(*args)
self.damage_boost = 5
self.params.append(self.damage_boost)
avatar.pickups[DeliveryPickup] += 1 # Add a single count of this item.

def __repr__(self):
return 'DamagePickup(damage_boost={})'.format(self.damage_boost)
return 'DeliveryPickup'

def serialise(self):
return {
'type': 'damage',
'damage_boost': self.damage_boost,
'type': 'delivery'
}


ALL_PICKUPS = (
HealthPickup,
InvulnerabilityPickup,
DamagePickup,
DeliveryPickup,
)
22 changes: 3 additions & 19 deletions aimmo-game/tests/test_simulation/test_custom_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import unittest

from simulation.location import Location
from simulation.pickups import HealthPickup
from simulation.pickups import DamagePickup
from simulation.pickups import InvulnerabilityPickup

from simulation.pickups import DeliveryPickup
from simulation.world_map import WorldMap
from simulation.custom_map import ScoreCellDecoder
from simulation.custom_map import ObstacleDecoder
Expand Down Expand Up @@ -64,27 +61,14 @@ def test_score_cell_decoder(self):
self.assertTrue(self.map.get_cell(Location(1, 1)).generates_score)

def test_pickup_decoder(self):
PickupDecoder("0").decode({
"x" : "0",
"y" : "1",
"type" : "health",
"health_restored" : "5"
}, self.map)
PickupDecoder("0").decode({
"x" : "1",
"y" : "0",
"type" : "damage"
}, self.map)
PickupDecoder("0").decode({
"x" : "1",
"y" : "1",
"type" : "invulnerability"
"type" : "delivery"
}, self.map)

self.assertTrue(self.map.get_cell(Location(0, 0)).pickup is None)
self.assertTrue(isinstance(self.map.get_cell(Location(0, 1)).pickup, HealthPickup))
self.assertTrue(isinstance(self.map.get_cell(Location(1, 0)).pickup, DamagePickup))
self.assertTrue(isinstance(self.map.get_cell(Location(1, 1)).pickup, InvulnerabilityPickup))
self.assertTrue(isinstance(self.map.get_cell(Location(1, 0)).pickup, DeliveryPickup))

def get_mock_level(map, parsers):
class MockLevel(JsonLevelGenerator):
Expand Down
28 changes: 6 additions & 22 deletions aimmo-game/tests/test_simulation/test_pickups.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,13 @@ def test_effect_added(self):
self.assertEqual(len(self.avatar.effects), 1)
self.assertIsInstance(list(self.avatar.effects)[0], self.effect_class)

class TestDeliveryPickup(_BaseCases.BasePickupTestCase):
pickup_class = pickups.DeliveryPickup

class TestHealthPickup(_BaseCases.BasePickupTestCase):
pickup_class = pickups.HealthPickup

def test_health_increases(self):
def test_delivery_picked_up(self):
self.assertEqual(self.avatar.pickups[pickups.DeliveryPickup], 0)
self.apply_pickup()
self.assertEqual(self.avatar.health, 8)

def test_serialise(self):
self.assertEqual(self.pickup.serialise(), {'type': 'health', 'health_restored': 3})


class TestInvulnerabilityPickup(_BaseCases.BasePickupEffectTestCase):
pickup_class = pickups.InvulnerabilityPickup
effect_class = effects.InvulnerabilityPickupEffect

def test_serialise(self):
self.assertEqual(self.pickup.serialise(), {'type': 'invulnerability'})


class TestDamagePickup(_BaseCases.BasePickupEffectTestCase):
pickup_class = pickups.DamagePickup
effect_class = effects.DamagePickupEffect
self.assertEqual(self.avatar.pickups[pickups.DeliveryPickup], 1)

def test_serialise(self):
self.assertEqual(self.pickup.serialise(), {'type': 'damage', 'damage_boost': 5})
self.assertEqual(self.pickup.serialise(), {'type': 'delivery'})

0 comments on commit a44d7c5

Please sign in to comment.