forked from renefritze/relaymanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelaystats.py
148 lines (133 loc) · 5.87 KB
/
relaystats.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
# -*- coding: utf-8 -*-
import time
import datetime
import tasbot
from tasbot.utilities import *
from tasbot.plugin import IPlugin
def elapsed_time(seconds, suffixes=['y','w','d','h','m','s'], add_s=False, separator=' '):
"""
Takes an amount of seconds and turns it into a human-readable amount of time.
"""
# the formatted time string to be returned
time = []
# the pieces of time to iterate over (days, hours, minutes, etc)
# - the first piece in each tuple is the suffix (d, h, w)
# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52),
(suffixes[1], 60 * 60 * 24 * 7),
(suffixes[2], 60 * 60 * 24),
(suffixes[3], 60 * 60),
(suffixes[4], 60),
(suffixes[5], 1)]
# for each time piece, grab the value and remaining seconds, and add it to
# the time string
for suffix, length in parts:
value = round(seconds / length)
if value > 0:
seconds = seconds % length
time.append('%s%s' % (str(value),(suffix, (suffix, suffix + 's')[value > 1])[add_s]))
if seconds < 1:
break
return separator.join(time)
bstr_nonneg = lambda n: n>0 and bstr_nonneg(n>>1).lstrip('0')+str(n&1) or '0'
class UserStatus:
def __init__(self, status, nick ):
sstr = bstr_nonneg( int(status) ).rjust( 31, "0" )
self.ingame = ( sstr[ -1 : 0 ] == "1" )
self.nick = nick
self.decimal = int(status)
class Main(IPlugin):
def __init__(self,name,tasclient):
IPlugin.__init__(self,name,tasclient)
self.slavetomanager = dict()
self.slavetousagecount = dict()
self.slavetoingamecount = dict()
self.battlestartingtime = dict()
self.managerlist = []
try:
self.statsfilename = os.path.join( self.app.config.get('tasbot',"cfg_dir"),"relaystats.txt")
except Exception:
self.statsfilename = "relaystats.txt"
def onload(self,tasc):
self.app = tasc.main
self.tasc = tasc
self.managerlist = self.app.config.get_optionlist('relaymanager', "managerlist")
self.loadStats()
def oncommandfromserver(self,command,args,socket):
if command == "SAIDPRIVATE" and len(args) > 1 and args[1] == "!stats":
if len(args) > 3:
return
managertoingamecount = dict()
managertousagecount = dict()
for slavename in self.slavetomanager:
managername = self.slavetomanager[slavename]
if not managername in managertoingamecount:
managertoingamecount[managername] = 0
if not managername in managertousagecount:
managertousagecount[managername] = 0
if slavename in self.slavetoingamecount:
managertoingamecount[managername] = managertoingamecount[managername] + self.slavetoingamecount[slavename]
if slavename in self.slavetousagecount:
managertousagecount[managername] = managertousagecount[managername] + self.slavetousagecount[slavename]
if len(args) == 2:
for managername in managertoingamecount:
ingame = elapsed_time( managertoingamecount[managername] )
socket.send("SAYPRIVATE %s %s was used %d times, and accumulated %s of ingame time\n"% \
(args[0], managername, managertousagecount[managername], ingame ) )
elif len(args) == 3 and args[2] in self.managerlist:
managername = args[2]
if managername in managertoingamecount:
ingame = elapsed_time( managertoingamecount[managername] )
socket.send("SAYPRIVATE %s %s was used %d times, and accumulated %s of ingame time\n"% \
(args[0], managername, managertousagecount[managername], ingame ) )
for slavename in self.slavetomanager:
if self.slavetomanager[slavename] == managername:
if slavename in self.slavetousagecount and slavename in self.slavetoingamecount:
ingame = elapsed_time( self.slavetoingamecount[slavename] )
socket.send("SAYPRIVATE %s %s was used %d times, and accumulated %s of ingame time\n"% \
(args[0], slavename, slavetousagecount[managername], ingame ) )
if command == "SAID" and len(args) > 4 and args[0] == "autohost":
sender = args[1]
self.managerlist = self.app.config.get_optionlist('relaymanager', "managerlist")
if sender in self.managerlist and args[2] == "Spawning":
botname = args[len(args)-1]
self.slavetomanager[botname] = sender
if command == "CLIENTSTATUS" and len(args) > 1 and args[0] in self.slavetomanager:
slavename = args[0]
ingame = ( int(args[1]) % 2 ) == 1
print slavename + " got ingame " + str(ingame)
now = time.time()
if not ingame and slavename in self.battlestartingtime:
deltatime = now - self.battlestartingtime[slavename]
del self.battlestartingtime[slavename]
managername = self.slavetomanager[slavename]
if not slavename in self.slavetousagecount:
self.slavetousagecount[slavename] = 0
if not slavename in self.slavetoingamecount:
self.slavetoingamecount[slavename] = 0
self.slavetousagecount[slavename] = self.slavetousagecount[slavename] + 1
self.slavetoingamecount[slavename] = self.slavetoingamecount[slavename] + deltatime
self.saveStats()
if ingame and not slavename in self.battlestartingtime:
self.battlestartingtime[slavename] = now
def onexit(self):
self.saveStats()
def loadStats( self ):
createFileIfMissing(self.statsfilename)
with open(self.statsfilename,'r') as statsfile:
for line in statsfile.readlines():
data = line.split("\t")
if data[0] == "slavetomanager":
self.slavetomanager[data[1]] = data[2]
if data[0] == "slavetousagecount":
self.slavetousagecount[data[1]] = int(data[2])
if data[0] == "slavetoingamecount":
self.slavetoingamecount[data[1]] = float(data[2])
def saveStats( self ):
with open(self.statsfilename,'wb') as statsfile:
for key,value in self.slavetomanager.items():
statsfile.write( "slavetomanager\t" + key + "\t" + str(value) + "\n" )
for key,value in self.slavetousagecount.items():
statsfile.write( "slavetousagecount\t" + key + "\t" + str(value) + "\n" )
for key,value in self.slavetoingamecount.items():
statsfile.write( "slavetoingamecount\t" + key + "\t" + str(value) + "\n" )