Project

General

Profile

« Previous | Next » 

Revision 17f9a695

Added by Leszek Koltunski over 4 years ago

progress with submitting one's high scores.

View differences:

src/main/java/org/distorted/dialog/RubikDialogScoresPagerAdapter.java
60 60
      }
61 61
    }
62 62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

  
65
  public void message(final String mess)
66
    {
67
    mAct.runOnUiThread(new Runnable()
68
      {
69
      @Override
70
      public void run()
71
        {
72
        for(int i=0; i<mNumTabs; i++)
73
          {
74
          mViews[i].message(mess);
75
          }
76
        }
77
      });
78
    }
79

  
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

  
82
  public void serverError(final String error)
83
    {
84
    char errorNumber = error.charAt(0);
85

  
86
    switch(errorNumber)
87
      {
88
      case '2': message("Name Taken");   // TODO
89
                break;
90
      case '3': message("Server error");
91
                break;
92
      }
93
    }
94

  
63 95
///////////////////////////////////////////////////////////////////////////////////////////////////
64 96

  
65 97
  private void prepareView()
......
113 145
      }
114 146
    }
115 147

  
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

  
118
  public void message(final String mess)
119
    {
120
    mAct.runOnUiThread(new Runnable()
121
      {
122
      @Override
123
      public void run()
124
        {
125
        for(int i=0; i<mNumTabs; i++)
126
          {
127
          mViews[i].message(mess);
128
          }
129
        }
130
      });
131
    }
132

  
133 148
///////////////////////////////////////////////////////////////////////////////////////////////////
134 149

  
135 150
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting)
src/main/java/org/distorted/scores/RubikScores.java
73 73
    mDeviceID= -1;
74 74
    }
75 75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

  
78
  private int privateGetDeviceID()
79
    {
80
    int id;
81

  
82
    try
83
      {
84
      String s = UUID.randomUUID().toString();
85
      id = (s!=null ? s.hashCode():0);
86
      }
87
    catch(Exception ex)
88
      {
89
      id = 0;
90
      android.util.Log.e("scores", "Exception in getDeviceID()");
91
      }
92

  
93
    return id<0 ? -id : id;
94
    }
95

  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

  
98
  private String getUnsubmittedList(int mode)
99
    {
100
    RubikObjectList list;
101
    StringBuilder builder = new StringBuilder();
102
    boolean first = true;
103
    int[] sizes;
104
    int length;
105

  
106
    for(int object=0; object<NUM_OBJECTS; object++)
107
      {
108
      list = RubikObjectList.getObject(object);
109
      sizes = list.getSizes();
110
      length = sizes.length;
111

  
112
      for(int size=0; size<length; size++)
113
        {
114
        for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
115
          {
116
          if( mSubmitted[object][size][scramble]==0 && mRecords[object][size][scramble]<NO_RECORD )
117
            {
118
            if( !first ) builder.append(',');
119
            else         first=false;
120

  
121
            switch(mode)
122
              {
123
              case 0: builder.append(list.name());
124
                      builder.append("_");
125
                      builder.append(sizes[size]);
126
                      break;
127
              case 1: builder.append(scramble);
128
                      break;
129
              case 2: builder.append(mRecords[object][size][scramble]);
130
                      break;
131
              }
132
            }
133
          }
134
        }
135
      }
136

  
137
    return builder.toString();
138
    }
139

  
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
// PUBLIC API
76 142
///////////////////////////////////////////////////////////////////////////////////////////////////
77 143

  
78 144
  public static RubikScores getInstance()
......
170 236
            {
171 237
            mRecords  [object][size][scramble] = time;
172 238
            mSubmitted[object][size][scramble] = subm;
173

  
239
/*
174 240
            if( time<NO_RECORD )
175 241
              {
176 242
              android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time+" submitted: "+subm);
177 243
              }
244
*/
178 245
            }
179 246
          else
180 247
            {
......
190 257
    mNumRuns        = preferences.getInt("scores_numRuns" , 0);
191 258
    mDeviceID       = preferences.getInt("scores_deviceid",-1);
192 259

  
193
    if( mDeviceID==-1 ) mDeviceID = getDeviceID();
260
    if( mDeviceID==-1 ) mDeviceID = privateGetDeviceID();
194 261
    }
195 262

  
196 263
///////////////////////////////////////////////////////////////////////////////////////////////////
......
234 301
    }
235 302

  
236 303
///////////////////////////////////////////////////////////////////////////////////////////////////
237
// TODO
238 304

  
239
  public void successfulSubmit()
305
  void successfulSubmit()
240 306
    {
241 307
    mNameIsVerified = true;
242 308

  
......
271 337
    if( mCountry.equals("do") ) mCountry = "dm";
272 338
    }
273 339

  
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

  
276
  int getDeviceID()
277
    {
278
    int id;
279

  
280
    try
281
      {
282
      String s = UUID.randomUUID().toString();
283
      id = (s!=null ? s.hashCode():0);
284
      }
285
    catch(Exception ex)
286
      {
287
      id = 0;
288
      android.util.Log.e("scores", "Exception in getDeviceID()");
289
      }
290

  
291
    return id<0 ? -id : id;
292
    }
293

  
294 340
///////////////////////////////////////////////////////////////////////////////////////////////////
295 341

  
296 342
  public long getRecord(int object, int size, int scramble)
......
319 365
    return false;
320 366
    }
321 367

  
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

  
370
  int getDeviceID()
371
    {
372
    return mDeviceID;
373
    }
374

  
322 375
///////////////////////////////////////////////////////////////////////////////////////////////////
323 376

  
324 377
  boolean isVerified()
......
356 409

  
357 410
///////////////////////////////////////////////////////////////////////////////////////////////////
358 411

  
359
  private String getUnsubmittedList(int mode)
412
  boolean thereAreUnsubmittedRecords()
360 413
    {
361 414
    RubikObjectList list;
362
    StringBuilder builder = new StringBuilder();
363
    boolean first = true;
364
    int[] sizes;
365 415
    int length;
366 416

  
367 417
    for(int object=0; object<NUM_OBJECTS; object++)
368 418
      {
369 419
      list = RubikObjectList.getObject(object);
370
      sizes = list.getSizes();
371
      length = sizes.length;
420
      length = list.getSizes().length;
372 421

  
373 422
      for(int size=0; size<length; size++)
374
        {
375 423
        for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
376
          {
377 424
          if( mSubmitted[object][size][scramble]==0 && mRecords[object][size][scramble]<NO_RECORD )
378 425
            {
379
            if( !first ) builder.append(',');
380
            else         first=false;
381

  
382
            switch(mode)
383
              {
384
              case 0: builder.append(list.name());
385
                      builder.append("_");
386
                      builder.append(sizes[size]);
387
                      break;
388
              case 1: builder.append(scramble);
389
                      break;
390
              case 2: builder.append(mRecords[object][size][scramble]);
391
                      break;
392
              }
426
            return true;
393 427
            }
394
          }
395
        }
396 428
      }
397 429

  
398
    return builder.toString();
430
    return false;
399 431
    }
400 432

  
401 433
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/scores/RubikScoresDownloader.java
39 39
    {
40 40
    void receive(String[][][] country, String[][][] name, float[][][] time);
41 41
    void message(String mess);
42
    void serverError(String error);
42 43
    }
43 44

  
44 45
  public static final int MAX_PLACES = 10;
......
106 107
    }
107 108

  
108 109
///////////////////////////////////////////////////////////////////////////////////////////////////
109
// TODO: handle the fact that the server might return 'error' (3) or 'name taken' (2)
110 110

  
111
  private void fillSubmittedValues()
112
    {
113
    fillDownloadedValues();
114
    }
115

  
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

  
118
  private void fillDownloadedValues()
111
  private boolean fillValues()
119 112
    {
120 113
    int begin=-1 ,end, len = mScores.length();
114
    String row;
121 115

  
122 116
    for(int i=0; i<mTotal; i++)
123 117
      for(int j=0; j<MAX_SCRAMBLE; j++)
......
132 126

  
133 127
      try
134 128
        {
135
        fillRow(mScores.substring(begin+1,end));
129
        row = mScores.substring(begin+1,end);
130

  
131
        if( row.length()==1 )
132
          {
133
          mReceiver.serverError(row);
134
          return false;
135
          }
136
        else
137
          {
138
          fillRow(row);
139
          }
136 140
        }
137 141
      catch(Exception ex)
138 142
        {
......
141 145

  
142 146
      begin = end;
143 147
      }
148

  
149
    return true;
144 150
    }
145 151

  
146 152
///////////////////////////////////////////////////////////////////////////////////////////////////
......
273 279
    return true;
274 280
    }
275 281

  
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

  
284
  private String constructDownloadURL()
285
    {
286
    RubikScores scores = RubikScores.getInstance();
287
    String name = URLencode(scores.getName());
288
    String veri = scores.isVerified() ? name : "";
289
    int numRuns = scores.getNumRuns();
290
    int numPlay = scores.getNumPlays();
291

  
292
    String url="https://distorted.org/magic/cgi-bin/download.cgi";
293
    url += "?n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&e="+mVersion+"d";
294
    url += "&o="+RubikObjectList.getObjectList()+"&min=0&max="+MAX_SCRAMBLE+"&l="+MAX_PLACES;
295

  
296
    return url;
297
    }
298

  
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

  
301
  private String constructSubmitURL()
302
    {
303
    RubikScores scores = RubikScores.getInstance();
304
    String name = URLencode(scores.getName());
305
    String veri = scores.isVerified() ? name : "";
306
    int numRuns = scores.getNumRuns();
307
    int numPlay = scores.getNumPlays();
308
    int deviceID= scores.getDeviceID();
309
    String objlist = scores.getUnsubmittedObjlist();
310
    String lvllist = scores.getUnsubmittedLevellist();
311
    String timlist = scores.getUnsubmittedTimelist();
312
    String country = scores.getCountry();
313
    int hash = computeHash(name,veri,country,lvllist,objlist,timlist,numRuns);
314

  
315
    String url="https://distorted.org/magic/cgi-bin/submit.cgi";
316
    url += "?n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion+"d";
317
    url += "&o="+objlist+"&l="+lvllist+"&t="+timlist+"&c="+country+"&h="+hash;
318
    url += "&oo="+RubikObjectList.getObjectList()+"&min=0&max="+MAX_SCRAMBLE+"&lo="+MAX_PLACES;
319

  
320
    return url;
321
    }
322

  
276 323
///////////////////////////////////////////////////////////////////////////////////////////////////
277 324

  
278 325
  private boolean gottaDownload()
......
287 334
    {
288 335
    boolean success=true;
289 336

  
290
    if( mMode==DOWNLOAD && gottaDownload() )
337
    try
291 338
      {
292
      mRunning = true;
293

  
294
      try
339
      if( mMode==DOWNLOAD && gottaDownload() )
295 340
        {
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);
341
        mRunning = true;
342
        success = network(constructDownloadURL());
307 343
        }
308
      catch( Exception e )
344
      if( mMode==SUBMIT )
309 345
        {
310
        mReceiver.message("Exception downloading records: "+e.getMessage() );
311
        }
346
        mRunning = true;
312 347

  
313
      fillDownloadedValues();
348
        if( RubikScores.getInstance().thereAreUnsubmittedRecords() )
349
          {
350
          success = network(constructSubmitURL());
351
          }
352
        }
314 353
      }
315

  
316
    if( mMode==SUBMIT )
354
    catch( Exception e )
317 355
      {
318
      mRunning = true;
319

  
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();
356
      mReceiver.message("Exception downloading records: "+e.getMessage() );
347 357
      }
348 358

  
349
    mRunning = false;
359
    if( mRunning )
360
      {
361
      success = fillValues();
362
      mRunning = false;
363
      }
350 364

  
351 365
    if( success )
352 366
      {
353 367
      mReceiver.receive(mCountry, mName, mTime);
368

  
369
      if( mMode==SUBMIT )
370
        {
371
        RubikScores.getInstance().successfulSubmit();
372
        }
354 373
      }
355 374
    }
356 375

  

Also available in: Unified diff