浏览代码

Win/Lose screen for multiplayer.

Aaro Saila 2 周之前
父节点
当前提交
7a4ebd0e36
共有 4 个文件被更改,包括 62 次插入 和 4 次删除
  1. 7 4
      src/common/networking.c
  2. 5 0
      src/common/networking.h
  3. 18 0
      src/game/MultiPlayer.c
  4. 32 0
      src/server/server.c

+ 7 - 4
src/common/networking.c

@@ -37,16 +37,18 @@ size_t NetMsgType_to_buf_size(const NetMsgType type) {
         return sizeof(BallUpdateMsg);
     case NET_MSG_SCORE_UPDATE:
         return sizeof(ScoreUpdateMsg);
+    case NET_MSG_WIN:
+        return sizeof(WinMsg);
     }
 
     SE_LOG_FATALF("Invalid msg type: %u", type);
 }
 
-#define FWRITE_CHECK                                                  \
-    if ((ferror(s) && errno == ECONNRESET) || feof(s)) {              \
+#define FWRITE_CHECK                                                     \
+    if ((ferror(s) && errno == ECONNRESET) || feof(s)) {                 \
         SE_LOG("Connection reset");                                      \
-        return false;                                                 \
-    } else {                                                          \
+        return false;                                                    \
+    } else {                                                             \
         SE_LOG_FATALF("fwrite failed: (%d) %s", errno, strerror(errno)); \
     }
 
@@ -132,6 +134,7 @@ int recv_msg_type(FILE *s, NetMsgType *const type) {
     case NET_MSG_IS_PLAYER_1:
     case NET_MSG_BALL_UPDATE:
     case NET_MSG_SCORE_UPDATE:
+    case NET_MSG_WIN:
         valid_type = true;
         break;
     }

+ 5 - 0
src/common/networking.h

@@ -16,6 +16,7 @@
     X(NET_MSG_IS_PLAYER_1) \
     X(NET_MSG_BALL_UPDATE) \
     X(NET_MSG_SCORE_UPDATE) \
+    X(NET_MSG_WIN) \
 
 #define X(msg) msg,
 
@@ -52,6 +53,10 @@ typedef struct [[gnu::packed]] {
     uint8_t player1; // bool
 } IsPlayer1Msg;
 
+typedef struct [[gnu::packed]] {
+    uint8_t win; // bool
+} WinMsg;
+
 const char *NetMsgType_to_str(const NetMsgType type);
 size_t NetMsgType_to_buf_size(const NetMsgType type);
 bool send_msg(FILE *s, const NetMsgType type, const uint8_t *const buf);

+ 18 - 0
src/game/MultiPlayer.c

@@ -246,6 +246,24 @@ Screen MultiPlayer_tick(MultiPlayer *const mp, const double delta) {
             case NET_MSG_SCORE_UPDATE:
                 handle_score_update_msg(mp);
                 break;
+            case NET_MSG_WIN:
+            {
+                WinMsg msg;
+                if (!recv_msg(mp->conn_s, NET_MSG_WIN, (uint8_t *) &msg)) {
+                    SE_LOG_ERROR("Connection reset");
+                    return PONG_MAIN_MENU;
+                }
+                if (msg.win == 1) {
+                    win_lose_render(true, mp->screen_w, mp->screen_h);
+                    return PONG_MAIN_MENU;
+                } else if (msg.win == 0) {
+                    win_lose_render(false, mp->screen_w, mp->screen_h);
+                    return PONG_MAIN_MENU;
+                } else {
+                    SE_LOG_ERRORF("Invalid WinMsg.win value: %d", msg.win);
+                }
+                break;
+            }
             }
             break;
         } else if (res == ECONNRESET) {

+ 32 - 0
src/server/server.c

@@ -34,6 +34,7 @@ static FILE *p2_conn = nullptr;
 static ServerState server_state = SS_LOBBY;
 static GameState game_state;
 static SCDArray_NetMsgType out_msgs;
+static bool l_win = false;
 
 SCDARRAY_IMPL(NetMsgType, NetMsgType)
 
@@ -104,6 +105,20 @@ static bool send_out_msgs() {
             }
             break;
         }
+        case NET_MSG_WIN:
+        {
+            WinMsg msg = {
+                .win = (uint8_t) l_win
+            };
+            if (!send_msg(p1_conn, NET_MSG_WIN, (uint8_t *) &msg)) {
+                return false;
+            }
+            msg.win = (uint8_t) !l_win;
+            if (!send_msg(p2_conn, NET_MSG_WIN, (uint8_t *) &msg)) {
+                return false;
+            }
+            return false;
+        }
         }
     }
 
@@ -194,6 +209,7 @@ static bool handle_player_input(const bool p1) {
         case NET_MSG_IS_PLAYER_1:
         case NET_MSG_BALL_UPDATE:
         case NET_MSG_SCORE_UPDATE:
+        case NET_MSG_WIN:
             break;
         }
         return true;
@@ -286,6 +302,22 @@ static void server_game() {
             assert_scufcont_success(SCDArray_NetMsgType_push_back(&out_msgs, &type));
             break;
         }
+        case GSEVENT_L_WIN:
+        {
+            SE_LOG("Win");
+            l_win = true;
+            const NetMsgType type = NET_MSG_WIN;
+            assert_scufcont_success(SCDArray_NetMsgType_push_back(&out_msgs, &type));
+            break;
+        }
+        case GSEVENT_R_WIN:
+        {
+            SE_LOG("Win");
+            l_win = false;
+            const NetMsgType type = NET_MSG_WIN;
+            assert_scufcont_success(SCDArray_NetMsgType_push_back(&out_msgs, &type));
+            break;
+        }
         }
 
         // Player output