def test_minimal(self): self.assertEqual(0, len(PlayerInfoBlock([0, 0]))) self.assertEqual('PlayerInfoBlock[]', repr(PlayerInfoBlock([0, 0]))) self.assertEqual(0, len(PlayerInfoBlock(['0', '0']))) self.assertEqual(0, len(PlayerInfoBlock(['1', 'test', '0']))) self.assertEqual('PlayerInfoBlock[]', repr(PlayerInfoBlock(['1', 'test', '0'])))
def OnServerRoundoverplayers(self, action, data): """ server.onRoundOverPlayers <end-of-round soldier info : player info block> Effect: The round has just ended, and <end-of-round soldier info> is the final detailed player stats """ #['server.onRoundOverPlayers', '8', 'clanTag', 'name', 'guid', 'teamId', 'kills', 'deaths', 'score', 'ping', # '17', 'RAID', 'mavzee', 'EA_4444444444444444555555555555C023', '2', '20', '17', '310', '147', 'RAID', 'NUeeE', # 'EA_1111111111111555555555555554245A', '2', '30', '18', '445', '146', '', 'Strzaerl', # 'EA_88888888888888888888888888869F30', '1', '12', '7', '180', '115', '10tr', 'russsssssssker', # 'EA_E123456789461416564796848C26D0CD', '2', '12', '12', '210', '141', '', 'Daezch', # 'EA_54567891356479846516496842E17F4D', '1', '25', '14', '1035', '129', '', 'Oldqsdnlesss', # 'EA_B78945613465798645134659F3079E5A', '1', '8', '12', '120', '256', '', 'TTETqdfs', # 'EA_1321654656546544645798641BB6D563', '1', '11', '16', '180', '209', '', 'bozer', # 'EA_E3987979878946546546565465464144', '1', '22', '14', '475', '152', '', 'Asdf 1977', # 'EA_C65465413213216656546546546029D6', '2', '13', '16', '180', '212', '', 'adfdasse', # 'EA_4F313565464654646446446644664572', '1', '4', '25', '45', '162', 'SG1', 'De56546ess', # 'EA_123132165465465465464654C2FC2FBB', '2', '5', '8', '75', '159', 'bsG', 'N06540RZ', # 'EA_787897944546565656546546446C9467', '2', '8', '14', '100', '115', '', 'Psfds', # 'EA_25654321321321000006546464654B81', '2', '15', '15', '245', '140', '', 'Chezear', # 'EA_1FD89876543216548796130EB83E411F', '1', '9', '14', '160', '185', '', 'IxSqsdfOKxI', # 'EA_481321313132131313213212313112CE', '1', '21', '12', '625', '236', '', 'Ledfg07', # 'EA_1D578987994651615166516516136450', '1', '5', '6', '85', '146', '', '5 56 mm', # 'EA_90488E6543216549876543216549877B', '2', '0', '0', '0', '192'] return self.getEvent('EVT_GAME_ROUND_PLAYER_SCORES', PlayerInfoBlock(data))
def test_2(self): bloc = PlayerInfoBlock(['1', 'param1', '2', 'bla1', 'bla2']) self.assertEqual(2, len(bloc)) self.assertEqual('bla1', bloc[0]['param1']) self.assertEqual('bla2', bloc[1]['param1']) self.assertEqual( "PlayerInfoBlock[{'param1': 'bla1'}{'param1': 'bla2'}]", repr(bloc))
def getPlayerScores(self): """Ask the server for a given client's team """ scores = {} try: pib = PlayerInfoBlock(self.write(('admin.listPlayers', 'all'))) for p in pib: scores[p['name']] = int(p['score']) except: self.debug('Unable to retrieve scores from playerlist') return scores
def getPlayerPings(self): """Ask the server for a given client's pings """ pings = {} try: pib = PlayerInfoBlock(self.write(('admin.listPlayers', 'all'))) for p in pib: pings[p['name']] = int(p['ping']) except: self.debug('Unable to retrieve pings from playerlist') return pings
def getPlayerList(self, maxRetries=None): """return a dict which keys are cid and values a dict of player properties as returned by admin.listPlayers. Does not return client objects""" data = self.write(('admin.listPlayers', 'all')) if not data: return {} players = {} pib = PlayerInfoBlock(data) for p in pib: players[p['name']] = p return players
def getClient(self, cid, _guid=None): """ Get a connected client from storage or create it B3 CID <--> ingame character name B3 GUID <--> EA_guid """ # try to get the client from the storage of already authed clients client = self.clients.getByCID(cid) if not client: if cid == 'Server': return self.clients.newClient('Server', guid='Server', name='Server', hide=True, pbid='Server', team=b3.TEAM_UNKNOWN, squad=SQUAD_NEUTRAL) # must be the first time we see this client words = self.write(('admin.listPlayers', 'player', cid)) pib = PlayerInfoBlock(words) if len(pib) == 0: self.debug('No such client found') return None p = pib[0] cid = p['name'] name = p['name'] # Let's see if we have a guid, either from the PlayerInfoBlock, # or passed to us by OnPlayerAuthenticated() if p['guid']: guid = p['guid'] elif _guid: guid = _guid else: # If we still don't have a guid, we cannot create a newclient without the guid! self.debug('No guid for %s, waiting for next event' % name) return None if 'clanTag' in p and len(p['clanTag']) > 0: name = "[" + p['clanTag'] + "] " + p['name'] client = self.clients.newClient(cid, guid=guid, name=name, team=self.getTeam(p['teamId']), teamId=int(p['teamId']), squad=p['squadId'], data=p) self.queueEvent(self.getEvent('EVT_CLIENT_JOIN', p, client)) return client
def test_1(self): bloc = PlayerInfoBlock(['1', 'param1', '1', 'blabla']) self.assertEqual(1, len(bloc)) self.assertEqual('blabla', bloc[0]['param1']) self.assertEqual("PlayerInfoBlock[{'param1': 'blabla'}]", repr(bloc))
class Scrambler(object): _plugin = None _getClients_method = None _last_round_scores = PlayerInfoBlock([0, 0]) def __init__(self): self._getClients_method = self._getClients_randomly def scrambleTeams(self): clients = self._getClients_method() if len(clients) == 0: return elif len(clients) < 3: self.debug("Too few players to scramble") else: self._scrambleTeams(clients) def setStrategy(self, strategy): """Set the scrambling strategy""" if strategy.lower() == 'random': self._getClients_method = self._getClients_randomly elif strategy.lower() == 'score': self._getClients_method = self._getClients_by_scores else: raise ValueError def onRoundOverTeamScores(self, playerInfoBlock): self._last_round_scores = playerInfoBlock def _scrambleTeams(self, listOfPlayers): team = 0 while len(listOfPlayers) > 0: self._plugin._movePlayer(listOfPlayers.pop(), team + 1) team = (team + 1) % 2 def _getClients_randomly(self): clients = self._plugin.console.clients.getList() random.shuffle(clients) return clients def _getClients_by_scores(self): allClients = self._plugin.console.clients.getList() self.debug('all clients : %r' % [x.cid for x in allClients]) sumofscores = reduce( lambda z, y: z + y, [int(data['score']) for data in self._last_round_scores], 0) self.debug('sum of scores is %s' % sumofscores) if sumofscores == 0: self.debug('no score to sort on, using ramdom strategy instead') random.shuffle(allClients) return allClients else: sortedScores = sorted(self._last_round_scores, key=lambda z: z['score']) self.debug('sorted score : %r' % sortedScores) sortedClients = [] for cid in [x['name'] for x in sortedScores]: # find client object for each player score clients = [c for c in allClients if c.cid == cid] if clients and len(clients) > 0: allClients.remove(clients[0]) sortedClients.append(clients[0]) self.debug('sorted clients A : %r' % [z.cid for z in sortedClients]) random.shuffle(allClients) for client in allClients: # add remaining clients (they had no score ?) sortedClients.append(client) self.debug('sorted clients B : %r' % [z.cid for z in sortedClients]) return sortedClients def debug(self, msg): self._plugin.debug('scramber:\t %s' % msg)