Skip to content

Commit

Permalink
Add server selection option
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjohnward committed Apr 19, 2016
1 parent 8683b57 commit c048c31
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 63 deletions.
13 changes: 8 additions & 5 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
<link rel="stylesheet" type="text/css" href="./styles/style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
function processInput(form) {
var TestVar = document.getElementsByName('inputbox')[0].value
function processInput() {
var username = document.getElementsByName('inputbox')[0].value;
const remote = require('electron').remote;
var player = remote.getGlobal("player");
player.name = TestVar;
var server = remote.getGlobal("server");
player.name = username;
server.url = document.getElementsByName('serverinput')[0].value;
window.location.href = "game.html";
}
</script>
</head>
<body>
<input type="text" name="inputbox"></input>
<button onclick="processInput()"></button>
Username: <input type="text" name="inputbox"></input>
Server: <input type="text" name="serverinput"></input>
<button onclick="processInput()">Login</button>
</body>
</html>
19 changes: 17 additions & 2 deletions client/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var users = remote.getGlobal("users");
var user_sprites = {}
var player = remote.getGlobal("player");
var level = remote.getGlobal("world");
var synced = remote.getGlobal("synced");


var word = "phaser";
Expand Down Expand Up @@ -269,9 +270,23 @@ function preload() {
}

function create() {

cursors = game.input.keyboard.createCursorKeys();
buildWorld();
player_sprite = new Sprite(player.name, "./assets/player.png", 2, 2);

console.log("test");

while(true) {
synced = remote.getGlobal("synced");
console.log(synced);
if (synced) {
level = remote.getGlobal("world");
buildWorld()
player_sprite = new Sprite(player.name, "./assets/player.png", 2, 2);
playerSync();
break;
}
}

//chat = new Chat();
//loadBitmapData();
//game.camera.follow(player_sprite);
Expand Down
128 changes: 72 additions & 56 deletions client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,10 @@ global["player"] = {
y: 5,
rotation: "up"
};

http.get(
"http://localhost:3000/world", function(response) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
//console.log(d);
body += d;
});
response.on('end', function() {

// Data reception is done, do whatever with it!
var parsed = JSON.parse(body);

//console.log(parsed);

global["world"] = parsed;
});
}
).end();

http.get(
"http://localhost:3000/users", function(response) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
//console.log(d);
body += d;
});
response.on('end', function() {

// Data reception is done, do whatever with it!
var parsed = JSON.parse(body);

//console.log(parsed);

global["users"] = parsed;
});
}
).end();
var server = global["server"] = {
url: ""
};
global["synced"] = false;

//prompt.start();
//prompt.get(['username'/*, 'email'*/], function (err, result) {
Expand Down Expand Up @@ -173,7 +137,13 @@ app.on('ready', function() {
});

setInterval(function(){
positionSync();
console.log(server.url);
console.log(global["player"].name);
if(server.url != "") {
console.log(server.url);
positionSync();
worldSync();
}
}, 500);

// Quit when all windows are closed.
Expand All @@ -194,19 +164,65 @@ app.on('activate', function () {
});

function positionSync() {
request.post(
'http://localhost:3000/user',
{
form:
{
name: JSON.stringify(player)
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
global["users"] = JSON.parse(response.body);
console.log(users);
}
}
);
if (global["player"].name == "") {
http.get(
server.url+"/world", function(response) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
//console.log(d);
body += d;
});
response.on('end', function() {

// Data reception is done, do whatever with it!
var parsed = JSON.parse(body);

//console.log(parsed);

global["world"] = parsed;
});
}
).end();
}
else {
request.post(
server.url+'/user',
{
form:
{
name: JSON.stringify(player)
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
global["users"] = JSON.parse(response.body);
console.log(users);
}
}
);
}
}

function worldSync() {
http.get(
server.url+"/world", function(response) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
//console.log(d);
body += d;
});
response.on('end', function() {

// Data reception is done, do whatever with it!
var parsed = JSON.parse(body);

//console.log(parsed);

global["world"] = parsed;
global["synced"] = true;
});
}
).end();
}

0 comments on commit c048c31

Please sign in to comment.