Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.68 KB | None | 0 0
  1. from telethon import TelegramClient, sync
  2. from telethon.tl.functions.messages import GetHistoryRequest
  3. from time import sleep
  4. from re import findall
  5. import socks
  6.  
  7. botname = 'BastionSiegeBot'
  8. period = 300  # Время большой паузы
  9. period_hop = 10  # Промежуток, через который выводятся сообщения во время большой паузы
  10. message_wait = 1  # Пауза между сообщениями
  11. people_reserve_percent = 0.5
  12.  
  13. api_id =
  14. api_hash = ""
  15. phone_number = ""
  16. nick_name = ""
  17. proxy = (socks.SOCKS5, "exp1.s5overss.mtpro.xyz", 39610, False, "mtpro_xyz", "mtpro_xyz_bot")
  18.  
  19. client = TelegramClient("iv1982", api_id, api_hash, proxy=proxy)
  20.  
  21. client.start()
  22.  
  23. if not client.is_user_authorized():
  24.     client.send_code_request(phone_number)
  25.     client.sign_in(phone_number, input('Enter the code: '))
  26.    
  27. entity = client.get_input_entity(botname)
  28.  
  29. status = {'people': 0, 'army': 0, 'gold': 0, 'wood': 0, 'stone': 0, 'food': 0, 'archers': 0, 'trebuchete_workers': 0,
  30.           'trebuchet_workers_max': 0, 'town_hall_level': 0, 'houses_level': 0, 'houses_people_max': 0,
  31.           'storage_level': 0, 'storage_workers': 0, 'storage_workers_max': 0, 'barraks_level': 0, 'army_max': 0,
  32.           'wall_level': 0, 'archers_max': 0, 'wall_durability': 0, 'wall_durability_max': 0, 'sawmill_level': 0,
  33.           'sawmill_workers': 0, 'sawmill_workers_max': 0, 'mine_level': 0, 'mine_workers': 0, 'mine_workers_max': 0,
  34.           'farm_level': 0, 'farm_workers': 0, 'farm_workers_max': 0, 'trebuchete_level': 0, 'change': 0, 'wood_max': 0,
  35.           'stone_max': 0, 'food_max': 0, 'storage_full_price': 0, 'storage_upgrade_gold': 0, 'storage_upgrade_wood': 0,
  36.           'storage_upgrade_stone': 0, 'town_hall_upgrade_gold': 0, 'town_hall_upgrade_wood': 0,
  37.           'town_hall_upgrade_stone': 0, 'houses_upgrade_gold': 0, 'houses_upgrade_wood': 0, 'houses_upgrade_stone': 0,
  38.           'gold_max': 0, 'gold_income': 0, 'people_income': 0}
  39.  
  40.  
  41. def get_message():  
  42.     result = client(GetHistoryRequest(
  43.         entity,
  44.         limit=1,
  45.         offset_date=None,
  46.         offset_id=0,
  47.         max_id=0,
  48.         min_id=0,
  49.         add_offset=0,
  50.         hash = 0
  51.     ))
  52.  
  53.     message = result.messages[0].message
  54.     return message
  55.  
  56. def update_status():  
  57.     message = get_message()
  58.     split = findall('\w+', message)
  59. #    print('\n    Split:', split)
  60.     newstatus = status
  61.     newstatus['change'] = 0
  62.  
  63.     if len(split) < 6:
  64.         if split[0] == 'Выбери' and split[1] == 'количество':
  65.             return status
  66.         badcount = 1
  67.         while len(split) < 6:
  68.             if badcount > 6:
  69.                 print('Break! Split: ', split)
  70.                 return status
  71.             print('не найден ответ!: ', split, '\nЖдем ', badcount, ':')
  72.             sleep(5)
  73.             message = get_message()
  74.             split = findall('\w+', message)
  75.             badcount += 1
  76.  
  77.     if split[0] == nick_name:
  78.  
  79.         people, armyn, gold, wood, stone, food = 0, 0, 0, 0, 0, 0
  80.  
  81.         if 'Жители' in split:
  82.             people_index = split.index('Жители')
  83.             people = int(split[people_index + 1])
  84.  
  85.         if 'Армия' in split:
  86.             army_index = split.index('Армия')
  87.             armyn = int(split[army_index + 1])
  88.  
  89.         if 'Золото' in split:
  90.             gold_index = split.index('Золото')
  91.             gold = int(split[gold_index + 1])
  92.  
  93.         if 'Дерево' in split:
  94.             wood_index = split.index('Дерево')
  95.             wood = int(split[wood_index + 1])
  96.  
  97.         if 'Камень' in split:
  98.             stone_index = split.index('Камень')
  99.             stone = int(split[stone_index + 1])
  100.  
  101.         if 'Еда' in split:
  102.             food_index = split.index('Еда')
  103.             food = int(split[food_index + 1])
  104.  
  105.         newstatus = {'people': people, 'army': armyn, 'gold': gold, 'wood': wood, 'stone': stone, 'food': food,
  106.                      'change': 1}
  107.  
  108.     if split[0] == 'Постройки':
  109.         newstatus = {'town_hall_level': int(split[1]), 'houses_level': int(split[5]), 'people': int(split[6]),
  110.                      'houses_people_max': int(split[7]), 'storage_level': int(split[2]),
  111.                      'storage_workers': int(split[3]), 'storage_workers_max': int(split[4]),
  112.                      'barraks_level': int(split[17]), 'army': int(split[18]), 'army_max': int(split[19]),
  113.                      'wall_level': int(split[20]), 'archers': int(split[21]), 'archers_max': int(split[22]),
  114.                      'sawmill_level': int(split[11]), 'sawmill_workers': int(split[12]),
  115.                      'sawmill_workers_max': int(split[13]), 'mine_level': int(split[14]),
  116.                      'mine_workers': int(split[15]),
  117.                      'mine_workers_max': int(split[16]), 'farm_level': int(split[8]), 'farm_workers': int(split[9]),
  118.                      'farm_workers_max': int(split[10]), 'change': 1}
  119.  
  120.     if split[0] == 'Победы':
  121.         newstatus = {'change': 1, 'wall_durability': int(split[15]), 'wall_durability_max': int(split[16]),
  122.                      'archers': int(split[17]), 'archers_max': int(split[18]), 'trebuchete_workers': int(split[20]),
  123.                      'trebuchet_workers_max': int(split[21]), 'army': int(split[22]), 'army_max': int(split[23]),
  124.                      'food': int(split[24])}
  125.  
  126.     if split[0] == 'Склад' and split[1] == 'Уровень':
  127.         move = 0
  128.         if split[20] == 'Улучшить':
  129.             move = -2
  130.         newstatus = {'storage_level': int(split[2]), 'storage_workers': int(split[4]),
  131.                      'storage_workers_max': int(split[5]), 'wood': int(split[7]), 'wood_max': int(split[8]),
  132.                      'stone': int(split[9]), 'stone_max': int(split[10]), 'food': int(split[11]),
  133.                      'food_max': int(split[12]), 'gold': int(split[17]), 'people': int(split[19]),
  134.                      'storage_upgrade_gold': int(split[23 + move]), 'storage_upgrade_wood': int(split[24 + move]),
  135.                      'storage_upgrade_stone': int(split[25 + move])}
  136.         if move == 0:
  137.             newstatus.update(storage_full_price=int(split[21]))
  138.  
  139.     if split[0] == 'Ратуша' and split[1] == 'Уровень':
  140.         newstatus = {'town_hall_level': int(split[2]), 'gold': int(split[4]), 'gold_max': int(split[6]),
  141.                      'gold_income': int(split[7]), 'town_hall_upgrade_gold': int(split[10]),
  142.                      'town_hall_upgrade_wood': int(split[11]), 'town_hall_upgrade_stone': int(split[12])}
  143.  
  144.     if split[0] == 'Дома' and split[1] == 'Уровень':
  145.         newstatus = {'houses_level': int(split[2]), 'people': int(split[4]), 'houses_people_max': int(split[5]),
  146.                      'people_income': int(split[6]), 'houses_upgrade_gold': int(split[16]),
  147.                      'houses_upgrade_wood': int(split[17]), 'houses_upgrade_stone': int(split[18])}
  148.  
  149. #    print('Новый статус', newstatus, '\n')
  150.     return newstatus
  151.  
  152. def longwait():
  153.     period_multiplier = period // period_hop
  154.     print('Ждем', period, 'секунд')
  155.     for i in range(0, period_multiplier):
  156.         print('Ждем:', i * period_hop, 'из', period_multiplier * period_hop + period % period_hop)
  157.         sleep(period_hop)
  158.     if period % period_hop:
  159.         sleep(period % period_hop)
  160.  
  161. def snd(message):
  162.     print('Отправка сообщения: ', message)
  163.     client.send_message(entity, str(message))
  164.     sleep(message_wait)
  165.  
  166. def button(*args):
  167.     print('\n  Строка кнопок: ', args)
  168.     btn_text = 'Команда: '
  169.     for btn in args:        
  170.         snd(btn)
  171.         status.update(update_status())
  172.     return status        
  173.  
  174. def forceupdate():
  175.     print('\n\n!Полное обновление!')
  176.     status.update(button('Наверх', 'Постройки', 'Ратуша', 'Назад', 'Склад', 'Назад', 'Дома', 'Наверх', 'Война', 'Наверх'))
  177.     return status
  178.  
  179. def buyres():
  180.     print('\n\n!Приобретение ресурсов.')
  181.     status.update(button('Наверх'))
  182.  
  183.     difwood = status['wood_max'] - status['wood']
  184.     difstone = status['stone_max'] - status['stone']
  185.     diffood = status['food_max'] - status['food']
  186.     difall = difwood + difstone + diffood
  187.     if diffood > int(status['food_max']/2):
  188.         print('Покупка ', diffood, ' еды!')
  189.         if status['gold']/2 > diffood:
  190.             num = diffood
  191.         else:
  192.             num = int(status['gold']/2)
  193.         status.update(button('Заполнить', 'Купить', 'Еда'))
  194.         snd(num)
  195.         status.update(button('Наверх'))
  196.         diffood = status['food_max'] - status['food']
  197.         difall = difwood + difstone + diffood
  198.     if difall * 6 < status['gold']:
  199.         if difwood > 0 or difstone > 0:
  200.             print('Полный склад')
  201.             status.update(button('Постройки', 'Склад', 'Заполнить'))
  202.     else:
  203.         status.update(button('Торговля', 'Купить'))
  204.         resfund = int(status['gold'] / 2)
  205.         if difwood + difstone < resfund:
  206.             buywood = difwood
  207.             buystone = difstone
  208.         else:
  209.             buywood = 0
  210.             buystone = 0
  211.  
  212.             if difstone > difwood and difstone - difwood < resfund:
  213.                 buywood += difstone - difwood
  214.                 resfund -= buywood
  215.             elif difstone < difwood and difwood - difstone < resfund:
  216.                 buystone += difwood - difstone
  217.                 resfund -= buystone
  218.  
  219.             buywood += int(resfund / 2)
  220.             buystone += int(resfund / 2)
  221.  
  222.         if buywood > 0:
  223.             print('Покупка ', buywood, ' дерева!')
  224.             status.update(button('Дерево'))
  225.             snd(buywood)
  226.             status.update(button('Назад'))
  227.         if buystone > 0:
  228.             print('Покупка ', buystone, ' камня!')
  229.             status.update(button('Камень'))
  230.             snd(buystone)
  231.             status.update(button('Назад'))
  232.  
  233.     status.update(button('Наверх'))
  234.     return status
  235.  
  236. def build():
  237.     print('\n\n!Модуль строительства.')
  238.  
  239.     town_hall = 0
  240.     houses = 0
  241.     storage = 0
  242.  
  243.     if status['wood'] > status['town_hall_upgrade_wood'] and status['stone'] > status['town_hall_upgrade_stone']:
  244.         town_hall = 1
  245.         if status['gold'] > status['town_hall_upgrade_gold']:
  246.             town_hall = 2
  247.         if town_hall == 1:
  248.             print('Улучшаем ратушу, ждем золото')
  249.             return status
  250.     elif status['wood'] > status['houses_upgrade_wood'] and status['stone'] > status['houses_upgrade_stone']:
  251.         houses = 1
  252.         if status['gold'] > status['houses_upgrade_gold']:
  253.             houses = 2
  254.         if houses == 1:
  255.             print('Улучшаем дома, ждем золото')
  256.             return status
  257.     elif status['wood'] > status['storage_upgrade_wood'] and status['stone'] > status['storage_upgrade_stone']:
  258.         storage = 1
  259.         if status['gold'] > status['storage_upgrade_gold']:
  260.             storage = 2
  261.         if storage == 1:
  262.             print('Улучшаем склад, ждем золото')
  263.             return status
  264.  
  265.     if town_hall == 2 or houses == 2 or storage == 2:        
  266.         status.update(button('Наверх', 'Постройки'))
  267.         if town_hall == 2:
  268.             print('Строим ратушу.')
  269.             status.update(button('Ратуша', 'Улучшить', 'Наверх'))
  270.             buyres()
  271.             return status
  272.         elif houses == 2:
  273.             print('Строим дома.')
  274.             status.update(button('Дома', 'Улучшить', 'Наверх'))
  275.             buyres()
  276.             return status
  277.         elif storage == 2:
  278.             print('Строим склад.')
  279.             status.update(button('Склад', 'Улучшить', 'Наверх'))
  280.             buyres()
  281.             return status
  282.  
  283.     return status
  284.  
  285. def army():
  286.     print('\n\n!Модуль армии.')
  287.     status.update(button('Наверх'))
  288.     print('Армия: ', status['army'], ' Максимальная армия: ', status['army_max'], ' Людей: ', status['people'], ' максимум людей: ',
  289.           status['houses_people_max'])
  290.     difarmy = status['army_max'] - status['army']
  291.     peoplespace = int(status['people'] - status['houses_people_max'] * people_reserve_percent)
  292.     if peoplespace > 0 and difarmy > 0:
  293.         if difarmy > peoplespace:
  294.             num = peoplespace
  295.         else:
  296.             num = difarmy
  297.         print('Набираем ', num, ' солдат.')
  298.         status.update(button('Война', 'Обучить', 'Казармы'))
  299.         snd(num)
  300.         status.update(button('Наверх'))
  301.     return status
  302.  
  303. loop = 0
  304. while True:
  305.  
  306.     if loop % 10 == 0:
  307.         status.update(forceupdate())
  308.         print('Обновленный статус', status, '\n')
  309.     else:
  310.         status.update(button('Наверх'))
  311.  
  312.     if status['army'] < status['army_max'] and status['people'] > 0:
  313.         status.update(army())
  314.        
  315.     if status['wood'] < status['wood_max'] or status['stone'] < status['stone_max'] or status['food_max'] - \
  316.             status["food"] > 100000:
  317.         status.update(buyres())
  318.  
  319.     min_build_gold = min(status['town_hall_upgrade_gold'], status['houses_upgrade_gold'],
  320.                          status['storage_upgrade_gold'])
  321.     if status['gold'] > min_build_gold:
  322.         status.update(build())
  323.  
  324.     client.disconnect()
  325.            
  326.     longwait()
  327.    
  328.     client.connect()
  329.    
  330.     loop += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement