Project

General

Profile

« Previous | Next » 

Revision 286d73ae

Added by Leszek Koltunski over 4 years ago

Port the downloading into the new 'magic' server.

View differences:

src/main/java/org/distorted/dialog/RubikDialogScoresView.java
81 81
    RubikScores scores = RubikScores.getInstance();
82 82

  
83 83
    boolean inserted = false;
84
    long myRecordInSeconds = scores.getRecord(object, size, scramble);
84
    long myRecordInMillis = scores.getRecord(object, size, scramble);
85
    float myRecordInSeconds = (myRecordInMillis/100)/10.0f;
86
    boolean mySubmitted = scores.isSubmitted(object, size, scramble);
85 87
    String myName = scores.getName();
86 88
    int myCountryID = scores.getCountryID();
87
    String myRecord = ( myRecordInSeconds<RubikScores.NO_RECORD ) ? Float.toString((myRecordInSeconds/100)/10.0f) : "??";
89
    String myRecord = ( myRecordInMillis<RubikScores.NO_RECORD ) ? Float.toString(myRecordInSeconds) : "??";
88 90
    String theirTime;
89 91
    int theirCountryID;
90 92

  
......
95 97
      {
96 98
      if( name[j] != null )
97 99
        {
98
        if( myRecordInSeconds<time[j] && !inserted )
100
        if( !mySubmitted && myRecordInSeconds<time[j] && !inserted )
99 101
          {
100 102
          inserted = true;
101 103
          View row = createRow(act, myCountryID, myName, myRecord, red);
......
128 130
    TextView textName = row.findViewById(R.id.scoresScrambleRowName);
129 131
    TextView textTime = row.findViewById(R.id.scoresScrambleRowTime);
130 132

  
131
    imgCoun.setImageResource(countryID!=0 ? countryID : R.drawable.unk);
133
    imgCoun.setImageResource(countryID!=0 ? countryID : R.drawable.un);
132 134
    textName.setText(name);
133 135
    textTime.setText(time);
134 136

  
src/main/java/org/distorted/object/RubikObjectList.java
113 113
    return -1;
114 114
    }
115 115

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

  
118
  public static int unpackObjectFromString(String obj)
119
    {
120
    int u = obj.indexOf('_');
121
    int l = obj.length();
122

  
123
    if( u>0 )
124
      {
125
      String name = obj.substring(0,u);
126
      int size = Integer.parseInt( obj.substring(u+1,l) );
127

  
128
      for(int i=0; i<NUM_OBJECTS; i++)
129
        {
130
        if( objects[i].name().equals(name) )
131
          {
132
          int s = getSize(i,size);
133
          return pack(i,s);
134
          }
135
        }
136
      }
137

  
138
    return -1;
139
    }
140

  
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

  
143
  public static String getObjectList()
144
    {
145
    String name;
146
    StringBuilder list = new StringBuilder();
147
    int len;
148
    int[] sizes;
149

  
150
    for(int i=0; i<NUM_OBJECTS; i++)
151
      {
152
      sizes = objects[i].mObjectSizes;
153
      len   = sizes.length;
154
      name  = objects[i].name();
155

  
156
      for(int j=0; j<len; j++)
157
        {
158
        if( i>0 || j>0 ) list.append(',');
159
        list.append(name);
160
        list.append('_');
161
        list.append(sizes[j]);
162
        }
163
      }
164

  
165
    return list.toString();
166
    }
167

  
116 168
///////////////////////////////////////////////////////////////////////////////////////////////////
117 169

  
118 170
  public static int getTotal()
src/main/java/org/distorted/scores/RubikScores.java
37 37
  private static RubikScores mThis;
38 38

  
39 39
  private long[][][] mRecords;
40
  private int [][][] mSubmitted;
41

  
40 42
  private String mName;
41 43
  private boolean mNameIsVerified;
42 44
  private int mNumRuns;
......
47 49

  
48 50
  private RubikScores()
49 51
    {
50
    mRecords = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
52
    mRecords   = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
53
    mSubmitted = new int [NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
51 54

  
52 55
    for(int i=0; i<NUM_OBJECTS; i++)
53 56
      for(int j=0; j<MAX_SIZE; j++)
54 57
        for(int k=0; k<MAX_SCRAMBLE; k++)
55 58
          {
56
          mRecords[i][j][k] = NO_RECORD;
59
          mRecords[i][j][k]   = NO_RECORD;
60
          mSubmitted[i][j][k] = 0;
57 61
          }
58 62

  
59 63
    mName = "YOU";
60 64
    mNameIsVerified = false;
61 65
    mNumPlays= -1;
62 66
    mNumRuns = -1;
63
    mCountryID = R.drawable.unk;
67
    mCountryID = R.drawable.un;
64 68
    }
65 69

  
66 70
///////////////////////////////////////////////////////////////////////////////////////////////////
......
101 105
          builder.append(sizes[size]);
102 106
          builder.append("=");
103 107
          builder.append(mRecords[object][size][scramble]);
108
          builder.append(",");
109
          builder.append(mSubmitted[object][size][scramble]);
104 110
          builder.append(" ");
105 111
          }
106 112
        }
......
118 124

  
119 125
  public void restorePreferences(SharedPreferences preferences)
120 126
    {
121
    String recordStr, subStr, nameStr, sizeStr, timeStr;
122
    int start, end, equals, underscore;
123
    int object, size;
127
    String recordStr, subStr, nameStr, sizeStr, timeStr, submStr;
128
    int start, end, equals, underscore, comma;
129
    int object, size, subm;
124 130
    long time;
125 131

  
126 132
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
......
139 145

  
140 146
        underscore = subStr.indexOf("_");
141 147
        equals = subStr.indexOf("=");
148
        comma = subStr.indexOf(",");
142 149

  
143
        if( underscore>=0 && equals>=0 )
150
        if( underscore>=0 && equals>=0 && comma>=0 )
144 151
          {
145 152
          nameStr = subStr.substring(0,underscore);
146 153
          sizeStr = subStr.substring(underscore+1, equals);
147
          timeStr = subStr.substring(equals+1);
154
          timeStr = subStr.substring(equals+1,comma);
155
          submStr = subStr.substring(comma+1);
148 156

  
149 157
          object = RubikObjectList.getOrdinal(nameStr);
150 158
          size   = RubikObjectList.getSize(object,Integer.parseInt(sizeStr));
151 159
          time   = Long.parseLong(timeStr);
160
          subm   = Integer.parseInt(submStr);
152 161

  
153
          if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE )
162
          if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE && subm>=0 && subm<=1 )
154 163
            {
155
            mRecords[object][size][scramble] = time;
164
            mRecords  [object][size][scramble] = time;
165
            mSubmitted[object][size][scramble] = subm;
156 166

  
157 167
            if( time<NO_RECORD )
158 168
              {
159
              android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time);
169
              android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time+" submitted: "+subm);
160 170
              }
161 171
            }
162 172
          else
......
227 237
    mCountryID = country;
228 238
    }
229 239

  
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
// TODO
242

  
243
  public void setSubmitted(int object, int size, int scramble)
244
    {
245
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
246

  
247
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
248
      {
249
      mSubmitted[object][size][scramble] = 1;
250
      }
251
    }
252

  
230 253
///////////////////////////////////////////////////////////////////////////////////////////////////
231 254

  
232 255
  public long getRecord(int object, int size, int scramble)
......
241 264
    return -1;
242 265
    }
243 266

  
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

  
269
  public boolean isSubmitted(int object, int size, int scramble)
270
    {
271
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
272

  
273
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=0 && scramble<MAX_SCRAMBLE )
274
      {
275
      return mSubmitted[object][size][scramble]==1;
276
      }
277

  
278
    return false;
279
    }
280

  
244 281
///////////////////////////////////////////////////////////////////////////////////////////////////
245 282
// TODO
246 283

  
src/main/java/org/distorted/scores/RubikScoresDownloader.java
38 38
    void exception(String exception);
39 39
    }
40 40

  
41
  public static final int MAX_PLACES = 12;
41
  public static final int MAX_PLACES = 10;
42 42

  
43 43
  private static final int DOWNLOAD   = 0;
44 44
  private static final int SUBMIT     = 1;
45 45
  private static final int IDLE       = 2;
46 46

  
47
  private static final String URL  ="http://koltunski.pl/rubik/cgi-bin";
48

  
49 47
  private final String[] hex = {
50 48
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
51 49
    "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
......
111 109
      {
112 110
      end = mScores.indexOf('\n', begin+1);
113 111
      if( end<0 ) end = len;
114
      fillRow(mScores.substring(begin+1,end));
112

  
113
      try
114
        {
115
        fillRow(mScores.substring(begin+1,end));
116
        }
117
      catch(Exception ex)
118
        {
119
        // faulty row - ignore
120
        }
121

  
115 122
      begin = end;
116 123
      }
117 124
    }
......
124 131
    int s2 = row.indexOf(' ',s1+1);
125 132
    int s3 = row.indexOf(' ',s2+1);
126 133
    int s4 = row.indexOf(' ',s3+1);
127
    int s5 = row.indexOf(' ',s4+1);
128
    int s6 = row.length();
134
    int s5 = row.length();
129 135

  
130 136
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
131 137
      {
132
      int size = Integer.parseInt( row.substring(0,s1) );
138
      int object = RubikObjectList.unpackObjectFromString( row.substring(0,s1) );
133 139

  
134
      if( size>=0 && size<mTotal )
140
      if( object>=0 && object<mTotal )
135 141
        {
136 142
        int level      = Integer.parseInt( row.substring(s1+1,s2) );
137
        int place      = Integer.parseInt( row.substring(s2+1,s3) );
138
        String name    = row.substring(s3+1, s4);
139
        int time       = Integer.parseInt( row.substring(s4+1,s5) );
140
        String country = row.substring(s5+1, s6);
143
        String name    = row.substring(s2+1, s3);
144
        int time       = Integer.parseInt( row.substring(s3+1,s4) );
145
        String country = row.substring(s4+1, s5);
141 146

  
142
        if(level>=0 && level<MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
147
        if(level>0 && level<=MAX_SCRAMBLE)
143 148
          {
144
          int p = mPlaces[size][level];
145
          mPlaces[size][level]++;
149
          int p = mPlaces[object][level];
150
          mPlaces[object][level]++;
146 151

  
147
          if( p!=place ) android.util.Log.e("downloader", "size="+size+" level="+level+" p="+p+" place="+place);
148

  
149
          mCountry[size][level][place] = country;
150
          mName   [size][level][place] = name;
151
          mTime   [size][level][place] = ((float)time)/10.0f;
152
          mCountry[object][level-1][p] = country;
153
          mName   [object][level-1][p] = name;
154
          mTime   [object][level-1][p] = ((float)(time/100))/10.0f;
152 155
          }
153 156
        }
154 157
      }
......
195 198

  
196 199
  private boolean doDownload()
197 200
    {
198
    String message=URL+"/download.cgi?n="+URLencode(mUserName)+"&r="+mNumRuns+"&e="+mVersion+"d";
201
    String message="https://distorted.org/magic/cgi-bin/download.cgi";
202
    message += "?n="+URLencode(mUserName)+"&r="+mNumRuns+"&e="+mVersion+"d";
203
    message += "&o="+RubikObjectList.getObjectList()+"&min=0&max="+MAX_SCRAMBLE+"&l="+MAX_PLACES;
199 204

  
200 205
    try
201 206
      {
......
270 275
        {
271 276
        mRunning = true;
272 277
        success = doDownload();
273
        fillValues();
274 278
        }
275 279
      }
276 280
    catch( Exception e )
......
278 282
      mReceiver.exception("Exception downloading records: "+e.getMessage() );
279 283
      }
280 284

  
285
    fillValues();
286

  
281 287
    mRunning = false;
282 288

  
283 289
    if( success )

Also available in: Unified diff