Ejemplo n.º 1
0
    def create_game(
            league_id,
            creator_id,
            message,
            metrics_by_opponent,
            sport_id):
        """ Create a Game and return it.

        Required:
        id      league_id           League id that Game belongs to
        id      creator_id          Player id of Game's creator
        str     message             Creator's message (headline)
        dict    metrics_by_opponent Metrics keyed on Opponent id
        id      sport_id            Sport id for this Game

        Return:
        Game                        newly created Game

        """

        # FIXME: how do we handle None properties?
        if message is None:
            message = "Good Game."

        # TODO: when games have properties, fill these in!
        #
        #raw_properties = {}
        #
        #properties = SqNode.prepare_node_properties(
        #        Game.property_keys(),
        #        raw_properties)

        # prepare a node prototype for this game
        prototype_node = editor.prototype_node(
                API_NODE_TYPE.GAME,
                {API_NODE_PROPERTY.SPORT_ID: sport_id})

        # prepare edge prototypes for schedule edges
        prototype_edges = editor.prototype_edge_and_complement(
                API_EDGE_TYPE.HAS_SCHEDULED,
                {},
                league_id)

        # prepare edge prototypes for creator edges
        prototype_edges.extend(editor.prototype_edge_and_complement(
                API_EDGE_TYPE.CREATED,
                {API_EDGE_PROPERTY.MESSAGE: message},
                creator_id))

        # prepare edge prototypes for result edges
        for opponent_id, metrics in metrics_by_opponent.items():
            for metric in metrics:
                # TODO: handle other metrics besides ResultMetric
                prototype_edges.extend(editor.prototype_edge_and_complement(
                        metric.result,
                        {},
                        opponent_id))

        return editor.create_node_and_edges(prototype_node, prototype_edges)
Ejemplo n.º 2
0
    def create_league(name, creator_id, opponent_ids=[]):
        """ Create and return a League.

        Required:
        str     name            name of League to be created
        id      creator_id      id of Person creating this League

        Optional:
        list    opponent_ids    ids of Teams/Players to add by default

        Return:
        League                  SqNode of Teams, Players, Schedules

        """

        raw_properties = {
                API_NODE_PROPERTY.NAME: name,
                }

        # squash the two into one set of flat, valid node properties
        properties = League.prepare_node_properties(
                League.property_keys(),
                raw_properties)

        # prepare a node prototype for this league
        prototype_node = editor.prototype_node(
                API_NODE_TYPE.LEAGUE,
                properties)

        # prepare edge prototypes for creator edges
        # TODO: All creators to attach a message to league creation
        prototype_edges = editor.prototype_edge_and_complement(
            API_EDGE_TYPE.CREATED,
            {},
            creator_id)

        # prepare edge prototypes for default opponents
        for opponent_id in opponent_ids:
            prototype_edges.extend(editor.prototype_edge_and_complement(
                API_EDGE_TYPE.IN_LEAGUE,
                {},
                opponent_id))

        return editor.create_node_and_edges(prototype_node, prototype_edges)
Ejemplo n.º 3
0
    def create_user(
            email,
            password_hash,
            referrer_url,
            third_parties={},
            inviter_id=None,
            ip=PROPERTY_VALUE.EMPTY,
            locale=PROPERTY_VALUE.EMPTY):
        """ Create a User and return it.

        Required:
        str     email           created User's email
        str     password_hash   created User's encrypted password
        url     referrer_url    source link clicked to cause this

        Optional:
        dict    third_parties   key/val dicts keyed on 3rd party
        id      inviter_id      User inviting/spawning User/Person
        str     ip              User's IP, if logged in
        str     locale          User's locale (ISO language+country)

        Return:
        User                    SqNode can log in, act as other SqNodes

        Example:
        Optional parameter third_parties should be defined as follows:

        {
            "fb" : {<CONVERT FROM JSON TO DICT AND LEAVE DATA AS IS>},
            "tw" : {<CONVERT FROM JSON TO DICT AND LEAVE DATA AS IS>},
        }

        This will be flattened, validated, and culled by SqNode.

        """

        # TODO: when there's an alternative to Facebook for login, revisit
        # authorization logic. some of this logic might be context-dependent.

        # email, password_hash, username, timezone, and locale are just being
        # initialized for now; we get these values from Facebook, but they
        # deserve placeholders in case we have to remove third party data.

        # these properties are not explicitly required. one or more third
        # parties may provide them instead.
        raw_properties = {
                API_NODE_PROPERTY.EMAIL: email,
                API_NODE_PROPERTY.PASSWORD_HASH: password_hash,
                }

        # TODO: figure out what else to do with referrer_url

        # no ip address would mean this user isn't logged in at creation time,
        # in which case the initialized empty values suffice.
        if ip:
            current_ts = int(time.time())

            raw_properties.update({
                    API_NODE_PROPERTY.REFERRER_URL: referrer_url,
                    API_NODE_PROPERTY.LAST_IP: ip,
                    API_NODE_PROPERTY.LOCALE: locale,
                    API_NODE_PROPERTY.LAST_LOGIN_TS: current_ts,
                    API_NODE_PROPERTY.LAST_AUTHORIZED_TS: current_ts,
                    })

        # squash the two into one set of flat, valid node properties
        properties = User.prepare_node_properties(
                User.property_keys(),
                raw_properties,
                third_parties)

        # TODO: add a static method call to generically check required fields
        # against statically defined lists in each class.

        # prepare a node prototype for this user
        prototype_node = editor.prototype_node(
                API_NODE_TYPE.USER,
                properties)

        # TODO: prepare an edge prototype for the inviter_id

        return editor.create_node_and_edges(prototype_node, [])
Ejemplo n.º 4
0
    def create_player(
            first_name,
            last_name,
            spawner_id,
            owner_id=None,
            third_parties={}):
        """ Create a Player and return it.

        Required:
        str     first_name          created Player's first name
        str     last_name           created Player's last name
        id      spawner_id          id of User creating this Player

        Optional:
        id      owner_id            id of User controlling this Player
        dict    third_parties       key/val dicts keyed on 3rd party

        Return:
        Player                      is a Person, acts as an Opponent

        Example:
        Optional parameter third_parties should be defined as follows:

        {
            "fb" : {<CONVERT FROM JSON TO DICT AND LEAVE DATA AS IS>},
            "tw" : {<CONVERT FROM JSON TO DICT AND LEAVE DATA AS IS>},
        }

        This will be flattened, validated, and culled by SqNode.

        """

        player_keys = Player.property_keys()

        # TODO: everything below should happen generically in Person._create()
        #return Person.create(
        #        raw_properties,
        #        Player.property_keys(),
        #        spawner_id,
        #        owner_id,
        #        third_parties)

        # FIXME: for now, we co-locate third party username/email with both
        # the associated User and Person...it's kinda bad.
        player_keys.extend([
                API_NODE_PROPERTY.USERNAME,
                API_NODE_PROPERTY.EMAIL,
                ])

        raw_properties = {
                API_NODE_PROPERTY.FIRST_NAME: first_name,
                API_NODE_PROPERTY.LAST_NAME: last_name,
                }

        properties = Player.prepare_node_properties(
                player_keys,
                raw_properties,
                third_parties)

        # TODO: add a static method call to generically check required fields
        # against statically defined lists in each class.

        # prepare a node prototype for this game
        prototype_node = editor.prototype_node(
                API_NODE_TYPE.PLAYER,
                properties)

        # prepare edge prototypes for spawner edges
        prototype_edges = editor.prototype_edge_and_complement(
                API_EDGE_TYPE.SPAWNED,
                {},
                spawner_id)

        if owner_id is None:
            owner_id = spawner_id

        # prepare edge prototypes for owner edges
        prototype_edges.extend(editor.prototype_edge_and_complement(
                API_EDGE_TYPE.OWNS,
                {},
                owner_id))

        prototype_edges.append(editor.prototype_edge(
                API_EDGE_TYPE.HAS_PRIMARY,
                {},
                None,
                owner_id))

        prototype_edges.append(editor.prototype_edge(
                API_EDGE_TYPE.DEFAULTS_TO,
                {},
                owner_id,
                None))

        return editor.create_node_and_edges(prototype_node, prototype_edges)