Skip to content

Commit

Permalink
fixed server issues with too many emits
Browse files Browse the repository at this point in the history
  • Loading branch information
ToWelie89 committed Mar 29, 2021
1 parent 273c7a1 commit 36c19b2
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 61 deletions.
2 changes: 1 addition & 1 deletion js/attack/attackModalController.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class AttackModalController {

if (this.multiplayerMode) {
// display territories in battle for other players
this.socketService.gameSocket.on('skipToNextPlayer', () => {
this.socketService.setGameListener('skipToNextPlayer', () => {
this.retreat();
});
}
Expand Down
28 changes: 14 additions & 14 deletions js/game/gameControllerMultiplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class GameControllerMultiplayer extends GameController {
}

setSocketListeners() {
this.socketService.gameSocket.on('playerWonNotifier', (endScreenData) => {
this.socketService.setGameListener('playerWonNotifier', (ev, {endScreenData}) => {
clearInterval(this.vm.hourGlassTimerInterval);
console.log('A player won', endScreenData);

Expand All @@ -490,7 +490,7 @@ class GameControllerMultiplayer extends GameController {
this.$rootScope.$apply();
});

this.socketService.gameSocket.on('newReinforcements', newTroops => {
this.socketService.setGameListener('newReinforcements', (ev, {newTroops}) => {
this.soundService.cardTurnIn.play();
for (let i = 0; i < newTroops; i++) {
setTimeout(() => {
Expand All @@ -501,23 +501,23 @@ class GameControllerMultiplayer extends GameController {
}
});

this.socketService.gameSocket.on('setHighlightedNotifier', territoryName => {
this.socketService.setGameListener('setHighlightedNotifier', (ev, {territoryName}) => {
document.querySelector(`${this.mapSelector} svg .country[id="${territoryName}"]`).classList.add('highlighted');
});

this.socketService.gameSocket.on('removeHighlightedNotifier', territoryName => {
this.socketService.setGameListener('removeHighlightedNotifier', (ev, {territoryName}) => {
document.querySelector(`${this.mapSelector} svg .country[id="${territoryName}"`).classList.remove('highlighted');
});

this.socketService.gameSocket.on('setArrowNotifier', (from, to, arrowType) => {
this.socketService.setGameListener('setArrowNotifier', (ev, {from, to, arrowType}) => {
setArrowBetweenTerritories(this.mapSelector, from, to, arrowType, true);
});

this.socketService.gameSocket.on('clearArrowNotifier', () => {
this.socketService.setGameListener('clearArrowNotifier', () => {
clearArrow(this.mapSelector, true);
});

this.socketService.gameSocket.on('updatedCardsForPlayer', (ownerUid, cards) => {
this.socketService.setGameListener('updatedCardsForPlayer', (ev, {ownerUid, cards}) => {
this.soundService.cardSelect.play();
const playerName = Array.from(this.gameEngine.players.values()).find(x => x.userUid === ownerUid).name;

Expand All @@ -533,7 +533,7 @@ class GameControllerMultiplayer extends GameController {
this.vm.turn = this.gameEngine.turn;
});

this.socketService.gameSocket.on('updateMapState', (territories, doNotRemoveHighlightClass) => {
this.socketService.setGameListener('updateMapState', (ev, {territories, doNotRemoveHighlightClass}) => {
this.gameEngine.map.getAllTerritoriesAsList().forEach(t => {
const territoryFromServer = territories.find(x => x.name === t.name);
t.owner = territoryFromServer.owner;
Expand All @@ -543,7 +543,7 @@ class GameControllerMultiplayer extends GameController {
this.$scope.$apply();
});

this.socketService.gameSocket.on('troopAddedToTerritoryNotifier', (territoryName, doNotRemoveHighlightClass = false) => {
this.socketService.setGameListener('troopAddedToTerritoryNotifier', (ev, {territoryName, doNotRemoveHighlightClass}) => {
this.gameEngine.addTroopToTerritory(territoryName);
this.vm.troopsToDeploy = this.gameEngine.troopsToDeploy;
this.$scope.$apply();
Expand All @@ -556,7 +556,7 @@ class GameControllerMultiplayer extends GameController {
this.mapService.updateMapForMultiplayer(this.gameEngine.filter, this.vm.myUid, doNotRemoveHighlightClass);
});

this.socketService.gameSocket.on('nextTurnNotifier', (turn, reinforcementData) => {
this.socketService.setGameListener('nextTurnNotifier', (ev, {turn, reinforcementData}) => {
this.gameEngine.selectedTerritory = undefined;
if (turn.newPlayer && turn.player.type === PLAYER_TYPES.HUMAN) {
this.initiateTimer();
Expand Down Expand Up @@ -606,7 +606,7 @@ class GameControllerMultiplayer extends GameController {
}
});

this.socketService.gameSocket.on('battleFoughtNotifier', (battleData, doNotRemoveHighlightClass = false) => {
this.socketService.setGameListener('battleFoughtNotifier', (ev, {battleData, doNotRemoveHighlightClass}) => {
console.log('battleData',battleData);

getTerritoryByName(this.gameEngine.map, battleData.attackerTerritory).numberOfTroops = battleData.attackerNumberOfTroops;
Expand All @@ -627,7 +627,7 @@ class GameControllerMultiplayer extends GameController {
this.updateChartData();
});

this.socketService.gameSocket.on('updateOwnerAfterSuccessfulInvasionNotifier', (updateOwnerData) => {
this.socketService.setGameListener('updateOwnerAfterSuccessfulInvasionNotifier', (ev, {updateOwnerData}) => {
getTerritoryByName(this.gameEngine.map, updateOwnerData.attackerTerritory).numberOfTroops = updateOwnerData.attackerTerritoryNumberOfTroops;
getTerritoryByName(this.gameEngine.map, updateOwnerData.defenderTerritory).numberOfTroops = updateOwnerData.defenderTerritoryNumberOfTroops;
getTerritoryByName(this.gameEngine.map, updateOwnerData.defenderTerritory).owner = updateOwnerData.owner;
Expand All @@ -638,15 +638,15 @@ class GameControllerMultiplayer extends GameController {
this.updateChartData();
});

this.socketService.gameSocket.on('updateMovementNotifier', (map) => {
this.socketService.setGameListener('updateMovementNotifier', (ev, {map}) => {
this.gameEngine.map.getAllTerritoriesAsList().forEach(t => {
const territoryFromServer = map.find(x => x.name === t.name);
t.owner = territoryFromServer.owner;
t.numberOfTroops = territoryFromServer.numberOfTroops;
});
});

this.socketService.gameSocket.on('messagesUpdated', (messages) => {
this.socketService.setGameListener('messagesUpdated', (ev, {messages}) => {
if (
!this.vm.muteChat &&
this.$rootScope.currentGamePhase === GAME_PHASES.GAME_MULTIPLAYER &&
Expand Down
2 changes: 1 addition & 1 deletion js/gameSetup/characterSelectionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CharacterSelectionController {
}

if (this.multiplayer) { // Is multiplayer game
this.socketService.gameSocket.on('updatedPlayers', players => {
this.socketService.setGameListener('updatedPlayers', players => {
this.vm.selectedPlayers = players.filter(x => x !== undefined);
this.vm.takenAvatars = this.vm.selectedPlayers.map(x => x.avatar);
this.$scope.$apply();
Expand Down
39 changes: 21 additions & 18 deletions js/multiplayer/lobbiesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,27 +278,29 @@ class LobbiesController {
}

setupSocket() {
this.socketService.createLobbiesSocket();
if (!this.socketService.lobbiesSocket) {
this.socketService.createLobbiesSocket();
}

this.vm.socketService.lobbiesSocket.on('connect', () => {
this.startPingInterval();
this.startPingInterval();

this.vm.socketService.lobbiesSocket.emit('getLobbies');
this.vm.socketService.lobbiesSocket.emit('getLobbies');

firebase.auth().onAuthStateChanged(user => {
if (user) {
this.setUser(user.displayName, user.uid);
} else {
this.setUser();
}
});
firebase.auth().onAuthStateChanged(user => {
if (user) {
this.setUser(user.displayName, user.uid);
} else {
this.setUser();
}
});

this.vm.socketService.lobbiesSocket.emit('fetchOnlineUsers');

this.vm.socketService.lobbiesSocket.on('updatedBioOfUserNotifier', response => {
this.socketService.setLobbiesListener('updatedBioOfUserNotifier', (ev, {response}) => {
this.setTooltipOfOnlineUser(response.uid, response);
});

this.vm.socketService.lobbiesSocket.on('onlineUsers', onlineUsers => {
this.socketService.setLobbiesListener('onlineUsers', (ev, {onlineUsers}) => {
this.vm.onlineUsers = onlineUsers;
console.log('Players online: ', onlineUsers);

Expand All @@ -316,10 +318,9 @@ class LobbiesController {
this.setTooltipOfOnlineUser(u.userUid, u.tooltipInfo);
}
});

});

this.vm.socketService.lobbiesSocket.on('currentLobbies', lobbies => {
this.socketService.setLobbiesListener('currentLobbies', (ev, {lobbies}) => {
this.vm.lobbies = [];
lobbies.forEach(lobby => {
this.vm.lobbies.push(lobby);
Expand All @@ -345,13 +346,15 @@ class LobbiesController {
this.$scope.$apply();
}, 1);
});
this.vm.socketService.lobbiesSocket.on('createNewRoomResponse', room => {
this.$rootScope.currentLobby = room;

this.socketService.setLobbiesListener('createNewRoomResponse', (ev, {room}) => {
this.$rootScope.currentGamePhase = GAME_PHASES.PLAYER_SETUP_MULTIPLAYER;
this.$rootScope.currentLobby = room;
this.$rootScope.$apply();
stopGlobalLoading();
});
this.vm.socketService.lobbiesSocket.on('disconnect', data => {

this.socketService.setLobbiesListener('disconnect', (ev, {data}) => {
if (this.$rootScope.currentGamePhase === GAME_PHASES.MULTIPLAYER_LOBBIES && data === 'transport close') {
this.$scope.$apply();
console.log('DISCONNECTED');
Expand Down
51 changes: 31 additions & 20 deletions js/multiplayer/lobbyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ class LobbiesController {
this.settings = settings;
this.vm.customCharacters = [];

this.$rootScope.$watch('currentLobby', () => {
this.initLobby();
this.lobbyId = -1;

if (this.$rootScope.currentLobbyWatcher) {
this.$rootScope.currentLobbyWatcher();
}
this.$rootScope.currentLobbyWatcher = this.$rootScope.$watch('currentLobby', (newVal, oldVal) => {
console.log(oldVal.id, newVal.id)
if (!!newVal && !this.lobbyInitialized) {
this.lobbyInitialized = true;
this.initLobby();
}
});

this.vm.victoryGoals = VICTORY_GOALS;
Expand Down Expand Up @@ -158,13 +167,9 @@ class LobbiesController {
this.vm.aiSpeedSliderOptions.disabled = !this.vm.userIsHost;
const userName = user.displayName ? user.displayName : user.email;

if (!this.socketService.lobbiesSocket) {
this.socketService.createLobbiesSocket();
}
if (!this.socketService.gameSocket) {
this.socketService.createGameSocket();
}

this.addSocketListeners();

firebase.database().ref('/users/' + user.uid).once('value').then(snapshot => {
Expand Down Expand Up @@ -264,15 +269,14 @@ class LobbiesController {
}

addSocketListeners() {
this.soundService.tick.play();
this.socketService.gameSocket.on('updatedColorOfPlayer', (playerUid, playerColor) => {
this.socketService.setGameListener('updatedColorOfPlayer', (ev, {playerUid, playerColor}) => {
const player = this.vm.players.find(p => p.userUid === playerUid);
if (player) {
player.color = playerColor;
}
});

this.socketService.gameSocket.on('messagesUpdated', (messages) => {
this.socketService.setGameListener('messagesUpdated', (ev, {messages}) => {
if (!this.vm.muteChat && this.$rootScope.currentGamePhase === GAME_PHASES.PLAYER_SETUP_MULTIPLAYER && this.vm.showLobbyChat) {
this.soundService.newMessage.play();
}
Expand All @@ -296,55 +300,54 @@ class LobbiesController {
console.log('Lobby messages', this.vm.lobbyChatMessages);
});

this.socketService.gameSocket.on('kicked', () => {
this.socketService.setGameListener('kicked', (ev, {}) => {
this.soundService.tick.play();
this.$rootScope.currentLobby = '';
this.$rootScope.currentGamePhase = GAME_PHASES.MULTIPLAYER_LOBBIES;
this.toastService.errorToast('', 'You have been kicked from the lobby.');
this.socketService.disconnectGameSocket();
});

this.socketService.gameSocket.on('hostLeft', () => {
this.socketService.setGameListener('hostLeft', (ev, {}) => {
this.$rootScope.currentLobby = '';
this.$rootScope.currentGamePhase = GAME_PHASES.MULTIPLAYER_LOBBIES;
this.toastService.infoToast('', 'The host has left the game.');
this.socketService.disconnectGameSocket();
});

this.socketService.gameSocket.on('updatedPlayers', players => {
this.socketService.setGameListener('updatedPlayers', (ev, {players}) => {
this.setPlayers(players);
console.log('Players updated in room', this.vm.players);
});

this.socketService.gameSocket.on('updatedLockedSlots', lockedSlots => {
this.socketService.setGameListener('updatedLockedSlots', (ev, {lockedSlots}) => {
this.soundService.tick.play();
this.vm.lockedSlots = lockedSlots;
this.setPlayers(this.vm.players);
this.vm.room.maxNumberOfPlayer = (CONSTANTS.MAX_NUMBER_OF_PLAYERS - this.vm.lockedSlots.length);
});

this.socketService.gameSocket.on('setTurnLengthNotifier', turnLength => {
this.socketService.setGameListener('setTurnLengthNotifier', (ev, {turnLength}) => {
this.soundService.tick.play();
this.vm.turnLength = turnLength;
this.$rootScope.turnLength = turnLength;
this.$scope.$apply();
window.dispatchEvent(new Event('resize'));
});

this.socketService.gameSocket.on('setAiSpeedNotifier', aiSpeed => {
this.socketService.setGameListener('setAiSpeedNotifier', (ev, {aiSpeed}) => {
this.soundService.tick.play();
this.vm.aiSpeed = aiSpeed;
this.$scope.$apply();
window.dispatchEvent(new Event('resize'));
});

this.socketService.gameSocket.on('setGoalNotifier', chosenGoal => {
this.socketService.setGameListener('setGoalNotifier', (ev, {chosenGoal}) => {
this.soundService.tick.play();
this.vm.chosenGoal = chosenGoal;
this.$scope.$apply();
});

this.socketService.gameSocket.on('gameStarted', (players, victoryGoal, territories, turn, troopsToDeploy, selectedMap) => {
this.socketService.setGameListener('gameStarted', (ev, {players, victoryGoal, territories, turn, troopsToDeploy, selectedMap}) => {
console.log('troopsToDeploy', troopsToDeploy);
this.gameEngine.currentGameIsMultiplayer = true;
this.gameEngine.turn = turn;
Expand All @@ -367,7 +370,7 @@ class LobbiesController {
this.$rootScope.$apply();
});

this.socketService.gameSocket.on('disconnect', data => {
this.socketService.setGameListener('disconnect', (ev, {data}) => {
if (data === 'transport close') { // server disconnected
this.toastService.errorToast(
'Lost connection',
Expand All @@ -378,6 +381,14 @@ class LobbiesController {
this.$rootScope.$apply();
}
});

/*
this.socketService.gameSocket.on('updatedColorOfPlayer', (playerUid, playerColor) => {
const player = this.vm.players.find(p => p.userUid === playerUid);
if (player) {
player.color = playerColor;
}
});*/
}

setPlayers(players) {
Expand Down
Loading

0 comments on commit 36c19b2

Please sign in to comment.