def global_eventloc(loc, all): """Send an event message to all players in the given location. (Location or key, or the entire realm.) """ ctx = EvalPropContext.get_current_context() iid = ctx.loctx.iid if ctx.level != LEVEL_EXECUTE: raise Exception('Events may only occur in action code') if isinstance(loc, two.execute.RealmProxy): locid = None elif isinstance(loc, two.execute.LocationProxy): res = yield motor.Op(ctx.app.mongodb.locations.find_one, {'_id':loc.locid, 'wid':ctx.loctx.wid}, {'_id':1}) if not res: raise KeyError('No such location') locid = loc.locid else: res = yield motor.Op(ctx.app.mongodb.locations.find_one, {'key':loc, 'wid':ctx.loctx.wid}, {'_id':1}) if not res: raise KeyError('No such location: %s' % (loc,)) locid = res['_id'] if is_typed_dict(all, 'text'): all = all.get('text', None) subctx = EvalPropContext(ctx.task, parent=ctx, level=LEVEL_MESSAGE) val = yield subctx.eval(all, evaltype=EVALTYPE_TEXT) else: val = str(all) others = yield ctx.task.find_location_players(iid, locid) ctx.task.write_event(others, val)
def global_move(dest, you=None, oleave=None, oarrive=None): """Move the player to another location in the same world, like {move}. The first argument must be a location or location key. The rest of the arguments must be string or {text}; they are messages displayed for the player and other players. """ ctx = EvalPropContext.get_current_context() if isinstance(dest, two.execute.LocationProxy): res = yield motor.Op(ctx.app.mongodb.locations.find_one, {'_id':dest.locid, 'wid':ctx.loctx.wid}, {'_id':1}) if not res: raise KeyError('No such location') locid = dest.locid else: res = yield motor.Op(ctx.app.mongodb.locations.find_one, {'key':dest, 'wid':ctx.loctx.wid}, {'_id':1}) if not res: raise KeyError('No such location: %s' % (dest,)) locid = res['_id'] youeval = False oleaveeval = False oarriveeval = False if you: if is_typed_dict(you, 'text'): you = you.get('text', None) youeval = True else: you = str(you) if oleave: if is_typed_dict(oleave, 'text'): oleave = oleave.get('text', None) oleaveeval = True else: oleave = str(oleave) if oarrive: if is_typed_dict(oarrive, 'text'): oarrive = oarrive.get('text', None) oarriveeval = True else: oarrive = str(oarrive) yield ctx.perform_move(locid, you, youeval, oleave, oleaveeval, oarrive, oarriveeval)
def global_event(you, others=None, player=None): """Send an event message to the current player, like {event}. The argument(s) must be string or {text}. The optional second argument goes to other players in the same location. If the player argument is provided, the event refers to that player instead -- but they must be in-world. """ ctx = EvalPropContext.get_current_context() if ctx.level != LEVEL_EXECUTE: raise Exception('Events may only occur in action code') youeval = False otherseval = False if you: if is_typed_dict(you, 'text'): you = you.get('text', None) youeval = True else: you = str(you) if others: if is_typed_dict(others, 'text'): others = others.get('text', None) otherseval = True else: others = str(others) if player is None: yield ctx.perform_event(you, youeval, others, otherseval) elif isinstance(player, two.execute.PlayerProxy): res = yield motor.Op(ctx.app.mongodb.playstate.find_one, {'_id':player.uid}, {'iid':1}) if not res: raise KeyError('No such player') if res['iid'] != ctx.loctx.iid: raise Exception('Player is not in this instance') yield ctx.perform_event_player(player.uid, you, youeval, others, otherseval) else: raise TypeError('event: must be player or None')