-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tested the new buy controller. Players will now be able to buy the techs they want via ActionType enums
- Loading branch information
1 parent
b16387d
commit 32baf64
Showing
5 changed files
with
161 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters