Advertisement
st3p4nb01

qna.habr

Jun 10th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.65 KB | None | 0 0
  1. # # # # # # # # PyQt5 # # # # # # # #  
  2. from PyQt5              import QtCore, QtGui, QtWidgets
  3. from PyQt5.QtCore       import QThread, pyqtSignal
  4. from PyQt5.QtWidgets    import QWidget, QProgressBar, QPushButton, QApplication
  5. from PyQt5.QtCore       import QBasicTimer
  6. from gui                import MainInterface
  7.  
  8. import asyncio, sys, re, logging, signal, discord, csv, config
  9. from discord.ext import commands
  10. from datetime import datetime
  11. from valve.source.a2s import ServerQuerier, NoResponseError
  12.  
  13. from subsidiary import pkill as PLAYER_KILL
  14. from subsidiary import revent as REWARD_EVENT
  15. from subsidiary import cevent as CONNECT_EVENT
  16. from subsidiary import pdeath as PLAYER_DEATH
  17. from subsidiary import server_name as SERVER_NAME
  18. from subsidiary import log as SERVER_LOG
  19. from subsidiary import dlog as PLAYERS_LOG
  20. from subsidiary import rlog as REWARDS_LOG
  21. from subsidiary import path_player as PATH_PLAYER
  22. from subsidiary import path_death as PATH_DEATH
  23. from subsidiary import path_killed as PATH_KILLED
  24. from subsidiary import path_reward as PATH_REWARD
  25. from subsidiary import path_yaml as PATH_YAML
  26.  
  27. from config import file as LOG
  28. from config import rewards_log as LOG_REWARDS
  29. from config import players_log as LOG_PLAYERS
  30. from config import server_log as LOG_SERVER
  31. from config import BOT_TOKEN as TOKEN
  32. from config import SERVER_NAME
  33. from config import LOGCHAN_ID as lchanID
  34. from config import VCHANNEL_ID as chanID
  35. from config import KILLEDCHAN_ID as kchadID
  36. from config import REWARDCHAN_ID as rchanID
  37. from config import EVENTCHAN_ID as echanID
  38. from config import SERVERCHAN_ID as schanID
  39. from config import MAX_PLAYERS as max_players
  40.  
  41.  
  42. now = datetime.now()
  43. gettime = now.strftime("%d/%m/%Y | %H:%M:%S > ")
  44.  
  45. bot = commands.Bot(command_prefix=';', help_command=None)
  46.  
  47. async def writecsv():
  48.     while True:    
  49.         try:
  50.             with ServerQuerier(config.SERVER_ADDRESS) as server:
  51.                 with open('csv/playerstats.csv', 'a', newline='') as f:
  52.                     csvup = csv.writer(f, delimiter=',')  
  53.                     players = server.info()['player_count']
  54.                     csvup.writerow([gettime, players])
  55.                     log(f"{gettime}Player count: {players}")
  56.         except NoResponseError:
  57.             with open('csv/playerstats.csv', 'a', newline='') as f:
  58.                 csvup = csv.writer(f, delimiter=',')  
  59.                 players = '0'
  60.                 csvup.writerow([gettime, players])
  61.                 log(f'{gettime}Unable to connect to server')
  62.         await asyncio.sleep(60)
  63.  
  64. async def deathcount():
  65.     while True:          
  66.         with open(log, encoding='utf-8', mode='r') as f:
  67.             f.seek(0,2)
  68.             while True:
  69.                 line = f.readline()
  70.                 if(re.search(PLAYER_DEATH, line)):
  71.                     pname = re.search(PLAYER_DEATH, line).group(1)
  72.                     with open('csv/deathlog.csv', 'a', newline='', encoding='utf-8') as dl:
  73.                         deathup = csv.writer(dl, delimiter=',')
  74.                         deathup.writerow([gettime, pname])
  75.                         log(f"{gettime}{pname} ΡƒΠΌΠ΅Ρ€!")
  76.                 await asyncio.sleep(0.2)
  77.  
  78. def log(log):
  79.     logging.info(log)
  80.  
  81. class QTextEditLogger(logging.Handler):
  82.  
  83.     def __init__(self, parent):
  84.         super().__init__()
  85.  
  86.         self.PVELogs = QtWidgets.QPlainTextEdit(parent.centralwidget)
  87.         self.PVELogs.setGeometry(QtCore.QRect(40, 50, 801, 491))
  88.         font = QtGui.QFont()
  89.         font.setFamily("Consolas")
  90.         font.setPointSize(10)
  91.         self.PVELogs.setFont(font)
  92.         self.PVELogs.setStyleSheet("background-color: rgb(30, 30, 30); color: rgb(255, 255, 255);")
  93.         self.PVELogs.setReadOnly(True)
  94.         self.PVELogs.setPlainText("")
  95.         self.PVELogs.setObjectName("PVELogs")
  96.  
  97.     def emit(self, record):
  98.         msg = self.format(record)
  99.         self.PVELogs.appendPlainText(msg)
  100.  
  101. class PVP(QtWidgets.QMainWindow, QWidget, MainInterface):
  102.     def __init__(self):
  103.         super().__init__()
  104.  
  105.         self.setupUi(self) #init gui
  106.  
  107.         # logging
  108.         logTextBox = QTextEditLogger(self)
  109.         #logTextBox.setFormatter(logging.Formatter(f'{gettime}%(message)s'))
  110.         logging.getLogger().addHandler(logTextBox)
  111.         logging.getLogger().setLevel(logging.DEBUG)
  112.  
  113.         root_logger= logging.getLogger()
  114.         root_logger.setLevel(logging.DEBUG)
  115.         handler = logging.FileHandler('PVPLog.log', 'w', 'utf-8')
  116.         handler.setFormatter(logging.Formatter('%(name)s, %(asctime)s, [%(levelname)s], %(message)s'))
  117.         root_logger.addHandler(handler)
  118.  
  119.         self.PVELogs.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
  120.  
  121.         self.thread = WorkThread()
  122.         self.thread.threadSignal.connect(self.func2)          
  123.         self.onPVE.clicked.connect(self.func1)      
  124.  
  125.         log(" ========================================== VALHEIMBY LOGS - v1.0.0 ===========================================")
  126.         log(" ==============================================================================================================\n")
  127.  
  128.     def func1(self):
  129.         self.thread.start()
  130.  
  131.     def func2(self, value):
  132.         pass
  133.  
  134. class WorkThread(QtCore.QThread):
  135.     threadSignal = QtCore.pyqtSignal(int)
  136.  
  137.     def __init__(self):
  138.         super().__init__()
  139.  
  140.     def run(self):    
  141.         for i in range(10):
  142.             self.msleep(200)
  143.             self.threadSignal.emit(i)
  144.  
  145. if __name__ == "__main__":
  146.     app = QtWidgets.QApplication(sys.argv)
  147.     mw = PVP()
  148.     mw.show()
  149.  
  150.     sys.exit(app.exec())
  151.    
  152. loop = asyncio.new_event_loop()
  153. loop.create_task(deathcount())
  154. asyncio.set_event_loop(loop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement