Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages | Examples

testserver/testserverframework.cpp

/// @cond EXCLUDEDTESTSOURCES
/***************************************************************************
 * The contents of this file are subject to the Mozilla Public             *
 * License Version 1.1 (the "License"); you may not use this file          *
 * except in compliance with the License. You may obtain a copy of         *
 * the License at http://www.mozilla.org/MPL/                              *
 *                                                                         *
 * Software distributed under the License is distributed on an "AS         *
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or              *
 * implied. See the License for the specific language governing            *
 * rights and limitations under the License.                               *
 *                                                                         *
 * The Original Code is Game Network Framework (GaNeF).                    *
 *                                                                         *
 * The Initial Developers of the Original Code are                         *
 * Lars Langer and Emanuel Greisen                                         *
 * Copyright (C) 2005. Lars Langer & Emanuel Greisen                       *
 * All Rights Reserved.                                                    *
 *                                                                         *
 * Contributor(s):                                                         *
 *   none yet....                                                          *
 *                                                                         *
 ***************************************************************************/
#include "testserverframework.h"

#include "testserversnakebite.h"

TestServerFramework::TestServerFramework()
   : ServerFramework(8000)
{
   std::cout << "Constructor called" << std::endl;
}


TestServerFramework::~TestServerFramework()
{
}

bool TestServerFramework::onInitialHandshake( ServerPacket * handshake, ServerPacket * reply )
{
   return true;
}

void TestServerFramework::onClientConnected( Client * _client )
{
   TestClient * client = static_cast<TestClient *>(_client);

   // First let him know about the existing snakes
   // Send all updates (create/update/delete)
   for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end(); ++it)
   {
      TestServerSnakeBite * sb = *it;
      sb->sendCreateObject(client);
   }

   // Some client connected: give him a snake
   TestServerSnakeBite * head = new TestServerSnakeBite(this, 5, 5, 0.1, 0, true, client);
   addTestServerSnakeBite(head);
   client->m_snake_head = head;
   for(int i = 0; i < 10; i++)
   {
      head = new TestServerSnakeBite(this, 10+7*i, 10+7*i, 0.1, head, false, client);
      addTestServerSnakeBite(head);
   }
   client->m_snake_tail = head;
}

void TestServerFramework::onClientDisconnected( Client * _client )
{
   TestClient * client = static_cast<TestClient *>(_client);
   // Remove all his snake-bites
   for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end();)
   {
      if((*it)->getOwner() == client)
      {
         TestServerSnakeBite * bite = *it;
         it = snakes.erase(it);
         delete bite;
      }
      else
      {
         it++;
      }
   }
}

void TestServerFramework::addTestServerSnakeBite( TestServerSnakeBite * bite )
{
   snakes.push_back(bite);
   // Send to the others
   bite->sendCreateObjectToAll();
   // Send to owner
   bite->sendCreateObject(bite->getOwner());
   bite->sendUpdate(bite->getOwner(), 30, true); // Let the owner know this is his
}



Client * TestServerFramework::createClient( const ipaddress & addr, int port )
{
   return new TestClient(this, addr, port);
}

void TestServerFramework::mainLoop( )
{
   std::cout << "mainLoop()\n";

   while(true)
   {
      // Get all incomming messages
      keepAlive();

      // Move all snake-bites
      for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end(); ++it)
      {
         TestServerSnakeBite * sb = *it;
         sb->updateMovement();
      }

      // Let any head eat colliding tails
      for(std::map<unsigned int, Client *>::iterator it1 = getAllClients().begin(); it1 != getAllClients().end(); ++it1)
      {
         TestClient * c1 = static_cast<TestClient *>(it1->second);
         for(std::map<unsigned int, Client *>::iterator it2 = getAllClients().begin(); it2 != getAllClients().end(); ++it2)
         {
            TestClient * c2 = static_cast<TestClient *>(it2->second);
            if(c1 == c2 || c2->m_snake_tail->isHead())
               continue;

            if(c1->m_snake_head->dist(*c2->m_snake_tail) < 10.0)
            {
               TestServerSnakeBite * eaten_tail = c2->m_snake_tail;
               // Remove the tail from "c2"
               c2->m_snake_tail = eaten_tail->m_nextbite;

               // Add the new tail to "c1"
               eaten_tail->m_nextbite = c1->m_snake_tail;
               c1->m_snake_tail = eaten_tail;
               eaten_tail->m_owner = c1;

               // Tell the new owner about his snake and the old about his loss
               eaten_tail->sendUpdate(c1, 30, true);
               eaten_tail->sendUpdate(c2, 40, true);

               // Tell everyone about the change in next_bite
               eaten_tail->sendUpdateToAll(20, true);
            }
         }
      }


      // Send all updates (create/update/delete)
      for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end(); ++it)
      {
         TestServerSnakeBite * sb = *it;
         sb->sendUpdateToAll(10, false);
         if(sb->isHead())
         {
            sb->sendUpdate(sb->getOwner(), 30, true);
         }
      }


      // Sleep a little
      psleep(100);
   }
}

/// @endcond

Generated on Mon Feb 6 12:24:50 2006 for Ganef by  doxygen 1.4.4