示例#1
0
def eat_2_target(bot, update):
    """
    :type bot: telegram.bot.Bot
    :type update: telegram.update.Update
    """
    current_user = update.callback_query.from_user
    data = json.loads(update.callback_query.data)
    session = get_db_session()
    event_id = data['e']
    event = DB.get_event(session, event_id=event_id)

    if event and not event.completed and current_user.username == event.created_by:
        target_id = data['t']
        update_dict = json.loads(event.description)
        update_dict['loser'] = target_id
        DB.update_event(session, event_id, {
            'description': json.dumps(update_dict),
            'completed': 1
        })

        # Create Transaction
        DB.create_transaction(session,
                              event_id,
                              from_id=update_dict['loser'],
                              to_id=update_dict['winner_1'],
                              fan_no=update_dict['fan_1'],
                              amount=update_dict['amount_1'])
        DB.create_transaction(session,
                              event_id,
                              from_id=update_dict['loser'],
                              to_id=update_dict['winner_2'],
                              fan_no=update_dict['fan_2'],
                              amount=update_dict['amount_2'])

        # Send Message to Group
        win_player_1 = DB.get_player(session, update_dict['winner_1'])
        win_player_2 = DB.get_player(session, update_dict['winner_2'])
        target_player = DB.get_player(session, update_dict['loser'])

        text = random.choice(String.EAT_2_MESSAGES).format(
            loser_first=target_player.first_name,
            loser_last=target_player.last_name,
            winner_1_first=win_player_1.first_name,
            winner_1_last=win_player_1.last_name,
            winner_2_first=win_player_2.first_name,
            winner_2_last=win_player_2.last_name,
            fan_1=update_dict['fan_1'],
            fan_2=update_dict['fan_2'])

        message = update.callback_query.message
        bot.editMessageText(timeout=5,
                            text=text,
                            chat_id=message.chat_id,
                            message_id=message.message_id)

        # Advance Game
        advance_game_status(
            bot,
            game_id=event.game_id,
            winner=[update_dict['winner_1'], update_dict['winner_2']])
示例#2
0
def eat_fan(bot, update):
    """
    :type bot: telegram.bot.Bot
    :type update: telegram.update.Update
    """
    current_user = update.callback_query.from_user
    data = json.loads(update.callback_query.data)
    session = get_db_session()
    event_id = data['e']
    event = DB.get_event(session, event_id=event_id)

    if event and not event.completed and current_user.username == event.created_by:
        fan = data['f']
        amount = PRICE_LIST.get(event.game.price).get(int(fan)) * 1.5

        update_dict = json.loads(event.description)
        update_dict['fan'] = int(fan)
        update_dict['amount'] = int(amount * 1.5)

        DB.update_event(session, event_id, {
            'description': json.dumps(update_dict),
            'completed': 1
        })

        # Create Transaction
        DB.create_transaction(session,
                              event_id=event_id,
                              from_id=update_dict['loser'],
                              to_id=update_dict['winner'],
                              fan_no=update_dict['fan'],
                              amount=amount,
                              self_touch=1)

        # Send Message to Group
        from_player = DB.get_player(session, update_dict['loser'])
        to_player = DB.get_player(session, update_dict['winner'])

        text = random.choice(String.WRAP_TOUCH_MESSAGES).format(
            winner_first=to_player.first_name,
            winner_last=to_player.last_name,
            loser_first=from_player.first_name,
            loser_last=from_player.last_name,
            fan=fan,
            amount=amount)

        message = update.callback_query.message
        bot.editMessageText(timeout=5,
                            text=text,
                            chat_id=message.chat_id,
                            message_id=message.message_id)

        # Advance Game
        advance_game_status(bot,
                            game_id=event.game_id,
                            winner=update_dict['winner'])
示例#3
0
def draw(bot, update):
    """
    :type bot: telegram.bot.Bot
    :type update: telegram.update.Update
    """
    user = update.message.from_user

    chat = update.message.chat

    if chat.type == Chat.PRIVATE:
        bot.sendMessage(update.message.chat_id,
                        String.ERROR_PRIVATE_CHAT,
                        timeout=5)
    else:
        session = get_db_session()

        game = DB.get_player_current_game(session,
                                          user,
                                          status=GameStatus.STARTED)

        if game:
            DB.create_event(session,
                            game_id=game.id,
                            message_id=update.message.message_id,
                            type=EventType.DRAW,
                            created_by=user.username,
                            completed=1)

            # Send Message to Chat Room
            bot.sendMessage(chat_id=game.chat_id,
                            text=random.choice(String.DRAW_MESSAGES),
                            timeout=5)

            advance_game_status(bot, game_id=game.id)
        else:
            with open('assets/eatshit.jpg', 'rb') as f:
                update.message.reply_photo(
                    photo=f, caption=String.ERROR_NO_GAME_EAT.encode('utf8'))
示例#4
0
def handle_eat_3_select_fan_callback(bot, update, index):
    """
    :type bot: telegram.bot.Bot
    :type update: telegram.update.Update
    :type index: int
    """
    current_user = update.callback_query.from_user
    data = json.loads(update.callback_query.data)
    session = get_db_session()
    event_id = data['e']
    event = DB.get_event(session, event_id=event_id)

    if event and not event.completed and current_user.username == event.created_by:
        fan = data['f']
        amount = PRICE_LIST.get(event.game.price).get(int(fan))
        update_dict = json.loads(event.description)
        update_dict['fan_{0}'.format(index)] = int(fan)
        update_dict['amount_{0}'.format(index)] = amount
        DB.update_event(session, event_id, {'description': json.dumps(update_dict)})

        if index != 3:
            # Ask Next Winner
            send_3eat_fan_select_keyboard(bot=bot,
                                          session=session,
                                          winner_id=update_dict['winner_{0}'.format(index + 1)],
                                          event=event,
                                          message=update.callback_query.message,
                                          index=index + 1)
        else:
            DB.update_event(session, event_id, {'completed': 1})

            # Create Transactions
            DB.create_transaction(session, event_id,
                                  from_id=update_dict['loser'],
                                  to_id=update_dict['winner_1'],
                                  fan_no=update_dict['fan_1'],
                                  amount=update_dict['amount_1'])
            DB.create_transaction(session, event_id,
                                  from_id=update_dict['loser'],
                                  to_id=update_dict['winner_2'],
                                  fan_no=update_dict['fan_2'],
                                  amount=update_dict['amount_2'])
            DB.create_transaction(session, event_id,
                                  from_id=update_dict['loser'],
                                  to_id=update_dict['winner_3'],
                                  fan_no=update_dict['fan_3'],
                                  amount=update_dict['amount_3'])

            # Send Message to Group
            game = DB.get_game(session, event.game_id)
            loser = DB.get_player(session, update_dict['loser'])
            winner_1 = DB.get_player(session, update_dict['winner_1'])
            winner_2 = DB.get_player(session, update_dict['winner_2'])
            winner_3 = DB.get_player(session, update_dict['winner_3'])

            text = random.choice(String.EAT_3_MESSAGES).format(
                loser_first=loser.first_name,
                loser_last=loser.last_name,
                winner_1_first=winner_1.first_name,
                winner_1_last=winner_1.last_name,
                winner_2_first=winner_2.first_name,
                winner_2_last=winner_2.last_name,
                winner_3_first=winner_3.first_name,
                winner_3_last=winner_3.last_name,
                fan_1=update_dict['fan_1'],
                fan_2=update_dict['fan_2'],
                fan_3=update_dict['fan_3']
            )

            message = update.callback_query.message
            bot.editMessageText(timeout=5, text=text,
                                chat_id=message.chat_id,
                                message_id=message.message_id)

            # Advance Game
            advance_game_status(bot, game_id=game.id,
                                winner=[update_dict['winner_1'], update_dict['winner_2'], update_dict['winner_3']])