-
Notifications
You must be signed in to change notification settings - Fork 4
/
PlayerManager.py
255 lines (211 loc) · 8.33 KB
/
PlayerManager.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
import host
import string
from time import time
from bf2 import g_debug
# Import Config Data
from bf2.BF2StatisticsConfig import stats_enable, http_backend_addr, http_backend_port
# Use MiniClient
from bf2.stats.miniclient import miniclient, http_get
# ingame scoremanager link
ingameScores = (
'deaths',
'kills',
'TKs',
'score',
'skillScore',
'rplScore',
'cmdScore',
'fracScore',
'rank',
'firstPlace',
'secondPlace',
'thirdPlace',
'bulletsFired',
'bulletsGivingDamage',
'bulletsFiredAndClear',
'bulletsGivingDamageAndClear'
)
class PlayerScore:
def __init__(self, index):
self.__dict__['index'] = index
self.reset()
def reset(self):
# these scores are only tracked in script
self.__dict__['heals'] = 0
self.__dict__['ammos'] = 0
self.__dict__['repairs'] = 0
self.__dict__['damageAssists'] = 0
self.__dict__['passengerAssists'] = 0
self.__dict__['driverAssists'] = 0
self.__dict__['targetAssists'] = 0
self.__dict__['driverSpecials'] = 0
self.__dict__['revives'] = 0
self.__dict__['teamDamages'] = 0
self.__dict__['teamVehicleDamages'] = 0
self.__dict__['cpCaptures'] = 0
self.__dict__['cpDefends'] = 0
self.__dict__['cpAssists'] = 0
self.__dict__['suicides'] = 0
self.__dict__['cpNeutralizes'] = 0
self.__dict__['cpNeutralizeAssists'] = 0
def __getattr__(self, name):
if name in self.__dict__: return self.__dict__[name]
elif name == 'dkRatio':
kills = host.pmgr_getScore(self.index, 'kills')
if kills == 0:
# div by zero is undefined -> 0:0 = 1 1:0 = 2 1:1 = 1
return 1.0 * host.pmgr_getScore(self.index, 'deaths') + 1
else:
return 1.0 * host.pmgr_getScore(self.index, 'deaths') / kills
elif name in ingameScores:
return host.pmgr_getScore(self.index, name)
else:
raise AttributeError, name
def __setattr__(self, name, value):
if name in self.__dict__:
self.__dict__[name] = value
return None
elif name in ingameScores:
return host.pmgr_setScore(self.index, name, value)
else:
raise AttributeError, name
class Player:
def __init__(self, index):
print "Player Manager module initialized"
self.index = index
self.score = PlayerScore(index)
self.profileid = 0
self.GlobalUpdateTime = 0
def isValid(self): return host.pmgr_isIndexValid(self.index)
def isRemote(self): return host.pmgr_p_get("remote", self.index)
def isAIPlayer(self): return host.pmgr_p_get("ai", self.index)
def isAlive(self): return host.pmgr_p_get("alive", self.index)
def isManDown(self): return host.pmgr_p_get("mandown", self.index)
def isConnected(self): return host.pmgr_p_get("connected", self.index)
# Added by Chump - for bf2statistics stats
# ------------------------------------------------------------------------------
# omero 2006-03-31
# ------------------------------------------------------------------------------
# using pm_local_pid_txt_file imported from BF2StatisticsConfig module
# for reading Nick/pID mappings.
# ------------------------------------------------------------------------------
# Wilson212, 2015-02-17
# ------------------------------------------------------------------------------
# Removed the method of fetching the pid from the pid.txt file. As omero has coded
# after the fact, the pid is now fetched from the back end, and has rendered the local
# pid text file obsolete. This also prevents pid clashes when using more then 1 server
# on an ASP back end server
# ------------------------------------------------------------------------------
def getProfileId(self):
# No stats, Then we dont bother fetching Pid from ASP
if not stats_enable:
return host.pmgr_p_get("profileid", self.index)
pid = self.profileid
# If we are a human player, attempt to get PID from the bf2 server
if not host.pmgr_p_get("ai", self.index) and pid == 0:
pid = int(host.pmgr_p_get("profileid", self.index))
# If we don't have a PID up to this point, fetch it from ASP back end
if pid == 0:
if g_debug: print "Retrieving Profile ID (%s) via HTTP/1.1 miniclient" % str(host.pmgr_p_get("name", self.index))
# URL for retrieving player ID via internal miniclient
player_nick = string.replace(str(host.pmgr_p_get("name", self.index)), ' ', '%20')
asp_playerid = '/ASP/getplayerid.aspx?nick=' + player_nick + '&ai=' + str(host.pmgr_p_get("ai", self.index))
if g_debug: print "URI: %s" % (asp_playerid)
# Fetch Data using miniclient
data = http_get( http_backend_addr, http_backend_port, asp_playerid )
if data and data[0] == 'O':
if g_debug: print "Received PID data is VALID, length %d" % int(len(data))
datalines = data.splitlines()
pidval = datalines[2].split('\t')
pid = int(pidval[1])
else:
print "Received PID data is INVALID, length %d" % int(len(data))
self.profileid = pid
return pid
def isFlagHolder(self): return host.pmgr_p_get("fholder", self.index)
def getTeam(self): return host.pmgr_p_get("team", self.index)
def setTeam(self, t): return host.pmgr_p_set("team", self.index, t)
def getPing(self): return host.pmgr_p_get("ping", self.index)
def getSuicide(self): return host.pmgr_p_get("suicide", self.index)
def setSuicide(self, t): return host.pmgr_p_set("suicide", self.index, t)
def getTimeToSpawn(self): return host.pmgr_p_get("tts", self.index)
def setTimeToSpawn(self, t): return host.pmgr_p_set("tts", self.index, t)
def getSquadId(self): return host.pmgr_p_get("sqid", self.index)
def isSquadLeader(self): return host.pmgr_p_get("isql", self.index)
def isCommander(self): return host.pmgr_p_get("commander", self.index)
def getName(self): return host.pmgr_p_get("name", self.index)
def setName(self, name): return host.pmgr_p_set("name", self.index, name)
def getSpawnGroup(self): return host.pmgr_p_get("sgr", self.index)
def setSpawnGroup(self, t): return host.pmgr_p_set("sgr", self.index, t)
def getKit(self): return host.pmgr_p_get("kit", self.index)
def getVehicle(self): return host.pmgr_p_get("vehicle", self.index)
def getDefaultVehicle(self): return host.pmgr_p_get("defaultvehicle", self.index)
def getPrimaryWeapon(self): return host.pmgr_p_get("weapon", self.index, 0)
def getAddress(self): return host.pmgr_p_get("addr", self.index)
def setIsInsideCP(self, val): return host.pmgr_p_set("isInsideCP", self.index, val)
def getIsInsideCP(self): return host.pmgr_p_get("isInsideCP", self.index)
# Functions used to stop STATS "update storm"
def setGlobalUpdateTime(self):
self.GlobalUpdateTime = time()
return self.GlobalUpdateTime
def getGlobalUpdateTime(self): return (time() - self.GlobalUpdateTime)
class PlayerManager:
def __init__(self):
print "PlayerManager created"
self._pcache = {}
def getNumberOfPlayers(self):
return host.pmgr_getNumberOfPlayers()
def getCommander(self, team):
return self.getPlayerByIndex(host.pmgr_getCommander(team))
def getPlayers(self):
indices = host.pmgr_getPlayers()
players = []
# NOTE: this uses getPlayerByIndex so we return cached player objects
# whenever we can
for i in indices: players.append(self.getPlayerByIndex(i))
return players
def getPlayerByIndex(self, index):
# dep: this uses a cache so that all references to a certain player
# index will always yield the same object, which is useful because you
# can then test them for object equality
valid = host.pmgr_isIndexValid(index)
if not valid:
if self._pcache.has_key(index):
print "Removed player index %d from player cache" % index
del self._pcache[index]
return None
if not self._pcache.has_key(index):
self._pcache[index] = Player(index)
return self._pcache[index]
def getNextPlayer(self, index):
startIndex = index
p = None
index = index + 1
while p == None and index != startIndex:
p = self.getPlayerByIndex(index)
index = index + 1
if index > 255: index = 0
if index > 63: index = 255
if not p:
return self.getPlayerByIndex(startIndex)
else:
return p
def getNumberOfPlayersInTeam(self, team):
players = self.getPlayers()
inTeam = 0
for p in players:
if p.getTeam() == team:
inTeam += 1
return inTeam
def getNumberOfAlivePlayersInTeam(self, team):
players = self.getPlayers()
inTeam = 0
for p in players:
if p.getTeam() == team and p.isAlive():
inTeam += 1
return inTeam
# allows temporary disabling of the onPlayerScore event.
def enableScoreEvents(self):
return host.pmgr_enableScoreEvents(1)
def disableScoreEvents(self):
return host.pmgr_enableScoreEvents(0)