def new_game(game_id): """Save new game results to database""" game = Game() game.game_id = game_id game.game_host = 'https://xgs8.c.bytro.com/' supremacy = Supremacy(game.game_id, game.game_host) while True: try: result = supremacy.game() except ServerChangeError as exception: new_server = str(exception) game.game_host = new_server supremacy.url = new_server continue break _update_game(game, result) game.start_at = datetime.fromtimestamp(result["startOfGame"]) game.password = result["password"] game.scenario = result["scenarioID"] game.ranked = result["ranked"] game.gold_round = result["goldRound"] game.ai_level = result["aiLevel"] game.country_selection = result["countrySelection"] game.time_scale = result["timeScale"] # game.team_setting = result["teamSettings"] game.victory_points = result["victoryPoints"] game.research_days_offset = result["researchDaysOffset"] if "researchTimeScale" in result: game.research_time_scale = result["researchTimeScale"] else: game.research_time_scale = 1.0 game.team_victory_points = result["teamVictoryPoints"] game_map = Map.query.filter(Map.map_id == result["mapID"]).first() if game_map is None: game_map = Map() game_map.map_id = result["mapID"] game_map.name = result["mapID"] game_map.slots = result["openSlots"] + result["numberOfPlayers"] db.session.add(game_map) db.session.commit() game.map_id = game_map.id db.session.add(game) db.session.commit() return game
def test_live(app, db): with app.test_client() as client: m = Map('foo', bbox=[1, 1, 1, 1]) db.session.add(m) db.session.commit() uuid = m.uuid.hex from app.live import socketio socketio_client = socketio.test_client(app, flask_test_client=client) socketio_client.emit('join', uuid) assert socketio_client.is_connected() r = socketio_client.get_received() assert (len(r) == 1) assert (r[0]['name'] == 'message') assert (r[0]['args'] == 'user joined') m.name = 'bar' db.session.add(m) db.session.commit() r = socketio_client.get_received() assert (len(r) == 1) assert (r[0]['name'] == 'map-updated') point = GeoFeature(geometry=GeoPoint([1, 1])) f = Feature(point) db.session.add(m) m.features.append(f) db.session.commit() r = socketio_client.get_received() assert (len(r) == 1) assert (r[0]['name'] == 'feature-created') f.style = {'color': 'red'} db.session.add(f) db.session.commit() r = socketio_client.get_received() assert (len(r) == 1) assert (r[0]['name'] == 'feature-updated') db.session.delete(m) db.session.commit() r = socketio_client.get_received() assert (len(r) == 2) assert (r[0]['name'] == 'map-deleted') assert (r[1]['name'] == 'feature-deleted')