Revision 4895fff6
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/scores/RubikScoresDownloader.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.scores; |
21 | 21 |
|
22 |
import android.support.v4.app.FragmentActivity; |
|
23 |
|
|
24 |
import org.distorted.magic.R; |
|
22 | 25 |
import org.distorted.object.RubikObjectList; |
23 | 26 |
|
24 | 27 |
import java.io.InputStream; |
... | ... | |
35 | 38 |
public interface Receiver |
36 | 39 |
{ |
37 | 40 |
void receive(String[][][] country, String[][][] name, float[][][] time); |
38 |
void exception(String exception);
|
|
41 |
void message(String mess);
|
|
39 | 42 |
} |
40 | 43 |
|
41 | 44 |
public static final int MAX_PLACES = 10; |
... | ... | |
82 | 85 |
private static boolean mRunning = false; |
83 | 86 |
private static int mMode = IDLE; |
84 | 87 |
private static Receiver mReceiver; |
85 |
private static String mUserName, mVersion; |
|
86 |
private static int mNumRuns; |
|
88 |
private static String mVersion; |
|
87 | 89 |
|
88 | 90 |
private static int mTotal = RubikObjectList.getTotal(); |
89 | 91 |
private static String mScores = ""; |
... | ... | |
94 | 96 |
private static int[][] mPlaces = new int[mTotal][MAX_SCRAMBLE]; |
95 | 97 |
private static RubikScoresDownloader mThis; |
96 | 98 |
|
99 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
100 |
// TODO |
|
101 |
|
|
102 |
private static int computeHash(String name, String veri, String country, String lvllist, |
|
103 |
String objlist, String timelist, int runnings) |
|
104 |
{ |
|
105 |
return 0; |
|
106 |
} |
|
107 |
|
|
108 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
109 |
// TODO: handle the fact that the server might return 'error' (3) or 'name taken' (2) |
|
110 |
|
|
111 |
private void fillSubmittedValues() |
|
112 |
{ |
|
113 |
fillDownloadedValues(); |
|
114 |
} |
|
115 |
|
|
97 | 116 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
98 | 117 |
|
99 |
private void fillValues() |
|
118 |
private void fillDownloadedValues()
|
|
100 | 119 |
{ |
101 | 120 |
int begin=-1 ,end, len = mScores.length(); |
102 | 121 |
|
... | ... | |
197 | 216 |
|
198 | 217 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
199 | 218 |
|
200 |
private boolean doDownload()
|
|
219 |
private boolean network(String url)
|
|
201 | 220 |
{ |
202 |
String message="https://distorted.org/magic/cgi-bin/download.cgi"; |
|
203 |
message += "?n="+URLencode(mUserName)+"&r="+mNumRuns+"&e="+mVersion+"d"; |
|
204 |
message += "&o="+RubikObjectList.getObjectList()+"&min=0&max="+MAX_SCRAMBLE+"&l="+MAX_PLACES; |
|
221 |
android.util.Log.e("down", "url: "+url); |
|
205 | 222 |
|
206 | 223 |
try |
207 | 224 |
{ |
208 |
java.net.URL connectURL = new URL(message);
|
|
225 |
java.net.URL connectURL = new URL(url);
|
|
209 | 226 |
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); |
210 | 227 |
|
211 | 228 |
conn.setDoInput(true); |
... | ... | |
227 | 244 |
} |
228 | 245 |
catch( final Exception e) |
229 | 246 |
{ |
230 |
mReceiver.exception("Failed to get an answer from the High Scores server");
|
|
247 |
mReceiver.message("Failed to get an answer from the High Scores server");
|
|
231 | 248 |
return false; |
232 | 249 |
} |
233 | 250 |
} |
234 | 251 |
catch( final UnknownHostException e ) |
235 | 252 |
{ |
236 |
mReceiver.exception("No access to Internet");
|
|
253 |
mReceiver.message("No access to Internet");
|
|
237 | 254 |
return false; |
238 | 255 |
} |
239 | 256 |
catch( final SecurityException e ) |
240 | 257 |
{ |
241 |
mReceiver.exception("Application not authorized to connect to the Internet");
|
|
258 |
mReceiver.message("Application not authorized to connect to the Internet");
|
|
242 | 259 |
return false; |
243 | 260 |
} |
244 | 261 |
catch( final Exception e ) |
245 | 262 |
{ |
246 |
mReceiver.exception(e.getMessage());
|
|
263 |
mReceiver.message(e.getMessage());
|
|
247 | 264 |
return false; |
248 | 265 |
} |
249 | 266 |
|
250 | 267 |
if( mScores.length()==0 ) |
251 | 268 |
{ |
252 |
mReceiver.exception("Failed to download scores");
|
|
269 |
mReceiver.message("Failed to download scores");
|
|
253 | 270 |
return false; |
254 | 271 |
} |
255 | 272 |
|
... | ... | |
270 | 287 |
{ |
271 | 288 |
boolean success=true; |
272 | 289 |
|
273 |
try
|
|
290 |
if( mMode==DOWNLOAD && gottaDownload() )
|
|
274 | 291 |
{ |
275 |
if( gottaDownload() ) |
|
292 |
mRunning = true; |
|
293 |
|
|
294 |
try |
|
276 | 295 |
{ |
277 |
mRunning = true; |
|
278 |
success = doDownload(); |
|
296 |
RubikScores scores = RubikScores.getInstance(); |
|
297 |
String name = URLencode(scores.getName()); |
|
298 |
String veri = scores.isVerified() ? name : ""; |
|
299 |
int numRuns = scores.getNumRuns(); |
|
300 |
int numPlay = scores.getNumPlays(); |
|
301 |
|
|
302 |
String url="https://distorted.org/magic/cgi-bin/download.cgi"; |
|
303 |
url += "?n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&e="+mVersion+"d"; |
|
304 |
url += "&o="+RubikObjectList.getObjectList()+"&min=0&max="+MAX_SCRAMBLE+"&l="+MAX_PLACES; |
|
305 |
|
|
306 |
success = network(url); |
|
279 | 307 |
} |
308 |
catch( Exception e ) |
|
309 |
{ |
|
310 |
mReceiver.message("Exception downloading records: "+e.getMessage() ); |
|
311 |
} |
|
312 |
|
|
313 |
fillDownloadedValues(); |
|
280 | 314 |
} |
281 |
catch( Exception e ) |
|
315 |
|
|
316 |
if( mMode==SUBMIT ) |
|
282 | 317 |
{ |
283 |
mReceiver.exception("Exception downloading records: "+e.getMessage() ); |
|
284 |
} |
|
318 |
mRunning = true; |
|
285 | 319 |
|
286 |
fillValues(); |
|
320 |
try |
|
321 |
{ |
|
322 |
RubikScores scores = RubikScores.getInstance(); |
|
323 |
String name = URLencode(scores.getName()); |
|
324 |
String veri = scores.isVerified() ? name : ""; |
|
325 |
int numRuns = scores.getNumRuns(); |
|
326 |
int numPlay = scores.getNumPlays(); |
|
327 |
int deviceID= scores.getDeviceID(); |
|
328 |
String objlist = scores.getUnsubmittedObjlist(); |
|
329 |
String lvllist = scores.getUnsubmittedLevellist(); |
|
330 |
String timlist = scores.getUnsubmittedTimelist(); |
|
331 |
String country = scores.getCountry(); |
|
332 |
int hash = computeHash(name,veri,country,lvllist,objlist,timlist,numRuns); |
|
333 |
|
|
334 |
String url="https://distorted.org/magic/cgi-bin/submit.cgi"; |
|
335 |
url += "?n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion+"d"; |
|
336 |
url += "&o="+objlist+"&l="+lvllist+"&t="+timlist+"&c="+country+"&h="+hash; |
|
337 |
url += "&oo="+RubikObjectList.getObjectList()+"&min=0&max="+MAX_SCRAMBLE+"&lo="+MAX_PLACES; |
|
338 |
|
|
339 |
success = network(url); |
|
340 |
} |
|
341 |
catch( Exception e ) |
|
342 |
{ |
|
343 |
mReceiver.message("Exception submitting records: "+e.getMessage() ); |
|
344 |
} |
|
345 |
|
|
346 |
fillSubmittedValues(); |
|
347 |
} |
|
287 | 348 |
|
288 | 349 |
mRunning = false; |
289 | 350 |
|
... | ... | |
323 | 384 |
|
324 | 385 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
325 | 386 |
|
326 |
public void download(Receiver receiver, String userName, String version, int numRuns)
|
|
387 |
public void download(Receiver receiver, FragmentActivity act)
|
|
327 | 388 |
{ |
328 | 389 |
mReceiver = receiver; |
329 | 390 |
mMode = DOWNLOAD; |
330 |
mUserName = userName; |
|
331 |
mVersion = version; |
|
332 |
mNumRuns = numRuns; |
|
391 |
mVersion = act.getString(R.string.app_version); |
|
333 | 392 |
|
334 | 393 |
Thread networkThrd = new Thread(this); |
335 | 394 |
networkThrd.start(); |
... | ... | |
337 | 396 |
|
338 | 397 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
339 | 398 |
|
340 |
public void submit() |
|
399 |
public void submit(Receiver receiver, FragmentActivity act)
|
|
341 | 400 |
{ |
342 |
mMode = SUBMIT; |
|
401 |
mReceiver = receiver; |
|
402 |
mMode = SUBMIT; |
|
403 |
mVersion = act.getString(R.string.app_version); |
|
343 | 404 |
|
344 |
android.util.Log.e("downloader", "submitting!"); |
|
405 |
Thread networkThrd = new Thread(this); |
|
406 |
networkThrd.start(); |
|
345 | 407 |
} |
346 | 408 |
} |
Also available in: Unified diff
Beginnings of submitting one's high scores.