From d7acac92279759eee38f9a1c924d8f097bec791d Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Sun, 23 Jun 2024 01:16:22 +0530 Subject: [PATCH] fix mid-game join --- src/MarbleWorld.hx | 28 ++++++++++++------ src/net/Net.hx | 2 +- src/net/NetCommands.hx | 64 +++++++++++++++++++++++------------------- 3 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/MarbleWorld.hx b/src/MarbleWorld.hx index 79bd849e..aa5e7960 100644 --- a/src/MarbleWorld.hx +++ b/src/MarbleWorld.hx @@ -240,6 +240,8 @@ class MarbleWorld extends Scheduler { public var _ready:Bool = false; + var _skipPreGame:Bool = false; + var _loadBegin:Bool = false; var _loaded:Bool = false; @@ -553,7 +555,12 @@ class MarbleWorld extends Scheduler { // } if (this.isMultiplayer) { // Push the pre - game - showPreGame(); + if (!_skipPreGame) { + showPreGame(); + } else { + _skipPreGame = false; + NetCommands.requestMidGameJoinState(Net.clientId); + } } this.gameMode.onMissionLoad(); } @@ -1360,14 +1367,17 @@ class MarbleWorld extends Scheduler { } // Scoreboard! - // var b = new OutputBitStream(); - // b.writeByte(NetPacketType.ScoreBoardInfo); - // var sbPacket = new ScoreboardPacket(); - // for (player in @:privateAccess this.playGui.playerList) { - // sbPacket.scoreBoard.set(player.id, player.score); - // } - // sbPacket.serialize(b); - // packets.push(b.getBytes()); + var b = new OutputBitStream(); + b.writeByte(NetPacketType.ScoreBoardInfo); + var sbPacket = new ScoreboardPacket(); + for (player in @:privateAccess this.playGui.playerList) { + sbPacket.scoreBoard.set(player.id, player.score); + sbPacket.rBoard.set(player.id, player.r); + sbPacket.yBoard.set(player.id, player.y); + sbPacket.bBoard.set(player.id, player.b); + } + sbPacket.serialize(b); + packets.push(b.getBytes()); return packets; } diff --git a/src/net/Net.hx b/src/net/Net.hx index 5433e7e4..c4307270 100644 --- a/src/net/Net.hx +++ b/src/net/Net.hx @@ -589,7 +589,7 @@ class Net { // if (MultiplayerLevelSelectGui.custSelected) { // NetCommands.playCustomLevelMidJoinClient(conn, MultiplayerLevelSelectGui.custPath); // } else - // NetCommands.playLevelMidJoinClient(conn, MPPlayMissionGui.currentCategoryStatic, MPPlayMissionGui.currentSelectionStatic); + NetCommands.playLevelMidJoinClient(conn, MPPlayMissionGui.currentCategoryStatic, MPPlayMissionGui.currentSelectionStatic); MarbleGame.instance.world.addJoiningClient(conn, () -> {}); var playerInfoBytes = sendPlayerInfosBytes(); for (dc => cc in clients) { diff --git a/src/net/NetCommands.hx b/src/net/NetCommands.hx index 9d868c00..06558e48 100644 --- a/src/net/NetCommands.hx +++ b/src/net/NetCommands.hx @@ -56,11 +56,13 @@ class NetCommands { // } // } - @:rpc(server) public static function playLevelMidJoin(index:Int) { + @:rpc(server) public static function playLevelMidJoin(category:String, levelIndex:Int) { if (Net.isClient) { - var difficultyMissions = MissionList.missionList['ultra']["multiplayer"]; - var curMission = difficultyMissions[index]; + MissionList.buildMissionList(); + var difficultyMissions = MissionList.missionList['multiplayer'][category]; + var curMission = difficultyMissions[levelIndex]; MarbleGame.instance.playMission(curMission, true); + @:privateAccess MarbleGame.instance.world._skipPreGame = true; } } @@ -146,35 +148,39 @@ class NetCommands { Net.serverInfo.state = "PLAYING"; MasterServerClient.instance.sendServerInfo(Net.serverInfo); // notify the server of the playing state } - } else { - // Mid game join - Console.log("Mid game join for client " + clientId); - // Send em our present world state - if (MarbleGame.instance.world != null) { - var packets = MarbleGame.instance.world.getWorldStateForClientJoin(); - var c = Net.clientIdMap[clientId]; - for (packet in packets) { - c.sendBytes(packet); - } - Net.clientIdMap[clientId].ready(); + } else {} + } + } - if (MarbleGame.instance.world.serverStartTicks == 0) { - var allReady = true; - for (id => client in Net.clientIdMap) { - if (client.state != GameplayState.GAME) { - allReady = false; - break; - } + @:rpc(client) public static function requestMidGameJoinState(clientId:Int) { + if (Net.isHost) { + // Mid game join + Console.log("Mid game join for client " + clientId); + // Send em our present world state + if (MarbleGame.instance.world != null) { + var packets = MarbleGame.instance.world.getWorldStateForClientJoin(); + var c = Net.clientIdMap[clientId]; + for (packet in packets) { + c.sendBytes(packet); + } + Net.clientIdMap[clientId].ready(); + + if (MarbleGame.instance.world.serverStartTicks == 0) { + var allReady = true; + for (id => client in Net.clientIdMap) { + if (client.state != GameplayState.GAME) { + allReady = false; + break; } - if (allReady) { - if (MarbleGame.instance.world != null) { - MarbleGame.instance.world.allClientsReady(); - } - } - } else { - // Send the start ticks - NetCommands.setStartTicksMidJoinClient(c, MarbleGame.instance.world.serverStartTicks, MarbleGame.instance.world.timeState.ticks); } + if (allReady) { + if (MarbleGame.instance.world != null) { + MarbleGame.instance.world.allClientsReady(); + } + } + } else { + // Send the start ticks + NetCommands.setStartTicksMidJoinClient(c, MarbleGame.instance.world.serverStartTicks, MarbleGame.instance.world.timeState.ticks); } } }