Inspiration
What it does
It allows developers of game bots to capture the players' information, create a room for the game and simulate the action of drawing cards from a deck.
How I built it
Built using telegram bot API for python
What's next for BossBot
May be developed to include more features for game creation.
source code
import time import random import json import telepot from pprint import pprint from telepot.loop import MessageLoop from telepot.delegate import per_chat_id, create_open, pave_event_space from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
join_cancel = ([('join', 'join'), ('cancel', 'cancel')])
whole_deck = ['stop', 'stop', 'stop', 'airplane', 'airplane', 'airplane', 'D_cousin', 'C_cousin', 'B_cousin', 'A_cousin'] all_capitalists = ['Ann', 'Bob', 'Calvin', 'David']
'''all_players = {"players": [{'chat_id': "37206895", 'name': "Zhengnan An", 'hand': [], 'wealth': 0, 'capitalist': None},
{'chat_id': "17506668",
'name': "Boss Yong",
'hand': [],
'wealth': 0,
'capitalist': None},
]}'''
all_transactions = [{'number_of_profits': 3, 'necessary_caps': ['Ann'], 'other_caps': ['Bob', 'Calvin', 'David'], 'min_other_caps_required': 1, 'Done': False},]
list = [300, 300, 300, 400, 400, 400, 500, 500, 500]
def draw_from_deck(player, number): """ The action of drawing a card. :param player: a dict representing a player :param number: an int representing number of cards drawn :return: the dict has been edited to represent the player who has drawn n cards """ global whole_deck
for i in range(0, number):
# this block represents drawing 1 card
# it will be executed n times
card_drawn = random.sample(whole_deck, 1)[0]
player['hand'].append(card_drawn)
whole_deck.remove(card_drawn)
def distribute_card_0(initial_players): """ This func is called when all players are ready. It assigns each player 1 capitalist, 2 cards in hand, and 0 wealth. :param initial_dict: the dict of players that has been generated when all players are ready :return: the edited dict, the players have all got their initial cards. """ global all_capitalists
for player in initial_players['players']:
# assign capitalist
cap_assigned = random.sample(all_capitalists, 1)[0]
player['capitalist'] = cap_assigned
all_capitalists.remove(cap_assigned)
print(all_capitalists)
# give each player 2 cards
draw_from_deck(player, 2)
class BossBot(telepot.helper.ChatHandler):
def __init__(self, *args, **kwargs):
super(BossBot, self).__init__(include_callback_query=True, *args, **kwargs)
self.game = {'players': []}
self.indicator = 'start'
self.location = random.randint(2, 12)
def send_custom_inline_keyboard(self, chat_id, text_info, send_tuple_list):
kb = []
for t in send_tuple_list:
kb.append([InlineKeyboardButton(text=t[0], callback_data=t[1])])
mark_up = InlineKeyboardMarkup(inline_keyboard=kb)
bot.sendMessage(chat_id, text=text_info, reply_markup=mark_up, parse_mode='Markdown')
def open(self, initial_msg, seed):
content_type, chat_type, chat_id = telepot.glance(initial_msg)
if chat_type == 'group' and initial_msg['text'] == "/start":
new_room_text = initial_msg['from']['first_name'] + ' has opened a new room.\n'
self.send_custom_inline_keyboard(chat_id, new_room_text, join_cancel)
self.game['group_id'] = chat_id
print(self.game)
self.indicator = 'joining'
with open('Gamedata.json', 'w') as openjson:
json.dump(self.game, openjson)
if chat_type == 'private':
pass
return True
def on_callback_query(self, msg):
query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
# Close inline keyboard
inline_message_id = msg['message']['chat']['id'], msg['message']['message_id']
bot.editMessageReplyMarkup(inline_message_id, reply_markup=None)
if self.indicator == 'joining':
joining_text = 'Players: \n'
if query_data == 'join':
for player in self.game['players']:
if from_id == player['chat_id']:
break
else:
# check whether the player has already joined
self.game['players'].append({'name': msg['from']['first_name'], 'property': 0, 'chat_id': from_id, 'hand': []})
for player in self.game['players']:
joining_text += player['name'] + '\n'
self.send_custom_inline_keyboard(self.game['group_id'], joining_text, join_cancel)
pprint(self.game['players'])
elif query_data == 'cancel':
joining_text = 'Players: \n'
for player in self.game['players']:
if from_id == player['chat_id']:
self.game['players'].remove(player)
for player in self.game['players']:
joining_text += player['name'] + '\n'
self.send_custom_inline_keyboard(self.game['group_id'], joining_text, join_cancel)
if len(self.game['players']) >=2:
distribute_card_0(self.game)
pprint(self.game['players'])
TOKEN = '380690112:AAE_JUJ7D6jAjQsEjcSMr7FGERjfjdD3kp0'
bot = telepot.DelegatorBot(TOKEN, [ pave_event_space()( per_chat_id(), create_open, BossBot, timeout=3600000), ]) MessageLoop(bot).run_as_thread() print('Listening ...')
while 1: time.sleep(200)
Log in or sign up for Devpost to join the conversation.