def getClient(self, cid, guid=None): """ Get a connected client from storage or create it B3 CID <--> character name B3 GUID <--> EA_guid """ client = None if guid: # try to get the client from the storage of already authed clients by guid client = self.clients.getByGUID(guid) if not client: # try to get the client from the storage of already authed clients by name 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, teamId=None, squadId=None) if guid: client = self.clients.newClient(cid, guid=guid, name=cid, team=b3.TEAM_UNKNOWN, teamId=None, squad=None) else: # must be the first time we see this client words = self.write(('admin.listPlayers', 'player', cid)) pib = PlayerInfoBlock(words) if not len(pib): self.debug('No such client found') return None p = pib[0] if 'guid' in p: client = self.clients.newClient(p['name'], guid=p['guid'], name=p['name'], team=self.getTeam(p['teamId']), teamId=int(p['teamId']), squad=p['squadId'], data=p) self.queueEvent(self.getEvent('EVT_CLIENT_JOIN', data=p, client=client)) return client
def test_R38_no_player(self): bloc = PlayerInfoBlock([ '8', 'name', 'guid', 'teamId', 'squadId', 'kills', 'deaths', 'score', 'rank', '0' ]) self.assertEqual(0, len(bloc)) self.assertRaises(IndexError, bloc.__getitem__, 0)
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 test_slice(self): bloc = PlayerInfoBlock(['2','param1','param2','4','player0-p1','player0-p2','player1-p1','player1-p2', 'player2-p1','player2-p2','player3-p1','player3-p2' ]) self.assertEqual(4, len(bloc)) self.assertEqual(2, len(bloc[1:3])) self.assertEqual('player1-p1', bloc[1:3][0]['param1']) self.assertEqual('player1-p2', bloc[1:3][0]['param2']) self.assertEqual('player2-p1', bloc[1:3][1]['param1']) self.assertEqual('player2-p2', bloc[1:3][1]['param2']) self.assertEqual("[{'param2': 'player1-p2', 'param1': 'player1-p1'}, {'param2': 'player2-p2', 'param1': 'player2-p1'}]", repr(bloc[1:3]))
def generate_scores(): scores = fakeConsole.getPlayerScores() score_list = ['2','name','score', len(scores)] for k, v in scores.items(): score_list.append(k) score_list.append(v) client = fakeConsole.getClient(k) if client: client.score = v p._scrambler._last_round_scores = PlayerInfoBlock(score_list)
def _get_player_type(self): """ Queries the type of player from the server. """ _player_type = 0 _player_name = self.name try: _player_info_block = PlayerInfoBlock(self.console.write(('admin.listPlayers', 'player', _player_name))) return int(_player_info_block[0]['type']) except Exception as err: self.console.error("Could not get player_type for player %s: %s" % (self.name, err), exc_info=err)
def test_R38_one_player(self): bloc = PlayerInfoBlock(['8', 'name', 'guid', 'teamId', 'squadId', 'kills', 'deaths', 'score', 'rank', '1', 'Cucurbitaceae', '', '1', '1', '0', '0', '0', '58']) self.assertEqual(1, len(bloc)) p1 = bloc[0] self.assertEqual("Cucurbitaceae", p1['name']) self.assertEqual("", p1['guid']) self.assertEqual("1", p1['teamId']) self.assertEqual("1", p1['squadId']) self.assertEqual("0", p1['kills']) self.assertEqual("0", p1['deaths']) self.assertEqual("0", p1['score']) self.assertEqual("58", p1['rank'])
def getPlayerPings(self, filter_client_ids=None): """ Ask the server for a given client's pings :param filter_client_ids: If filter_client_id is an iterable, only return values for the given client ids. """ pings = {} if filter_client_ids is None: filter_client_ids = [] try: player_info_block = PlayerInfoBlock(self.write(('admin.listPlayers', 'all'))) for player in player_info_block: if player['name'] in filter_client_ids or len(filter_client_ids) == 0: pings[player['name']] = int(player['ping']) except (ValueError, TypeError): pass # continue if the ping value is empty except Exception as err: self.error('Unable to retrieve pings from player list', exc_info=err) return pings
class Scrambler: _plugin = None _getClients_method = None _last_round_scores = PlayerInfoBlock([0, 0]) def __init__(self, plugin): self._plugin = plugin 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 x, y: x + 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 x: x['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' % map(lambda x: x.cid, sortedClients)) random.shuffle(allClients) for client in allClients: # add remaining clients (they had no score ?) sortedClients.append(client) self.debug('sorted clients B : %r' % map(lambda x: x.cid, sortedClients)) return sortedClients def debug(self, msg): self._plugin.debug('scramber:\t %s' % msg)
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))
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'])))