Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ChinmayMittal/Maze-Game int…
Browse files Browse the repository at this point in the history
…o main
  • Loading branch information
ChinmayMittal committed Apr 16, 2022
2 parents 18dcadf + 7590203 commit 0ed5644
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 4 deletions.
49 changes: 47 additions & 2 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <netdb.h>
#include <algorithm>
#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)
{
Expand Down Expand Up @@ -100,8 +101,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)
Expand Down Expand Up @@ -164,6 +170,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<GameResultMessage *>(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;
Expand Down
5 changes: 4 additions & 1 deletion Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class LGame : public LScreen
LTexture tilesTexture;
LTexture ashTexture;
LTexture dogTexture;
LTexture profTexture ;
LTexture profTexture;

TileAtlas tileAtlas;

Expand All @@ -72,6 +72,7 @@ class LGame : public LScreen
void initTasks();
bool setTiles();
void initEntities();
void gameEnd();

LTimer globalTime;
Text *nameText;
Expand All @@ -88,6 +89,8 @@ class LGame : public LScreen

std::vector<Text *> taskTexts;

bool gameEnded = false;

public:
LGame(LWindow &window, std::string playerName, std::string opponentName, int &sockfd, sockaddr_in &theiraddr);
void handleEvent(SDL_Event &e);
Expand Down
51 changes: 51 additions & 0 deletions MessageStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,37 @@ int serialize(Message *msg, char *data)
bytesUsed++;
break;
}
case 3:
{
GameEndMessage *msgGameEnd = dynamic_cast<GameEndMessage *>(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<GameResultMessage *>(msg);
bool *p = (bool *)q;
*p = msgGameResult->won;
p++;
bytesUsed += sizeof(bool);
char *r = (char *)p;
*r = '\0';
r++;
bytesUsed++;
break;
}
}
return bytesUsed;
}
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions MessageStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ const int secondPerHealthDecrease = 1;
const int secondPerHealthDecreasewithYulu = 5;
const int taskStatusBarWidth = 100;
const int gyPadding = 8;
const int tasksVPWidth = 240;
const int tasksVPWidth = 240;
const int gameLenSecs = 48;
33 changes: 33 additions & 0 deletions server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<in_addr_t, in_addr_t> opponents;

// Creating socket file descriptor
Expand Down Expand Up @@ -135,11 +140,39 @@ int main()
//}
break;
}
case 3:
{
GameEndMessage *gameEndMsg = dynamic_cast<GameEndMessage *>(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));
}
}
}

0 comments on commit 0ed5644

Please sign in to comment.