def send_initial_data(self, player): """ Send the initial data (global data from the configuration file) """ print "Sending initial data to player #%d" % player for option in self.config.options('initial'): send_player_message(self.players_connection[player], '%s %.2f\n' % (option, self.config.getfloat('initial', option))) print '%s %f' % (option, self.config.getfloat('initial', option)) send_player_message(self.players_connection[player], 'end\n')
def send_turn_data(self, player): """ Sends all messages in the queue then clear the queue """ print '- send_turn_data - #%d' % player player_conn = self.players_connection[player] # 1) Send contextual data send_player_message(player_conn, 'turn %d\n' % self.turn) send_player_message(player_conn, 'players %d\n' % len(self.players_left)) send_player_message(player_conn, 'scores %s\n' % ' '.join([str(elt) for elt in self.team_scores])) for message in self.players_message[player]: # Sends the message to the player send_player_message(player_conn, message.__str__() + '\n') # Clear message queue self.players_message[player] = [] send_player_message(player_conn, 'end\n')
def manage_player_connection(self, host, port): """ Awaits for every player to connect to the server and fill local connections """ i = 1 # Corresponds to the number of player managed while i <= self.arena.nb_players: print "Waiting for player #%d to connect" % i player_conn = get_player_connection(host, port, self.config.getfloat('initial', 'timing_limit')) self.players_connection[i] = player_conn # Awaits for nick identification msg = get_player_message(player_conn) nick_re = r"nick\s+(\w+)" m = re.match(nick_re, msg) if not m: # The message received does not match print "A player attempted to connect but his message was not correct" print "Awaiting a message that looks like: nick <pseudo>, but received %s" % msg # Close the connection player_conn.close() continue nick = m.group(1) if nick in self.players_nick.values(): nick += '_' self.players_nick[i] = nick print "Player #%d connected - nick %s" % (i, nick) # Answer back the chosen nick send_player_message(player_conn, "nick %s %d\n" % (nick, i)) # Send initial data self.send_initial_data(i) # Initial zombie for this player for z in self.entities.get_player_zombies(i): self.players_message[i].append(message.ZombieMessage(z._id, z._row, z._col)) i += 1
def manage_end_game(self): """ This function deals with the end of a game It sends 'end' message to players left then send final data to all players """ print '--- manage_end_game' # Send end to players left for player in xrange(1, self.arena.nb_players + 1): send_player_message(self.players_connection[player], 'end\n') # Compute winners/loosers loosers = range(1, self.arena.nb_players + 1) if len(self.players_left) == 1: # Only one player left = winner, others = loosers winners = self.players_left loosers.remove(winners[0]) else: # No players left or at least two players if len(self.players_left) == 0: # No players left, look for max score for all players possible_winners = range(1, self.arena.nb_players + 1) else: # Left players, the winner is among them possible_winners = self.players_left # Look for max max_score = max(self.team_scores) winners = [player for player in possible_winners if self.team_scores[player - 1] == max_score] loosers = [player for player in loosers if player not in winners] print 'winners are:', ', '.join([str(elt) for elt in winners]) print 'loosers are:', ', '.join([str(elt) for elt in loosers]) # for all players, sends final data for player in winners + loosers: send_player_message(self.players_connection[player], 'final %d %d\n' % ((1 if player in winners else 0), self.turn)) send_player_message(self.players_connection[player], 'scores %s\n' % (' '.join([str(elt) for elt in self.team_scores]))) self.players_connection[player].close()