示例#1
0
def send_turn(phone):
    ''' manually send a turn '''
    turn_data = request.get_json()
    try:
        player = models.find_player(phone)
    except NoResultFound:
        return failure('no player found with that phone number')

    player = player.set_contacted(True)

    # send a turn
    queue = []
    for text in turn_data['text']:
        logging.info('sending message: %s', text)
        sms = TWILIO.send(text, '+%s' % player.phone)
        queue.append(sms)
        time.sleep(DELAY)

        models.add_message(player, turn_data, sms)

    if 'options' in turn_data and len(turn_data['options']):
        options_text = GAME.format_options(turn_data, player.name)
        logging.info('sending message: %s', options_text)
        sms = TWILIO.send(options_text, '+%s' % player.phone)
        queue.append(sms)
        time.sleep(DELAY)

        models.add_message(player, turn_data, sms)

    return success(queue)
示例#2
0
def respond():
    ''' receives a reply from twilio '''
    sms = request.values.to_dict()
    phone = sms['From'].replace('+', '')

    try:
        player = models.find_player(phone)
    except NoResultFound:
        # contact from an unknown number
        player = models.add_player(None, phone)

        turn_data = {'text': ['Pardon me, but what name do you go by?'], 'uid': None}
        player.set_pending_turn(turn_data)
        return success(player.pending_turn)
    else:
        player.set_show(True)

        # TODO: this is maybe the wrong way to get this
        previous_turn = player.pending_turn

        turn = GAME.process_response(previous_turn, sms['Body'], player.name)
        player.set_pending_turn(turn)

        models.add_message(player, {'text': sms['Body']}, sms, incoming=True)

        return success(turn)
示例#3
0
def update_player(phone):
    ''' show or hide a game '''
    data = request.get_json()
    player = models.find_player(phone)

    if 'name' in data:
        player.set_name(data['name'])
    if 'show' in data:
        player.set_show(data['show'])
    if 'notes' in data:
        player.set_notes(data['notes'])
    return success(player.serialize())
示例#4
0
def set_current_turn(uid, phone):
    ''' shift the current pending turn to a given uid '''
    player = models.find_player(phone)
    turn = GAME.get_turn(uid, player.name)
    player.set_pending_turn(turn)
    return json.dumps(turn)
示例#5
0
def get_history(phone):
    ''' gets the history of a single game '''
    player = models.find_player(phone)
    return success(player.serialize())