clean up lobby join leave stuff

This commit is contained in:
RandomityGuy 2024-04-25 00:23:34 +05:30
parent 6ed7b208d3
commit e533c69794
8 changed files with 99 additions and 28 deletions

View file

@ -67,9 +67,9 @@ class Particle {
public function update(time:Float, dt:Float) { public function update(time:Float, dt:Float) {
var t = dt; var t = dt;
var a = this.acc; var a = this.acc;
a = a.sub(this.vel.multiply(this.o.dragCoefficient)); a.load(a.sub(this.vel.multiply(this.o.dragCoefficient)));
this.vel = this.vel.add(a.multiply(dt)); this.vel.load(this.vel.add(a.multiply(dt)));
this.position = this.position.add(this.vel.multiply(dt)); this.position.load(this.position.add(this.vel.multiply(dt)));
this.currentAge += dt; this.currentAge += dt;
@ -257,7 +257,7 @@ class ParticleEmitter {
this.currentWaitPeriod = this.o.ejectionPeriod; this.currentWaitPeriod = this.o.ejectionPeriod;
var pos = this.getPosAtTime(time).clone(); var pos = this.getPosAtTime(time).clone();
if (this.o.spawnOffset != null) if (this.o.spawnOffset != null)
pos = pos.add(this.o.spawnOffset()); // Call the spawnOffset function if it's there pos.load(pos.add(this.o.spawnOffset())); // Call the spawnOffset function if it's there
// This isn't necessarily uniform but it's fine for the purpose. // This isn't necessarily uniform but it's fine for the purpose.
var randomPointOnSphere = new Vector(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1).normalized(); var randomPointOnSphere = new Vector(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1).normalized();
randomPointOnSphere.x *= this.spawnSphereSquish.x; randomPointOnSphere.x *= this.spawnSphereSquish.x;

View file

@ -297,7 +297,7 @@ class Util {
return false; return false;
} }
public static function formatScore(score:Float) { public static inline function formatScore(score:Float) {
var scoreInt = Std.int(Math.round(score)); var scoreInt = Std.int(Math.round(score));
return '${scoreInt}'; return '${scoreInt}';
} }
@ -349,7 +349,7 @@ class Util {
return str; return str;
} }
public static function getKeyForButton(button:Int) { public static inline function getKeyForButton(button:Int) {
var keyName = Key.getKeyName(button); var keyName = Key.getKeyName(button);
if (keyName == "MouseLeft") if (keyName == "MouseLeft")
keyName = "the Left Mouse Button"; keyName = "the Left Mouse Button";
@ -362,7 +362,7 @@ class Util {
return keyName; return keyName;
} }
public static function getKeyForButton2(button:Int) { public static inline function getKeyForButton2(button:Int) {
var keyName = Key.getKeyName(button); var keyName = Key.getKeyName(button);
if (keyName == "MouseLeft") if (keyName == "MouseLeft")
keyName = "Left Mouse"; keyName = "Left Mouse";

View file

@ -1,5 +1,6 @@
package gui; package gui;
import net.Net;
import modes.GameMode.ScoreType; import modes.GameMode.ScoreType;
import mis.MisParser; import mis.MisParser;
import hxd.BitmapData; import hxd.BitmapData;
@ -154,14 +155,16 @@ class EndGameGui extends GuiImage {
bottomBar.vertSizing = Bottom; bottomBar.vertSizing = Bottom;
innerCtrl.addChild(bottomBar); innerCtrl.addChild(bottomBar);
var retryButton = new GuiXboxButton("Retry", 160); if (!Net.isMP || Net.isHost) {
retryButton.position = new Vector(400, 0); var retryButton = new GuiXboxButton("Retry", 160);
retryButton.vertSizing = Bottom; retryButton.position = new Vector(400, 0);
retryButton.horizSizing = Right; retryButton.vertSizing = Bottom;
retryButton.gamepadAccelerator = ["B"]; retryButton.horizSizing = Right;
retryButton.accelerators = [hxd.Key.ESCAPE, hxd.Key.BACKSPACE]; retryButton.gamepadAccelerator = ["B"];
retryButton.pressedAction = (e) -> restartFunc(retryButton); retryButton.accelerators = [hxd.Key.ESCAPE, hxd.Key.BACKSPACE];
bottomBar.addChild(retryButton); retryButton.pressedAction = (e) -> restartFunc(retryButton);
bottomBar.addChild(retryButton);
}
// var lbButton = new GuiXboxButton("Leaderboard", 220); // var lbButton = new GuiXboxButton("Leaderboard", 220);
// lbButton.position = new Vector(750, 0); // lbButton.position = new Vector(750, 0);

View file

@ -163,6 +163,9 @@ class MultiplayerLevelSelectGui extends GuiImage {
state: Net.clientConnection.lobbyReady, state: Net.clientConnection.lobbyReady,
platform: Net.getPlatform() platform: Net.getPlatform()
}); });
}
if (Net.clientIdMap != null) {
for (c => v in Net.clientIdMap) { for (c => v in Net.clientIdMap) {
playerListArr.push({ playerListArr.push({
name: v.name, name: v.name,
@ -313,7 +316,6 @@ class MultiplayerLevelSelectGui extends GuiImage {
} }
return true; return true;
} }
setLevelFn = setLevel;
levelSelectOpts.position = new Vector(380, 430); levelSelectOpts.position = new Vector(380, 430);
levelSelectOpts.extent = new Vector(815, 94); levelSelectOpts.extent = new Vector(815, 94);
@ -324,6 +326,12 @@ class MultiplayerLevelSelectGui extends GuiImage {
NetCommands.setLobbyLevelIndex(i); NetCommands.setLobbyLevelIndex(i);
return true; return true;
}; };
setLevelFn = (idx) -> {
setLevel(idx);
levelSelectOpts.setCurrentOption(idx);
};
levelSelectOpts.setCurrentOption(currentSelectionStatic); levelSelectOpts.setCurrentOption(currentSelectionStatic);
setLevel(currentSelectionStatic); setLevel(currentSelectionStatic);
innerCtrl.addChild(levelSelectOpts); innerCtrl.addChild(levelSelectOpts);
@ -367,12 +375,14 @@ class MultiplayerLevelSelectGui extends GuiImage {
platform: Net.getPlatform() platform: Net.getPlatform()
}); });
} }
for (c => v in Net.clientIdMap) { if (Net.clientIdMap != null) {
playerListArr.push({ for (c => v in Net.clientIdMap) {
name: v.name, playerListArr.push({
state: v.lobbyReady, name: v.name,
platform: v.platform state: v.lobbyReady,
}); platform: v.platform
});
}
} }
playerList.setTexts(playerListArr.map(player -> { playerList.setTexts(playerListArr.map(player -> {
@ -383,4 +393,10 @@ class MultiplayerLevelSelectGui extends GuiImage {
public function updatePlayerCount(pub:Int, priv:Int, publicTotal:Int, privateTotal:Int) { public function updatePlayerCount(pub:Int, priv:Int, publicTotal:Int, privateTotal:Int) {
updatePlayerCountFn(pub, priv, publicTotal, privateTotal); updatePlayerCountFn(pub, priv, publicTotal, privateTotal);
} }
override function dispose() {
super.dispose();
playSelectedLevel = null;
setLevelFn = null;
}
} }

View file

@ -33,6 +33,12 @@ class MasterServerClient {
ws.onmessageString = (m) -> { ws.onmessageString = (m) -> {
handleMessage(m); handleMessage(m);
} }
ws.onerror = (m) -> {
MarbleGame.canvas.pushDialog(new MessageBoxOkDlg("Failed to connect to master server: " + m));
open = false;
ws = null;
instance = null;
}
} }
public static function process() { public static function process() {

View file

@ -162,14 +162,17 @@ class Net {
onPacketReceived(client, clientDatachannel, new InputBitStream(b)); onPacketReceived(client, clientDatachannel, new InputBitStream(b));
} }
clientDatachannel.onClosed = () -> { clientDatachannel.onClosed = () -> {
var weLeftOurselves = !Net.isClient; // If we left ourselves, this would be set to false due to order of ops, disconnect being called first, and then the datachannel closing
disconnect(); disconnect();
if (MarbleGame.instance.world != null) { if (MarbleGame.instance.world != null) {
MarbleGame.instance.quitMission(); MarbleGame.instance.quitMission();
} }
if (!(MarbleGame.canvas.content is MultiplayerLoadingGui)) { if (!weLeftOurselves) {
var loadGui = new MultiplayerLoadingGui("Server closed"); if (!(MarbleGame.canvas.content is MultiplayerLoadingGui)) {
MarbleGame.canvas.setContent(loadGui); var loadGui = new MultiplayerLoadingGui("Server closed");
loadGui.setErrorStatus("Server closed"); MarbleGame.canvas.setContent(loadGui);
loadGui.setErrorStatus("Server closed");
}
} }
} }
clientDatachannel.onError = (msg) -> { clientDatachannel.onError = (msg) -> {
@ -195,7 +198,8 @@ class Net {
Net.isMP = false; Net.isMP = false;
Net.isClient = false; Net.isClient = false;
Net.isHost = false; Net.isHost = false;
Net.client.close(); if (Net.client != null)
Net.client.close();
Net.client = null; Net.client = null;
Net.clientId = 0; Net.clientId = 0;
Net.clientIdMap.clear(); Net.clientIdMap.clear();
@ -361,6 +365,8 @@ class Net {
for (cc in clients) { for (cc in clients) {
cc.sendBytes(b); cc.sendBytes(b);
} }
// Send our current mission to connecting client
NetCommands.setLobbyLevelIndexClient(conn, MultiplayerLevelSelectGui.currentSelectionStatic);
} }
} }
@ -456,6 +462,11 @@ class Net {
} }
} }
public static function sendPacketToClient(client:GameConnection, packetData:OutputBitStream) {
var bytes = packetData.getBytes();
client.sendBytes(bytes);
}
public static function addDummyConnection() { public static function addDummyConnection() {
if (Net.isHost) { if (Net.isHost) {
addGhost(Net.clientId++); addGhost(Net.clientId++);

View file

@ -12,7 +12,11 @@ import gui.MultiplayerLoadingGui;
@:build(net.RPCMacro.build()) @:build(net.RPCMacro.build())
class NetCommands { class NetCommands {
@:rpc(server) public static function setLobbyLevelIndex(i:Int) { @:rpc(server) public static function setLobbyLevelIndex(i:Int) {
MultiplayerLevelSelectGui.setLevelFn(i); if (MultiplayerLevelSelectGui.setLevelFn == null) {
MultiplayerLevelSelectGui.currentSelectionStatic = i;
} else {
MultiplayerLevelSelectGui.setLevelFn(i);
}
} }
@:rpc(server) public static function playLevel(levelIndex:Int) { @:rpc(server) public static function playLevel(levelIndex:Int) {

View file

@ -15,6 +15,8 @@ class RPCMacro {
deserialize:Array<Expr> deserialize:Array<Expr>
}> = new Map(); }> = new Map();
var fieldsToAdd = [];
for (field in fields) { for (field in fields) {
if (field.meta.length > 0 && field.meta[0].name == ':rpc') { if (field.meta.length > 0 && field.meta[0].name == ':rpc') {
switch (field.kind) { switch (field.kind) {
@ -74,6 +76,32 @@ class RPCMacro {
Net.sendPacketToAll(stream); Net.sendPacketToAll(stream);
} }
}; };
var origExpr = f.expr;
var lastExprSingle = macro {
if (Net.isHost) {
var stream = new net.BitStream.OutputBitStream();
stream.writeByte(NetPacketType.NetCommand);
stream.writeByte($v{rpcFnId});
$b{serializeFns};
Net.sendPacketToClient(client, stream);
}
};
var singleClientfn:Field = {
name: field.name + "Client",
pos: Context.currentPos(),
access: [APublic, AStatic],
kind: FFun({
args: [
{
name: "client",
type: haxe.macro.TypeTools.toComplexType(Context.getType('net.ClientConnection.GameConnection'))
}
].concat(f.args),
expr: macro $b{[origExpr, lastExprSingle]}
})
};
fieldsToAdd.push(singleClientfn);
f.expr = macro $b{[f.expr, lastExpr]}; f.expr = macro $b{[f.expr, lastExpr]};
@ -138,6 +166,9 @@ class RPCMacro {
}; };
fields.push(deserializeField); fields.push(deserializeField);
for (fn in fieldsToAdd) {
fields.push(fn);
}
return fields; return fields;
} }