@@ -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
175139setInterval ( 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
196166function 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