def test_locked_robot(self): '''Tests with a locked robot.''' population_control = PopulationControl() database = MemcachedDatabase() database.get_robot(TestGiveBirth.ROBOT_ID, for_update=True) with self.assertRaises(LockAlreadyAquiredError): population_control.execute_command("123", "give_birth", [TestGiveBirth.ROBOT_ID]) database.rollback()
def test_rollback(self): '''Tests if calling rollback works correctly.''' database = MemcachedDatabase() new_robot = Robot("test_rollback_87162", "123") database.add_robot(new_robot, (1, 1)) database.rollback() database.commit() with self.assertRaises(RobotNotFoundError): database.get_robot("test_rollback_87162")
def test_locked(self): '''Tests with a locked square.''' database = MemcachedDatabase() world = World() robot = Robot("test_locked_robot_190083", "123") # Setting the energy to zero, so the updater tries to update the square too. robot.set_energy(0) world.add_robot(robot, (5, 9)) database.commit() world.get_square((5, 9), for_update=True) with self.assertRaises(LockAlreadyAquiredError): database.get_robot(robot.get_id(), for_update=True)
def test_rollback(self): '''Tests if changes rolls back correctly.''' database = MemcachedDatabase() robot_id = "test_rollback_18098" robot = Robot(robot_id, "123") database.add_robot(robot, (11, 0)) database.commit() new_robot = database.get_robot(robot_id, for_update=True) new_robot.set_alive(False) database.rollback() database.commit() new_robot_2 = database.get_robot(robot_id) self.assertNotEqual(new_robot.get_alive(), new_robot_2.get_alive())
class AdminHandler: '''Handles the requests related to GUI.''' def __init__(self): self._authenticator = Authenticator() self._database = MemcachedDatabase() def execute_command(self, password, command, args): '''Executes the specified command.''' if not isinstance(password, str): raise InvalidArgumentsError( "Expected {0} as password, found {1}.".format( type(str), type(password))) self._authenticator.authenticate_admin(password) if command == "map_data": return self._get_map_data(args) def _get_map_data(self, args): '''Returns the information about the world's map. @args: Should be a list of locations. Each location is a string in the form of "x,y". ''' squares = self._database.get_squares(args) result = {} for location, square in squares.items(): robot_id = square.get_robot_id() if robot_id is not None: robot = self._database.get_robot(robot_id) robot_info = { 'name': robot.get_name(), 'has_water': robot.get_has_water(), 'energy': robot.get_energy(), 'life': robot.get_life(), 'honor': robot.get_honor() } else: robot_info = None plant = square.get_plant() if plant is not None: plant_info = { 'water_level': plant.get_water_level(), 'matured': plant.is_matured(), 'age': plant.get_age() } else: plant_info = None result[location] = { 'surface_type': square.get_type(), 'plant': plant_info, 'robot': robot_info } return result
def test_for_update(self): '''Tests of for_update flag works correctly.''' database = MemcachedDatabase() robot_id = "test_for_update_18762" robot = Robot(robot_id, "123") database.add_robot(robot, (10, 0)) database.commit() new_robot = database.get_robot(robot_id, for_update=True) # Testing lock with self.assertRaises(LockAlreadyAquiredError): database.get_robot(robot_id, for_update=True) # Testing commit. new_robot.set_alive(False) # It shouldn't be changed yet. new_robot_2 = database.get_robot(robot_id) self.assertNotEqual(new_robot.get_alive(), new_robot_2.get_alive()) # Actually committing changes. database.commit() new_robot_2 = database.get_robot(robot_id) self.assertEqual(new_robot.get_alive(), new_robot_2.get_alive()) # Lock should be freed. database.get_robot(robot_id, for_update=True) database.rollback()
def test_ok(self): '''Tests a valid situation.''' population_control = PopulationControl() database = MemcachedDatabase() configs = Configs() robot = database.get_robot(TestGiveBirth.ROBOT_ID, for_update=True) robot.set_honor(configs.get_robots_birth_required_honor() + 1) database.commit() new_password = population_control.execute_command("123", "give_birth", [TestGiveBirth.ROBOT_ID]) database.commit() updated_robot = database.get_robot(TestGiveBirth.ROBOT_ID, for_update=False) self.assertEqual(updated_robot.get_honor(), 1) self.assertTrue(isinstance(new_password, str)) self.assertEqual(len(new_password), 16)
def test_eating_not_matured(self): '''Tests when robot eat a non matured plant.''' world = World() database = MemcachedDatabase() action_manager = ActionManager() plant = Plant() plant.set_age(1) world.plant(plant, TestEatAction.LOCATION) database.commit() robot = database.get_robot(TestEatAction.ROBOT_ID, for_update=True) robot.set_energy(10) database.commit() action_manager.do_action("123", "eat", [TestEatAction.ROBOT_ID]) database.commit() updated_robot = database.get_robot(TestEatAction.ROBOT_ID) self.assertEqual(updated_robot.get_energy(), robot.get_energy() - 1)
def test_ok(self): '''Tests a valid situation.''' population_control = PopulationControl() database = MemcachedDatabase() configs = Configs() robot = database.get_robot(TestGiveBirth.ROBOT_ID, for_update=True) robot.set_honor(configs.get_robots_birth_required_honor() + 1) database.commit() new_password = population_control.execute_command( "123", "give_birth", [TestGiveBirth.ROBOT_ID]) database.commit() updated_robot = database.get_robot(TestGiveBirth.ROBOT_ID, for_update=False) self.assertEqual(updated_robot.get_honor(), 1) self.assertTrue(isinstance(new_password, str)) self.assertEqual(len(new_password), 16)
def test_directions(self): '''Tests if moving in different directions works correctly.''' robot_id = "test_directions_154332" robot = Robot(robot_id, "123") world = World() database = MemcachedDatabase() world.add_robot(robot, (10, 2)) database.commit() action_manager = ActionManager() action_manager.do_action("123", "move", [robot_id, "N"]) database.commit() robot = database.get_robot(robot_id) self.assertEqual(robot.get_location(), (10, 1)) action_manager.do_action("123", "move", [robot_id, "NE"]) database.commit() robot = database.get_robot(robot_id) self.assertEqual(robot.get_location(), (11, 0)) action_manager.do_action("123", "move", [robot_id, "E"]) database.commit() robot = database.get_robot(robot_id) self.assertEqual(robot.get_location(), (12, 0)) action_manager.do_action("123", "move", [robot_id, "SE"]) database.commit() robot = database.get_robot(robot_id) self.assertEqual(robot.get_location(), (13, 1)) action_manager.do_action("123", "move", [robot_id, "S"]) database.commit() robot = database.get_robot(robot_id) self.assertEqual(robot.get_location(), (13, 2)) action_manager.do_action("123", "move", [robot_id, "SW"]) database.commit() robot = database.get_robot(robot_id) self.assertEqual(robot.get_location(), (12, 3)) action_manager.do_action("123", "move", [robot_id, "W"]) database.commit() robot = database.get_robot(robot_id) self.assertEqual(robot.get_location(), (11, 3)) action_manager.do_action("123", "move", [robot_id, "NW"]) database.commit() robot = database.get_robot(robot_id) self.assertEqual(robot.get_location(), (10, 2))
def test_blocked_location(self): '''Tests adding the robot to a blocked square.''' database = MemcachedDatabase() robot = Robot("test_blocked_location_91882", "123") # There's a rock here. self._world.add_robot(robot, (6, 1)) database.commit() received_robot = database.get_robot(robot.get_id()) self.assertNotEqual(received_robot.get_location(), (6, 1))
def test_ok(self): '''Tests a good scenario.''' population_control = PopulationControl() database = MemcachedDatabase() database.add_password("iujeh87UYh6512ewQ") robot_info = population_control.execute_command("iujeh87UYh6512ewQ", "born", [None, "RDaniel"]) database.commit() gotted_robot = database.get_robot(robot_info['robot_id']) self.assertEqual(gotted_robot.get_name(), "RDaniel")
class AdminHandler: '''Handles the requests related to GUI.''' def __init__(self): self._authenticator = Authenticator() self._database = MemcachedDatabase() def execute_command(self, password, command, args): '''Executes the specified command.''' if not isinstance(password, str): raise InvalidArgumentsError("Expected {0} as password, found {1}.".format(type(str), type(password))) self._authenticator.authenticate_admin(password) if command == "map_data": return self._get_map_data(args) def _get_map_data(self, args): '''Returns the information about the world's map. @args: Should be a list of locations. Each location is a string in the form of "x,y". ''' squares = self._database.get_squares(args) result = {} for location, square in squares.items(): robot_id = square.get_robot_id() if robot_id is not None: robot = self._database.get_robot(robot_id) robot_info = {'name': robot.get_name(), 'has_water': robot.get_has_water(), 'energy': robot.get_energy(), 'life': robot.get_life(), 'honor': robot.get_honor()} else: robot_info = None plant = square.get_plant() if plant is not None: plant_info = {'water_level': plant.get_water_level(), 'matured': plant.is_matured(), 'age': plant.get_age()} else: plant_info = None result[location] = {'surface_type': square.get_type(), 'plant': plant_info, 'robot': robot_info} return result
def test_ok(self): '''Adds a good robot object to the world.''' database = MemcachedDatabase() robot = Robot("world_ok_robot_38364", "123") self._world.add_robot(robot, (5, 0)) database.commit() gotted_robot = database.get_robot(robot.get_id()) self.assertEqual(gotted_robot.get_alive(), robot.get_alive()) all_robots = database.get_all_robot_ids() self.assertIn(robot.get_id(), all_robots)
def test_not_enough_honor(self): '''Tests a robot with few honors, trying to give birth.''' population_control = PopulationControl() database = MemcachedDatabase() configs = Configs() robot = database.get_robot(TestGiveBirth.ROBOT_ID, for_update=True) robot.set_honor(configs.get_robots_birth_required_honor() - 1) database.commit() with self.assertRaises(NotEnoughHonorError): population_control.execute_command("123", "give_birth", [TestGiveBirth.ROBOT_ID]) database.rollback()
def test_ok(self): '''Tests a good scenario.''' population_control = PopulationControl() database = MemcachedDatabase() database.add_password("iujeh87UYh6512ewQ") robot_info = population_control.execute_command( "iujeh87UYh6512ewQ", "born", [None, "RDaniel"]) database.commit() gotted_robot = database.get_robot(robot_info['robot_id']) self.assertEqual(gotted_robot.get_name(), "RDaniel")
def test_locked_location(self): '''Tests adding a robot to a locked location.''' database = MemcachedDatabase() robot = Robot("test_locked_location_0023", "123") # Locking 8,1 database.get_square((8, 1), for_update=True) self._world.add_robot(robot, (8, 1)) database.commit() received_robot = database.get_robot(robot.get_id()) self.assertNotEqual(received_robot.get_location(), (8, 1))
def test_simple_add(self): '''Test adding a single robot to database.''' database = MemcachedDatabase() new_robot = Robot("test_simple_add_", "123") # No exception should be raise. database.add_robot(new_robot, (0, 0)) database.commit() gotted_robot = database.get_robot("test_simple_add_") self.assertEqual(gotted_robot.get_id(), new_robot.get_id()) self.assertEqual(gotted_robot.get_alive(), new_robot.get_alive()) self.assertEqual(gotted_robot.get_password(), new_robot.get_password())
def test_out_of_energy_robot(self): '''Tests when a robot ran out of energy.''' database = MemcachedDatabase() world = World() robot = Robot("test_out_of_energy_robot_18773", "123") robot.set_energy(0) world.add_robot(robot, (1, 9)) database.commit() got_robot = database.get_robot("test_out_of_energy_robot_18773", for_update=False) self.assertFalse(got_robot.get_alive()) square = world.get_square((1, 9)) self.assertIsNone(square.get_robot_id())
def test_out_of_life_robot(self): '''Tests when a robot ran out of life.''' database = MemcachedDatabase() world = World() robot = Robot("test_out_of_life_robot_9022", "123") robot.set_life(0) world.add_robot(robot, (0, 9)) database.commit() received_robot = database.get_robot("test_out_of_life_robot_9022", for_update=False) self.assertFalse(received_robot.get_alive()) square = world.get_square((0, 9)) self.assertIsNone(square.get_robot_id())
class ActionManager: def __init__(self): self._authenticator = Authenticator() self._database = MemcachedDatabase() configs = Configs() self._robots_action_delay = configs.get_robots_actions_delay() / 1000 self._actions = {'status': StatusAction(), 'sense': SenseAction(), 'move': MoveAction(), 'plant': PlantAction(), 'pick_water': PickWaterAction(), 'info': InfoAction(), 'eat': EatAction(), 'water': WaterAction()} def do_action(self, password, action_type, args): '''Will do the requested action.''' if len(args) == 0: raise exceptions.InvalidArgumentsError("`robot_id' should be passes as the first argument.") robot_id = args[0] if not isinstance(robot_id, str): raise exceptions.InvalidArgumentsError( "First argument (robot_id) should be a string, not {0}".format(type(robot_id))) robot = None try: robot = self._database.get_robot(robot_id, for_update=True) self._authenticator.authenticate_robot(robot, password) actions_delay = time.time() - robot.get_last_executed_action_time() if actions_delay < self._robots_action_delay: # Robot is sending actions too fast. Putting a delay between its actions. time.sleep(self._robots_action_delay - actions_delay) handler = self._get_action_handler(action_type) result = handler.do_action(robot, args) # Reducing age and energy. robot.set_energy(robot.get_energy() - 1) robot.set_life(robot.get_life() - 1) # Updating last executed action time. robot.set_last_executed_action_time(time.time()) return result except LockAlreadyAquiredError as error: # Logging all concurrency errors, so we can investigate them later. Logger().info("LockAlreadyAquiredError: {0}".format(error)) raise except AuthenticationFailedError: raise except BinarySkyException: # Concurrency exception is a fault of the server. Also, we ignored # authentication errors. Otherwise, robot should lose energy and age. # Note that we didn't handled unexpected errors (e.g. Exception class). # XXX: Dirty code. It shouldn't rollback and/or commit database changes. # But right now, there's no better way. self._database.rollback() if robot is None: # Seems we couldn't even get the robot. So, robot didn't do any actions # and shouldn't lose energy and age. raise robot = self._database.get_robot(robot_id, for_update=True) robot.set_energy(robot.get_energy() - 1) robot.set_life(robot.get_life() - 1) robot.set_last_executed_action_time(time.time()) self._database.commit() raise def _get_action_handler(self, action_type): '''Returns instance that should handle this action.''' if not isinstance(action_type, str): raise exceptions.InvalidActionError("Action type must be str, not {0}".format(type(action_type))) handler = self._actions.get(action_type) if handler is None: raise exceptions.InvalidActionError("Action {0} does not exists.".format(action_type)) return handler
class TestActionManager(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestActionManager, self).__init__(*args, **kwargs) self._action_manager = ActionManager() self._database = MemcachedDatabase() def tearDown(self): # Rolling back any remaining thing. self._database.rollback() def test_not_exist_robot(self): '''Test if ActionManager handles not-existed robot.''' with self.assertRaises(RobotNotFoundError): self._action_manager.do_action("123", "move", ["not_existed_robot_id"]) def test_bad_robot_id(self): '''Test invalid robot IDs.''' # Note that listeners checks if `args' is a list, so action manager won't receive another type. with self.assertRaises(actions.exceptions.InvalidArgumentsError): self._action_manager.do_action("123", "move", []) with self.assertRaises(actions.exceptions.InvalidArgumentsError): self._action_manager.do_action("123", "move", [None]) def test_invalid_password(self): '''Test if ActionManager authenticate passwords correctly.''' robot = Robot("test_invalid_password_95312", "andhue-ifue876-fkdnpw-1") self._database.add_robot(robot, (3, 1)) self._database.commit() with self.assertRaises(AuthenticationFailedError): self._action_manager.do_action("ieukjdf-ioquiwe-751io", "status", ["test_invalid_password_95312"]) def test_dead_robot(self): '''Test if ActionManager checks a dead robot.''' robot = Robot("test_dead_robot_98176", "1234") robot.set_alive(False) self._database.add_robot(robot, (3, 2)) self._database.commit() with self.assertRaises(AuthenticationFailedError): self._action_manager.do_action("1234", "status", ["test_dead_robot_98176"]) def test_bad_actions(self): '''Test wrong action IDs.''' robot = Robot("test_bad_actions_2376", "123") self._database.add_robot(robot, (4, 1)) self._database.commit() with self.assertRaises(actions.exceptions.InvalidActionError): self._action_manager.do_action("123", "not-exist-action", ["test_bad_actions_2376"]) self._database.rollback() with self.assertRaises(actions.exceptions.InvalidActionError): self._action_manager.do_action("123", 5432, ["test_bad_actions_2376"]) self._database.rollback() with self.assertRaises(actions.exceptions.InvalidActionError): self._action_manager.do_action("123", None, ["test_bad_actions_2376"]) self._database.rollback() with self.assertRaises(actions.exceptions.InvalidActionError): self._action_manager.do_action("123", "", ["test_bad_actions_2376"]) def test_ok(self): '''Execute a fine action.''' robot = Robot("test_ok_action_3278", "4467yrt-ddfjh-1u872-oiie") self._database.add_robot(robot, (3, 3)) self._database.commit() initial_energy = robot.get_energy() initial_age = robot.get_life() result = self._action_manager.do_action("4467yrt-ddfjh-1u872-oiie", "status", ["test_ok_action_3278"]) self.assertEqual(result['alive'], True) # Robot should lost energy and age. self._database.commit() robot = self._database.get_robot("test_ok_action_3278") self.assertEqual(robot.get_energy(), initial_energy - 1) self.assertEqual(robot.get_life(), initial_age - 1) def test_losing_energy_on_error(self): '''Tests if ActionManager reduces energy and age after an exception.''' robot = Robot("test_losing_energy_on_error_981", "091oikjdmncj") self._database.add_robot(robot, (5, 3)) self._database.commit() initial_energy = robot.get_energy() initial_age = robot.get_life() with self.assertRaises(actions.exceptions.InvalidActionError): self._action_manager.do_action("091oikjdmncj", "invalid_action", ["test_losing_energy_on_error_981"]) self._database.commit() robot = self._database.get_robot("test_losing_energy_on_error_981") self.assertEqual(robot.get_energy(), initial_energy - 1) self.assertEqual(robot.get_life(), initial_age - 1) # Robot shouldn't lose energy on authentication error. with self.assertRaises(AuthenticationFailedError): self._action_manager.do_action("wrong pass", "invalid_action", ["test_losing_energy_on_error_981"]) self._database.rollback() robot = self._database.get_robot("test_losing_energy_on_error_981") self.assertEqual(robot.get_energy(), initial_energy - 1) self.assertEqual(robot.get_life(), initial_age - 1) def test_delay(self): '''Tests delay between robot actions.''' robot = Robot("test_delay_1223", "09112345") self._database.add_robot(robot, (6, 3)) self._database.commit() self._action_manager.do_action("09112345", "sense", [robot.get_id()]) self._database.commit() start_time = time.time() self._action_manager.do_action("09112345", "sense", [robot.get_id()]) elapsed_time = time.time() - start_time self._database.commit() # one millisecond reduced from delay to cover error. self.assertGreater(elapsed_time, 0.029)
class ObjectUpdater(DatabaseHook): '''This class checks objects in the world, and updates them if required. Responsibilities of this class: * Killing and removing a robot from the world, if its energy reached zero or it ran out of life. * Removing a plant from the world if its water level reached zero or it became too old. * Maturing a plant if it reached a certain age. * Updating a plant's water level during time. ''' def __init__(self): self._database = MemcachedDatabase() self._configs = Configs() def robot_got(self, robot_object, locked_for_update): '''Checks and updates the specified robot's object.''' if robot_object.get_energy() <= 0 or robot_object.get_life() <= 0: if not locked_for_update: # This would call this method again, and robot will be updated. return self._database.get_robot(robot_object.get_id(), for_update=True) robot_object.set_alive(False) # Removing the robot from its location. try: square = self._database.get_square(robot_object.get_location(), for_update=True) except LockAlreadyAquiredError: # Trying one more time. time.sleep(0.02) square = self._database.get_square(robot_object.get_location(), for_update=True) square.set_robot_id(None) # XXX: Though it's a very dirty thing to do, we have to commit these changes, because # later this robot will face an Authentication Exception, and our changes will be # lost. self._database.commit() # Immediately, locking the robot object. It's not atomic, so there's a little chance # that concurrency happens. But, it shouldn't be a problem, since the robot is already # dead, and can't do anything anyway. self._database.get_lock(robot_object.get_id()) return robot_object def square_got(self, location, square_object, locked_for_update): '''Checks and updates specified square object.''' plant = square_object.get_plant() if plant is None: return square_object # Time passed from the last time this plant updated. last_update = time.time() - plant.get_last_update() passed_cycles = math.floor(last_update / (self._configs.get_plant_cylce() / 1000)) if passed_cycles <= 0: # No cycle passed, no need to be updated. return square_object if not locked_for_update: # This will call this method again. try: return self._database.get_square(location, for_update=True) except LockAlreadyAquiredError: # Trying one more time. time.sleep(0.02) return self._database.get_square(location, for_update=True) plant.set_age(plant.get_age() + passed_cycles) plant.set_water_level(plant.get_water_level() - (passed_cycles * self._configs.get_plant_lose_water_in_cycle())) if plant.get_age() > self._configs.get_plant_max_age( ) or plant.get_water_level() <= 0: # Plant is dead! Removing it from the world. square_object.set_plant(None) plant.set_last_update(time.time()) return square_object
class PopulationControl: '''Controls the population of the world. i.e. controlling if a robot can have a child, or gives birth to new robots. ''' def __init__(self): self._authenticator = Authenticator() self._database = MemcachedDatabase() self._world = World() self._id_generator = IDGenerator() self._configs = Configs() def execute_command(self, password, command, args): '''Executes the specified command.''' if not isinstance(password, str): raise InvalidArgumentsError( "Expected {0} as password, found {1}.".format( type(str), type(password))) if command == "born": return self._born(password, args) elif command == "give_birth": return self._give_birth(password, args) def _born(self, password, args): '''Gives birth to a robot for the first time. @param args: List of arguments. The first argument can be a parent robot. If provided, the new robot will be born software near its parent. If not, it will be born on a random place. ''' parent_robot_id = None if len(args) > 0: parent_robot_id = args[0] if parent_robot_id is not None: if not isinstance(parent_robot_id, str): raise InvalidArgumentsError( "`parent_robot_id' must be `str', not {0}".format( type(parent_robot_id))) parent_robot = self._database.get_robot(parent_robot_id) born_location = parent_robot.get_location() else: world_size = self._world.get_size() born_location = (random.randint(0, world_size[0] - 1), random.randint(0, world_size[1] - 1)) robot_name = "" if len(args) == 2: robot_name = args[1] self._authenticator.authenticate_new_robot(password) new_robot = Robot(self._id_generator.get_robot_id(), self._id_generator.get_password(), name=robot_name) self._world.add_robot(new_robot, (born_location[0], born_location[1])) return { 'robot_id': new_robot.get_id(), 'password': new_robot.get_password() } def _give_birth(self, password, args): '''Checks if robot has the permission to give birth to a child. If so, it generates and returns a new password. ''' if len(args) != 1: raise InvalidArgumentsError( "`give_birth' takes exactly one argument.") robot_id = args[0] if not isinstance(robot_id, str): raise InvalidArgumentsError( "Expected {0} as robot_id, found {1}".format( type(str), type(args[0]))) robot = self._database.get_robot(robot_id, for_update=True) required_honor = self._configs.get_robots_birth_required_honor() if robot.get_honor() < required_honor: raise NotEnoughHonorError( "Robot needs {0} honor to give birth, but has {1}.".format( required_honor, robot.get_honor())) robot.set_honor(robot.get_honor() - required_honor) # This while is exists, to prevent generating duplicated passwords. while True: try: new_password = self._id_generator.get_password() self._database.add_password(new_password) break except DuplicatedPasswordError: # pragma: no cover continue return new_password
class ObjectUpdater(DatabaseHook): '''This class checks objects in the world, and updates them if required. Responsibilities of this class: * Killing and removing a robot from the world, if its energy reached zero or it ran out of life. * Removing a plant from the world if its water level reached zero or it became too old. * Maturing a plant if it reached a certain age. * Updating a plant's water level during time. ''' def __init__(self): self._database = MemcachedDatabase() self._configs = Configs() def robot_got(self, robot_object, locked_for_update): '''Checks and updates the specified robot's object.''' if robot_object.get_energy() <= 0 or robot_object.get_life() <= 0: if not locked_for_update: # This would call this method again, and robot will be updated. return self._database.get_robot(robot_object.get_id(), for_update=True) robot_object.set_alive(False) # Removing the robot from its location. try: square = self._database.get_square(robot_object.get_location(), for_update=True) except LockAlreadyAquiredError: # Trying one more time. time.sleep(0.02) square = self._database.get_square(robot_object.get_location(), for_update=True) square.set_robot_id(None) # XXX: Though it's a very dirty thing to do, we have to commit these changes, because # later this robot will face an Authentication Exception, and our changes will be # lost. self._database.commit() # Immediately, locking the robot object. It's not atomic, so there's a little chance # that concurrency happens. But, it shouldn't be a problem, since the robot is already # dead, and can't do anything anyway. self._database.get_lock(robot_object.get_id()) return robot_object def square_got(self, location, square_object, locked_for_update): '''Checks and updates specified square object.''' plant = square_object.get_plant() if plant is None: return square_object # Time passed from the last time this plant updated. last_update = time.time() - plant.get_last_update() passed_cycles = math.floor(last_update / (self._configs.get_plant_cylce() / 1000)) if passed_cycles <= 0: # No cycle passed, no need to be updated. return square_object if not locked_for_update: # This will call this method again. try: return self._database.get_square(location, for_update=True) except LockAlreadyAquiredError: # Trying one more time. time.sleep(0.02) return self._database.get_square(location, for_update=True) plant.set_age(plant.get_age() + passed_cycles) plant.set_water_level(plant.get_water_level() - (passed_cycles * self._configs.get_plant_lose_water_in_cycle())) if plant.get_age() > self._configs.get_plant_max_age() or plant.get_water_level() <= 0: # Plant is dead! Removing it from the world. square_object.set_plant(None) plant.set_last_update(time.time()) return square_object