<commit_before>#include "GalactiCombatServer.h"

GalactiCombatServer::GalactiCombatServer() : clients(0), state(PAUSE), scoreboard(), lastFrameTime(std::time(0))
{
    if(SDL_Init(0) != 0) {
        printf("SDL_Init done goofed: %s\n", SDL_GetError());
        exit(1);
    }
    
    if(SDLNet_Init() != 0) {
        printf("SDLNet_Init done goofed: %s\n",SDLNet_GetError());
    }
}

GalactiCombatServer::~GalactiCombatServer()
{
    SDLNet_Quit();
    SDL_Quit();
}

std::vector<Client*> GalactiCombatServer::getClients()
{
    return clients;
}

Client* GalactiCombatServer::findClientBySocket(TCPsocket sock)
{
    int i;
    for(i = 0; i < clients.size(); i++)
        if(clients[i]->sock == sock)
            return clients[i];
        return 0;
}

Client* GalactiCombatServer::findClientByName(std::string name)
{
    int i;
    for(i = 0; i < clients.size(); i++)
        if(clients[i]->name == name)
            return clients[i];
        return 0;
}

Client* GalactiCombatServer::addClient(TCPsocket sock, std::string name)
{
    printf("Adding client %s\n", const_cast<char*>(name.c_str()));
    
    clients.push_back(new Client());
    clients.back()->sock = sock;
    clients.back()->name = name;
    clients.back()->inputController = new ClientSpaceShipController();
    clients.back()->ready = false;
    
    double pos_x = (std::rand() % (ROOM_SIZE/2 - 250)) * (std::rand() % 2 == 0 ? 1 : -1);
    double pos_z = (std::rand() % (ROOM_SIZE/2 - 250)) * (std::rand() % 2 == 0 ? 1 : -1);
    double pos_y = (std::rand() % (ROOM_HIGH - 500)) + 250;
    spaceShips.push_back(new SpaceShip(name, mSoundMgr, clients.back()->inputController,
                                       mSceneMgr->getRootSceneNode()->createChildSceneNode(name + "ParentNode", Ogre::Vector3(pos_x, pos_y, pos_z)), NULL, 30 ));
    physicsSimulator->addGameObject(spaceShips.back(), RESTITUTION, true, true);
    
    return(clients.back());
}

void GalactiCombatServer::removeClient(int i)
{
    if(i < 0 && i >= clients.size())
        return;
    
    // Remove it from the game
    physicsSimulator->removeGameObject(spaceShips[i]);
    delete spaceShips[i];
    spaceShips.erase(spaceShips.begin() + i);
    
    // Remove it from our list of clients
    SDLNet_TCP_Close(clients[i]->sock);
    delete clients[i]->inputController;
    delete clients[i];
    clients.erase(clients.begin() + i);
}

/*
 *        sendToAll(char*, bool):
 *        This method takes the data stored in the address given in the first argument
 *        and sends it to all the clients that are connected.
 * 
 *        buf: the data to be sent to everyone
 *        TCP: a boolean that answers the question "Do you want to send this using TCP?"
 *                At the time this comment was written, UDPSend has not been written, so 
 *                always set this to true.
 */
void GalactiCombatServer::sendToAll(char *buf, bool TCP)
{
    TCP = true;	//NOTE: temporary
    
    if(!buf || !clients.size())
        return;
    
    for(int cindex = 0; cindex < clients.size(); )
    {
        if(TCP)
        {
            if(TCPSend(clients[cindex]->sock, buf))
                cindex++;
            else
            {
                std::cerr << "Failed to send message" << buf << "to " << clients[cindex]->name << std::endl;<commit_msg>Debugging<commit_after>#include "GalactiCombatServer.h"

GalactiCombatServer::GalactiCombatServer() : clients(0), state(PAUSE), scoreboard(), lastFrameTime(std::time(0))
{
    if(SDL_Init(0) != 0) {
        printf("SDL_Init done goofed: %s\n", SDL_GetError());
        exit(1);
    }
    
    if(SDLNet_Init() != 0) {
        printf("SDLNet_Init done goofed: %s\n",SDLNet_GetError());
    }
}

GalactiCombatServer::~GalactiCombatServer()
{
    SDLNet_Quit();
    SDL_Quit();
}

std::vector<Client*> GalactiCombatServer::getClients()
{
    return clients;
}

Client* GalactiCombatServer::findClientBySocket(TCPsocket sock)
{
    int i;
    for(i = 0; i < clients.size(); i++)
        if(clients[i]->sock == sock)
            return clients[i];
        return 0;
}

Client* GalactiCombatServer::findClientByName(std::string name)
{
    int i;
    for(i = 0; i < clients.size(); i++)
        if(clients[i]->name == name)
            return clients[i];
        return 0;
}

Client* GalactiCombatServer::addClient(TCPsocket sock, std::string name)
{
    printf("Adding client %s\n", const_cast<char*>(name.c_str()));
    
    clients.push_back(new Client());
    clients.back()->sock = sock;
    clients.back()->name = name;
    clients.back()->inputController = new ClientSpaceShipController();
    clients.back()->ready = false;
    
    double pos_x = (std::rand() % (ROOM_SIZE/2 - 250)) * (std::rand() % 2 == 0 ? 1 : -1);
    double pos_z = (std::rand() % (ROOM_SIZE/2 - 250)) * (std::rand() % 2 == 0 ? 1 : -1);
    double pos_y = (std::rand() % (ROOM_HIGH - 500)) + 250;
    spaceShips.push_back(new SpaceShip(name, mSoundMgr, clients.back()->inputController,
                                       mSceneMgr->getRootSceneNode()->createChildSceneNode(name + "ParentNode", Ogre::Vector3(pos_x, pos_y, pos_z)), NULL, 30 ));
    physicsSimulator->addGameObject(spaceShips.back(), RESTITUTION, true, true);
    
    return(clients.back());
}

void GalactiCombatServer::removeClient(int i)
{
    if(i < 0 && i >= clients.size())
        return;
    
    // Remove it from the game
    physicsSimulator->removeGameObject(spaceShips[i]);
    delete spaceShips[i];
    spaceShips.erase(spaceShips.begin() + i);
    
    // Remove it from our list of clients
    SDLNet_TCP_Close(clients[i]->sock);
    delete clients[i]->inputController;
    delete clients[i];
    clients.erase(clients.begin() + i);
}

/*
 *        sendToAll(char*, bool):
 *        This method takes the data stored in the address given in the first argument
 *        and sends it to all the clients that are connected.
 * 
 *        buf: the data to be sent to everyone
 *        TCP: a boolean that answers the question "Do you want to send this using TCP?"
 *                At the time this comment was written, UDPSend has not been written, so 
 *                always set this to true.
 */
void GalactiCombatServer::sendToAll(char *buf, bool TCP)
{
    TCP = true;	//NOTE: temporary
    
    if(!buf || !clients.size())
        return;
    
    for(int cindex = 0; cindex < clients.size(); )
    {
        if(TCP)
        {
            if(TCPSend(clients[cindex]->sock, buf)) {
                std::cout << "Sent message '" << buf << "' to " << clients[cindex]->name << std::endl;
                cindex++;
            }
            else
            {
                std::cerr << "Failed to send message '" << buf << "' to " << clients[cindex]->name << std::endl;
{'fraction_non_alphanumeric': 0.1513265154883652, 'fraction_numerical': 0.010967837557432933, 'mean_word_length': 2.722007722007722, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2229025', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2224, 'n_words': 510}