def __init__(self, difficulty): self.difficulty = difficulty if self.difficulty is not None: # Setting size by difficulty if self.difficulty == 1: self.width = 5 self.height = 5 elif self.difficulty == 2: self.width = 7 self.height = 7 elif self.difficulty == 3: self.width = 11 self.height = 11 else: return print( "ERROR: Out of index for maze puzzle difficulty. Please use difficulties 1(Easiest) to 3(Hardest)" ) # Generate the Plane self.coordinates = [] for x in range(self.width): for y in range(self.height): self.coordinates.append((x, y)) self.spawnpoint = Coords.Coords(random.randint(0, self.width), random.randint(0, self.height)) self.endpoint = Coords.Coords(random.randint(0, self.width), random.randint(0, self.height)) self.point_difference = math.sqrt( math.pow((self.endpoint.get_x() - self.spawnpoint.get_x()), 2) + math.pow((self.endpoint.get_y() - self.spawnpoint.get_y()), 2)) while (self.point_difference < (self.difficulty * 2)): self.spawnpoint = Coords.Coords(random.randint(0, self.width), random.randint(0, self.height)) self.endpoint = Coords.Coords(random.randint(0, self.width), random.randint(0, self.height)) self.point_difference = math.sqrt( math.pow((self.endpoint.get_x() - self.spawnpoint.get_x()), 2) + math.pow((self.endpoint.get_y() - self.spawnpoint.get_y()), 2)) self.player_coords = self.spawnpoint else: print("ERROR: Please specify a maze difficulty")
def __generate_separated_planet(self): separated_max_count = self.__max_planet_count - len(self.__planets) separated_num = 0 try_num = 0 separated = [self.__planets[0]] while True: x = random.randint(-self.__screen_length / 2, self.__screen_length / 2) y = random.randint(-self.__screen_height / 2, self.__screen_height / 2) check = True new_planet = Planet( Coords(x, y), random.choices( [PlanetType.SMALL, PlanetType.MEDIUM, PlanetType.BIG], [600, 300, 200])[0]) if abs(new_planet.coords.x) > self.__screen_length / 2 - self.__planet_free_space_radius or \ abs(new_planet.coords.y) > self.__screen_height / 2 - self.__planet_free_space_radius: check = False subradius = self.__start_position_radius * math.sin( math.pi / self.__players) for i in self.__planets[1:1 + self.__players]: if new_planet.coords.calc_distance(i.coords) < subradius: check = False break # for i in self.__planets: # # print(new_planet.coords.calc_distance(i[1].coords), i[0]) # if new_planet.coords.calc_distance(i.coords) < 2 * PLANET_FREE_SPACE_RADIUS: # check = False # break for i in separated: if new_planet.coords.calc_distance( i.coords) < 2 * self.__planet_free_space_radius: check = False break if check: separated.append(new_planet) try_num = 0 else: try_num += 1 if try_num > self.__max_gen_try * 200: break continue separated_num += 1 if separated_num >= separated_max_count: break self.__planets += separated[1:]
def __init__(self, manager_config_file, agents_config_file): self.flags = Flags() self.state = States.STARTING self.logger = logging.getLogger("manager") self.capture_dir_base = "" self.capture_dir = "" self.q_user_commands = SimpleQueue( ) # Cola de comandos provenientes de de teclado o botonera self.agents = AgentProxies(self.flags.quit) self.dbi = None self.coordinates = Coords() self.segment_coords_ini = Coords() self.segment_current_length = 0 self.segment_current_init_time = 0 self.folio = None self.mgr_cfg = dict() self.set_up(manager_config_file, agents_config_file) self.session = None # Identificador de sesión de captura. La sesión inicia y termina con el apriete del boton (o tecla 's') self.segment = 0000 # Identificador del segmento dentro de la sesión. Número correlativo self.sys_id = 'NNN'
def __generate_subplanet(self): subplanet_max_count = round( (self.__max_planet_count - self.__players - 1) * 0.6 / self.__players) max_try = 25 for planet in self.__planets[1:]: subplanet_num = 0 try_num = 0 subplanets = [] while True: sub_alpha = random.randint(0, 359) sub_radius = random.randint( 2 * self.__planet_free_space_radius, int(self.__start_position_radius * math.sin(math.pi / self.__players)) - self.__planet_free_space_radius) check = True new_planet = Planet( Coords( sub_radius * math.cos(sub_alpha * math.pi / 180) + planet.coords.x, sub_radius * math.sin(sub_alpha * math.pi / 180) + planet.coords.y), random.choices( [PlanetType.SMALL, PlanetType.MEDIUM, PlanetType.BIG], [600, 300, 200])[0]) if abs(new_planet.coords.x) > self.__screen_length / 2 - self.__planet_free_space_radius or \ abs(new_planet.coords.y) > self.__screen_height / 2 - self.__planet_free_space_radius: try_num += 1 if try_num > max_try: break continue for i in subplanets: if new_planet.coords.calc_distance( i.coords) < 2 * self.__planet_free_space_radius: check = False break if check: subplanets.append(new_planet) try_num = 0 else: try_num += 1 if try_num > max_try: break continue subplanet_num += 1 if subplanet_num >= subplanet_max_count: break self.__planets += subplanets
def test_calc_distance(self): c = Coords(x, y) testList = [Coords(0, 0), Coords(x, y), Coords(5, 12)] for i in range(len(testList)): with self.subTest(i=i): self.assertEqual( c.calc_distance(testList[i]), ((x - testList[i].x)**2 + (y - testList[i].y)**2)**0.5, "wrong calc_distance for point " + str(testList[i].x) + "," + str(testList[i].y))
def test_constructor(self): c = Coords(x, y) self.assertEqual(c.x, x, 'incorrect x') self.assertEqual(c.y, y, 'incorrect y')
def test_get_coord(self): c = Coords(x, y) self.assertEqual(c.get_coord(), (x, y), 'returned value is incorrect')
def test_radius_calculation(self): c = Coords(x, y) self.assertEqual(c.radius_calculation(), (c.x**2 + c.y**2)**(0.5), 'incorrect radius-vector')
def test_get_dictionary(self): c = Coords(x, y) self.assertEqual(c.get_dict()["x"], x, 'incorrect convertion to dict (x)') self.assertEqual(c.get_dict()["y"], y, 'incorrect convertion to dict (x)')
def test_str(self): c = Coords(x, y) self.assertEqual(c.__str__(), "(10, 15)", 'incorrect format of str')
from handlers import MainHandler from workers import GpsModuleWorker PORT = 12345 # replace this with random port when register will be working def make_app(coords): return Application([ (r'/', MainHandler, { "coords": coords }), (r'/healthcheck', HealthCheckHandler), ]) if __name__ == '__main__': os.environ['GPS_ROOT'] = os.getcwd() logging_config() logging.info("starting gps app") coords = Coords() #gps_worker = GpsModuleWorker(coords) methods = [ method for method in dir(coords) if callable(getattr(coords, method)) and method[0] != '_' ] service = Service('192.168.1.22', PORT, 'GPS', methods) service.register_interface() app = make_app(coords) app.listen(PORT) IOLoop.current().start()
def __generate_start_position(self, players_ids): players_count = len(players_ids) planets = [[0, Planet(Coords(0, 0), PlanetType.BIGGEST), 0]] alpha = random.randint(0, int(360 / players_count)) for player_id in players_ids: coord = Coords() tang = math.tan(alpha * math.pi / 180) if alpha >= 360: alpha -= 360 if alpha == 90 or alpha == 270: coord.x = 0 coord.y = self.__screen_height * math.sin( alpha * math.pi / 180) / 2 elif alpha == 180 or alpha == 0: coord.y = 0 coord.x = self.__screen_length * math.cos( alpha * math.pi / 180) / 2 elif 0 < alpha < 90: if alpha >= self.__border_angle: height_border = self.__screen_height / 2 coord.x = height_border / tang coord.y = height_border else: height_border = self.__screen_length / 2 coord.y = height_border * tang coord.x = height_border elif 180 < alpha < 270: if alpha >= self.__border_angle + 180: height_border = self.__screen_height / 2 coord.x = -height_border / tang coord.y = -height_border else: height_border = self.__screen_length / 2 coord.y = -height_border * tang coord.x = -height_border elif 90 < alpha < 180: if alpha <= 180 - self.__border_angle: height_border = self.__screen_height / 2 coord.x = height_border / tang coord.y = height_border else: height_border = self.__screen_length / 2 coord.y = abs(height_border * tang) coord.x = -height_border elif 270 < alpha < 360: if alpha <= 360 - self.__border_angle: height_border = self.__screen_height / 2 coord.x = abs(height_border / tang) coord.y = -height_border else: height_border = self.__screen_length / 2 coord.y = height_border * tang coord.x = self.__screen_length / 2 planets.append([ coord.radius_calculation(), Planet(coord, PlanetType.BIG, player_id), alpha ]) alpha += 360 / players_count min_rad = min(planets[1:], key=lambda x: x[0]) self.__start_position_radius = min_rad[ 0] - self.__planet_free_space_radius for i in planets[1:]: i[1].coords.x = int(self.__start_position_radius * math.cos(i[2] * math.pi / 180)) i[1].coords.y = int(self.__start_position_radius * math.sin(i[2] * math.pi / 180)) i[0] = self.__start_position_radius self.__planets += [planet[1] for planet in planets]