Beispiel #1
0
    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
Beispiel #2
0
    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(),
            }
        })
Beispiel #3
0
    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)