def test_db(self): hat, game = start_game(self.db_file) self.assertIsNone(game.room_for_player(1)) game.add_player(1, "room1") game.add_player(3, "room2") game.add_player(2, "room1") game.add_player(4, "room2") self.assertEqual(game.room_size("room1"), 2) self.assertEqual(game.room_size("room2"), 2) self.assertEqual(game.room_for_player(2), "room1") self.assertIsNone(hat.get_word("room1")) self.assertTrue(hat.add_word("первое", 1, "room1")) self.assertTrue(hat.add_word("second", 1, "room1")) self.assertEqual(hat.words_in_hat("room1"), 2) self.assertTrue(hat.add_word("треТье", 1, "room1")) self.assertFalse(hat.remove_word("кусь", "room1")) self.assertTrue(hat.add_word("чеТвертое", 1, "room1")) self.assertTrue(hat.add_word("пятое", 1, "room1")) self.assertTrue(hat.remove_word("пятое", "room1")) self.assertFalse(hat.add_word("qcь", 1, "room1")) self.assertFalse(hat.add_word("", 1, "room1")) self.assertFalse(hat.add_word("первое", 1, "room1")) self.assertTrue(hat.get_word("room1")) self.assertTrue(hat.get_word("room1")) self.assertTrue(hat.get_word("room1")) self.assertTrue(hat.get_word("room1")) self.assertIsNone(hat.get_word("room1"))
def main(): configs = { 'prod': prod_config, 'staging': staging_config, } parser = argparse.ArgumentParser(description='Hat bot') parser.add_argument('db_file', help='SQLite database file') parser.add_argument('log_file', help='Log file name') parser.add_argument('config', help='Environment', choices=list(configs.keys())) args = parser.parse_args() config = configs[args.config] # Initialize random from time for later use random.seed(datetime.now()) # Read dictionary files into RAM global dictionaries dictionaries = read_dictionaries() logging.basicConfig( filename=args.log_file, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) global hat, game hat, game = start_game(args.db_file) updater = Updater(token=config.token, use_context=True) # Get the dispatcher to register handlers dp = updater.dispatcher dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) dp.add_handler(CommandHandler("getword", getword)) dp.add_handler(CommandHandler("leaveroom", leaveroom)) dp.add_handler(CommandHandler("removeword", removeword)) dp.add_handler(CommandHandler("settimer", settimer)) dp.add_handler(CommandHandler("ready", ready)) dp.add_handler(CommandHandler("results", results)) dp.add_handler(CommandHandler("force_start", force_start)) dp.add_handler(CommandHandler("finish_round", finish_round)) dp.add_handler(CommandHandler("subscribe", subscribe)) dp.add_handler(MessageHandler(Filters.text(ready_button), start_turn)) dp.add_handler(MessageHandler(Filters.text(buttons), continue_turn)) dp.add_handler(MessageHandler(Filters.text, echo)) # log all errors dp.add_error_handler(error) # Start the Bot updater.start_polling() updater.idle()
def game(message): cnt = 0 actor_image_p, db_handle, actual_name = db.start_game() actor_image = IMG.open(actor_image_p) i_mode = image.optimalmode(actor_image.size) shatters = image.splitim(actor_image, i_mode) back = image.generatebackground( tuple(map(lambda x: x + 30, actor_image.size)), i_mode) actor_image.close() image.temp(back, message.chat.id) fname = str(message.chat.id) imobj = open(fname + '.jpg', 'rb') a = bot.send_photo(message.chat.id, imobj, reply_markup=telebot.types.ForceReply()) imobj.close() def guess(message): nonlocal cnt, a if message.text[0].isalpha() and message.text[1].isdigit(): ins_pos = image.msg_to_pos(message.text, i_mode) if ins_pos < len(shatters) and ord(message.text[0].lower( )) - 97 < i_mode and ord(message.text[1]) - 48 < i_mode: image.addguessed(back, shatters[ins_pos]) image.temp(back, fname) cnt += 1 tmpd = open(fname + '.jpg', 'rb') a = bot.send_photo(message.chat.id, tmpd, reply_markup=telebot.types.ForceReply()) tmpd.close() else: a = bot.send_message(message.chat.id, 'Out of range! Use available area only.', reply_markup=telebot.types.ForceReply()) bot.register_for_reply(a, guess) else: if message.text.lower() in actual_name.split(','): bot.send_message( message.chat.id, 'You won! It took you {} attempts, nice result!'.format( cnt)) cleanup([fname + '.jpg', actor_image_p]) else: a = bot.send_message(message.chat.id, 'Nope :(', reply_markup=telebot.types.ForceReply()) cnt += 1 bot.register_for_reply(a, guess) bot.register_for_reply(a, guess) if cnt == len(shatters): bot.send_message(message.chat.id, 'You`ve Lost.') cleanup([fname, vactor_image_p]) return None
import texts import prod_config import staging_config from db import start_game, HatWrapper from telegram.ext import Updater, CommandHandler, MessageHandler, Filters from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove from round import Round logging.basicConfig(filename=sys.argv[2], format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) hat, game = start_game(sys.argv[1]) allowed_rooms = list(map(str.strip, open("rooms.txt").readlines())) experimental_rooms = list(map(str.strip, open("experimental_rooms.txt").readlines())) personal_rooms = list(map(str.strip, open("personal_rooms.txt").readlines())) def start(update, context): """Send a message when the command /start is issued.""" update.message.reply_text(texts.start_message) def help(update, context): """Send a message when the command /help is issued.""" update.message.reply_text(texts.help_message)