def force_turn(self): user = getUser() world = worlds.get(user.wid) country: Country = countries.get(world.current, world.wid) world_countries = world.countries dict_countries = OrderedDict((c.iso, c) for c in world_countries) if not country: # ??? return try: round_end_events = turns.end_turn(world, country, dict_countries) except turns.TurnException as e: return {"err": e.reason} except turns.EndGameException as e: # fake end game: used for playtesting # we don't schedule the world to be deleted and don't give ratings return ApiResponse({ "route": "Game:end_game", "winner": e.reason }) worlds.save(world) return ApiResponse({ "route": "Game:end_turn", "iso": country.iso, "turn_end": { "turns": world.turns, "current": world.current, }, "round_end": round_end_events.to_dict() if round_end_events else None })
def move(self, area_id, to_id, user: User): error, world, country, area1 = self._accessControl(user, area_id) if error: return error area2: Area = areas.get(to_id, user.wid) is_kill = bool(area2.unit) try: is_conquer = movement.move_to(area1, area2, world.map) except movement.MoveException as e: return {"err": e.reason} if not world.has_moved: world.has_moved = True worlds.save(world) areas.save(area1) areas.save(area2) self.server.send_to_world( user.wid, { "route": self.name + ":move", "iso": user.iso, "area_id": area_id, "to_id": to_id, "events": { "conquer": is_conquer, "kill": is_kill } })
def _set_up_game(l_countries, adict): _delete_all() world = World(current='P1', wid=WID, map='autotest_map') l_users = [] for i, country in enumerate(l_countries): country.name = 'Country ' + country.iso country.wid = world.wid country.order = i + 1 l_users.append( User(iso=country.iso, email='*****@*****.**', password='******', token='t', salt='s', username='******' + country.iso, wid=world.wid, elo=1100, division=1)) conn_graph = {} l_areas = {} cid = lambda x, y: 'a{}{}'.format(x, y) for y in range(0, 4): for x in range(0, 4): area = adict[x][y] area.id = cid(x, y) area.wid = world.wid area.iso2 = area.iso if area.build in ('barr', 'house', 'cita'): area.tile = 'city' l_areas[area.id] = area conn_graph[area.id] = [ cid(x - 1, y), cid(x + 1, y), cid(x, y - 1), cid(x, y + 1), ] service.switch_conn_graph(conn_graph) worlds.save(world) countries.save_all(l_countries) areas.save_all(l_areas.values()) users.save_all(l_users) # reset entities countries.calculate_pop(WID) return world, l_users, l_countries, l_areas
def start_world(world, AI=False): l_countries, l_areas = create_world_entities(world, AI=AI) # set first player as current tb = TurnBox(world, list(map(lambda c: c.iso, sorted(l_countries, key=lambda c: c.order)))) tb.start() # save entities countries.save_all(l_countries) areas.save_all(l_areas) worlds.save(world)
def new_world(self): user = getUser() if not user.username: return "Login first" world = World(name="Test world", map='hu_test', max_rounds=None) worlds.save(world) start_world(world, AI=True) user.wid = world.wid user.iso = 'UK' users.save(world) return 'New world created: {}'.format(world.wid)
def _set_up_match(l_countries, l_areas): _delete_all() world = World(wid=WID, current='P1', name="Test World", map='autotest_map', max_rounds=None) l_users = [] for i, country in enumerate(l_countries): country.name = 'Country ' + country.iso country.wid = world.wid country.order = i + 1 l_users.append( User(iso=country.iso, email='*****@*****.**', password='******', token='t', salt='s', username='******' + country.iso, wid=world.wid, elo=1100, division=1)) for area in l_areas: area.wid = world.wid area.iso2 = area.iso # todo: later: add conn graph to GTML service.switch_conn_graph({}) worlds.save(world) countries.save_all(l_countries) areas.save_all(l_areas) users.save_all(l_users) # reset entities countries.calculate_pop(WID)
def buy(self, area_id, item_id, user: User, sacrifice=False): error, world, country, area = self._accessControl(user, area_id) if error: return error if world.has_moved: return {"err": "cant_buy_after_move"} try: if sacrifice: economy.sacrifice_shield(country, area, item_id) else: economy.buy_item(area, country, item_id) except economy.service.BuyException as e: return {"err": e.reason} areas.save(area) countries.save(country) cost = economy.get_cost(item_id) if sacrifice: del cost['gold'] cost["shields"] = 1 # a sacrifice counts as a normal move, you can't buy afterwards! if not world.has_moved: world.has_moved = True worlds.save(world) self.server.send_to_world( user.wid, { "route": self.name + ":buy", "iso": country.iso, "area_id": area_id, "item_id": item_id, "cost": cost })
def start(self): if not request.args['uids']: return ApiResponse({'result': False, 'no': -1}) uids = request.args['uids'].split(',') world = worlds.get(self.WID) l_users = users.list(uids) # delete world if world is not None: worlds.delete(world) h = histories.get(self.WID) if h is not None: histories.delete(h) # create world world = World(wid=self.WID, name="Playtest world", map=self.MAP, max_rounds=None) isos = load_isos('game/maps/{}.gtml'.format(world.map)) if len(isos) < len(l_users): return ApiResponse({'result': False, 'no': len(isos)}) # force-enter users: for user, iso in zip(l_users, isos): user.wid = world.wid user.iso = iso # Save everything and start the game worlds.save(world) start_world(world, AI=True) users.save_all(l_users) return ApiResponse({'result': 'OK'})
def end_turn(self, user: User, timeout=None): if user.wid is None: return {"err": "not_in_match"} world = worlds.get(user.wid) if world.current != user.iso: # Someone else has requested the turn end # Check if the current user is out of its turn time: if not turns_serv.check_timeout(world, current_iso=timeout): # current user is still in time, this request is refused return {"err": "not_your_turn"} curr_country = countries.get(world.current, user.wid) world_countries = world.countries dict_countries = OrderedDict((c.iso, c) for c in world_countries) # if len(world.isos) < 2: # return {"err": "waiting_for_players"} try: round_end_events = turns_serv.end_turn(world, curr_country, dict_countries) winner_iso = None except turns_serv.EndGameException as e: winner_iso = e.reason round_end_events = e.events except turns_serv.TurnException as e: return {"err": e.reason} self.server.send_to_world( user.wid, { "route": self.name + ":end_turn", "iso": user.iso, "turn_end": { "turns": world.turns, "current": world.current, }, "round_end": round_end_events.to_dict() if round_end_events else None }) if winner_iso: winner = winner_iso if winner_iso != '-1' else None # create match history world_users = users.list_all(world.wid) endgame.create_match_history(world, world_users, world_countries, winner=winner) # change user ratings endgame.apply_rating(world_users, winner=winner) users.save_all(world_users) # this schedules the world to be deleted at a later time world.turns = -1 self.server.send_to_world(user.wid, { "route": self.name + ":end_game", "winner": winner_iso }) worlds.save(world)
def set_map(world, map_id): world.map = map_id worlds.save(world)
def create(self, user): world, l_countries = create_world() worlds.save(world) return self.join(world.wid, user)