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)
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)
def create_comment(commenter_id, object_id, message): """ Create a Comment and return it. Required: id commenter_id the Person who posted the comment id object_id the object that the comment was posted to str message the body of the Comment Return: Comment??? """ prototype_edges = editor.prototype_edge_and_complement( API_EDGE_TYPE.COMMENTED_ON, {API_EDGE_PROPERTY.MESSAGE: message}, commenter_id, object_id) return editor.create_edges(prototype_edges)
def create_created(creator_id, object_id, message): """ Create a Created Edge and return it. Required: id creator_id the creator of that object id object_id the object that was created str message the message the creator attached Return: Created??? """ prototype_edges = editor.prototype_edge_and_complement( API_EDGE_TYPE.CREATED, {API_EDGE_PROPERTY.MESSAGE: message}, creator_id, object_id) return editor.create_edges(prototype_edges)
def join_league(person_id, league_id, tagger_id=None): """ Add a Person to a League. Required: id person_id ID of Person to add to League id league_id ID of League the Person is joining Optional: id tagger_id ID of Person who tagged/added/invited the joiner Return: bool success """ # TODO: for now, tagger_id is unused. fix this when we implement # tagging and/or invites. also, figure out whether we will need to # track the tagging/inviting User in addition to Person. for now, the # only call to this method is from create_player(), but there we don't # actually have access to the tagging/inviting Player. fix that too. return editor.create_edges(editor.prototype_edge_and_complement( API_EDGE_TYPE.IN_LEAGUE, {}, person_id, league_id))
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)