-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathirc_handler.py
181 lines (166 loc) · 6.74 KB
/
irc_handler.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# coding=utf-8
import re
import api
from src.lang import buildLabels
from src.Constants import buildingTopList, PLASMATECHNOLOGY
# TODO get server from commandline
server = "uni117.ogame.de"
api = api.Api(server, "var", quick=True)
def handle_command(connection, e, command):
target = e.target
if e.type == "privmsg" or command.startswith("!help"):
target = e.source.nick
try:
if command[0] != '!':
return
command = command[1:]
print command
retStr = realy_handle_command(command, connection=connection, target=target)
except Exception, ex:
print "error ", ex
retStr = [u"Fehler, probier nochmal (Versuch 1/3)"]
if not isinstance(retStr, list):
connection.privmsg(target, "Fehler :(")
else:
for line in retStr:
connection.privmsg(target, line)
def realy_handle_command(command, connection=False, target=False):
retStr = []
s = re.search("^(?P<search>(a|f)?)(?P<type>(player)|(alliance)) (?P<name>.+)$", command)
if s:
if s.group("search") == "a":
api.quick = False
else:
api.quick = True
name = s.group("name").decode("utf-8")
if s.group("type") == "player":
if s.group("search") == "f":
data = api.findPlayer(name, 6)
else:
data = api.getPlayerString(name)
if s.group("type") == "alliance":
if s.group("search") == "f":
data = api.findAlliance(name, 6)
else:
data = api.getAllianceString(name)
retStr = "".join(data).split("\n")
elif command == "help":
retStr.append(u"!player name - infos zu einem spieler")
retStr.append(u"!alliance tag - infos zu einer allianz")
retStr.append(u"!aplayer bzw. aalliance - gibt erweiterte infos")
retStr.append(u"!fplayer bzw. falliance - gibt 6 spieler/allianz nach Ähnlichkeit zurück")
retStr.append(u"!which met kris deut [plasma] [temp] [kurs] - gibt Vorschlag welche Mine am besten zu bauen ist - help which für mehr")
retStr.append(u"!awhich same as which, but a bit more detailed")
retStr.append(u"!diff playername [hours] - highscore diff")
retStr.append(u"!inactive yourpos [radius] [duration] [minscore] [maxscore] [maxdefperplanet]- finds inactive")
elif command == "help which":
retStr.append(u"Beispielaufruf: !which 18 15 12 für met=18,kris=15 und deut=12 - plasmatech=0 und maximale temperatur=50 da nicht angegeben, kurs=2:1:1")
retStr.append(u"Beispielaufruf: !which 18 15 12 3 45 3:2:1 nun ist plasma=3, temp=45 und der kurs 3:2:1")
elif command.startswith("which ") or command.startswith("awhich"):
args = command.split(" ")
plasma = 0
temp = 50
mse = [2.0, 1.0, 1.0]
buildings = {}
if len(args) > 2:
buildings[1] = int(args[1])
buildings[2] = int(args[2])
if len(args) > 3:
buildings[3] = int(args[3])
if len(args) > 4:
plasma = int(args[4])
if len(args) > 5:
temp = int(args[5])
if len(args) > 6:
mse = []
for i in args[6].split(":"):
mse.append(float(i))
if command.startswith("which "):
bId = buildingTopList(buildings, {PLASMATECHNOLOGY:plasma}, temp, mse)[0]["bId"]
retStr.append("Am besten baust du: %s" % buildLabels[bId])
else:
t = buildingTopList(buildings, {PLASMATECHNOLOGY:plasma}, temp, mse)
i = 0
for build in t:
i+=1
bId = build["bId"]
atime = build["atime"]
import datetime
atime = str(datetime.timedelta(seconds=atime))
retStr.append("%d: %s %s" % (i, buildLabels[bId], atime))
elif command == "dbupdate":
import db
if connection:
connection.privmsg(target, "updating db - this takes some time..")
db.update(server)
retStr.append("updated")
elif command.startswith("dbquery"):
import db
try:
res = db.query(command[8:])
except Exception, ex:
connection.privmsg(target, ex)
if len(command[8:]) == 445:
retStr.append("Your query can't be longer than 445 chars")
retStr.append(command[8:])
return
if res is None:
retStr = ["No result for your query"]
max = 9
for line in res:
max -= 1
if max == 0:
break
retStr.append(str(line).replace("\n", "")[:406]+" ...")
elif command.startswith("inactive") or command.startswith("ainactive"):
args = command[9:].split(" ")
import db
amount = 6
if command.startswith("ainactive"):
args = command[10:].split(" ")
amount = 12
radius = 15
duration = 60*60*24
minScore = 5000
maxScore = 9999999
maxDef = 9999999
if len(args) < 1 or args[0].find(":") == -1:
retStr.append("usage !inactive yourpos [radius] [duration] [minscore] [maxscore] [maxdefperplanet]")
retStr.append("example !inactive 3:338 15 2 5000 20000 - sucht alle planeten zwischen 3:323 und 3:353 wo spieler 2Tage inaktive und zw. 5k und 20k punkte hat")
else:
if len(args) > 0:
position = args[0]
if len(args) > 1:
radius = int(args[1])
if len(args) > 2:
duration = int(args[2]) * 60*60*24
if len(args) > 3:
minScore = int(args[3])
if len(args) > 4:
maxScore = int(args[4])
if len(args) > 5:
maxDef = int(args[5])
retStr = "".join(db.listInactivityPlayer(position, radius, duration, minScore, maxScore, amount, maxDef)).split("\n")
elif command.startswith("diff"):
args = command[5:].split(" ")
import db
hours = 12
if len(args) < 1:
retStr.append("usage !diff playername [hours]")
else:
if len(args) > 0:
player = args[0].decode("utf-8")
if len(args) > 1:
hours = int(args[1])
retStr = db.highscoreChange(server, player, hours).split("\n")
return retStr
if __name__ == "__main__":
def Args():
pass
args = Args()
import argparse
parser = argparse.ArgumentParser(description='get info from ogame api db',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--command', '-c', type=str, help='command to execute')
args = parser.parse_args()
print "\n".join(realy_handle_command(args.command))