def send_starts(chat_id): game_id = get_game_id(chat_id, storage) storage[game_id]['kill'] = dict() storage[game_id]['keyboard_ids'] = dict() storage[game_id]['vote'] = dict() for chat_id in storage[game_id]['info']: if maf(chat_id, storage): role = 'maf' if com(chat_id, storage): role = 'com' if doc(chat_id, storage): role = 'doc' if cits(chat_id, storage): role = 'citizen' sent_data = send_message( chat_id, u'игроков %s, мафов %s, комиссар %s, доктор %s, горожане %s, продолжительность раунда %s, продолжительность лечения доктора %s, мафы знакомы %s, словесная подсказка мафам %s, инфа об успешном лечении %s.\n\nВаша роль: %s' % (len(storage[game_id]['info']), storage[game_id]['mafs_count'], storage[game_id]['com_count'], storage[game_id]['doc_count'], storage[game_id]['cits_count'], storage[game_id]['roundtime'], storage[game_id]['curetime'], storage[game_id]['mafs_known'], storage[game_id]['mafs_prompt'], storage[game_id]['cured_known'], role.upper()), reply_markup=create_keyboard(chat_id)) # sent_data = send_message(chat_id, 'aaa', reply_markup=create_keyboard(chat_id)) storage[game_id]['keyboard_ids'][chat_id] = sent_data['message_id'] if storage[game_id]['mafs_known']: mafs_acquaintance(game_id, send_message, storage) # znakomstvo mafov
def reply(chat_id): game_id = get_game_id(chat_id, storage) save_vars(dumpfilename, storage) if 'alive' not in storage[game_id]: return if chat_id not in storage[game_id]['alive']: return logger_on_button.info('Killed?') if check_mafs_murder(game_id): logger_on_button.info('Yes, edit keyboards') edit_keyboards(game_id) logger_on_button.info('No') comissaire_check(game_id) logger_on_button.info('Killed in vote?') if check_votes(game_id): logger_on_button.info('Yes, edit keyboards') try: edit_keyboards(game_id) except: pass logger_on_button.info('No') logger_on_button.info('Checking alive') check_alive(game_id) logger_on_button.info('OK')
def on_callback_query(msg): query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query') chat_id = msg['message']['chat']['id'] # if chat_id not in storage: # null(chat_id, 'dict', 'started', 'delay', 'info') game_id = get_game_id(chat_id, storage) logger_on_button.info('button pressed, query data = %s' % query_data) thread_parse = Thread(target=parse_query, args=( chat_id, query_data, )) thread_parse.start()
def create_keyboard(chat_id): game_id = get_game_id(chat_id, storage) buttons = list() buttons.append([ InlineKeyboardButton(text='DELAY: ', callback_data='ignore'), InlineKeyboardButton(text='0 sec', callback_data='0sec'), InlineKeyboardButton(text='3 sec', callback_data='3sec'), InlineKeyboardButton(text='6 sec', callback_data='6sec'), InlineKeyboardButton(text='9 sec', callback_data='9sec') ]) buttons.append([InlineKeyboardButton(text='-', callback_data='ignore')]) buttons.append( [InlineKeyboardButton(text='VOTE:', callback_data='ignore')]) if chat_id in storage[game_id]['alive']: for candidate_chat_id, name in storage[game_id]['info'].items(): if candidate_chat_id in storage[game_id]['alive']: buttons.append([ InlineKeyboardButton(text=name, callback_data='vote %s' % candidate_chat_id) ]) action = False if maf(chat_id, storage): action = 'kill' elif com(chat_id, storage): action = 'check' elif doc(chat_id, storage): action = 'cure' if action: buttons.append( [InlineKeyboardButton(text='-', callback_data='ignore')]) buttons.append([ InlineKeyboardButton(text='%s:' % action.upper(), callback_data='ignore') ]) for candidate_chat_id, name in storage[game_id]['info'].items(): if candidate_chat_id in storage[game_id]['alive']: buttons.append([ InlineKeyboardButton(text=name, callback_data='%s %s' % (action, candidate_chat_id)) ]) return InlineKeyboardMarkup(inline_keyboard=buttons)
def set_roles(chat_id): game_id = get_game_id(chat_id, storage) mafs_count = storage[game_id]['mafs_count'] com_count = storage[game_id]['com_count'] doc_count = storage[game_id]['doc_count'] cits_count = storage[game_id]['cits_count'] if mafs_count + com_count + doc_count + cits_count != len( storage[game_id]['info']): send_message(chat_id, "len(info') != roles_count") return False storage[game_id]['mafs'] = list() storage[game_id]['cits'] = list() storage[game_id]['goods'] = list() storage[game_id]['mafs'] = random.sample( set(storage[game_id]['info'].keys()), mafs_count) storage[game_id]['com'] = random.sample( set(storage[game_id]['info'].keys()) - set(storage[game_id]['mafs']), com_count) storage[game_id]['doc'] = random.sample( set(storage[game_id]['info'].keys()) - set(storage[game_id]['mafs']) - set(storage[game_id]['com']), doc_count) storage[game_id]['cits'] = list( set(storage[game_id]['info'].keys()) - set(storage[game_id]['mafs']) - set(storage[game_id]['com']) - set(storage[game_id]['doc'])) storage[game_id]['goods'] = list( set(storage[game_id]['info'].keys()) - set(storage[game_id]['mafs'])) if storage[game_id]['com'] == []: storage[game_id]['com'] = None else: storage[game_id]['com'] = storage[game_id]['com'][0] if storage[game_id]['doc'] == []: storage[game_id]['doc'] = None else: storage[game_id]['doc'] = storage[game_id]['doc'][0] storage[game_id]['alive'] = list() for chat_id in storage[game_id]['info']: storage[game_id]['alive'].append(chat_id) return True
def startgame(chat_id): game_id = get_game_id(chat_id, storage) if game_id is None: send_message(chat_id, 'create game first') # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! return logger_on_chat.info('setting roles') if not set_roles(chat_id): return storage[game_id]['can_kill'] = True storage[game_id]['can_check'] = True storage[game_id]['check'] = None storage[game_id]['can_cure'] = True storage[game_id]['cure'] = None storage[game_id]['can_vote'] = True storage[game_id]['started'] = True send_starts(game_id) thread_time = Thread(target=timer, args=(game_id, )) thread_time.start()
def on_chat_message(msg): save_vars(dumpfilename, storage) content_type, chat_type, chat_id = telepot.glance(msg) game_id = get_game_id(chat_id, storage) # if chat_id not in storage: # null(chat_id, 'dict', 'started', 'delay', 'info') save_vars(dumpfilename, storage) if not content_type == 'text': return text = msg['text'].lower() logger_on_chat.info('message recieved, text = %s' % text) if text == '/start': send_message(chat_id, startmessage) elif text == '/help': send_message(chat_id, helpmessage) elif text == 'helpcreate' or text == '/helpcreate': send_message(chat_id, help_create_message) elif text[:6] == 'create' or text[:7] == '/create': create_game(chat_id, game_id) return elif text[:7] == 'connect' or text[:8] == '/connect': connect(chat_id, text) return elif text[: 4] == 'kick' or text[: 5] == '/kick' and chat_id in storage and game_id is not None: text = text.split() del storage[game_id]['info'][int(text[1])] return # elif text[:4] == 'name' or text[:5] == '/name': # changename(text, game_id, chat_id, storage) elif text[:4] == 'name': logger_on_chat.info('Name command passed') if game_id is not None: storage[game_id]['info'][chat_id] = text.split()[1] return elif text == 'go' or text == '/go': logger_on_chat.info('startgame launched') startgame(chat_id) return elif text == 'end' or text == '/end' and chat_id in storage: storage[game_id]['started'] = False # elif text == 'st': # print_storage(game_id, storage) elif text == 'st': pprint(storage) return elif text == 'sto': send_message(chat_id, str(storage)) return if game_id is None: send_message( chat_id, u'нет активной игры. создайте свою или присоединитесь к чужой') return if not storage[game_id]['started']: send_message(chat_id, u'игра создана, но не начата') return logger_on_chat.info('Creating keyboard') send_message(chat_id, '.', reply_markup=create_keyboard(chat_id)) with open(dumpfilename, 'w') as dumpfile: pickle.dump(storage, dumpfile)
def name(chat_id, text): game_id = get_game_id(chat_id, storage) storage[game_id]['info'][chat_id] = text
def parse_query(chat_id, query_data): if query_data == 'ignore': return game_id = get_game_id(chat_id, storage) if query_data[-3:] == 'sec': storage[game_id]['delay'][chat_id] = int(query_data[0]) else: sleep(storage[game_id]['delay'][chat_id]) query_data = query_data.split() logger_on_button.debug('query data = %s' % query_data) key = query_data[0] value = query_data[1] if value == 'true': value = True elif value == 'false': value = False if key == 'maf': storage[game_id]['mafs_count'] = int(value) elif key == 'com': storage[game_id]['com_count'] = int(value) elif key == 'doc': storage[game_id]['doc_count'] = int(value) elif key == 'cits': storage[game_id]['cits_count'] = int(value) elif key == 'roundtime': storage[game_id]['roundtime'] = float(value) elif key == 'curetime': storage[game_id]['curetime'] = float(value) elif key == 'mafs_known': storage[game_id]['mafs_known'] = value elif key == 'mafs_prompt': storage[game_id]['mafs_prompt'] = value elif key == 'cured_known': storage[game_id]['cured_known'] = value if 'alive' not in storage[game_id]: return if chat_id not in storage[game_id][ 'alive'] or not storage[game_id]['started']: return if key == 'vote' and storage[game_id]['can_vote']: storage[game_id]['vote'][chat_id] = int(value) send_message( chat_id, 'OK, voting %s' % get_name(game_id, int(value), storage)) elif key == 'kill' and storage[game_id]['can_kill']: storage[game_id]['kill'][chat_id] = int(value) send_message( chat_id, 'OK, trying to kill %s' % get_name(game_id, int(value), storage)) elif key == 'check' and storage[game_id]['can_check']: storage[game_id]['check'] = int(value) elif key == 'cure' and storage[game_id]['can_cure']: storage[game_id]['cure'] = int(value) send_message( chat_id, 'OK, curing %s' % get_name(game_id, int(value), storage)) elif key == 'check' and not storage[game_id]['can_vote']: send_message(chat_id, "u can't vote") elif key == 'check' and not storage[game_id]['can_kill']: send_message(chat_id, "u can't kill") elif key == 'check' and not storage[game_id]['can_check']: send_message(chat_id, "u can't check") elif key == 'check' and not storage[game_id]['can_cure']: send_message(chat_id, "u can't cure") reply(chat_id)