fix refresh spamming

This commit is contained in:
RandomityGuy 2024-06-06 22:34:20 +05:30
parent 89580f1e23
commit 52b86baebc
2 changed files with 18 additions and 4 deletions

View file

@ -124,16 +124,24 @@ class MPServerListGui extends GuiImage {
backButton.pressedAction = (e) -> MarbleGame.canvas.setContent(new MainMenuGui());
bottomBar.addChild(backButton);
var refreshing = false;
var refreshButton = new GuiXboxButton("Refresh", 220);
refreshButton.position = new Vector(750, 0);
refreshButton.vertSizing = Bottom;
refreshButton.horizSizing = Right;
refreshButton.pressedAction = (e) -> {
if (refreshing)
return;
refreshing = true;
MasterServerClient.connectToMasterServer(() -> {
MasterServerClient.instance.getServerList((servers) -> {
ourServerList = servers;
updateServerListDisplay();
refreshing = false;
});
}, () -> {
refreshing = false;
});
}
bottomBar.addChild(refreshButton);

View file

@ -41,7 +41,7 @@ class MasterServerClient {
var stopMutex:sys.thread.Mutex = new sys.thread.Mutex();
#end
public function new(onOpenFunc:() -> Void) {
public function new(onOpenFunc:() -> Void, onErrorFunc:() -> Void) {
#if hl
wsThread = sys.thread.Thread.create(() -> {
hl.Gc.enable(false);
@ -78,9 +78,15 @@ class MasterServerClient {
responses.add(() -> {
MarbleGame.canvas.pushDialog(new MessageBoxOkDlg("Failed to connect to master server: " + m));
});
if (onErrorFunc != null)
responses.add(() -> {
onErrorFunc();
});
#end
#if js
MarbleGame.canvas.pushDialog(new MessageBoxOkDlg("Failed to connect to master server: " + m));
if (onErrorFunc != null)
onErrorFunc();
#end
#if hl
stopMutex.acquire();
@ -159,16 +165,16 @@ class MasterServerClient {
#end
}
public static function connectToMasterServer(onConnect:() -> Void) {
public static function connectToMasterServer(onConnect:() -> Void, onError:() -> Void = null) {
if (instance == null)
instance = new MasterServerClient(onConnect);
instance = new MasterServerClient(onConnect, onError);
else {
if (instance.open)
onConnect();
else {
if (instance != null && instance.ws != null)
instance.ws.close();
instance = new MasterServerClient(onConnect);
instance = new MasterServerClient(onConnect, onError);
}
}
}