<commit_before># coding=utf-8

"""This module, quasar_world_server.py, defines the class and operations for running the QuasarWorldServer."""

from channels import Group
from channels.sessions import channel_session


WEB_SOCKET_MESSAGE_TYPE_ALL_PLAYERS                 = '|A|'
WEB_SOCKET_MESSAGE_TYPE_CONNECTION                  = '|C|'
WEB_SOCKET_MESSAGE_TYPE_DISCONNECTED                = '|D|'
WEB_SOCKET_MESSAGE_TYPE_CHAT_MESSAGE                = '|M|'
WEB_SOCKET_MESSAGE_TYPE_LOOK_AT_UPDATE              = '|L|'
WEB_SOCKET_MESSAGE_TYPE_POSITION_UPDATE             = '|P|'
WEB_SOCKET_MESSAGE_TYPE_POSITION_AND_LOOK_AT_UPDATE = '|U|'

SERVER_USER_ID = 'server'


def send_message_to_all_users_in_group(message, group_name):
    """Sends a message to all users in the specified group."""
    Group(group_name).send({'text': message})


def send_chat_message_to_all_logged_in_users(chat_message):
    """Sends a global server message to everyone currently logged in."""
    Group('users').send({'text': SERVER_USER_ID + WEB_SOCKET_MESSAGE_TYPE_CHAT_MESSAGE + chat_message})


class QuasarPlayer(object):
    """Represents a single logged in user."""

    def __init__(self, reply_channel_key):
        self._reply_channel_key = reply_channel_key
        self._player_name       = None
        self._last_position     = None
        self._last_look_at      = None

    def set_last_position(self, position):
        """Sets the last known position of this player."""
        self._last_position = position

    def set_last_look_at(self, look_at):
        """Sets the last known look at of this player."""
        self._last_look_at = look_at

    @property
    def web_socket_key(self):
        """Returns the unique web socket key that this player currently has."""
        return self._reply_channel_key

    @property
    def player_name(self):
        """Returns the name of the player."""
        return self._player_name

    @player_name.setter
    def player_name(self, val):
        """Sets the player name for this object."""
        self._player_name = val

    def get_name_and_last_position(self):
        """Returns the name and last position of this player."""
        if str(self._last_position) == 'None':
            self._last_position = '0,0,0'
        if str(self._last_look_at) == 'None':
            self._last_look_at = '0,0,0'
        return self._player_name + '!' + str(self._last_position) + '!' + str(self._last_look_at)


class QuasarPlayerServer(object):
    """Handles the multi-player logic."""

    # TODO : Need to add a tick rate for sending out messages.
    # Currently every time a user sends a message all other users will get notifications as well, immediately.
    # Adding a tick rate will better ensure smooth performance as number of con-current users grow.

    def __init__(self):
        self._clients = []

    def get_all_players_data(self):
        """Returns the names and last position of all currently logged in players."""
        all_data = ''
        for p in self._clients:
            all_data += p.get_name_and_last_position() + '@'
        return all_data

    def get_specific_player(self, player_name):
        """Gets the specific player object."""
        for p in self._clients:
            if p.player_name == player_name:
                return p
        return None

    def parse_message(self, user, command, data, reply_channel_key):
        """Parses the message passed in and performs the needed operations."""

        print('Parsing the following :')
        print(user)
        print(command)
        print(data)

        if command == WEB_SOCKET_MESSAGE_TYPE_CONNECTION:
            self.player_logged_in(reply_channel_key, data)
            reply_channel_key.send({'text': SERVER_USER_ID + WEB_SOCKET_MESSAGE_TYPE_ALL_PLAYERS + self.get_all_players_data()})
        elif command == WEB_SOCKET_MESSAGE_TYPE_POSITION_UPDATE:
            player = self.get_specific_player(user)
            if player is None:
                print('Look into this!!')
            else:
                player.set_last_position(data)
            send_message_to_all_users_in_group(user + command + data, 'users')
        elif command == WEB_SOCKET_MESSAGE_TYPE_LOOK_AT_UPDATE:
            player = self.get_specific_player(user)
            if player is None:
                print('Look into this!!')
            else:
                player.set_last_look_at(data)
            send_message_to_all_users_in_group(user + command + data, 'users')
        elif command == WEB_SOCKET_MESSAGE_TYPE_POSITION_AND_LOOK_AT_UPDATE:
            player = self.get_specific_player(user)<commit_msg>general updating 28<commit_after># coding=utf-8

"""This module, quasar_world_server.py, defines the class and operations for running the QuasarWorldServer."""

from channels import Group
from channels.sessions import channel_session


WEB_SOCKET_MESSAGE_TYPE_ALL_PLAYERS                 = '|A|'
WEB_SOCKET_MESSAGE_TYPE_CONNECTION                  = '|C|'
WEB_SOCKET_MESSAGE_TYPE_DISCONNECTED                = '|D|'
WEB_SOCKET_MESSAGE_TYPE_CHAT_MESSAGE                = '|M|'
WEB_SOCKET_MESSAGE_TYPE_LOOK_AT_UPDATE              = '|L|'
WEB_SOCKET_MESSAGE_TYPE_POSITION_UPDATE             = '|P|'
WEB_SOCKET_MESSAGE_TYPE_POSITION_AND_LOOK_AT_UPDATE = '|U|'

SERVER_USER_ID = 'server'


def send_message_to_all_users_in_group(message, group_name):
    """Sends a message to all users in the specified group."""
    Group(group_name).send({'text': message})


def send_chat_message_to_all_logged_in_users(chat_message):
    """Sends a global server message to everyone currently logged in."""
    Group('users').send({'text': SERVER_USER_ID + WEB_SOCKET_MESSAGE_TYPE_CHAT_MESSAGE + chat_message})


class QuasarPlayer(object):
    """Represents a single logged in user."""

    def __init__(self, reply_channel_key):
        self._reply_channel_key = reply_channel_key
        self._player_name       = None
        self._last_position     = None
        self._last_look_at      = None

    def set_last_position(self, position):
        """Sets the last known position of this player."""
        self._last_position = position

    def set_last_look_at(self, look_at):
        """Sets the last known look at of this player."""
        self._last_look_at = look_at

    @property
    def web_socket_key(self):
        """Returns the unique web socket key that this player currently has."""
        return self._reply_channel_key

    @property
    def player_name(self):
        """Returns the name of the player."""
        return self._player_name

    @player_name.setter
    def player_name(self, val):
        """Sets the player name for this object."""
        self._player_name = val

    def get_name_and_last_position(self):
        """Returns the name and last position of this player."""
        if str(self._last_position) == 'None':
            self._last_position = '0,0,0'
        if str(self._last_look_at) == 'None':
            self._last_look_at = '0,0,0'
        return self._player_name + '!' + str(self._last_position) + '!' + str(self._last_look_at)


class QuasarPlayerServer(object):
    """Handles the multi-player logic."""

    # TODO : Need to add a tick rate for sending out messages.
    # Currently every time a user sends a message all other users will get notifications as well, immediately.
    # Adding a tick rate will better ensure smooth performance as number of con-current users grow.

    def __init__(self):
        self._clients = []

    def get_all_players_data(self):
        """Returns the names and last position of all currently logged in players."""
        all_data = ''
        for p in self._clients:
            all_data += p.get_name_and_last_position() + '@'
        return all_data

    def get_specific_player(self, player_name):
        """Gets the specific player object."""
        for p in self._clients:
            if p.player_name == player_name:
                return p
        return None

    def parse_message(self, user, command, data, reply_channel_key):
        """Parses the message passed in and performs the needed operations."""

        print('Parsing the following :')
        print(user)
        print(command)
        print(data)

        for p in self._clients:
            print(str(p.player_name) + '\t' + str(user) + '\t' + str(p.player_name == user))

        if command == WEB_SOCKET_MESSAGE_TYPE_CONNECTION:
            self.player_logged_in(reply_channel_key, data)
            reply_channel_key.send({'text': SERVER_USER_ID + WEB_SOCKET_MESSAGE_TYPE_ALL_PLAYERS + self.get_all_players_data()})
        elif command == WEB_SOCKET_MESSAGE_TYPE_POSITION_UPDATE:
            player = self.get_specific_player(user)
            if player is None:
                print('Look into this!!')
            else:
                player.set_last_position(data)
            send_message_to_all_users_in_group(user + command + data, 'users')
        elif command == WEB_SOCKET_MESSAGE_TYPE_LOOK_AT_UPDATE:
            player = self.get_specific_player(user)
            if player is None:
                print('Look into this!!')
            else:
                player.set_last_look_at(data)
            send_message_to_all_users_in_group(user + command + data, 'users')
        elif command == WEB_SOCKET_MESSAGE_TYPE_POSITION_AND_LOOK_AT_UPDATE:
            player = self.get_specific_player(user)
{'fraction_non_alphanumeric': 0.0900212314225053, 'fraction_numerical': 0.0016985138004246285, 'mean_word_length': 2.811084142394822, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1094441', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2763, 'n_words': 703}