diff --git a/Game.cpp b/Game.cpp index 916caa9..4b37436 100644 --- a/Game.cpp +++ b/Game.cpp @@ -21,6 +21,7 @@ #include #include #include "MessageStructs.h" +#include "MainMenu.h" LGame::LGame(LWindow &window, std::string playerName, std::string opponentName, int &sockfd, sockaddr_in &theiraddr) : LScreen(window), playerName(playerName), opponentName(opponentName), sockfd(sockfd), theirAddr(theiraddr) { @@ -91,8 +92,13 @@ void LGame::update() players[0].setLastTileType(newTileType); int secs = globalTime.getTicks() / 1000; - std::string mins = std::to_string(3 * (secs % 20)); - std::string hours = std::to_string((secs / 20)); + int secsPerHour = gameLenSecs / 24; + std::string mins = std::to_string((60 / secsPerHour) * (secs % secsPerHour)); + std::string hours = std::to_string(secs / secsPerHour); + if (secs / secsPerHour >= 24) + { + gameEnd(); + } if (mins.size() <= 1) mins = "0" + mins; if (hours.size() <= 1) @@ -155,6 +161,45 @@ void LGame::update() moneyTextOpp->setText("MONEY: " + std::to_string(players[1].getMoney())); } +void LGame::gameEnd() +{ + GameEndMessage gameEndMessage; + gameEndMessage.points = players[0].getPoints(); + gameEndMessage.money = players[0].getMoney(); + gameEndMessage.health = players[0].getHealth(); + int bytesUsed = serialize(&gameEndMessage, buf); + + sendto(sockfd, buf, bytesUsed, 0, (const struct sockaddr *)&theirAddr, + sizeof(theirAddr)); + + unsigned int len = sizeof(theirAddr); + int type; + Message *msg; + do + { + int n; + do + { + n = recvfrom(sockfd, (char *)recBuf, 512, MSG_WAITALL, (struct sockaddr *)&theirAddr, &len); + } while (n == -1); + msg = deserialize(recBuf); + } while (msg->type != 4); + GameResultMessage *gameResultMessage = dynamic_cast(msg); + if (gameResultMessage->won) + { + std::cout << "YOU WON!" << std::endl; + } + else + { + std::cout << "YOU LOST!" << std::endl; + } + std::cout << "Going back to main menu in 5 secs..." << std::endl; + delete msg; + SDL_Delay(5000); + MainMenu *mainMenu = new MainMenu(window, sockfd, theirAddr); + window.setCurrScreen(mainMenu); +} + void LGame::render(SDL_Renderer *renderer) { SDL_Rect viewport; diff --git a/Game.h b/Game.h index 103f600..c4e695a 100644 --- a/Game.h +++ b/Game.h @@ -59,7 +59,7 @@ class LGame : public LScreen LTexture tilesTexture; LTexture ashTexture; LTexture dogTexture; - LTexture profTexture ; + LTexture profTexture; TileAtlas tileAtlas; @@ -72,6 +72,7 @@ class LGame : public LScreen void initTasks(); bool setTiles(); void initEntities(); + void gameEnd(); LTimer globalTime; Text *nameText; @@ -88,6 +89,8 @@ class LGame : public LScreen std::vector taskTexts; + bool gameEnded = false; + public: LGame(LWindow &window, std::string playerName, std::string opponentName, int &sockfd, sockaddr_in &theiraddr); void handleEvent(SDL_Event &e); diff --git a/MessageStructs.cpp b/MessageStructs.cpp index dd72090..d4d78ea 100644 --- a/MessageStructs.cpp +++ b/MessageStructs.cpp @@ -67,6 +67,37 @@ int serialize(Message *msg, char *data) bytesUsed++; break; } + case 3: + { + GameEndMessage *msgGameEnd = dynamic_cast(msg); + *q = msgGameEnd->points; + q++; + *q = msgGameEnd->money; + q++; + bytesUsed += 2 * sizeof(int); + float *p = (float *)q; + *q = msgGameEnd->health; + q++; + bytesUsed += sizeof(float); + char *r = (char *)q; + *r = '\0'; + r++; + bytesUsed++; + break; + } + case 4: + { + GameResultMessage *msgGameResult = dynamic_cast(msg); + bool *p = (bool *)q; + *p = msgGameResult->won; + p++; + bytesUsed += sizeof(bool); + char *r = (char *)p; + *r = '\0'; + r++; + bytesUsed++; + break; + } } return bytesUsed; } @@ -130,6 +161,26 @@ Message *deserialize(char *data) p++; return msgGameUpdate; } + case 3: + { + GameEndMessage *msgGameEnd = new GameEndMessage(); + msgGameEnd->points = *q; + q++; + msgGameEnd->money = *q; + q++; + float *p = (float *)q; + msgGameEnd->health = *p; + p++; + return msgGameEnd; + } + case 4: + { + GameResultMessage *msgGameResult = new GameResultMessage(); + bool *p = (bool *)q; + msgGameResult->won = *p; + p++; + return msgGameResult; + } default: { return NULL; diff --git a/MessageStructs.h b/MessageStructs.h index 542639e..e1d9b26 100644 --- a/MessageStructs.h +++ b/MessageStructs.h @@ -39,6 +39,26 @@ struct GameUpdateMessage : Message float health; }; +struct GameEndMessage : Message +{ + GameEndMessage() + { + type = 3; + } + int points; + int money; + float health; +}; + +struct GameResultMessage : Message +{ + GameResultMessage() + { + type = 4; + } + bool won; +}; + int serialize(Message *msg, char *data); Message *deserialize(char *data); diff --git a/constants.h b/constants.h index 85b13f2..7eed89d 100644 --- a/constants.h +++ b/constants.h @@ -7,4 +7,5 @@ const int secondPerHealthDecrease = 1; const int secondPerHealthDecreasewithYulu = 5; const int taskStatusBarWidth = 100; const int gyPadding = 8; -const int tasksVPWidth = 240; \ No newline at end of file +const int tasksVPWidth = 240; +const int gameLenSecs = 48; \ No newline at end of file diff --git a/server/server.cpp b/server/server.cpp index 584b777..eb0846e 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -23,6 +23,11 @@ int main() bool waiting = false; std::string waitingName; + bool p1Ended = false; + bool p2Ended = false; + int p1Points = 0; + int p2Points = 0; + std::map opponents; // Creating socket file descriptor @@ -135,11 +140,39 @@ int main() //} break; } + case 3: + { + GameEndMessage *gameEndMsg = dynamic_cast(msg); + if (cliaddr.sin_addr.s_addr == waitaddr.sin_addr.s_addr) + { + p1Ended = true; + p1Points = gameEndMsg->points; + std::cout << "P1 Points " << p1Points << std::endl; + } + else + { + p2Ended = true; + p2Points = gameEndMsg->points; + std::cout << "P2 Points " << p2Points << std::endl; + } + } default: { break; } } delete msg; + if (p1Ended && p2Ended) + { + GameResultMessage gameResP1, gameResP2; + gameResP1.won = p1Points >= p2Points; + int numBytes = serialize(&gameResP1, msgBuffer); + sendto(sockfd, msgBuffer, numBytes, 0, (const struct sockaddr *)&waitaddr, + sizeof(waitaddr)); + gameResP2.won = p2Points >= p1Points; + numBytes = serialize(&gameResP2, msgBuffer); + sendto(sockfd, msgBuffer, numBytes, 0, (const struct sockaddr *)&otherAddr, + sizeof(otherAddr)); + } } } \ No newline at end of file