Example #1
0
def pay_prizes(sender, channel, roll, prizes, symbols):
    for index, symbol in enumerate(symbols):
        if set(roll) == set([symbol]):
            prize = prizes[index]
            message_queue.add(channel, "{}x3, won {} chips".format(symbol, prize))
            chips[sender] += prize
            return
Example #2
0
def play(sender, channel):
    global game_status, last_roll
    
    if game_status is not GrassStatus.ROLLING:
        message_queue.add(channel, "not your turn yet")
        return

    if sender != current_player:
        return

    roll = random.randint(1, 6)
    if roll in (1, 6):
        message_queue.add(channel, "{} rolls {} and puts one chip on the table".format(current_player, roll))
        put_chips(current_player, 1)
        if chips[current_player] <= 0:
            message_queue.add(channel, "{} is poor now lol".format(current_player))
            kick(current_player, channel)
        next_turn(channel)
    elif roll == 5:
        message_queue.add(channel, "{} rolls {} and takes one chip from the table".format(current_player, roll))
        take_chips(current_player, 1)
        next_turn(channel)
    elif roll in (2, 3, 4):
        message_queue.add(channel, "{} rolls {}, place a bet and roll again or pass".format(current_player, roll))
        game_status = GrassStatus.WAITING_FOR_BET
        last_roll = roll
Example #3
0
def buy_chips(sender, channel, amount):
    if bank.transfer_money(sender, config.nickname, amount * CHIP_VALUE):
        print(f"Slots: {sender} bought {amount} chips")
        message_queue.add(channel, f"{sender} bought {amount} chips")
    else:
        return

    chips[sender] += amount
Example #4
0
def buy_chips(sender, channel, amount):
    if bank.transfer_money(sender, config.nickname, amount * CHIP_VALUE):
        slots_logger.info(f"{sender} bought {amount} chips")
        message_queue.add(channel, f"{sender} bought {amount} chips")
    else:
        return

    chips[sender] += amount
Example #5
0
def pay_prizes(sender, channel, roll, prizes, symbols):
    for index, symbol in enumerate(symbols):
        if set(roll) == set([symbol]):
            prize = prizes[index]
            message_queue.add(channel,
                              "{}x3, won {} chips".format(symbol, prize))
            chips[sender] += prize
            return
Example #6
0
def cash_out(sender, channel):
    if chips[sender] == 0:
        message_queue.add(channel, "nothing to cash out")
        return

    current_chips = chips[sender]
    amount = current_chips * CHIP_VALUE
    bank.transfer_money(config.nickname, sender, amount)
    chips[sender] = 0
    print(f"Slots: {sender} cashed out {current_chips} chips, {amount:.2f} bux")
    message_queue.add(sender, f"you got {amount:.2f} newbux from slots")
Example #7
0
def add_player(sender, channel):
    if game_status is not GrassStatus.WAITING_FOR_PLAYERS:
        return

    if bank.transfer_money(sender, config.nickname, INITIAL_CHIPS * chip_value):
        message_queue.add(channel, "{} joins the game".format(sender))
    else:
        return

    chips.update({sender: INITIAL_CHIPS})
    turns.insert(counter, sender)
    put_chips(sender, INITIAL_CHIPS_ON_TABLE)
Example #8
0
def cash_out(sender, channel):
    if chips[sender] == 0:
        message_queue.add(channel, "nothing to cash out")
        return

    current_chips = chips[sender]
    amount = current_chips * CHIP_VALUE
    bank.transfer_money(config.nickname, sender, amount)
    chips[sender] = 0
    slots_logger.info(
        f"{sender} cashed out {current_chips} chips, {amount:.2f} bux")
    message_queue.add(sender, f"you got {amount:.2f} newbux from slots")
Example #9
0
def start(sender, channel):
    global game_status, turns
    
    if game_status is not GrassStatus.WAITING_FOR_PLAYERS:
        message_queue.add(channel, "create a game first")
        return
    
    if sender != owner:
        message_queue.add(channel, "wait for {} to start the game".format(owner))
        return

    game_status = GrassStatus.ROLLING
    random.shuffle(turns)
    next_turn(channel)
Example #10
0
def new_game(sender, channel, value):
    global game_status, chips, turns, counter, current_player, owner
    global chip_value, chips_on_table

    if game_status is not GrassStatus.FINISHED:
        message_queue.add(channel, "finish this game first before starting a new one")
        return
    
    game_status = GrassStatus.WAITING_FOR_PLAYERS
    chips = {}
    turns = []
    counter = 0
    current_player = ""
    chips_on_table = 0
    last_roll = 0
    owner = sender
    chip_value = value
    add_player(sender, channel)
Example #11
0
def next_turn(channel):
    global counter, current_player
    
    if len(turns) == 1:
        finish(channel)
        return

    if game_status == GrassStatus.FINISHED:
        return
    
    counter = (counter + 1) % len(turns) 
    current_player = turns[counter]
    print_chips(channel)

    if chips_on_table == 0:
        message_queue.add(channel, "no chips left")
        abort(channel)

    if current_player == config.nickname:
        bot_play(channel)
Example #12
0
def start(sender, channel, game, auto=False):
    if game == Games.EASY:
        reels, symbols, prizes, bet = EASY_REELS, EASY_SYMBOLS, EASY_PRIZES, EASY_BET
    elif game == Games.HARD:
        reels, symbols, prizes, bet = HARD_REELS, HARD_SYMBOLS, HARD_PRIZES, HARD_BET
    else:
        message_queue.add(channel, "that game doesn't exist")
        return

    current_chips = chips[sender]
    if chips[sender] < bet:
        message_queue.add(
            channel,
            "you only have {} chips, this game requires {} per play".format(
                current_chips, bet))
        return

    if sender in ongoing_games:
        message_queue.add(channel, "calm down")
        return

    if auto:
        player_thread = threading.Thread(target=auto_play,
                                         args=(sender, channel, reels, symbols,
                                               prizes, bet))
        ongoing_games.add(sender)
        player_thread.start()
    else:
        single_play(sender, channel, reels, symbols, prizes, bet)
Example #13
0
def abort(channel):
    global game_status
    
    if game_status == GrassStatus.FINISHED:
        message_queue.add(channel, "no games to cancel")
        return

    for player, current_chips in chips.items():
        amount = current_chips * chip_value
        bank.transfer_money(config.nickname, player, amount)
        message_queue.add(player, "you got {:.2f} newbux from grass".format(amount))
    
    message_queue.add(channel, "forced end of game")
    game_status = GrassStatus.FINISHED
Example #14
0
def transfer_money(source, destination, amount):
    if not database.user_exists(source) or not database.user_exists(destination):
        message_queue.add(source, "who that")
        return False

    if ask_money(source) < amount * (1 + TRANSFER_FEE_RATE):
        message_queue.add(source, "you're poor lol")
        return False

    real_amount = amount * 1000

    base = real_amount
    # Round to nearest multiple of 2 cents
    fee = math.ceil(real_amount * TRANSFER_FEE_RATE / 2) * 2
    database.take_money(source, base + fee)
    database.give_money(destination, base)
    database.give_money(config.nickname, fee)
    message_queue.add(source, f"sent {amount:.2f} newbux to {destination} ({fee / 1000:.2f} fee)")
    message_queue.add(destination, f"received {amount:.2f} newbux from {source} (sender paid a {fee / 1000:.2f} fee)")

    bank_logger.info(f"Transfer {source} -> {destination}: {amount:.2f} bux ({fee / 1000:.2f} fee)")
    return True
Example #15
0
def finish(channel):
    global game_status, chips, turns
    
    if game_status == GrassStatus.FINISHED:
        message_queue.add(channel, "no games to finish")
        return

    winner = turns[0]
    message_queue.add(channel, "{} takes all {} chips from the table".format(winner, chips_on_table))
    take_chips(winner, chips_on_table)
    bank.transfer_money(config.nickname, winner, chips[winner] * chip_value)
    chips = {}
    turns = []
    message_queue.add(channel, "end of game")
    game_status = GrassStatus.FINISHED
Example #16
0
def transfer_money(source, destination, amount):
    if not database.user_exists(source) or not database.user_exists(destination):
        message_queue.add(source, "who that")
        return False

    if ask_money(source) < amount * (1 + TRANSFER_FEE_RATE):
        message_queue.add(source, "you're poor lol")
        return False

    real_amount = amount * 1000

    base = real_amount
    # Round to nearest multiple of 2 cents
    fee = math.ceil(real_amount * TRANSFER_FEE_RATE / 2) * 2
    database.take_money(source, base + fee)
    database.give_money(destination, base)
    database.give_money(config.nickname, fee)
    message_queue.add(source, f"sent {amount:.2f} newbux to {destination} ({fee / 1000:.2f} fee)")
    message_queue.add(destination, f"received {amount:.2f} newbux from {source} (sender paid a {fee / 1000:.2f} fee)")

    timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M")
    print(f"{timestamp} Transfer {source} -> {destination}: {amount:.2f} bux ({fee / 1000:.2f} fee)")
    return True
Example #17
0
def islamic_gommunism(source, target, amount, channel, users):
    if channel not in users.keys() or target == source:
        message_queue.add(source, "smoke weed and protest against bankers")
        return False

    other_users = [user for user in users[channel] if user not in (target, config.nickname)]

    if not database.user_exists(source) or not database.user_exists(destination):
        message_queue.add(source, "who that")
        return False

    if not transfer_money(target, config.nickname, amount):
        message_queue.add(source, f"{target} isn't ready for the intifada yet")
        return False

    if not transfer_money(source, config.nickname, amount):
        # Do nothing and fail
        return False

    for user in other_users:
        transfer_money(config.nickname, user, amount / len(other_users))

    message_queue.add(channel, "alhamdulillah")
Example #18
0
def islamic_gommunism(source, target, amount, channel, users):
    if channel not in users.keys() or target == source:
        message_queue.add(source, "smoke weed and protest against bankers")
        return False

    other_users = [user for user in users[channel] if user not in (target, config.nickname)]

    if not database.user_exists(source) or not database.user_exists(target):
        message_queue.add(source, "who that")
        return False
    
    if not transfer_money(source, config.nickname, amount):
        # Do nothing and fail
        return False

    if not transfer_money(target, config.nickname, amount):
        message_queue.add(source, f"{target} isn't ready for the intifada yet")
        return False

    for user in other_users:
        transfer_money(config.nickname, user, amount / len(other_users))

    message_queue.add(channel, "alhamdulillah")
Example #19
0
def start(sender, channel, game, auto=False):
    if game == Games.EASY:
        reels, symbols, prizes, bet = EASY_REELS, EASY_SYMBOLS, EASY_PRIZES, EASY_BET
    elif game == Games.HARD:
        reels, symbols, prizes, bet = HARD_REELS, HARD_SYMBOLS, HARD_PRIZES, HARD_BET
    else:
        message_queue.add(channel, "that game doesn't exist")
        return

    current_chips = chips[sender]
    if chips[sender] < bet:
        message_queue.add(channel, "you only have {} chips, this game requires {} per play".format(current_chips, bet))
        return

    if sender in ongoing_games:
        message_queue.add(channel, "calm down")
        return
    
    if auto:
        player_thread = threading.Thread(target=auto_play, args=(sender, channel, reels, symbols, prizes, bet))
        ongoing_games.add(sender)
        player_thread.start()
    else:
        single_play(sender, channel, reels, symbols, prizes, bet)
Example #20
0
def stop(sender, channel):
    if sender in ongoing_games:
        ongoing_games.remove(sender)
        print(f"Slots: {sender} stopped auto-play with {chips[sender]} chips")
        message_queue.add(channel, "why'd you stop, gentile")
Example #21
0
def slot_roll(sender, channel, reels, symbols):
    chips_left = chips[sender]
    roll = [random.choices(symbols, reel_probs)[0] for reel_probs in reels]
    text_result = "[{}], {} chips left".format(" - ".join(roll), chips_left)
    message_queue.add(channel, text_result)
    return roll
Example #22
0
def stop(sender, channel):
    if sender in ongoing_games:
        ongoing_games.remove(sender)
        slots_logger.info(
            f"{sender} stopped auto-play with {chips[sender]} chips")
        message_queue.add(channel, "why'd you stop, gentile")
Example #23
0
def print_chips(channel):
    if game_status in (GrassStatus.ROLLING, GrassStatus.WAITING_FOR_BET):
        all_chips = ", ".join("{}: {} chips".format(name[:3], chips) for name, chips in chips.items())
        message_queue.add(channel, all_chips)
        message_queue.add(channel, "{} goes next, {} chips on the table".format(current_player, chips_on_table))
Example #24
0
def bet(sender, amount, channel):
    global game_status, last_roll
   
    amount = int(amount)
    if game_status != GrassStatus.WAITING_FOR_BET:
        message_queue.add(channel, "why are you betting")
        return

    if sender != current_player: 
        message_queue.add(channel, "not your turn to bet")
        return

    if amount > chips_on_table:
        message_queue.add(channel, "there are only {} chips on the table".format(chips_on_table))
        return

    if amount > chips[sender]:
        message_queue.add(channel, "you can't bet more than what you have")
        return

    roll = random.randrange(1, 6)
    if roll <= last_roll:
        message_queue.add(channel, "{} rolls {}, loses and puts {} chips on the table".format(current_player, roll, amount))
        put_chips(current_player, amount)
        if chips[current_player] <= 0:
            message_queue.add(channel, "{} rubbed hands way too hard".format(current_player))
            kick(current_player, channel)
    else:
        message_queue.add(channel, "{} rolls {}, wins and takes {} chips from the table".format(current_player, roll, amount))
        take_chips(current_player, amount)
    
    last_roll = 0
    game_status = GrassStatus.ROLLING
    next_turn(channel)        
Example #25
0
def run_command(message_data):
    sender = message_data["sender"]
    full_text = message_data["full_text"]
    # "#channel" if room, "sender" if private message
    channel = message_data["channel"]
    command = message_data["command"]
    args = message_data["args"]

    def cringe_words(message):
        for w in ["xd", "spic", "cring", "derail"]:
            if w in "".join(c for c in "".join(message.lower().split())
                            if c.isalpha()):
                return True
        return False

    if sender == "eyy" and cringe_words(full_text):
        execute("KICK #bogota eyy ebin")

    # Refuse to do anything if not well fed by the users
    """
    if not food.enough(sender):
        message_queue.add("nah")
        return
    """

    # Reply to mention with a random quote
    if config.nickname in full_text and sender != config.nickname:
        message_queue.add(channel, random_quote(sender))

    # Give bux to users
    bank.make_money(sender, full_text)

    if not command:
        return

    elif command == "help":
        message_queue.add(
            sender,
            "Grass: grass-new (chip-value), grass-start, grass-join, gr, gb (amount), gp, gs, grass-cancel"
        )
        message_queue.add(
            sender,
            "Slots: slot-chips (amount), easy-slot <auto>, hard-slot <auto>, slot-stop, slot-cashout"
        )
        message_queue.add(
            sender,
            "Misc: pick [list], roll (number), ask (query), fetch (wikipedia_article), calc (expression), bux, sendbux (user) (amount), sharia (target) (amount)"
        )

    # Random choice
    elif command == "pick":
        if len(args) > 1:
            message_queue.add(channel, random.choice(args))

    # Die roll
    elif command == "roll":
        if not args:
            max_roll = 6
        elif len(args) == 1 and args[0].isdigit():
            max_roll = int(args[0])

        message_queue.add(channel, random.randint(1, max_roll))

    # Wolfram Alpha query
    elif command == "ask":
        response = wolfram.ask(" ".join(args))
        message_queue.add(channel, response)

    # Calculator
    elif command == "calc":
        expression = ''.join(args)
        result = str(calculate(expression))
        message_queue.add(channel, result)

    # Wikipedia fetch
    elif command == "fetch":
        article_name = ' '.join(args)
        extract = wikipedia.fetch(article_name)
        message_queue.add(channel, extract)

    # Check balance
    elif command == "bux":
        amount = bank.ask_money(sender)
        message_queue.add(channel,
                          "{} has {:.2f} newbux".format(sender, amount))

    # Transfer money
    elif command == "sendbux":
        if len(args) != 2:
            message_queue.add(channel, "eh")
            return
        source, destination, amount = sender, args[0], args[1]
        if not is_number(amount):
            message_queue.add(source, "numbers please")
            return
        bank.transfer_money(source, destination, float(amount))

    # Redistribute wealth
    elif command == "sharia":
        if len(args) != 2:
            message_queue.add(channel, "eh")
            return
        source, target, amount = sender, args[0], args[1]
        if not is_number(amount):
            message_queue.add(source, "numbers please")
            return
        bank.islamic_gommunism(source, target, float(amount), channel, users)

    # Grass game
    elif command == "grass-new":
        if len(args) < 1:
            message_queue.add(channel, "how much for each chip")
            return
        chip_value = args[0]
        if not is_number(chip_value):
            message_queue.add(source, "numbers please")
            return
        grass.new_game(sender, channel, float(chip_value))

    elif command == "grass-join":
        grass.add_player(sender, channel)

    elif command == "grass-start":
        grass.start(sender, channel)

    elif command == "gr":
        grass.play(sender, channel)

    elif command == "gb":
        if len(args) < 1:
            message_queue.add(channel, "how much are you betting")
            return
        bet = args[0]
        if not is_number(bet):
            message_queue.add(channel, "numbers please")
            return
        grass.bet(sender, bet, channel)

    elif command == "gp":
        grass.pass_turn(sender, channel)

    elif command == "gs":
        grass.print_chips(channel)

    elif command == "grass-cancel":
        grass.abort(channel)

    # Slot machine
    elif command == "slot-chips":
        if len(args) < 1:
            message_queue.add(channel, "how many are you buying")
            return
        amount = args[0]
        if not is_number(amount):
            message_queue.add(channel, "numbers please")
            return
        slots.buy_chips(sender, channel, int(amount))

    elif command == "easy-slot":
        auto = False
        if len(args) == 1 and args[0] == "auto":
            auto = True
        slots.start(sender, channel, slots.Games.EASY, auto=auto)

    elif command == "hard-slot":
        auto = False
        if len(args) == 1 and args[0] == "auto":
            auto = True
        slots.start(sender, channel, slots.Games.HARD, auto=auto)

    elif command == "slot-stop":
        slots.stop(sender, channel)

    elif command == "slot-cashout":
        slots.cash_out(sender, channel)

    ## Owner commands ##
    if sender == config.owner:
        # Disconnect
        if command == "quit":
            execute("QUIT")
            sys.exit(0)

        # Send message from bot
        elif command == "say":
            if len(args) > 1:
                message = ' '.join(args[1:])
                message_queue.add(args[0], message)

        # Print userlist
        elif command == "users":
            message_queue.add(channel, str(users))

        # Bot joins
        elif command == "join":
            channel = args[0]
            execute("JOIN %s" % channel)

        # Bot parts
        elif command == "part":
            execute("PART %s" % channel)
            del users[channel]

        # Bot kicks
        elif command == "kick":
            user = args[0]
            reason = " ".join(args[1:])
            if not reason:
                reason = "huh"
            bot_kick(channel, user, reason)

        # Module reloads
        elif command == "reload":
            module_name = args[0]
            importlib.reload(sys.modules[module_name])
            message_queue.add(channel, "aight")
Example #26
0
def slot_roll(sender, channel, reels, symbols):
    chips_left = chips[sender]
    roll = [random.choices(symbols, reel_probs)[0] for reel_probs in reels]
    text_result = "[{}], {} chips left".format(" - ".join(roll), chips_left)
    message_queue.add(channel, text_result)
    return roll