-
Notifications
You must be signed in to change notification settings - Fork 0
/
ark_chatlog.py
executable file
·157 lines (143 loc) · 5.12 KB
/
ark_chatlog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from sharedsrc.logger import CHATLOG_LOG
from sharedsrc.adminbot import Adminbot
import logging.config
from lib2to3.pgen2.token import AT
logging.config.dictConfig(CHATLOG_LOG)
import threading
import multiprocessing
import sys
from sharedsrc.cmd_helper import CMD
import os
import time
from sharedsrc.conf_helper import ConfHelper
from sharedsrc.playerdb_helper import Player, PlayerDBHelper
from sharedsrc.chatline import Chatline
cfgh = ConfHelper(update=True, autoupdate=True)
class Chatlog(object):
'''
Just the main class
'''
spath = os.path.dirname(sys.argv[0])
def __init__(self):
self.l = logging.getLogger(self.__class__.__name__)
self.l.info("Initialized ARK Chatlog tool")
worker = CLWorker(self.spath)
worker.start()
try:
while True:
worker.fetchChatlog()
time.sleep(float(cfgh.readCfg("CHATLOG_FETCH_INTERVALL")))
except KeyboardInterrupt:
pass
except Exception as e:
self.l.warn(str(e))
pass
finally:
self.l.info("terminated...")
worker.stop()
sys.exit(0)
class CLWorker(threading.Thread):
'''
Do the work in threads
'''
def __init__(self, spath):
'''
Initializes the worker
:param spath: path of this script
'''
super(CLWorker, self).__init__()
self.l = logging.getLogger(self.__class__.__name__)
self._stopit = threading.Event()
self.activeTasks = []
self.queue = []
self.l.info("Initialized ARK Chatlog worker")
self.spath=spath
self.cmd = CMD()
self.l.info("Started command line helper")
self.lock = threading.Lock()
self.adminbot = None
self.pdbh = None
if(int(cfgh.readCfg("CHATBOT_ENABLED")) == 1):
self.pdbh=PlayerDBHelper()
self.adminbot=Adminbot(os.path.join(self.spath, "thirdparty/mcrcon"),self.pdbh)
def run(self):
while not self.isStopped():
for aT in self.activeTasks:
if aT.get("finished")==True:
self.activeTasks.remove(aT)
if len(self.queue) > 0 and len(self.activeTasks) < 1: #multiprocessing.cpu_count():
jobObj = self.queue.pop(0)
self.activeTasks.append(jobObj)
jobObj.get("job").daemon = True
jobObj.get("job").start()
else:
time.sleep(0.1)
def folderhealth(self, abspath):
'''
Create the direcotry of the path if it doesn't exist
:param abspath:
'''
if not os.path.exists(os.path.dirname(abspath)):
self.l.warn("Created folder "+os.path.dirname(abspath))
os.mkdir(os.path.dirname(abspath))
def fetchChatlog(self):
for port in cfgh.readCfg("RCON_PORTS").split(" "):
fPI = threading.Thread(target=self.__fetchChatlog,args=(port,))
self.queue.append({"job":fPI,"finished":False})
def __fetchChatlog(self,port):
self.l.debug("fetch chatlog")
try:
output = self.cmd.proc(
args=[
os.path.join(self.spath, "thirdparty/mcrcon"), "-c",
"-H", "127.0.0.1",
"-P", port,
"-p", cfgh.readGUSCfg("ServerAdminPassword"),
"GetChat"
]
)
if output[1]:
self.l.warn("Fetching players failed!")
self.l.error(output[1])
if output[0]:
filepath=os.path.join(self.spath,cfgh.readCfg("CHATLOG_DB"))
try:
self.lock.acquire(True)
for line in output[0].split("\n"):
chatlineObj = Chatline.create(port=port, line=line)
if(chatlineObj):
chatlineObj.write(filepath)
self.usebot(chatlineObj)
finally:
self.lock.release()
finally:
self.__subthread_suicide()
def usebot(self,chatlineObj):
if(int(cfgh.readCfg("CHATBOT_ENABLED")) == 1):
players = self.pdbh.getPlayersByName(chatlineObj.steamPlayer)
playerObj = None
if(players):
playerObj=players[0]#take the first matching
fPI = threading.Thread(target=self.adminbot.react,args=(
chatlineObj,
playerObj,
self.__subthread_suicide,))
self.queue.append({"job":fPI,"finished":False})
def __subthread_suicide(self):
for aT in self.activeTasks:
if aT.get("job")==threading.currentThread():
aT.update({"finished":True})
def stop(self):
'''
Stop the thread and all it's childs. May take a few seconds
'''
self._stopit.set()
def isStopped(self):
'''
True when the thread is terminated
'''
return self._stopit.isSet()
if __name__ == '__main__':
Chatlog()