st3p4nb01

qna.habr

Jun 8th, 2021
833
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #qna.habr
  2.  
  3. from datetime import datetime
  4. from colorama import Fore, Style, init
  5. import time, os, re
  6. import csv, asyncio
  7. import colorama
  8. from valve.source.a2s import ServerQuerier, NoResponseError
  9. import yaml
  10. import config
  11.  
  12. pdeath = '.*?Got character ZDOID from (\w+) : 0:0'
  13. sevent = '.*?Got character ZDOID from (\\w+)\\b(?!:)'
  14. pkill = '.*? (\\w+):(\\w+)\\b(?!:)'
  15. revent = '.*? (\w+):(\w+):(\w+)'
  16. log = config.file
  17. dlog = config.players_log
  18. rlog = config.rewards_log
  19. path_player = 'csv/playerstats.csv'
  20. path_death = 'csv/deathlog.csv'
  21. path_killed = 'csv/killedlog.csv'
  22. path_reward = 'csv/rewardslog.csv'
  23. path_yaml = 'csv/rewardscount.yaml'
  24.  
  25. colorama.init()
  26.  
  27. async def timenow():
  28.     now = datetime.now()
  29.     gettime = now.strftime("%d/%m/%Y %H:%M:%S")
  30.     return gettime
  31.  
  32. async def writecsv():
  33.     while True:    
  34.         try:
  35.             with ServerQuerier(config.SERVER_ADDRESS) as server:
  36.                 with open('csv/playerstats.csv', 'a', newline='') as f:
  37.                     csvup = csv.writer(f, delimiter=',')  
  38.                     curtime, players = await timenow(), server.info()['player_count']
  39.                     csvup.writerow([curtime, players])
  40.                     print(f"{Fore.MAGENTA}{curtime} > Player count: {players}{Style.RESET_ALL}")
  41.         except NoResponseError:
  42.             with open('csv/playerstats.csv', 'a', newline='') as f:
  43.                 csvup = csv.writer(f, delimiter=',')  
  44.                 curtime, players = await timenow(), '0'
  45.                 csvup.writerow([curtime, players])
  46.                 print(Fore.RED + curtime, 'Unable to connect to server' + Style.RESET_ALL)
  47.         await asyncio.sleep(60)
  48.  
  49. async def deathcount():
  50.     while True:          
  51.         with open(log, encoding='utf-8', mode='r') as f:
  52.             f.seek(0,2)
  53.             while True:
  54.                 line = f.readline()
  55.                 if(re.search(pdeath, line)):
  56.                     pname = re.search(pdeath, line).group(1)
  57.                     curtime = await timenow()
  58.                     with open('csv/deathlog.csv', 'a', newline='', encoding='utf-8') as dl:
  59.                         deathup = csv.writer(dl, delimiter=',')
  60.                         deathup.writerow([curtime, pname])
  61.                         print(Fore.CYAN + f"{curtime} > {pname} умер!" + Style.RESET_ALL)
  62.                 await asyncio.sleep(0.2)
  63.  
  64. async def killcount():
  65.     while True:
  66.         with open(dlog, encoding='utf-8', mode='r') as f:
  67.             f.seek(0,2)
  68.             while True:
  69.                 line = f.readline()
  70.                 if(re.search(pkill, line)):
  71.                     names = line[12:].split(":")
  72.                     player, attacker = names[0], names[1]
  73.                     attacker = attacker.strip()
  74.  
  75.                     if attacker == "greydwarf_elite":
  76.                         attacker = "Грейдворф-дикарём"
  77.                     elif attacker == "hatchling":
  78.                         attacker = "Драконом"
  79.                     elif attacker == "goblin":
  80.                         attacker = "Гоблином"
  81.                     elif attacker == "wolf":
  82.                         attacker = "Волком"
  83.                     elif attacker == "troll":
  84.                         attacker = "Троллем"
  85.                     elif attacker == "fenring":
  86.                         attacker = "Фенрингом"
  87.                     elif attacker == "deathsquito":
  88.                         attacker = "Комаром смерти"
  89.                     elif attacker == "lox":
  90.                         attacker = "Локсом"
  91.                     elif attacker == "greyling":
  92.                         attacker = "Грейлингом"
  93.                     elif attacker == "wraith":
  94.                         attacker = "Призраком"
  95.                     elif attacker == "stonegolem":
  96.                         attacker = "Каменным големом"
  97.                     elif attacker == "boar":
  98.                         attacker = "Кабаном"
  99.                     elif attacker == "serpent":
  100.                         attacker = "Змеёй"
  101.                     elif attacker == "neck":
  102.                         attacker = "Никсом"
  103.                     elif attacker == "greydwarf":
  104.                         attacker = "Грейдворфом"
  105.                     elif attacker == "goblinarcher":
  106.                         attacker = "Гоблином-стрелком"
  107.  
  108.                         pass
  109.  
  110.                     else:
  111.  
  112.                         curtime = await timenow()
  113.                         with open('csv/killedlog.csv', 'a', newline='', encoding='utf-8') as dl:
  114.                             killedup = csv.writer(dl, delimiter=',')
  115.                             killedup.writerow([curtime, 'Игрок ' + attacker + ' убил игрока ' + player])
  116.                             print(Fore.CYAN + f"{curtime} > Игрок {attacker} убил {player}" + Style.RESET_ALL)
  117.  
  118.                         with open('csv/killedcount.csv', 'a', newline='', encoding='utf-8') as dl:
  119.                             killedup = csv.writer(dl, delimiter=',')
  120.                             killedup.writerow([curtime, attacker])
  121.                 await asyncio.sleep(0.2)
  122.  
  123. async def joinedcount():
  124.     while True:
  125.         with open(log, encoding='utf-8', mode='r') as f:
  126.             f.seek(0,2)
  127.             while True:
  128.  
  129.                 line = f.readline()
  130.                 first_try = '.*? User (\w+) connected.'
  131.                 second_try = '.*? User (\w+)\s(\w+) connected.'
  132.                 curtime = await timenow()
  133.  
  134.                 if(re.search(first_try, line)):
  135.                     player = re.search(first_try, line).group(1)
  136.                     with open('csv/joinedcount.csv', 'a', newline='', encoding='utf-8') as j:
  137.                         joinedup = csv.writer(j, delimiter=',')
  138.                         joinedup.writerow([curtime, player])
  139.                         print(Fore.LIGHTGREEN_EX + f"{curtime} > {Fore.WHITE}{player}{Fore.LIGHTGREEN_EX} присоединился на сервер! {Style.RESET_ALL}")
  140.  
  141.                 if(re.search(second_try, line)):
  142.                     player = re.search(second_try, line).group(1), re.search(second_try, line).group(2)
  143.                     player = f"{player[0]} {player[1]}"
  144.                     with open('csv/joinedcount.csv', 'a', newline='', encoding='utf-8') as j:
  145.                         joinedup = csv.writer(j, delimiter=',')
  146.                         joinedup.writerow([curtime, player])
  147.                         print(Fore.LIGHTGREEN_EX + f"{curtime} > {Fore.WHITE}{player}{Fore.LIGHTGREEN_EX} присоединился на сервер! {Style.RESET_ALL}")
  148.  
  149.                 await asyncio.sleep(0.2)
  150.  
  151. loop = asyncio.get_event_loop()
  152. loop.create_task(joinedcount())
  153. loop.create_task(deathcount())
  154. loop.create_task(killcount())
  155. loop.create_task(writecsv())
  156. loop.run_forever()
RAW Paste Data