Skip to content

Commit

Permalink
Tested controller (#53)
Browse files Browse the repository at this point in the history
Tested the new buy controller. Players will now be able to buy the techs they want via ActionType enums
  • Loading branch information
KingPhilip14 authored Dec 26, 2023
1 parent b16387d commit 32baf64
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 8 deletions.
1 change: 0 additions & 1 deletion game/common/avatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ def __create_abilities_dict(self) -> dict:
def buy_new_tech(self, tech_name: str) -> bool:
"""By giving the name of a tech, this method attempts to buy the tech. It returns a boolean representing if
the purchase was successful or not."""
# to prevent players from using this whenever, there can be another check here to see if they are at their base

tech_info: TechInfo = self.__tech_tree.tech_info(tech_name)

Expand Down
10 changes: 10 additions & 0 deletions game/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ class ActionType(Enum):
DEFUSE_DOWN = auto()
DEFUSE_LEFT = auto()
DEFUSE_RIGHT = auto()
BUY_IMPROVED_DRIVETRAIN = auto()
BUY_SUPERIOR_DRIVETRAIN = auto()
BUY_OVERDRIVE_DRIVETRAIN = auto()
BUY_IMPROVED_MINING = auto()
BUY_SUPERIOR_MINING = auto()
BUY_OVERDRIVE_MINING = auto()
BUY_DYNAMITE = auto()
BUY_LANDMINES = auto()
BUY_EMPS = auto()
BUY_TRAP_DEFUSAL = auto()


# Added for Quarry Rush
Expand Down
59 changes: 59 additions & 0 deletions game/controllers/buy_tech_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from game.common.game_object import GameObject
from game.common.player import Player
from game.common.map.game_board import GameBoard
from game.common.map.tile import Tile
from game.common.enums import *
from game.quarry_rush.station.company_station import CompanyStation
from game.utils.vector import Vector
from game.controllers.controller import Controller


class BuyTechController(Controller):
"""
This controller simplifies buying techs by letting players pass in an ActionType enum representing the
tech they want to buy. It will check if the client's avatar is first on their respective base. If so,
it will call the methods needed to purchase the desired tech.
"""
def __init__(self):
super().__init__()

def handle_actions(self, action: ActionType, client: Player, world: GameBoard):
if not self.__is_on_home_base(client, world): # escapes method if not on home base
return

tech_name: str = ''

match action:
case ActionType.BUY_IMPROVED_DRIVETRAIN:
tech_name = 'Improved Drivetrain'
case ActionType.BUY_SUPERIOR_DRIVETRAIN:
tech_name = 'Superior Drivetrain'
case ActionType.BUY_OVERDRIVE_DRIVETRAIN:
tech_name = 'Overdrive Drivetrain'
case ActionType.BUY_IMPROVED_MINING:
tech_name = 'Improved Mining'
case ActionType.BUY_SUPERIOR_MINING:
tech_name = 'Superior Mining'
case ActionType.BUY_OVERDRIVE_MINING:
tech_name = 'Overdrive Mining'
case ActionType.BUY_DYNAMITE:
tech_name = 'Dynamite'
case ActionType.BUY_LANDMINES:
tech_name = 'Landmines'
case ActionType.BUY_EMPS:
tech_name = 'EMPs'
case ActionType.BUY_TRAP_DEFUSAL:
tech_name = 'Trap Defusal'

client.avatar.buy_new_tech(tech_name) # buy the tech specified

def __is_on_home_base(self, client: Player, world: GameBoard):
avatar_pos: Vector = client.avatar.position # get the position of the avatar
tile: Tile = world.game_map[avatar_pos.y][avatar_pos.x] # get the tile the avatar is on

if not isinstance(tile.occupied_by, CompanyStation): # if not a CompanyStation, immediately return False
return False

station: CompanyStation = tile.occupied_by # confirmed station is a CompanyStation from if statement

return station.company == client.avatar.company # return if the companies match
88 changes: 88 additions & 0 deletions game/test_suite/tests/test_buy_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import unittest

from game.common.enums import Company
from game.common.map.game_board import GameBoard
from game.controllers.movement_controller import MovementController
from game.controllers.buy_tech_controller import BuyTechController
from game.common.stations.station import Station
from game.common.stations.occupiable_station import OccupiableStation
from game.common.map.wall import Wall
from game.quarry_rush.station.company_station import ChurchStation, TuringStation
from game.utils.vector import Vector
from game.common.player import Player
from game.common.action import ActionType
from game.common.avatar import Avatar
from game.common.game_object import GameObject


class TestBuyController(unittest.TestCase):
"""
A class to test buying all techs.
"""

def setUp(self):
self.movement_controller = MovementController()
self.buy_tech_controller = BuyTechController()
self.avatar: Avatar = Avatar(position=Vector(0, 0), company=Company.TURING)
self.avatar.science_points = 10000 # set science points to unlock techs

self.locations: dict[tuple[Vector]: list[GameObject]] = {
(Vector(0, 0),): [ChurchStation()], # top left
(Vector(0, 0),): [self.avatar],
(Vector(1, 0),): [TuringStation()], # top right
}

# make a 2x2 game map
self.world = GameBoard(0, Vector(4, 4), self.locations, False)
self.client = Player(None, None, [], self.avatar)
self.world.generate_map()

# test that buying a tech when not on your home base doesn't work
def test_not_on_home_base(self):
self.buy_tech_controller.handle_actions(ActionType.BUY_IMPROVED_MINING, self.client, self.world)

# will be a size of 1 due to default tech provided in tech tree
self.assertTrue(len(self.client.avatar.get_researched_techs()) == 1)

def test_on_random_tile(self):
# move to a tile with nothing on it and try to buy a tech; result shouldn't change from above
self.movement_controller.handle_actions(ActionType.MOVE_DOWN, self.client, self.world)
self.test_not_on_home_base()

# test buying a tech with 0 science points
def test_buying_no_science_points(self):
self.movement_controller.handle_actions(ActionType.MOVE_RIGHT, self.client, self.world)
self.avatar.science_points = 0

# will be a size of 1 due to default tech provided in tech tree
self.assertTrue(len(self.client.avatar.get_researched_techs()) == 1)

# test that buying all techs works; taking emp route
def test_buying_all_techs(self):
self.movement_controller.handle_actions(ActionType.MOVE_RIGHT, self.client, self.world)

self.buy_tech_controller.handle_actions(ActionType.BUY_IMPROVED_DRIVETRAIN, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_SUPERIOR_DRIVETRAIN, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_OVERDRIVE_DRIVETRAIN, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_IMPROVED_MINING, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_SUPERIOR_MINING, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_OVERDRIVE_MINING, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_DYNAMITE, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_LANDMINES, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_EMPS, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_TRAP_DEFUSAL, self.client, self.world)

# check final size of researched techs
self.assertTrue(len(self.client.avatar.get_researched_techs()) == 10)

# test that you can't buy a tech you didn't build up to in the tech tree
def test_buying_tech_out_of_order(self):
self.movement_controller.handle_actions(ActionType.MOVE_RIGHT, self.client, self.world)

self.buy_tech_controller.handle_actions(ActionType.BUY_SUPERIOR_DRIVETRAIN, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_DYNAMITE, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_EMPS, self.client, self.world)
self.buy_tech_controller.handle_actions(ActionType.BUY_TRAP_DEFUSAL, self.client, self.world)

# check final size of researched techs
self.assertTrue(len(self.client.avatar.get_researched_techs()) == 1)
11 changes: 4 additions & 7 deletions game/test_suite/tests/test_movement_controller.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import unittest

from game.common.map.game_board import GameBoard
from game.controllers.movement_controller import MovementController
from game.common.stations.station import Station
from game.common.stations.occupiable_station import OccupiableStation
from game.common.map.wall import Wall
from game.utils.vector import Vector
from game.common.player import Player
from game.common.action import ActionType
from game.common.avatar import Avatar
from game.common.game_object import GameObject
from game.common.map.game_board import GameBoard
from game.common.player import Player
from game.controllers.movement_controller import MovementController
from game.utils.vector import Vector


class TestMovementControllerIfWall(unittest.TestCase):
Expand Down

0 comments on commit 32baf64

Please sign in to comment.