void Server::sendPlanesActives(int cfd){ PlanesActives* planes = new PlanesActives; planes->blue = true; planes->red = true; planes->green = true; planes->yellow = true; mutex theMutex; theMutex.lock(); for( map<int, Player*>::iterator it = this->players.begin(); it != this->players.end(); ++it ) { // if game is running and player is inactive, skip if( this->running && !( it->second->isActive() ) ) continue; // if already a player with that color if( it->second->getColor() == "azul" ) { planes->blue = false; } else if( it->second->getColor() == "rojo" ) { planes->red = false; } else if( it->second->getColor() == "verde" ) { planes->green = false; } else if( it->second->getColor() == "amarillo" ) { planes->yellow = false; } } theMutex.unlock(); Transmitter* tmt = new Transmitter( cfd, this->logger ); if( !( tmt->sendData( planes ) ) ) { DEBUG_WARN( "No se pude enviar respuesta a cliente. JOB: Server::addPlayer" ); this->logger->error( "No se pude enviar respuesta a cliente. JOB: Server::addPlayer" ); } delete planes; delete tmt; }
void Server::sendConf(int cfd){ this->config = GameParser::parse("gameconf.xml"); Transmitter* tmt = new Transmitter( cfd, this->logger ); AvionConf* avion = this->config->avion; if( !( tmt->sendData( avion ) ) ) { DEBUG_WARN( "No se pude enviar respuesta a cliente. JOB: Server::send avion" ); this->logger->error( "No se pude enviar respuesta a cliente. JOB: Server::send avion" ); } EscenarioConf* escenario = this->config->escenario; if( !( tmt->sendData( escenario ) ) ) { DEBUG_WARN( "No se pude enviar respuesta a cliente. JOB: Server::send escenario" ); this->logger->error( "No se pude enviar respuesta a cliente. JOB: Server::send escenario" ); } vector<ElementoConf*> elementos = this->config->elementos; for (int var = 0; var < elementos.size(); ++var) { ElementoConf* elemento = elementos[var]; if( !( tmt->sendData( elemento ) ) ) { DEBUG_WARN( "No se pude enviar respuesta a cliente. JOB: Server::send elementos" ); this->logger->error( "No se pude enviar respuesta a cliente. JOB: Server::send elementos" ); } } vector<SpriteConf* > sprites = this->config->sprites; for (int var = 0; var < sprites.size(); ++var) { SpriteConf* sprite = sprites[var]; if( !( tmt->sendData( sprite ) ) ) { DEBUG_WARN( "No se pude enviar respuesta a cliente. JOB: Server::send Sprites" ); this->logger->error( "No se pude enviar respuesta a cliente. JOB: Server::send Sprites" ); } } if( !( tmt->sendEndDataConf(this->clientCount)) ) { DEBUG_WARN( "No se pude enviar respuesta a cliente. JOB: Server::sendConf" ); this->logger->error( "No se pude enviar respuesta a cliente. JOB: Server::sendConf" ); } cout<<"Envio toda la Configuracion"<<endl; delete tmt; }
void Server::createPlayers() { map<int, Player*>::iterator it2 = this->players.begin(); for (int i = 0; i < this->players.size(); i++) { // if player not active if( !( it2->second->isActive() ) ) { // send stage config this->sendConf(it2->first); // if game already started if( this->running ) { // get stage offset this->queryCurrentStageOffset(); // send stage offset to player this->sendCurrentStageOffset( it2->first ); } } // send other players data for (map<int, Player*>::iterator it = this->players.begin(); it != this->players.end(); ++it) { Transmitter* tmt = new Transmitter(it2->first, this->logger); PlayerData* player = new PlayerData; strcpy(player->name, it->second->getName().c_str()); strcpy(player->color, it->second->getColor().c_str()); player->x = it->second->getX(); player->y = it->second->getY(); while (!tmt->sendData(player, "PR")); delete player; delete tmt; } // activate player it2->second->activate(); it2++; } }
void Server::sendData( int cfd, Evento* data ) { Transmitter* tmt = new Transmitter( cfd, this->logger ); tmt->sendData( data ); }
void Server::sendCurrentStageOffset( int clientFD ) { while( this->stageData == NULL ); Transmitter* tmt = new Transmitter( clientFD, this->logger ); tmt->sendData( this->stageData, "SD" ); delete tmt; }
void Server::addPlayer(PlayerData* data, int cfd) { string validName = "Y", validColor = "Y"; mutex theMutex; string selectedName(data->name); string selectedColor(data->color); bool createPlayer = true; bool encontrePlayer = false; theMutex.lock(); for (map<int, Player*>::iterator it = this->players.begin() ; it != this->players.end() ; ++it) { // if already a player with that name if (selectedName == it->second->getName()) { createPlayer = false; validName = "N"; cout<<"Encuentro Player"<<endl; // if running game and player with such name is not active if ( this->running && !(it->second->isActive())) { // resume player game cout<<"Resume Game"<<endl; selectedColor = it->second->getColor(); posicionInicialX = it->second->getX(); posicionInicialY = it->second->getY(); delete it->second; this->players.erase(it); createPlayer = true; validName = "R"; validColor = "R"; break; } } // if already a player with that color if (selectedColor == it->second->getColor()) { createPlayer = false; validColor = "N"; } } theMutex.unlock(); if (createPlayer && (this->players.size() < this->maxClientCount) ) { // Add new player cout<<"Creo Jugador"<<endl; Player* p = new Player(selectedName, selectedColor, posicionInicialX, posicionInicialY); theMutex.lock(); this->players[cfd] = p; posicionInicialX += 100; theMutex.unlock(); } else { cout<<"No creo jugador"<<endl; createPlayer = false; validName = "N"; validColor = "N"; } // Create response PlayerData* response = new PlayerData; // Fill response struct strcpy(response->name, validName.c_str()); strcpy(response->color, validColor.c_str()); cout<<"name :"<<validName<<" .Color: "<<validColor<<endl; Transmitter* tmt = new Transmitter(cfd, this->logger); if (!(tmt->sendData(response))) { DEBUG_WARN("No se pude enviar respuesta a cliente. JOB: Server::addPlayer"); this->logger->error("No se pude enviar respuesta a cliente. JOB: Server::addPlayer"); } delete response; delete tmt; theMutex.lock(); if (createPlayer && this->players.size() == this->maxClientCount) { cout << "send players" << endl; this->createPlayers(); if( !( this->running ) ) this->running = true; } theMutex.unlock(); }