Skip to content

Commit

Permalink
better connections
Browse files Browse the repository at this point in the history
  • Loading branch information
mattieFM committed Sep 16, 2023
1 parent c511ddd commit 97f9dd9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions www/mods/_multiplayer/hostMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ MATTIE.scenes.multiplayer.host.prototype.addOptionsBtns = function(){
}).bind(this));
this._optionsWindow.setHandler(MATTIE.CmdManager.returnToMultiplayer, (()=>{
MATTIE.multiplayer.getCurrentNetController().destroyAllConns();
MATTIE.multiplayer.getCurrentNetController().resetNet();
MATTIE.menus.multiplayer.openMultiplayer();

}).bind(this));
Expand Down
2 changes: 1 addition & 1 deletion www/mods/_multiplayer/joinMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ MATTIE.scenes.multiplayer.join.prototype.addOptionsBtns = function(){

this._optionsWindow.setHandler(MATTIE.CmdManager.joinGame, (()=>{
this._inputWin.close();
MATTIE.multiplayer.clientController.hostId = this._inputWin.getInput()
MATTIE.multiplayer.clientController.hostId = (this._inputWin.getInput()).trim();
MATTIE.menus.multiplayer.openLobby();
}).bind(this));

Expand Down
2 changes: 1 addition & 1 deletion www/mods/_multiplayer/lobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ MATTIE.scenes.multiplayer.lobby.prototype.create = function(){
btns[MATTIE.TextManager.forceStart] = MATTIE.CmdManager.forceStart
this._optionsWindow = new MATTIE.windows.horizontalBtns(175+300+10, btns, 2);
this._optionsWindow.setHandler(MATTIE.CmdManager.disconnect, (()=>{
MATTIE.multiplayer.getCurrentNetController().disconnectAllConns();
MATTIE.multiplayer.clientController.disconnectAllConns();
MATTIE.menus.multiplayer.openMultiplayer();
}).bind(this))

Expand Down
8 changes: 6 additions & 2 deletions www/mods/_multiplayer/netControllerV2/baseNetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BaseNetController extends EventEmitter {
disconnectAllConns(){
this.canTryToReconnect = true;
if(this.self){
this.self.disconnect();
this.self.disconnect()
}
// if(this.conn){
// this.conn.disconnect();
Expand All @@ -47,8 +47,8 @@ class BaseNetController extends EventEmitter {

reconnectAllConns(){
if(this.self){
if(!this.self.disconnected)
this.self.reconnect();
this.self.disconnected = false;
}
this.setIsClient();
// if(this.conn){
Expand Down Expand Up @@ -76,6 +76,10 @@ class BaseNetController extends EventEmitter {
}

clearControlVars(){
this.started = false;
this.players = {};
/** @type {[PlayerModel]} */
this.netPlayers = {};
MATTIE.multiplayer.isClient = false;
MATTIE.multiplayer.isHost = false;
MATTIE.multiplayer.isActive = false;
Expand Down
19 changes: 15 additions & 4 deletions www/mods/_multiplayer/netControllerV2/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,34 @@ class ClientController extends BaseNetController {
}

open(){
if(!this.lastHostId) this.lastHostId = "";
this.initEmitterOverrides(); //override stuff for interceptors
this.setIsClient();
if(!this.self){
if(!this.lastHostId.includes(this.hostId) || !this.canTryToReconnect){
this.self = new Peer();
this.self.on('open', ()=>{
this.peerId = this.self.id;
this.player.setPeerId(this.peerId)
console.info(`Client opened at: ${this.peerId}`)
console.info(`Attempting to connect to host at: ${this.hostId}`)
MATTIE.multiplayer.clientController.connect();
this.connect();
})
}else{
this.connect();
}




}

connect(hostId=this.hostId){
if(this.lastHostId === hostId && this.canTryToReconnect) {
if(!this.lastHostId) this.lastHostId = "";
if(this.lastHostId.includes(hostId) && this.canTryToReconnect) {
console.log("last")
this.reconnectAllConns();
this.sendPlayerInfo();
this.lastHostId = hostId;
return
}

Expand All @@ -60,7 +70,8 @@ class ClientController extends BaseNetController {
this.conn.on("data", (data) => {
//if(!this.self.disconnected)
this.onData(data,this.conn);
})
});

this.lastHostId = hostId;
}

Expand Down
4 changes: 4 additions & 0 deletions www/mods/commonLibs/_common/menus/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ MATTIE.windows.textInput.prototype.initEventHandler = function() {
}

document.addEventListener('keydown', this._listenFunc, false);

setTimeout(() => { //automatically close after 50 seconds
this.close();
}, 50000);

}

Expand Down

0 comments on commit 97f9dd9

Please sign in to comment.