Additional testing, entity deletion, and player disconnection

This commit is contained in:
Layla 2020-05-01 02:24:34 -04:00
parent b94b469c39
commit 9a6ba77e72
No known key found for this signature in database
GPG Key ID: A494D9357BA1BE31
7 changed files with 235 additions and 21 deletions

View File

@ -6,4 +6,6 @@ ADD / /dt
RUN cd /dt; /dt/build.sh
CMD ["/dt/builds/server.out"]
CMD ["/dt/builds/server.out"]
EXPOSE 7777/udp

View File

@ -7,4 +7,11 @@ if [ -f builds/server.out ]; then
rm builds/server.out
fi
g++ main.cpp -o builds/server.out -lenet -lpthread
g++ main.cpp -o builds/server.out -lenet -lpthread
if [ $? = 0 ]
then
echo "Build successfully!"
else
exit -1
fi

View File

@ -28,18 +28,16 @@ void * console_logic(void *)
while(console_is_running)
{
if(!std::getline(std::cin, input_string))
if(std::getline(std::cin, input_string))
{
std::cout << "Console I/O error!" << std::endl;
}
if(input_string == "stop")
{
console_is_running = false;
}
else
{
std::cout << "Invalid console command!" << std::endl;
if(input_string == "stop")
{
console_is_running = false;
}
else
{
std::cout << "Invalid console command!" << std::endl;
}
}
}
return 0;

View File

@ -17,8 +17,13 @@ class GameMap
std::string get_entity_dump(void);
std::string spawn_entity(std::string,std::string,int,int);
std::string move_entity(std::string,std::string,int,int);
int get_entity_pos_x(std::string,std::string);
int get_entity_pos_y(std::string,std::string);
int get_size_x(void);
int get_size_y(void);
bool entity_exists(std::string, std::string);
bool remove_entity(std::string, std::string);
private:
int ** map_data;
int size_x;
@ -66,12 +71,36 @@ std::string GameMap::move_entity(std::string entity_id, std::string entity_type,
{
if(entities[i].get_id() == entity_id && entities[i].get_type() == entity_type)
{
if(map_data[entities[i].get_x() + x][entities[i].get_y() + y] % 2 == 1)
GameEntity* entity = &entities[i];
//if(map_data[entities[i].get_x() + x][entities[i].get_y() + y] % 2 == 1)
//{
if(entity->get_x() + x < 0)
{
entities[i].set_x(entities[i].get_x() + x);
entities[i].set_y(entities[i].get_y() + y);
entity->set_x(0);
}
return entities[i].get_dump();
else if(entity->get_x() + x >= this->get_size_x())
{
entity->set_x(this->get_size_x() - 1);
}
else
{
entity->set_x(entities[i].get_x() + x);
}
if(entity->get_y() + y < 0)
{
entity->set_y(0);
}
else if(entity->get_y() + y >= this->get_size_y())
{
entity->set_y(this->get_size_y() - 1);
}
else
{
entity->set_y(entities[i].get_y() + y);
}
return entity->get_dump();
}
}
return "";
@ -137,4 +166,84 @@ int GameMap::get_size_y(void)
return size_y;
}
bool GameMap::entity_exists(std::string entity_id, std::string entity_type)
{
for(int i = 0; i < entities.size(); i++)
{
GameEntity* entity = &entities[i];
if(entity->get_id() == entity_id)
{
if(entity->get_type() == entity_type)
{
return true;
}
}
}
return false;
}
bool erase(std::vector<GameEntity> &v, GameEntity key)
{
for(auto it = v.begin(); it != v.end();)
{
if (it->get_id() == key.get_id())
{
it = v.erase(it);
return true;
}
else
{
++it;
}
}
return false;
}
bool GameMap::remove_entity(std::string entity_id, std::string entity_type)
{
for(int i = 0; i < entities.size(); i++)
{
if(entities[i].get_id() == entity_id && entities[i].get_type() == entity_type)
{
return erase(entities, entities[i]);
}
}
return false;
}
int GameMap::get_entity_pos_x(std::string entity_id, std::string entity_type)
{
for(int i = 0; i < entities.size(); i++)
{
GameEntity* entity = &entities[i];
if(entity->get_id() == entity_id)
{
if(entity->get_type() == entity_type)
{
return entity->get_x();
}
}
}
return -1;
}
int GameMap::get_entity_pos_y(std::string entity_id, std::string entity_type)
{
for(int i = 0; i < entities.size(); i++)
{
GameEntity* entity = &entities[i];
if(entity->get_id() == entity_id)
{
if(entity->get_type() == entity_type)
{
return entity->get_y();
}
}
}
return -1;
}
#endif

View File

@ -97,11 +97,22 @@ int main (int argc, char ** argv)
case ENET_EVENT_TYPE_DISCONNECT:
std::cout << event.peer -> data << " disconnected." << std::endl;
//Remove peer data on disconnect
// DELETE ENTITY HERE
//Clear username data and remove entity
std::string username = usernames[event.peer -> incomingPeerID];
if(username != "")
{
std::cout << "Removing '" << username << "'s player data!" << std::endl;
gamemap.remove_entity(username,"player");
usernames[event.peer -> incomingPeerID] = "";
usernames[event.peer -> incomingPeerID] = "";
std::string resp = "2|delete,player:" + username;
const char* data = resp.c_str();
ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE);
enet_host_broadcast(server, 0, packet);
}
//Open peer for new connection
event.peer -> data = NULL;
}
}

View File

@ -1,4 +1,9 @@
#!/bin/sh
mkdir -p builds
g++ tests.cpp -o builds/tests.out -lgtest
./builds/tests.out
if [ $? = 0 ]
then
./builds/tests.out
else
exit -1
fi

View File

@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include <string>
#include <iostream>
#include "gameentity.hpp"
#include "gamemap.hpp"
@ -61,6 +62,87 @@ TEST(GameMapTest, CheckMapSize)
EXPECT_EQ(map.get_size_x(), 16);
EXPECT_EQ(map.get_size_y(), 4);
}
TEST(GameMapTest, CreateEntity)
{
GameMap map = CreateMapEntity();
map.spawn_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE);
EXPECT_TRUE(map.entity_exists(TEST_ENTITY_ID, TEST_ENTITY_TYPE));
}
TEST(GameMapTest, IntialEntityPos)
{
GameMap map = CreateMapEntity();
map.spawn_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, 0,0);
EXPECT_EQ(map.get_entity_pos_x(TEST_ENTITY_ID, TEST_ENTITY_TYPE), 0);
EXPECT_EQ(map.get_entity_pos_y(TEST_ENTITY_ID, TEST_ENTITY_TYPE), 0);
}
TEST(GameMapTest, MoveEntityFromZero)
{
GameMap map = CreateMapEntity();
map.spawn_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, 0,0);
//Move entity
map.move_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, 2, 1);
EXPECT_EQ(map.get_entity_pos_x(TEST_ENTITY_ID, TEST_ENTITY_TYPE), 2);
EXPECT_EQ(map.get_entity_pos_y(TEST_ENTITY_ID, TEST_ENTITY_TYPE), 1);
}
TEST(GameMapTest, MoveEntityRelative)
{
GameMap map = CreateMapEntity();
map.spawn_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, 0,0);
//Move entity
map.move_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, 2, 1);
//Move entity again!
map.move_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, 1, 1);
EXPECT_EQ(map.get_entity_pos_x(TEST_ENTITY_ID, TEST_ENTITY_TYPE), 3);
EXPECT_EQ(map.get_entity_pos_y(TEST_ENTITY_ID, TEST_ENTITY_TYPE), 2);
}
TEST(GameMapTest, MoveEntityNegativeFromZero)
{
GameMap map = CreateMapEntity();
map.spawn_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, 0, 0);
//Move entity
map.move_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, -1, -1);
EXPECT_EQ(map.get_entity_pos_x(TEST_ENTITY_ID, TEST_ENTITY_TYPE), 0);
EXPECT_EQ(map.get_entity_pos_y(TEST_ENTITY_ID, TEST_ENTITY_TYPE), 0);
}
TEST(GameMapTest, MoveEntityNegativeFromNonZero)
{
GameMap map = CreateMapEntity();
map.spawn_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, 1, 1);
//Move entity
std::cout << std::string(map.move_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, -2, -2)) << std::endl;
EXPECT_EQ(map.get_entity_pos_x(TEST_ENTITY_ID, TEST_ENTITY_TYPE), 0);
EXPECT_EQ(map.get_entity_pos_y(TEST_ENTITY_ID, TEST_ENTITY_TYPE), 0);
}
TEST(GameMapTest, StopEntityFromMovingOutOfBounds)
{
GameMap map = CreateMapEntity();
map.spawn_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, map.get_size_x()-1, map.get_size_y()-1);
//Move entity
map.move_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE, 2, 2);
EXPECT_EQ(map.get_entity_pos_x(TEST_ENTITY_ID, TEST_ENTITY_TYPE), map.get_size_x() - 1);
EXPECT_EQ(map.get_entity_pos_y(TEST_ENTITY_ID, TEST_ENTITY_TYPE), map.get_size_y() - 1);
}
TEST(GameMapTest, RemoveMapEntity)
{
GameMap map = CreateMapEntity();
map.spawn_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE);
EXPECT_TRUE(map.remove_entity(TEST_ENTITY_ID, TEST_ENTITY_TYPE));
EXPECT_FALSE(map.entity_exists(TEST_ENTITY_ID, TEST_ENTITY_TYPE));
}
int main(int argc, char* argv[])
{