Skip to content

Commit 59d3c43

Browse files
committedMay 17, 2019
fix: playercount miscalculation
The previous way of calculating the player count apparently didn't work. Now when the player leaves the server, they're removed from the local cache. This is then used to get the player count. closes #5
1 parent b04bcb4 commit 59d3c43

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed
 

‎js/src/socket.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,21 @@ var localCache = {};
102102
function playerLeft(playerName){
103103
if (localCache[playerName].marker != null || localCache[playerName].marker != undefined){
104104
clearMarker(localCache[playerName].marker);
105-
localCache[playerName].marker = null;
105+
delete localCache[playerName];
106106
}
107107

108108
if ($("#playerSelect option[value='" + playerName + "']").length > 0){
109109
$("#playerSelect option[value='" + playerName + "']").remove();
110110
}
111111

112-
playerCount --;
112+
113+
playerCount = Object.keys(localCache).length;
114+
console.log("Playerleft playercount: " + playerCount);
113115
$("#player_count").text(playerCount);
114116
}
115117

116118
function getPlayerInfoHtml(plr){
117-
var html = '<div class="row info-body-row"><strong>Position:</strong>&nbsp;X {' + plr.pos.x.toFixed(4) + "} Y {" + plr.pos.y.toFixed(4) + "} Z {" + plr.pos.z.toFixed(4) + "}</div>"
119+
var html = '<div class="row info-body-row"><strong>Position:</strong>&nbsp;X {' + plr.pos.x.toFixed(4) + "} Y {" + plr.pos.y.toFixed(4) + "} Z {" + plr.pos.z.toFixed(4) + "}</div>";
118120
for(var key in plr){
119121
//console.log("found key: "+ key);
120122
if (key == "name" || key == "pos" || key == "icon"){ // I should probably turn this into a array or something
@@ -133,9 +135,9 @@ function getPlayerInfoHtml(plr){
133135

134136
function doPlayerUpdate(players){
135137
console.log(players);
136-
var _pc = 0;
138+
137139
players.forEach(function(plr){
138-
_pc ++;
140+
139141
if (plr == null || plr.name == undefined || plr.name == "") return;
140142

141143
if ( !(plr.identifer in localCache) ){
@@ -200,7 +202,7 @@ function doPlayerUpdate(players){
200202

201203
});
202204

203-
console.log("Playercount: " + _pc);
204-
$("#player_count").text(_pc);
205-
playerCount = _pc;
205+
playerCount = Object.keys(localCache).length;
206+
console.log("Playercount: " + playerCount);
207+
$("#player_count").text(playerCount);
206208
}

0 commit comments

Comments
 (0)