コード例 #1
0
ファイル: main.cpp プロジェクト: huwdp/Chat-Client-and-Server
void handleNewMessage(char* inBuffer, int size, char** outBuffer, int *outSize)
{
    //Create a Chat Message
    ChatMessage *m = new ChatMessage();
    //Decode Chat Message
    m->decode(inBuffer, size);
    //Search the users list for nickname
    bool found = false;

    pthread_mutex_lock(&userListMutex);
    for(vector<Join*>::iterator it = users.begin(); it != users.end(); ++it)
    {
        Join *user = (*it);
        //Remove nickname if it exists
        if (strcmp(m->getSender(), user->getNickname()) == 0)
        {
            found = true;
        }
    }
    pthread_mutex_unlock(&userListMutex);

    //If the name exists add the name and message to the messages list
    if (found)
    {
        char* message = new char[strlen(m->getSender()) + 2 + strlen(m->getMessage()) + 1]; //sender + ": " + message + null
        sprintf(message, "%s: %s", m->getSender(), m->getMessage()); //easier than dealing with strcpy

        pthread_mutex_lock(&messageListMutex);
        messageList.push_back(message);
        pthread_mutex_unlock(&messageListMutex);
    }

    //Create appropriate confirm message
    Confirm *c = new Confirm();
    c->setResponse(found);
    //Encode this message and return
    (*outSize)=c->encode(outBuffer);

    delete m;
    delete c;
}
コード例 #2
0
ファイル: Handlers.cpp プロジェクト: noobasaurus/IntWars
bool Game::handleChatBoxMessage(HANDLE_ARGS) {
   ChatMessage *message = reinterpret_cast<ChatMessage *>(packet->data);
   //Lets do commands
   if(message->msg == '.') {
      const char *cmd[] = { ".set", ".gold", ".speed", ".health", ".xp", ".ap", ".ad", ".mana", ".model", ".help", ".spawn", ".size", ".junglespawn", ".skillpoints", ".level", ".tp"};
      
      // help command
      if (strncmp(message->getMessage(), cmd[9], strlen(cmd[9])) == 0) {
         return true;
      }
      
      // set
      if(strncmp(message->getMessage(), cmd[0], strlen(cmd[0])) == 0) {
         uint32 blockNo, fieldNo;
         float value;
         sscanf(&message->getMessage()[strlen(cmd[0])+1], "%u %u %f", &blockNo, &fieldNo, &value);
         blockNo = 1 << (blockNo - 1);
         uint32 mask = 1 << (fieldNo - 1);
         peerInfo(peer)->getChampion()->getStats().setStat(blockNo, mask, value);
         return true;
      }
      
      // gold
      if(strncmp(message->getMessage(), cmd[1], strlen(cmd[1])) == 0) {
         float gold = (float)atoi(&message->getMessage()[strlen(cmd[1]) + 1]);
         peerInfo(peer)->getChampion()->getStats().setGold(gold);
         return true;
      }

      // speed
      if(strncmp(message->getMessage(), cmd[2], strlen(cmd[2])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[2])+1]);

         printf("Setting speed to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setMovementSpeed(data);
         return true;
      }

      // spawn
      if(strncmp(message->getMessage(), cmd[10], strlen(cmd[10])) == 0)
      {
         static const MinionSpawnPosition positions[] = {   SPAWN_BLUE_TOP,
                                                            SPAWN_BLUE_BOT,
                                                            SPAWN_BLUE_MID,
                                                            SPAWN_RED_TOP,
                                                            SPAWN_RED_BOT,
                                                            SPAWN_RED_MID, 
                                                         };
           
         for(int i = 0; i < 6; ++i) {                                     
            Minion* m = new Minion(map, GetNewNetID(), MINION_TYPE_MELEE, positions[i]);
            map->addObject(m);
            notifyMinionSpawned(m);
         }
         return true;
      }

      //health
      if(strncmp(message->getMessage(), cmd[3], strlen(cmd[3])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[3])+1]);

         peerInfo(peer)->getChampion()->getStats().setCurrentHealth(data);
         peerInfo(peer)->getChampion()->getStats().setMaxHealth(data);

         notifySetHealth(peerInfo(peer)->getChampion());

         return true;
      }

      // xp
      if(strncmp(message->getMessage(), cmd[4], strlen(cmd[4])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[5])+1]);

         printf("Setting experience to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setExp(data);
         return true;
      }
      
      // ap
      if(strncmp(message->getMessage(), cmd[5], strlen(cmd[5])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[5])+1]);

         printf("Setting AP to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setBonusApFlat(data);
         return true;
      }
      
      // ad
      if(strncmp(message->getMessage(), cmd[6], strlen(cmd[6])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[5])+1]);

         printf("Setting AD to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setBonusAdFlat(data);
         return true;
      }
      
      // Mana
      if(strncmp(message->getMessage(), cmd[7], strlen(cmd[7])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[7])+1]);

         printf("Setting Mana to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setCurrentMana(data);
         peerInfo(peer)->getChampion()->getStats().setMaxMana(data);
         return true;
      }
      
      // Model
      if(strncmp(message->getMessage(), cmd[8], strlen(cmd[8])) == 0) {
         std::string sModel = (char *)&message->getMessage()[strlen(cmd[8]) + 1];
         peerInfo(peer)->getChampion()->setModel(sModel);
         return true;
      }
      
      // Size
      if(strncmp(message->getMessage(), cmd[11], strlen(cmd[11])) == 0) {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[11])+1]);

         printf("Setting size to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setSize(data);
         return true;
      }

      // Mob Spawning-Creating, updated
      if(strncmp(message->getMessage(), cmd[12], strlen(cmd[12])) == 0) {
         const char *cmd[] = { "c baron" , "c wolves", "c red", "c blue", "c dragon", "c wraiths", "c golems"};
         return true;
      }

      // Skillpoints
      if(strncmp(message->getMessage(), cmd[13], strlen(cmd[13])) == 0) {
         peerInfo(peer)->getChampion()->setSkillPoints(17);

         SkillUpResponse skillUpResponse(peerInfo(peer)->getChampion()->getNetId(), 0, 0, 17);
         sendPacket(peer, skillUpResponse, CHL_GAMEPLAY);
         return true;
      }
      
      // Level
      if(strncmp(message->getMessage(), cmd[14], strlen(cmd[14])) == 0) {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[14])+1]);

         peerInfo(peer)->getChampion()->getStats().setLevel(data);
         return true;
      }
      
      // tp
      if(strncmp(message->getMessage(), cmd[15], strlen(cmd[15])) == 0) {
         float x, y;
         sscanf(&message->getMessage()[strlen(cmd[15])+1], "%f %f", &x, &y);

         notifyTeleport(peerInfo(peer)->getChampion(), x, y);
         return true;
      }

   }

   switch(message->type) {
   case CMT_ALL:
      return broadcastPacket(packet->data, packet->dataLength, CHL_COMMUNICATION);
   case CMT_TEAM:
      return broadcastPacketTeam(peerInfo(peer)->getTeam(), packet->data, packet->dataLength, CHL_COMMUNICATION);
   default:
      //Logging->errorLine("Unknown ChatMessageType\n");
      return sendPacket(peer, packet->data, packet->dataLength, CHL_COMMUNICATION); 
   }
   
   return false;
}
コード例 #3
0
ファイル: Handlers.cpp プロジェクト: JNeiger/IntWars
bool Game::handleChatBoxMessage(HANDLE_ARGS) {
   ChatMessage *message = reinterpret_cast<ChatMessage *>(packet->data);
   //Lets do commands
   if(message->msg == '.') {
      const char *cmd[] = { ".set", ".gold", ".speed", ".health", ".xp", ".ap", ".ad", ".mana", ".model", ".help", ".spawn", ".size", ".junglespawn", ".skillpoints", ".level", ".tp", ".coords", ".ch"};
      std::ostringstream debugMsg;
      
      // help command
      if (strncmp(message->getMessage(), cmd[9], strlen(cmd[9])) == 0) {
         return true;
      }
      
      // set
      if(strncmp(message->getMessage(), cmd[0], strlen(cmd[0])) == 0) {
         uint32 blockNo, fieldNo;
         float value;
         sscanf(&message->getMessage()[strlen(cmd[0])+1], "%u %u %f", &blockNo, &fieldNo, &value);
         blockNo = 1 << (blockNo - 1);
         uint32 mask = 1 << (fieldNo - 1);
         peerInfo(peer)->getChampion()->getStats().setStat(blockNo, mask, value);
         return true;
      }
      
      // gold
      if(strncmp(message->getMessage(), cmd[1], strlen(cmd[1])) == 0) {
         float gold = (float)atoi(&message->getMessage()[strlen(cmd[1]) + 1]);
         peerInfo(peer)->getChampion()->getStats().setGold(gold);
         return true;
      }

      // speed
      if(strncmp(message->getMessage(), cmd[2], strlen(cmd[2])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[2])+1]);

         printf("Setting speed to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setMovementSpeed(data);
         return true;
      }

      //health
      if(strncmp(message->getMessage(), cmd[3], strlen(cmd[3])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[3])+1]);

         peerInfo(peer)->getChampion()->getStats().setCurrentHealth(data);
         peerInfo(peer)->getChampion()->getStats().setMaxHealth(data);

         notifySetHealth(peerInfo(peer)->getChampion());

         return true;
      }

      // xp
      if(strncmp(message->getMessage(), cmd[4], strlen(cmd[4])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[5])+1]);

         printf("Setting experience to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setExp(data);
         return true;
      }
      
      // ap
      if(strncmp(message->getMessage(), cmd[5], strlen(cmd[5])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[5])+1]);

         printf("Setting AP to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setBonusApFlat(data);
         return true;
      }
      
      // ad
      if(strncmp(message->getMessage(), cmd[6], strlen(cmd[6])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[5])+1]);

         printf("Setting AD to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setBonusAdFlat(data);
         return true;
      }
      
      // Mana
      if(strncmp(message->getMessage(), cmd[7], strlen(cmd[7])) == 0)
      {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[7])+1]);

         printf("Setting Mana to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setCurrentMana(data);
         peerInfo(peer)->getChampion()->getStats().setMaxMana(data);
         return true;
      }
      
      // Model
      if(strncmp(message->getMessage(), cmd[8], strlen(cmd[8])) == 0) {
         std::string sModel = (char *)&message->getMessage()[strlen(cmd[8]) + 1];
         peerInfo(peer)->getChampion()->setModel(sModel);
         return true;
      }
      
      // Size
      if(strncmp(message->getMessage(), cmd[11], strlen(cmd[11])) == 0) {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[11])+1]);

         printf("Setting size to %f\n", data);

         peerInfo(peer)->getChampion()->getStats().setSize(data);
         return true;
      }

      // Mob Spawning-Creating, updated
      if(strncmp(message->getMessage(), cmd[12], strlen(cmd[12])) == 0) {
         const char *cmd[] = { "c baron" , "c wolves", "c red", "c blue", "c dragon", "c wraiths", "c golems"};
         return true;
      }

      // Skillpoints
      if(strncmp(message->getMessage(), cmd[13], strlen(cmd[13])) == 0) {
         peerInfo(peer)->getChampion()->setSkillPoints(17);

         SkillUpResponse skillUpResponse(peerInfo(peer)->getChampion()->getNetId(), 0, 0, 17);
         sendPacket(peer, skillUpResponse, CHL_GAMEPLAY);
         return true;
      }
      
      // Level
      if(strncmp(message->getMessage(), cmd[14], strlen(cmd[14])) == 0) {
         float data = (float)atoi(&message->getMessage()[strlen(cmd[14])+1]);

         peerInfo(peer)->getChampion()->getStats().setLevel(data);
         return true;
      }
      
      // tp
      if(strncmp(message->getMessage(), cmd[15], strlen(cmd[15])) == 0) {
         float x, y;
         sscanf(&message->getMessage()[strlen(cmd[15])+1], "%f %f", &x, &y);

         notifyTeleport(peerInfo(peer)->getChampion(), x, y);
         return true;
      }
      
       // coords
      if(strncmp(message->getMessage(), cmd[16], strlen(cmd[16])) == 0) {
         printf("At %f;%f\n", peerInfo(peer)->getChampion()->getX(), peerInfo(peer)->getChampion()->getY());
         debugMsg << "At Coords - X:" << peerInfo(peer)->getChampion()->getX() << " Y:" << peerInfo(peer)->getChampion()->getY() << " Z:" << peerInfo(peer)->getChampion()->getZ();
         notifyDebugMessage(debugMsg.str());
         return true;
      }
      
      // Ch(ampion)
      if(strncmp(message->getMessage(), cmd[17], strlen(cmd[17])) == 0) {
         std::string champ = (char *)&message->getMessage()[strlen(cmd[17]) + 1];
         Champion* c = new Champion(champ, map, peerInfo(peer)->getChampion()->getNetId(), peerInfo(peer)->userId);
      
         c->setPosition(peerInfo(peer)->getChampion()->getX(), peerInfo(peer)->getChampion()->getY());
         c->setModel(champ); // trigger the "modelUpdate" proc
         
         map->removeObject(peerInfo(peer)->getChampion());
         delete peerInfo(peer)->getChampion();
         map->addObject(c);
         
         peerInfo(peer)->setChampion(c);
         
         return true;
      }

   }

   switch(message->type) {
   case CMT_ALL:
      return broadcastPacket(packet->data, packet->dataLength, CHL_COMMUNICATION);
   case CMT_TEAM:
      return broadcastPacketTeam(peerInfo(peer)->getTeam(), packet->data, packet->dataLength, CHL_COMMUNICATION);
   default:
      //Logging->errorLine("Unknown ChatMessageType\n");
      return sendPacket(peer, packet->data, packet->dataLength, CHL_COMMUNICATION); 
   }
   
   return false;
}