Skip to content

Commit c048c31

Browse files
committed
Add server selection option
1 parent 8683b57 commit c048c31

File tree

3 files changed

+97
-63
lines changed

3 files changed

+97
-63
lines changed

client/index.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
<link rel="stylesheet" type="text/css" href="./styles/style.css" />
55
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
66
<script>
7-
function processInput(form) {
8-
var TestVar = document.getElementsByName('inputbox')[0].value
7+
function processInput() {
8+
var username = document.getElementsByName('inputbox')[0].value;
99
const remote = require('electron').remote;
1010
var player = remote.getGlobal("player");
11-
player.name = TestVar;
11+
var server = remote.getGlobal("server");
12+
player.name = username;
13+
server.url = document.getElementsByName('serverinput')[0].value;
1214
window.location.href = "game.html";
1315
}
1416
</script>
1517
</head>
1618
<body>
17-
<input type="text" name="inputbox"></input>
18-
<button onclick="processInput()"></button>
19+
Username: <input type="text" name="inputbox"></input>
20+
Server: <input type="text" name="serverinput"></input>
21+
<button onclick="processInput()">Login</button>
1922
</body>
2023
</html>

client/js/game.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var users = remote.getGlobal("users");
55
var user_sprites = {}
66
var player = remote.getGlobal("player");
77
var level = remote.getGlobal("world");
8+
var synced = remote.getGlobal("synced");
89

910

1011
var word = "phaser";
@@ -269,9 +270,23 @@ function preload() {
269270
}
270271

271272
function create() {
273+
272274
cursors = game.input.keyboard.createCursorKeys();
273-
buildWorld();
274-
player_sprite = new Sprite(player.name, "./assets/player.png", 2, 2);
275+
276+
console.log("test");
277+
278+
while(true) {
279+
synced = remote.getGlobal("synced");
280+
console.log(synced);
281+
if (synced) {
282+
level = remote.getGlobal("world");
283+
buildWorld()
284+
player_sprite = new Sprite(player.name, "./assets/player.png", 2, 2);
285+
playerSync();
286+
break;
287+
}
288+
}
289+
275290
//chat = new Chat();
276291
//loadBitmapData();
277292
//game.camera.follow(player_sprite);

client/main.js

Lines changed: 72 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,10 @@ global["player"] = {
1212
y: 5,
1313
rotation: "up"
1414
};
15-
16-
http.get(
17-
"http://localhost:3000/world", function(response) {
18-
// Continuously update stream with data
19-
var body = '';
20-
response.on('data', function(d) {
21-
//console.log(d);
22-
body += d;
23-
});
24-
response.on('end', function() {
25-
26-
// Data reception is done, do whatever with it!
27-
var parsed = JSON.parse(body);
28-
29-
//console.log(parsed);
30-
31-
global["world"] = parsed;
32-
});
33-
}
34-
).end();
35-
36-
http.get(
37-
"http://localhost:3000/users", function(response) {
38-
// Continuously update stream with data
39-
var body = '';
40-
response.on('data', function(d) {
41-
//console.log(d);
42-
body += d;
43-
});
44-
response.on('end', function() {
45-
46-
// Data reception is done, do whatever with it!
47-
var parsed = JSON.parse(body);
48-
49-
//console.log(parsed);
50-
51-
global["users"] = parsed;
52-
});
53-
}
54-
).end();
15+
var server = global["server"] = {
16+
url: ""
17+
};
18+
global["synced"] = false;
5519

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

175139
setInterval(function(){
176-
positionSync();
140+
console.log(server.url);
141+
console.log(global["player"].name);
142+
if(server.url != "") {
143+
console.log(server.url);
144+
positionSync();
145+
worldSync();
146+
}
177147
}, 500);
178148

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

196166
function positionSync() {
197-
request.post(
198-
'http://localhost:3000/user',
199-
{
200-
form:
201-
{
202-
name: JSON.stringify(player)
203-
}
204-
},
205-
function (error, response, body) {
206-
if (!error && response.statusCode == 200) {
207-
global["users"] = JSON.parse(response.body);
208-
console.log(users);
209-
}
210-
}
211-
);
167+
if (global["player"].name == "") {
168+
http.get(
169+
server.url+"/world", function(response) {
170+
// Continuously update stream with data
171+
var body = '';
172+
response.on('data', function(d) {
173+
//console.log(d);
174+
body += d;
175+
});
176+
response.on('end', function() {
177+
178+
// Data reception is done, do whatever with it!
179+
var parsed = JSON.parse(body);
180+
181+
//console.log(parsed);
182+
183+
global["world"] = parsed;
184+
});
185+
}
186+
).end();
187+
}
188+
else {
189+
request.post(
190+
server.url+'/user',
191+
{
192+
form:
193+
{
194+
name: JSON.stringify(player)
195+
}
196+
},
197+
function (error, response, body) {
198+
if (!error && response.statusCode == 200) {
199+
global["users"] = JSON.parse(response.body);
200+
console.log(users);
201+
}
202+
}
203+
);
204+
}
205+
}
206+
207+
function worldSync() {
208+
http.get(
209+
server.url+"/world", function(response) {
210+
// Continuously update stream with data
211+
var body = '';
212+
response.on('data', function(d) {
213+
//console.log(d);
214+
body += d;
215+
});
216+
response.on('end', function() {
217+
218+
// Data reception is done, do whatever with it!
219+
var parsed = JSON.parse(body);
220+
221+
//console.log(parsed);
222+
223+
global["world"] = parsed;
224+
global["synced"] = true;
225+
});
226+
}
227+
).end();
212228
}

0 commit comments

Comments
 (0)