multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Files at this revision

API Documentation at this revision

Comitter:
vsoltan
Date:
Sat Nov 28 03:22:06 2020 +0000
Parent:
31:f70cf03c8ef9
Child:
33:98ba7b0b8c2b
Commit message:
flush the socket upon game restart

Changed in this revision

gamestate.cpp Show annotated file Show diff for this revision Revisions of this file
gamestate.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/gamestate.cpp	Sat Nov 28 02:54:12 2020 +0000
+++ b/gamestate.cpp	Sat Nov 28 03:22:06 2020 +0000
@@ -10,7 +10,6 @@
     this->p2_loc = playerTwoLocation; 
     this->ball_loc = ballLocation; 
     this->has_started = 0; 
-    this->connected = 0;
     this->countdown = 0; 
     this->is_done = 0; 
     this->score[0] = 0; 
@@ -31,8 +30,7 @@
 
 void GameState::updateAndRender(MbedJSONValue *serverResponse, Graphics *gfx) {
     string typeResponse = (*serverResponse)["type"].get<std::string>(); 
-    if (typeResponse == "connected") {
-        connected = 1; 
+    if (typeResponse == "connected") { 
         if (serverResponse->hasMember("player")) {
           this->localPlayerNum = (char)(*serverResponse)["player"].get<int>();    
         }
@@ -99,10 +97,6 @@
     return this->localPlayerNum; 
 }
 
-char GameState::isConnected() {
-    return this->connected; 
-}
-
 int GameState::getCountdown() {
     return this->countdown; 
 }
--- a/gamestate.h	Sat Nov 28 02:54:12 2020 +0000
+++ b/gamestate.h	Sat Nov 28 03:22:06 2020 +0000
@@ -23,7 +23,6 @@
         Coord ball_loc; 
         char is_done; 
         char has_started; 
-        char connected; 
         int countdown; 
         char localPlayerNum; 
         char lobbyHash[21]; 
@@ -38,7 +37,6 @@
         char getLocalPlayerNum(); 
         int getCountdown(); 
         char hasStarted();
-        char isConnected(); 
         void setCountdown(int val); 
         int getPlayerOneScore();
         int getPlayerTwoScore();
--- a/main.cpp	Sat Nov 28 02:54:12 2020 +0000
+++ b/main.cpp	Sat Nov 28 03:22:06 2020 +0000
@@ -19,6 +19,8 @@
 volatile int moveData = 0;
 volatile int menuPress = 0; 
 
+int numGamesPlayed = 0; 
+
 // interrupts service routines 
 
 void pressButtonMenu(void) {
@@ -59,6 +61,7 @@
     while (1) { // keep program running
         if (menuPress) {
             GameState *gs = new GameState();
+            numGamesPlayed++; 
             
             // request an open lobby from the server 
             sock.sendTo(nist, connectionRequest, strlen(connectionRequest)); 
@@ -68,7 +71,7 @@
             middleButton.reset(); 
             rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE);
             
-            while (!gs->isConnected() || !gs->done()) {
+            while (!gs->done()) {
                 if (sendFlag != 0) {
                     serverRequest["type"] = "move"; 
                     serverRequest["hash"] = "jaredyeagersflipflop"; 
@@ -84,6 +87,9 @@
                 }
                 int bytesRead = sock.receiveFrom(nist, tmp_buffer, sizeof(tmp_buffer));
                 tmp_buffer[bytesRead] = 0; 
+                if (numGamesPlayed == 2) {
+                    printf("read: %s\n\r", tmp_buffer); 
+                }
                 if (bytesRead > 0) {
                     parse(serverResponse, tmp_buffer); 
                     gs->updateAndRender(&serverResponse, gfx);
@@ -98,7 +104,12 @@
             int len = disconnectRequestContent.size(); 
             char *disconnectRequestPayload = (char *)disconnectRequestContent.c_str(); 
             
-            sock.sendTo(nist, disconnectRequestPayload, len);  
+            sock.sendTo(nist, disconnectRequestPayload, len); 
+            
+            // cleanup the socket from the previous game
+            int flushBytes = 0; 
+            while ((receiveFrom = sock.receiveFrom(nist, tmp_buffer, sizeof(tmp_buffer))) != 0) {}
+             
             gfx->renderGameOver(gs);
             
             // reset the game