From 9a6ba77e72bdbc18ba10ee3780bf6f51769240db Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Fri, 1 May 2020 02:24:34 -0400 Subject: [PATCH] Additional testing, entity deletion, and player disconnection --- server/Dockerfile | 4 +- server/build.sh | 9 +++- server/console.hpp | 20 ++++---- server/gamemap.hpp | 117 ++++++++++++++++++++++++++++++++++++++++++-- server/main.cpp | 17 +++++-- server/run_tests.sh | 7 ++- server/tests.cpp | 82 +++++++++++++++++++++++++++++++ 7 files changed, 235 insertions(+), 21 deletions(-) diff --git a/server/Dockerfile b/server/Dockerfile index 56cf0ff..7c6cc7f 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -6,4 +6,6 @@ ADD / /dt RUN cd /dt; /dt/build.sh -CMD ["/dt/builds/server.out"] \ No newline at end of file +CMD ["/dt/builds/server.out"] + +EXPOSE 7777/udp \ No newline at end of file diff --git a/server/build.sh b/server/build.sh index 03b3bd2..bebd33a 100755 --- a/server/build.sh +++ b/server/build.sh @@ -7,4 +7,11 @@ if [ -f builds/server.out ]; then rm builds/server.out fi -g++ main.cpp -o builds/server.out -lenet -lpthread \ No newline at end of file +g++ main.cpp -o builds/server.out -lenet -lpthread + +if [ $? = 0 ] +then + echo "Build successfully!" +else + exit -1 +fi \ No newline at end of file diff --git a/server/console.hpp b/server/console.hpp index 0e54b4a..a9e8db8 100644 --- a/server/console.hpp +++ b/server/console.hpp @@ -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; diff --git a/server/gamemap.hpp b/server/gamemap.hpp index 86a02a1..d152ae8 100644 --- a/server/gamemap.hpp +++ b/server/gamemap.hpp @@ -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 &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 \ No newline at end of file diff --git a/server/main.cpp b/server/main.cpp index b80ca8f..59e7c93 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -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; } } diff --git a/server/run_tests.sh b/server/run_tests.sh index e715c40..64232df 100755 --- a/server/run_tests.sh +++ b/server/run_tests.sh @@ -1,4 +1,9 @@ #!/bin/sh mkdir -p builds g++ tests.cpp -o builds/tests.out -lgtest -./builds/tests.out \ No newline at end of file +if [ $? = 0 ] +then + ./builds/tests.out +else + exit -1 +fi \ No newline at end of file diff --git a/server/tests.cpp b/server/tests.cpp index 16f3a42..b2a594a 100644 --- a/server/tests.cpp +++ b/server/tests.cpp @@ -1,6 +1,7 @@ #include #include +#include #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[]) {