Skip to content

Commit

Permalink
fixed issue with authenticated users position not saving
Browse files Browse the repository at this point in the history
callbacks within callbacks
  • Loading branch information
Johnsmith0508 committed Mar 7, 2016
1 parent 7c97a63 commit 6aaadb5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 24 deletions.
35 changes: 18 additions & 17 deletions app.js
Expand Up @@ -133,6 +133,7 @@ exports.start = function(port) {
socket /*.broadcast.to(socket.id)*/ .emit('user created');
console.info(user.name + " joined ");
userName = user.name;
User[userName] = new THREE.Object3D();
if (config.enableMysql) {
connection.query('SELECT * FROM ' + config.mysql.table, function(err, rows, fields) {
for (var i = 0; i < rows.length; i++) {
Expand All @@ -142,9 +143,24 @@ exports.start = function(port) {
break;
}
}
if (config.enableRedis && User[userName].posSaved) {
redisClient.hset("cubeuser:" + userName, 'keyConfig', user.keyConfig);
redisClient.hgetall("cubeuser:" + userName, function(err, obj) {
if (obj !== null) {
User[userName].phisObj.position.set(parseInt(obj.x), parseInt(obj.y), parseInt(obj.z));
User[userName].health = obj.health || 100;
try {
User[userName].items = JSON.parse(obj.items);
} catch (e) {
console.info("Can't read empty array");
console.warn(e);
console.warn(e.stack);
}
}
});
}
});
}
User[userName] = new THREE.Object3D();
User[userName].sid = socket.id;
User[userName].model = user.model;
User[userName].items = {};
Expand Down Expand Up @@ -180,22 +196,7 @@ exports.start = function(port) {
rotation: User[user.name].rotation.toArray()
});

if (config.enableRedis && User[userName].posSaved) {
redisClient.hset("cubeuser:" + userName, 'keyConfig', user.keyConfig);
redisClient.hgetall("cubeuser:" + userName, function(err, obj) {
if (obj !== null) {
User[userName].phisObj.position.set(parseInt(obj.x), parseInt(obj.y), parseInt(obj.z));
User[userName].health = obj.health || 100;
try {
User[userName].items = JSON.parse(obj.items);
} catch (e) {
console.info("Can't read empty array");
console.warn(e);
console.warn(e.stack);
}
}
});
}

socket.broadcast.emit('chat message', user.name + " Joined!");
});
socket.on('keys pressed', function(keys) {
Expand Down
3 changes: 2 additions & 1 deletion index.html
Expand Up @@ -128,7 +128,8 @@

<body>
<p id="ErrorMsg" hidden>Error #{error code}, please report to the developer</p>
<a href="//github.com/johnsmith0508/cubes" class="github-corner"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#fff; color:#151513; position: absolute; top: 0; border: 0; right: 0;"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>
<!--a href="//github.com/johnsmith0508/cubes" class="github-corner"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#fff; color:#151513; position: absolute; top: 0; border: 0; right: 0;"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a-->
<!--a href="https://github.com/johnsmith0508/cubes/issues" style="position:absolute;top:5px;right:5px;"><img src="https://img.shields.io/github/issues/johnsmith0508/cubes.svg" /></a-->
<style>
.github-corner:hover .octo-arm {
animation: octocat-wave 560ms ease-in-out
Expand Down
66 changes: 65 additions & 1 deletion js/GUI.js
Expand Up @@ -13,6 +13,7 @@ GUI.origin = new THREE.Vector3(0, 0, 0);
@constructor
*/
GUI.guiScene = function() {
var self = this;
/** the camera that is used
@private */
this.camera = new THREE.OrthographicCamera(window.innerWidth / -2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / -2, -500, 1000);
Expand Down Expand Up @@ -84,4 +85,67 @@ GUI.guiScene = function() {
sprite.textWidth = textWidth;
return sprite;
}
}
/**
@construtor
*/
this.Inventory = function(rows,columns) {
this.height = rows * 100 + 1;
this.width = columns * 100 + 1;
this.rows = rows;
this.columns = columns;
this.canvas = document.createElement("canvas");
this.canvas.height = this.height;
this.canvas.width = this.width;
this.grid = this.canvas.getContext("2d");
this.grid.beginPath();
this.grid.rect(0,0,this.width,this.height);
this.grid.fillStyle = "white";
this.grid.fill();
this.grid.lineWidth = 5;
this.grid.moveTo(0,0);
this.grid.lineTo(0,this.width);
this.grid.lineTo(this.height,this.width);
this.grid.lineTo(this.height,0);
this.grid.lineTo(0,0);
this.grid.stroke();
this.grid.lineWidth = 2;
for (var i = 0; i <= this.width; i+= 100)
{
this.grid.moveTo(i,0);
this.grid.lineTo(i,this.height);
}
for (var i = 0; i <= this.height; i+= 100)
{
this.grid.moveTo(0,i);
this.grid.lineTo(this.width,i);
}
this.grid.stroke();
this.canvas.style.zIndex = 9001;
this.canvas.style.position = 'absolute';
this.canvas.style.top = "5px";
this.canvas.style.left = "5px";
//document.body.appendChild(this.canvas);
this.texture = new THREE.Texture(this.canvas);
this.texture.needsUpdate = true;
this.material = new THREE.SpriteMaterial({map:this.texture});
this.sprite = new THREE.Sprite(this.material);
this.sprite.scale.set(this.width / 4, this.height / 4 , this.height / 4);
self.scene.add(this.sprite);
this.hidden = true;
this.containerObject = new THREE.Object3D();
this.addItemStack = function(itemStack) {
this.containerObject.add(itemStack.model);
}
this.show = function() {
this.canvas.style.visibility = "visible";
this.hidden = false;
}
this.hide = function() {
this.canvas.style.visibility = "hidden";
this.hidden = true;
}
this.toggle = function() {
if(this.hidden){this.show();}else{this.hide();}
}
}
}
1 change: 0 additions & 1 deletion js/chatlogic.js
Expand Up @@ -2,7 +2,6 @@
var hasChatOpen = false;
$(document.body).on('click','#send',function(){
hasChatOpen = false;
//console.log("send button clicked");
if($('#msgIn').val().length > 0){
socket.emit('chat message', $('#msgIn').val());
$('#msgIn').val('');
Expand Down
5 changes: 1 addition & 4 deletions js/client.js
Expand Up @@ -252,7 +252,6 @@ var submitHandler = function() {
registerChatSocket();
if ($("#name").val().length > 0) {
modelType = $(".model:checked").val();
console.log(keycode);
socket.emit('create user', {
name: $("#name").val(),
model: modelType,
Expand Down Expand Up @@ -329,7 +328,6 @@ var mainLoop = function() {
}

var preInit = function() {
//console.log('preinit start');
userName = $("#name").val();
$('#login').hide();
$("#threeJsRenderWindow").append(renderer.domElement);
Expand Down Expand Up @@ -566,12 +564,11 @@ $(function() {
}
}
} catch (e) {
console.log(e)
console.log(e);
}}
});
$("#server").val(config.client.defaultServer);
if (getCookie('login')) {
console.log("asked");
$("#name").hide().val(getCookie('login'));
$("#loginTypes").hide();
$("#logoutButton").show();
Expand Down

0 comments on commit 6aaadb5

Please sign in to comment.