00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "testserverframework.h"
00025
00026 #include "testserversnakebite.h"
00027
00028 TestServerFramework::TestServerFramework()
00029 : ServerFramework(8000)
00030 {
00031 std::cout << "Constructor called" << std::endl;
00032 }
00033
00034
00035 TestServerFramework::~TestServerFramework()
00036 {
00037 }
00038
00039 bool TestServerFramework::onInitialHandshake( ServerPacket * handshake, ServerPacket * reply )
00040 {
00041 return true;
00042 }
00043
00044 void TestServerFramework::onClientConnected( Client * _client )
00045 {
00046 TestClient * client = static_cast<TestClient *>(_client);
00047
00048
00049
00050 for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end(); ++it)
00051 {
00052 TestServerSnakeBite * sb = *it;
00053 sb->sendCreateObject(client);
00054 }
00055
00056
00057 TestServerSnakeBite * head = new TestServerSnakeBite(this, 5, 5, 0.1, 0, true, client);
00058 addTestServerSnakeBite(head);
00059 client->m_snake_head = head;
00060 for(int i = 0; i < 10; i++)
00061 {
00062 head = new TestServerSnakeBite(this, 10+7*i, 10+7*i, 0.1, head, false, client);
00063 addTestServerSnakeBite(head);
00064 }
00065 client->m_snake_tail = head;
00066 }
00067
00068 void TestServerFramework::onClientDisconnected( Client * _client )
00069 {
00070 TestClient * client = static_cast<TestClient *>(_client);
00071
00072 for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end();)
00073 {
00074 if((*it)->getOwner() == client)
00075 {
00076 TestServerSnakeBite * bite = *it;
00077 it = snakes.erase(it);
00078 delete bite;
00079 }
00080 else
00081 {
00082 it++;
00083 }
00084 }
00085 }
00086
00087 void TestServerFramework::addTestServerSnakeBite( TestServerSnakeBite * bite )
00088 {
00089 snakes.push_back(bite);
00090
00091 bite->sendCreateObjectToAll();
00092
00093 bite->sendCreateObject(bite->getOwner());
00094 bite->sendUpdate(bite->getOwner(), 30, true);
00095 }
00096
00097
00098
00099 Client * TestServerFramework::createClient( const ipaddress & addr, int port )
00100 {
00101 return new TestClient(this, addr, port);
00102 }
00103
00104 void TestServerFramework::mainLoop( )
00105 {
00106 std::cout << "mainLoop()\n";
00107
00108 while(true)
00109 {
00110
00111 keepAlive();
00112
00113
00114 for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end(); ++it)
00115 {
00116 TestServerSnakeBite * sb = *it;
00117 sb->updateMovement();
00118 }
00119
00120
00121 for(std::map<unsigned int, Client *>::iterator it1 = getAllClients().begin(); it1 != getAllClients().end(); ++it1)
00122 {
00123 TestClient * c1 = static_cast<TestClient *>(it1->second);
00124 for(std::map<unsigned int, Client *>::iterator it2 = getAllClients().begin(); it2 != getAllClients().end(); ++it2)
00125 {
00126 TestClient * c2 = static_cast<TestClient *>(it2->second);
00127 if(c1 == c2 || c2->m_snake_tail->isHead())
00128 continue;
00129
00130 if(c1->m_snake_head->dist(*c2->m_snake_tail) < 10.0)
00131 {
00132 TestServerSnakeBite * eaten_tail = c2->m_snake_tail;
00133
00134 c2->m_snake_tail = eaten_tail->m_nextbite;
00135
00136
00137 eaten_tail->m_nextbite = c1->m_snake_tail;
00138 c1->m_snake_tail = eaten_tail;
00139 eaten_tail->m_owner = c1;
00140
00141
00142 eaten_tail->sendUpdate(c1, 30, true);
00143 eaten_tail->sendUpdate(c2, 40, true);
00144
00145
00146 eaten_tail->sendUpdateToAll(20, true);
00147 }
00148 }
00149 }
00150
00151
00152
00153 for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end(); ++it)
00154 {
00155 TestServerSnakeBite * sb = *it;
00156 sb->sendUpdateToAll(10, false);
00157 if(sb->isHead())
00158 {
00159 sb->sendUpdate(sb->getOwner(), 30, true);
00160 }
00161 }
00162
00163
00164
00165 psleep(100);
00166 }
00167 }
00168
00169