-
Notifications
You must be signed in to change notification settings - Fork 2
/
party.py
528 lines (449 loc) · 13.8 KB
/
party.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import dill # type: ignore
from json import loads as loadJson
from itertools import groupby
from multiprocessing.connection import Client
from os import getuid
from re import sub as reSub
from shlex import split
from time import sleep, time
from tf import eval as tfeval # type: ignore
from typing import cast, FrozenSet, Mapping, NamedTuple, Optional, Sequence, Set
from partytypes import Member, MemberDataSource, Place, State
from tfutils import tfprint
from utils import NoValue, strtoi
SOCKET_FILE = "/var/run/user/{0}/bcproxy-tf-scripts-party".format(getuid())
CONN = Client(SOCKET_FILE, "AF_UNIX")
#: binds for targetting party members
#: the fourth column will be first in the output as well (partyoutput.getStatus)
PLACE_KEYBINDS: Sequence[Sequence[Optional[str]]] = [
[],
[None, "^[7", "^[8", "^[9", "^[6"],
[None, "^[u", "^[i", "^[o", "^[y"],
[None, "^[j", "^[k", "^[l", "^[h"],
[None, "^[m", "^[,", "^[.", "^[n"],
]
#: Batclient party status update message (62) fields
PARTY_STATUS_UPDATE_FIELDS = [
"player",
"race",
"gender",
"level",
"hp",
"maxhp",
"sp",
"maxsp",
"ep",
"maxep",
"party_name",
"place_x",
"place_y",
"creator",
"formation",
"member",
"entry",
"following",
"leader",
"linkdead",
"resting",
"idle",
"invisible",
"dead",
"stunned",
"unconscious",
"party_exp",
"total_party_exp",
"party_length_in_seconds",
"party_creation_time",
]
MINION_HP_STATUS_FIELDS = [
"player",
"minion",
"unknown1",
"hp",
"maxhp",
"sp",
"maxsp",
"ep",
"maxep",
"unknown2",
"unknown3",
]
def parseMessage(message) -> Member:
"""
Parse incoming party update message
:param message: message as a string with fields separated by spaces
:returns: dictionary with fields and their respective values
boolean and number fields are converted to respective
python data types
"""
d = dict(zip(PARTY_STATUS_UPDATE_FIELDS, split(message)))
x = strtoi(d["place_x"])
y = strtoi(d["place_y"])
place = None if x == None or y == None else Place(cast(int, x), cast(int, y))
return Member(
str(d["player"]),
int(d["hp"]),
int(d["maxhp"]),
int(d["sp"]),
int(d["maxsp"]),
int(d["ep"]),
int(d["maxep"]),
place,
bool(d["formation"] == "1"),
bool(d["member"] == "1"),
bool(d["entry"] == "1"),
bool(d["following"] == "1"),
bool(d["leader"] == "1"),
bool(d["linkdead"] == "1"),
bool(d["resting"] == "1"),
bool(d["idle"] == "1"),
bool(d["invisible"] == "1"),
bool(d["dead"] == "1"),
int(d["stunned"]),
int(d["unconscious"]),
None,
time(),
MemberDataSource.BCPROXY,
)
def parseMinionHpStatusMessage(message) -> Optional[Member]:
d = dict(zip(MINION_HP_STATUS_FIELDS, split(message)))
name = str(d["minion"]).capitalize()
member = getMemberByName(name)
if member == None:
return None
member = cast(Member, member)
return Member(
name,
int(d["hp"]),
int(d["maxhp"]),
int(d["sp"]),
int(d["maxsp"]),
int(d["ep"]),
int(int(d["maxep"]) / 10),
member.place,
member.formation,
member.member,
member.entry,
member.following,
member.leader,
member.linkdead,
member.resting,
member.idle,
member.invisible,
member.dead,
member.stunned,
member.unconscious,
member.ambushed,
time(),
MemberDataSource.MINION_HP_STATUS,
)
def getMemberByName(name: str) -> Optional[Member]:
global state
for member in state.members:
if member.name == name:
return member
return None
UNKNOWN_PLACES: Sequence[Place] = [
Place(1, 4),
Place(2, 4),
Place(3, 4),
Place(4, 4),
Place(4, 1),
Place(4, 2),
Place(4, 3),
]
#: batclient messages will have out-of-formation places as 6,1 or something
VALID_PLACES: FrozenSet[Place] = frozenset(
[
Place(1, 1),
Place(1, 2),
Place(1, 3),
Place(2, 1),
Place(2, 2),
Place(2, 3),
Place(3, 1),
Place(3, 2),
Place(3, 3),
]
)
def calculatePlaces():
global state
noPlace: Set[Member] = set([])
places: Mapping[Place, Member] = {}
for member in state.members:
# unknown place
if member.place == None or member.place not in VALID_PLACES:
noPlace.add(member)
continue
membersInThisPlaceUpdatedAt = map(
lambda m: m.updatedAt,
filter(lambda m: m.place == member.place, state.members),
)
# allMembersInThisPlaceFromBcproxy = True
allMembersInThisPlaceFromBcproxy = (
len(
list(
filter(
lambda m: m.source == MemberDataSource.PSS
and m.place == member.place,
state.members,
)
)
)
== 0
)
# PSS is the authoritiative source for member location as it includes
# also the nergal minions and riftwalker entities
#
# for bcproxy-only, if this is the most recent update for this place,
# set it here
#
# this also works if there is only one member for this place
if member.source == MemberDataSource.PSS or (
allMembersInThisPlaceFromBcproxy
and member.updatedAt == max(membersInThisPlaceUpdatedAt)
):
places[member.place] = member
else:
noPlace.add(member)
# check if members with unknown place can be left to their previous place
for member in noPlace.copy():
if member.name in state.previousPlaces:
prevPlace = state.previousPlaces[member.name]
if prevPlace not in places:
places[prevPlace] = member
noPlace.remove(member)
# store the previous places
previousPlaces: Mapping[str, Place] = {}
for (place, member) in places.items():
previousPlaces[member.name] = place
state = state._replace(previousPlaces=previousPlaces)
# just sort the rest by name the unknown members and set to locations
for (place, member) in zip(UNKNOWN_PLACES, sorted(noPlace, key=lambda m: m.name)):
places[place] = member
state = state._replace(places=places)
def sendState():
global state
CONN.send_bytes(dill.dumps(state))
def changeTargetName(name: str):
global state
tfeval("/trigger You are now target-healing {0}.".format(name))
state = state._replace(target=name)
sendState()
def changeTargetPlace(placeRaw: str):
global state
placeList = placeRaw.split()
place = Place(int(placeList[0]), int(placeList[1]))
if place in state.places:
target = reSub(r"^[+]", "", state.places[place].name)
state = state._replace(target=target)
tfeval("/trigger You are now target-healing {0}.".format(target))
sendState()
def pssStart(opts):
tfeval("/edit -c100 party_pss")
tfeval("/edit -c100 party_pss_end")
def pssEnd(opts):
global state
if state.pssHasMinions:
state = state._replace(pssHasMinions=False)
else:
tfeval("/edit -c0 -ag party_pss")
tfeval("/edit -c0 -ag party_pss_end")
tfeval("/edit -ag party_pss_start")
def handleNewMember(newMember: Member):
global state
newMembers = set(
[
newMember if member.name == newMember.name else member
for member in state.members
]
)
if newMember not in newMembers:
newMembers.add(newMember)
state = state._replace(members=frozenset(newMembers))
calculatePlaces()
sendState()
def triggerPartyMsg(msg: str):
newMember = parseMessage(msg)
handleNewMember(newMember)
def triggerMinionHpStatusMsg(msg: str):
newMember = parseMinionHpStatusMessage(msg)
if newMember != None:
handleNewMember(cast(Member, newMember))
def triggerPartyLeave(msg: str):
global state
member = msg.capitalize()
toBeRemoved = None
for m in state.members:
if member == m.name:
toBeRemoved = m
if m.name == "Astrax" or m.name == "Ruska":
state = State(frozenset([]), {}, {}, None, False, False)
return
if toBeRemoved:
newMembers = set(state.members)
newMembers.remove(toBeRemoved)
state = state._replace(members=frozenset(newMembers))
calculatePlaces()
sendState()
def spepstringToInt(s: str) -> Optional[int]:
if s == "full":
return 100
elif s == "V. high" or s == "VERY high":
return 80
elif s == "high":
return 60
elif s == "medium":
return 40
elif s == "low":
return 20
elif s == "very low" or "VERY low":
return 10
elif s == "negative":
return 0
else:
return None
def manualPs(opts: str):
tfeval("/edit -an party_pss")
tfeval("/edit -an party_pss_start")
tfeval("/edit -an party_pss_end")
tfeval("@ps")
def pssParse(opts):
global state
d = loadJson(opts)
d["idle"] = d["idle"] == "*"
d["x"] = strtoi(d["x"])
d["y"] = strtoi(d["y"])
d["name"] = d["name"].strip()
d["hp"] = strtoi(d["hp"])
d["maxhp"] = strtoi(d["maxhp"])
d["sp"] = strtoi(d["sp"])
d["maxsp"] = strtoi(d["maxsp"])
d["ep"] = strtoi(d["ep"])
d["maxep"] = strtoi(d["maxep"])
d["spepstring"] = d["spepstring"].strip()
# get nergal minions sp and ep strings
if d["sp"] == None and d["spepstring"] != "":
spep = d["spepstring"].split(" ")
d["sp"] = spepstringToInt(spep[0].strip())
d["ep"] = spepstringToInt(spep[-1].strip())
d["maxsp"] = 100
d["maxep"] = 100
newMember = pssJsonToMember(d)
if newMember.name[:1] == "+" or state.manualMinions:
state = state._replace(pssHasMinions=True)
if newMember.name[:1] == "+":
newMember = newMember._replace(name=newMember.name[1:])
# leave invis to be handled by batclient messages
if newMember.name != "Someone":
handleNewMember(newMember)
def pssJsonToMember(j) -> Member:
unc = 0
if j["state"] == "unc":
unc = 1
elif j["state"][:4] == "unc|":
unc = int(j["state"][-1:]) + 2
stu = 0
if j["state"] == "stu":
stu = 1
elif j["state"][:4] == "stu|":
stu = int(j["state"][-1:])
return Member(
j["name"],
j["hp"],
j["maxhp"],
j["sp"],
j["maxsp"],
j["ep"],
j["maxep"],
Place(j["x"], j["y"]),
j["state"] == "form",
j["state"] == "mbr",
j["state"] == "ent",
j["state"] == "fol",
j["state"] == "ldr",
j["state"] == "ld",
j["state"] == "rest",
j["idle"],
j["name"] == "Someone",
j["state"] == "dead",
stu,
unc,
j["state"][:3] == "amb",
time(),
MemberDataSource.PSS,
)
def ginfo(s: str):
global state
# we'll want front row to be first, so this is done in a bit difficult way
names = list()
places = state.places
for y in [1, 2, 3]:
for x in [1, 2, 3]:
place = Place(x, y)
if place in places:
names.append(places[place].name)
for place in UNKNOWN_PLACES:
if place in places:
names.append(places[place].name)
tfeval("/python_call ginfo.partyReport {0}".format(" ".join(names)))
def toggleManualMinions(s: str):
global state
if state.manualMinions:
tfprint("Manual minions disabled")
state = state._replace(manualMinions=False)
else:
tfprint("Manual minions enabled")
state = state._replace(manualMinions=True)
def setup():
tfeval(
"/def -mglob -agGL -p10 -q -t'∴party *' bcproxy_party = "
+ "/python_call party.triggerPartyMsg \%-1"
)
tfeval(
"/def -mglob -agGL -p10 -q -t'∴partyleave *' bcproxy_partyleave = "
+ "/python_call party.triggerPartyLeave \%-1"
)
tfeval(
"/def -mglob -agGL -p10 -q -t'∴minionhpstatus *' bcproxy_minionhpstatus = "
+ "/python_call party.triggerMinionHpStatusMsg \%-1"
)
for x in range(1, 5):
for y in range(1, 5):
tfeval(
"/def -i -q -b'{0}' = /python_call party.changeTargetPlace {1} {2}".format(
PLACE_KEYBINDS[y][x], x, y
)
)
tfeval(
"/def -i -ag -c0 -p20 -mregexp -t`"
+ "^\\\|(.)(([1-3?])\\\.([1-3?]) )?" # idle, y, x
+ "([+]?[A-Za-z ]+) " # name {12}
+ "([a-z]+\\\|?[0-9]?)? +" # state
+ "([0-9]+)\\\( *([0-9]+)\\\) +" # hp / maxhp
+ "(([0-9]+)\\\( *([0-9]+)\\\) +" # sp / maxsp
+ "([0-9]+)\\\( *([0-9]+)\\\)|.+) +" # ep / maxep
+ "\\\| .+ \\\| .+ \\\|\$" # level + exp
+ "` party_pss = /python_call party.pssParse "
+ '{ "idle": "\%P1", "y": "\%P3", "x": "\%P4", '
+ '"name": "\%P5", '
+ '"state": "\%P6", '
+ '"hp": "\%P7", "maxhp": "\%P8", '
+ '"spepstring": "\%P9", '
+ '"sp": "\%P10", "maxsp": "\%P11", '
+ '"ep": "\%P12", "maxep": "\%P13" }'
+ ""
)
tfeval(
"/def -i -F -ag -p20 -msimple -t`"
+ ",-----------------------------------------------------------------------------."
+ "` party_pss_start = /python_call party.pssStart"
)
tfeval(
"/def -i -F -ag -c0 -p20 -msimple -t`"
+ "\\\`-----------------------------------------------------------------------------'"
+ "` party_pss_end = /python_call party.pssEnd"
)
tfprint("Loaded party.py")
setup()
state = State(frozenset([]), {}, {}, None, False, False)