def testArchtypeHashkeyGeneration(self): corran = { "name": "corranhorn", "points": 46, "ship": "ewing", "upgrades": {"ept": ["veteraninstincts"], "system": ["firecontrolsystem"], "amd": ["r2d2"], "mod": ["engineupgrade"]} } dash = { "name": "dashrendar", "points": 54, "ship": "yt2400freighter", "upgrades": {"ept": ["pushthelimit"], "cannon": ["heavylasercannon"], "crew": ["kananjarrus"], "title": ["outrider"]} } xws1 = {"faction": "rebel", "name": "Corran46 Dash 54", "pilots": [corran, dash], "points": 100, "vendor": { "yasb": { "builder": "(Yet Another) X-Wing Miniatures Squad Builder", "builder_url": "https://geordanr.github.io/xwing/", "link": "https://geordanr.github.io/xwing/?f=Rebel%20Alliance&d=v4!s!75:27,36,-1,3:-1:3:;95:18,23,-1,159:14:-1:&sn=Unnamed%20Squadron" } }, "version": "0.3.0" } xws2 = {"faction": "rebel", "name": "Dash 54 Corran46 ", "pilots": [dash, corran], "points": 100, "vendor": { "yasb": { "builder": "(Yet Another) X-Wing Miniatures Squad Builder", "builder_url": "https://geordanr.github.io/xwing/", "link": "https://geordanr.github.io/xwing/?f=Rebel%20Alliance&d=v4!s!75:27,36,-1,3:-1:3:;95:18,23,-1,159:14:-1:&sn=Unnamed%20Squadron" } }, "version": "0.3.0" } converter = XWSToJuggler(xws1) archtype1, first_time_archtype_seen1 = converter.convert(self.pm) converter = XWSToJuggler(xws2) archtype2, first_time_archtype_seen2 = converter.convert(self.pm) print(archtype1.hashkey) print(archtype2.hashkey) self.assertTrue(archtype1.hashkey == archtype2.hashkey)
def get_from_fab(): try: fab = request.args.get('fab') tourney_id = request.args.get('tourney_id') tourney_list_id = request.args.get('tourney_list_id') pm = PersistenceManager(myapp.db_connector) tourney_list = pm.get_tourney_list(tourney_list_id) fetcher = FabFetcher() xws = fetcher.fetch( fab ) converter = XWSToJuggler(xws) converter.convert( pm, tourney_list ) pm.db_connector.get_session().commit() return jsonify(tourney_id=tourney_id, tourney_list_id=tourney_list_id) except Exception, e: mail_error( "Unable to fetch from fab for url " + fab + ", reason: " + str(e)) response = jsonify(message=str(e)) response.status_code = (500) return response
def update_escrowed_lists(self,pm, league, escrowed_lists): fetcher = GeneralXWSFetcher() for el in escrowed_lists: player1_name = el['player1'] player2_name = el['player2'] list1 = el['list1'] list2 = el['list2'] player1 = pm.get_tier_player_by_name(player1_name, league.name) player2 = pm.get_tier_player_by_name(player2_name, league.name) if player1 and player2: match = pm.get_match_by_players(league.id, player1.id, player2.id) if match: dirty = False if match.player1_list_url is None and list1 is not None: match.player1_list_url = list1 try: xws = fetcher.fetch(list1) converter = XWSToJuggler(xws) match.player1_list, _ = converter.convert(pm) dirty = True except Exception as err: message = "unable to fetch list, reason: " + str(err) print(message) if match.player2_list_url is None and list2 is not None: match.player2_list_url = list2 try: xws = fetcher.fetch(list2) converter = XWSToJuggler(xws) match.player2_list, _ = converter.convert(pm) dirty = True except Exception as err: message = "unable to fetch list, reason: " + str(err) print(message) if dirty: pm.db_connector.get_session().add(match) pm.db_connector.get_session().commit()
def add_from_voidstate(): try: voidstate_id = request.args.get('voidstate_id') tourney_id = request.args.get('tourney_id') tourney_list_id = request.args.get('tourney_list_id') pm = PersistenceManager(myapp.db_connector) tourney_list = pm.get_tourney_list(tourney_list_id) fetcher = VoidStateXWSFetcher() xws = fetcher.fetch(voidstate_id) converter = XWSToJuggler(xws) converter.convert( pm, tourney_list ) pm.db_connector.get_session().commit() return jsonify(tourney_id=tourney_id, tourney_list_id=tourney_list.id) except Exception, e: mail_error( "Unable to fetch from voidstate for id " + voidstate_id + ", reason: " + str(e)) response = jsonify(message=str(e)) response.status_code = (500) return response
def extract_players(self, t, tourney): pm = PersistenceManager(myapp.db_connector) tlists_by_id = {} tlists_by_name = {} if t.has_key(PLAYERS): players = t[PLAYERS] i = 1 for p in players: player = None ranking = None # first see if the tourney already has a player and player list matching this player's name #if so, update it rather than creating a new one if p.has_key(PLAYER_ID): player = tourney.get_player_by_id(p[PLAYER_ID]) if player is None: return self.bail( "couldn't find player with id %d, giving up" % ( p[PLAYER_ID], 403)), None, None else: if not p.has_key(PLAYER_NAME): return self.bail("neither an id or a name was provided, giving up ", 403), None, None player = tourney.get_player_by_name(p[PLAYER_NAME]) if player is None: player = TourneyPlayer(player_name="Player %d" % ( i )) ranking = TourneyRanking(player=player) player.result = ranking tourney_list = TourneyList(tourney=tourney, player=player) tlists_by_id[player.id] = tourney_list # stash it away for later use if p.has_key(PLAYER_NAME): tlists_by_name[p[PLAYER_NAME]] = tourney_list tourney.tourney_players.append(player) tourney.tourney_lists.append(tourney_list) tourney.rankings.append(ranking) player.tourney_lists.append(tourney_list) i = i + 1 else: ranking = player.result tlists_by_id[player.id] = player.get_first_tourney_list() if p.has_key(PLAYER_NAME): tlists_by_name[p[PLAYER_NAME]] = tlists_by_id[player.id] # add list via XWS if XWS in p: try: XWSToJuggler(p[XWS]).convert(pm, tourney_list) except Exception as e: print "Could not convert XWS: {} (XWS was {!r})".format(e, p[XWS]) self.extract_player(p, player, ranking) return None, tlists_by_id, tlists_by_name
def post(self, tourney_id, player_id): pm = PersistenceManager(myapp.db_connector) helper = TournamentApiHelper(pm) json_data = None try: json_data = request.get_json(force=True) except Exception: return helper.bail("bad json received!", 403) if not helper.isint(tourney_id): return helper.bail("invalid tourney_id %d passed to player post" % ( tourney_id), 400) if not helper.isint(player_id): return helper.bail("invalid player %d passed to player post" % ( player_id), 400) pm = PersistenceManager(myapp.db_connector) tourney = pm.get_tourney_by_id(tourney_id) bail = helper.check_token(json_data, tourney) if bail: return bail player = tourney.get_player_by_id(player_id) if player is None: return helper.bail("couldn't find player %d, bailing out" % ( player_id), 400) dirty = False # add list via XWS if XWS in json_data: dirty = True try: tourney_list = player.tourney_lists[-1] XWSToJuggler(json_data[XWS]).convert(pm, tourney_list) except: pm.db_connector.get_session().rollback() return helper.bail('Could not add list via XWS', 400) # other potential edits if dirty: pm.db_connector.get_session().commit() return jsonify({ 'player': { 'id': player.id, 'tourney_id': tourney.id, 'name': player.get_player_name(), } })
def create_tourney(cryodex, tourney_name, tourney_date, tourney_type, round_length, sets_used, country, state, city, venue, email, participant_count, tourney_format): pm = PersistenceManager(myapp.db_connector) t = Tourney(tourney_name=tourney_name, tourney_date=tourney_date, tourney_type=tourney_type, round_length=round_length, email=email, entry_date=datetime.datetime.now(), participant_count=participant_count, locked=False, format=tourney_format) pm.db_connector.get_session().add(t) #add the players players = {} lists = {} for player in cryodex.players.keys(): tp = TourneyPlayer( tourney=t, player_name=player) tlist = TourneyList(tourney=t, player=tp) pm.db_connector.get_session().add(tlist) players[player] = tp lists[player] = tlist pm.db_connector.get_session().commit() for round_type in cryodex.rounds.keys(): rounds = cryodex.rounds[round_type] for round in rounds: tr = TourneyRound(round_num=int(round.number), round_type=round.get_round_type(), tourney=t) pm.db_connector.get_session().add(tr) for round_result in round.results: rr = None if round_result.bye: p1_tourney_list = lists[ round_result.player1 ] rr = RoundResult(round=tr, list1=p1_tourney_list, list2=None, winner=None, loser=None, list1_score=None, list2_score=None, bye=round_result.bye, draw=round_result.draw) else: p1_tourney_list = lists[round_result.player1] p2_tourney_list = None p2_tourney_list = lists[round_result.player2] winner = None loser = None if round_result.player1 == round_result.winner: winner = p1_tourney_list loser = p2_tourney_list else: winner = p2_tourney_list loser = p1_tourney_list rr = RoundResult(round=tr, list1=p1_tourney_list, list2=p2_tourney_list, winner=winner, loser=loser, list1_score=round_result.player1_score, list2_score=round_result.player2_score, bye=round_result.bye, draw=round_result.draw) pm.db_connector.get_session().add(rr) add_sets_and_venue_to_tourney(city, country, pm, sets_used, state, t, venue) #finally load the rankings for rank in cryodex.ranking.rankings: r = TourneyRanking(tourney=t, player=players[rank.player_name], rank=rank.rank, elim_rank=rank.elim_rank, mov=rank.mov, sos=rank.sos, score=rank.score, dropped=rank.dropped) pm.db_connector.get_session().add(r) pm.db_connector.get_session().commit() if rank.list_id is not None and len(rank.list_id) > 0: #cryodex provided a list id ... load it try: tourney_list = lists[rank.player_name] fetcher = VoidStateXWSFetcher() xws = fetcher.fetch(rank.list_id) converter = XWSToJuggler(xws) converter.convert(pm, tourney_list) except Exception as err: print ("unable to fetch list id " + rank.list_id + " from voidstate, reason: " + str(err) ) mail_error(errortext=str(err) + "<br><br>Unable to fetch list id " + rank.list_id + " from voidstate" ) #and commit all the work pm.db_connector.get_session().commit() return t
def testArchtypeHashkeyGeneration(self): corran = { "name": "corranhorn", "points": 46, "ship": "ewing", "upgrades": { "ept": ["veteraninstincts"], "system": ["firecontrolsystem"], "amd": ["r2d2"], "mod": ["engineupgrade"] } } dash = { "name": "dashrendar", "points": 54, "ship": "yt2400freighter", "upgrades": { "ept": ["pushthelimit"], "cannon": ["heavylasercannon"], "crew": ["kananjarrus"], "title": ["outrider"] } } xws1 = { "faction": "rebel", "name": "Corran46 Dash 54", "pilots": [corran, dash], "points": 100, "vendor": { "yasb": { "builder": "(Yet Another) X-Wing Miniatures Squad Builder", "builder_url": "https://geordanr.github.io/xwing/", "link": "https://geordanr.github.io/xwing/?f=Rebel%20Alliance&d=v4!s!75:27,36,-1,3:-1:3:;95:18,23,-1,159:14:-1:&sn=Unnamed%20Squadron" } }, "version": "0.3.0" } xws2 = { "faction": "rebel", "name": "Dash 54 Corran46 ", "pilots": [dash, corran], "points": 100, "vendor": { "yasb": { "builder": "(Yet Another) X-Wing Miniatures Squad Builder", "builder_url": "https://geordanr.github.io/xwing/", "link": "https://geordanr.github.io/xwing/?f=Rebel%20Alliance&d=v4!s!75:27,36,-1,3:-1:3:;95:18,23,-1,159:14:-1:&sn=Unnamed%20Squadron" } }, "version": "0.3.0" } converter = XWSToJuggler(xws1) archtype1, first_time_archtype_seen1 = converter.convert(self.pm) converter = XWSToJuggler(xws2) archtype2, first_time_archtype_seen2 = converter.convert(self.pm) print(archtype1.hashkey) print(archtype2.hashkey) self.assertTrue(archtype1.hashkey == archtype2.hashkey)