Project

General

Profile

Download (8.14 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / magic / RubikScoresDownloader.java @ d8aa4ba8

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.magic;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
class RubikScoresDownloader implements Runnable
25
  {
26
  interface Receiver
27
    {
28
    void receive(String[][][][] scores);
29
    }
30

    
31
  static final int MAX_PLACES = 10;
32

    
33
  private static final int DOWNLOAD   = 0;
34
  private static final int SUBMIT     = 1;
35
  private static final int IDLE       = 2;
36

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

    
39
  private static boolean mRunning = false;
40
  private static Thread mNetworkThrd = null;
41
  private static int mMode = IDLE;
42
  private static Receiver mReceiver;
43

    
44
  private static String mScores = "";
45
  private static String[][][][] mValues = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES][3];
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  private void fillValues()
50
    {
51
    int begin=-1 ,end, len = mScores.length();
52

    
53
    while( begin<len )
54
      {
55
      end = mScores.indexOf('\n', begin+1);
56
      if( end<0 ) end = len;
57
      fillRow(mScores.substring(begin+1,end));
58
      begin = end;
59
      }
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  private void fillRow(String row)
65
    {
66
    int s1 = row.indexOf(' ');
67
    int s2 = row.indexOf(' ',s1+1);
68
    int s3 = row.indexOf(' ',s2+1);
69
    int s4 = row.indexOf(' ',s3+1);
70
    int s5 = row.indexOf(' ',s4+1);
71
    int s6 = row.length();
72

    
73
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
74
      {
75
      int size = Integer.valueOf( row.substring(0,s1) );
76

    
77
      if( size>=0 && size<RubikSize.LENGTH )
78
        {
79
        int level      = Integer.valueOf( row.substring(s1+1,s2) );
80
        int place      = Integer.valueOf( row.substring(s2+1,s3) );
81
        String name    = row.substring(s3+1, s4);
82
        int time       = Integer.valueOf( row.substring(s4+1,s5) );
83
        String country = row.substring(s5+1, s6);
84
        String realTime= String.valueOf(time/10.0f);
85

    
86
        if(level>=0 && level<RubikActivity.MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
87
          {
88
          mValues[size][level][place][0] = country;
89
          mValues[size][level][place][1] = name;
90
          mValues[size][level][place][2] = realTime;
91
          }
92
        else
93
          {
94
          android.util.Log.e("fillRow", "row invalid: level or place invalid: "+row);
95
          }
96
        }
97
      else
98
        {
99
        android.util.Log.e("fillRow", "row invalid: size invalid: "+row);
100
        }
101
      }
102
    else
103
      {
104
      android.util.Log.e("fillRow", "row invalid: "+row);
105
      }
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  private void doDownload()
111
    {
112
mScores =
113

    
114
"0 0 0 INEED7X7X7 1 rus" + "\n" +
115
"0 0 1 Tilly 1 uk" + "\n" +
116
"0 0 2 ARNOLD 1 rus" + "\n" +
117
"0 0 3 ALBERTO 1 mex" + "\n" +
118
"0 0 4 TO 1 ger" + "\n" +
119
"0 1 0 INEED7X7X7 1 rus" + "\n" +
120
"0 1 1 Tilly 1 uk" + "\n" +
121
"0 1 2 ALBERTO 1 mex" + "\n" +
122
"0 1 3 RINAT 1 rus" + "\n" +
123
"0 1 4 BOOW 1 tha" + "\n" +
124
"0 2 0 Neriel 3 bra" + "\n" +
125
"0 2 1 NPPN 3 vie" + "\n" +
126
"0 2 2 FREDRIK 4 den" + "\n" +
127
"0 2 3 OHIOCUBER 4 usa" + "\n" +
128
"0 2 4 S13 4 usa" + "\n" +
129
"0 3 0 NIKITOS 8 rus" + "\n" +
130
"0 3 1 THISNAVIS 8 usa" + "\n" +
131
"0 3 2 MDR04 9 ita" + "\n" +
132
"0 3 3 ROBERT04 9 unk" + "\n" +
133
"0 3 4 DULCE 10 mex" + "\n" +
134
"1 0 0 INEED7X7X7 1 rus" + "\n" +
135
"1 0 1 Tilly 1 uk" + "\n" +
136
"1 0 2 REBECCA 1 fra" + "\n" +
137
"1 0 3 PRAKOBKRIT 1 tha" + "\n" +
138
"1 0 4 E 1 mex" + "\n" +
139
"1 1 0 BAYLEY 3 usa" + "\n" +
140
"1 1 1 DEMONAIRE 3 uk" + "\n" +
141
"1 1 2 UMMWT 3 ina" + "\n" +
142
"1 1 3 MDR04 4 ita" + "\n" +
143
"1 1 4 FREDRIK 4 den" + "\n" +
144
"1 2 0 HAGER 5 egy" + "\n" +
145
"1 2 1 S13 7 usa" + "\n" +
146
"1 2 2 UTEK 7 ina" + "\n" +
147
"1 2 3 UMMWT 7 ina" + "\n" +
148
"1 2 4 210JITQU 7 ukr" + "\n" +
149
"1 3 0 UTEK 16 ina" + "\n" +
150
"1 3 1 LOLO766 21 fra" + "\n" +
151
"1 3 2 NIKITOS 21 rus" + "\n" +
152
"1 3 3 PYCUK1707 21 rus" + "\n" +
153
"1 3 4 KEDAR 22 ind" + "\n" +
154
"2 0 0 INEED7X7X7 1 rus" + "\n" +
155
"2 0 1 HOMER0815 1 ger" + "\n" +
156
"2 0 2 EIP 1 usa" + "\n" +
157
"2 0 3 THEBIGBOSS 1 fra" + "\n" +
158
"2 0 4 PHONG 1 vie" + "\n" +
159
"2 1 0 SATERNSPY9 3 unk" + "\n" +
160
"2 1 1 S13 4 usa" + "\n" +
161
"2 1 2 ALSOELAN 4 usa" + "\n" +
162
"2 1 3 NIKITOS 4 rus" + "\n" +
163
"2 1 4 SEPIA6MIRA 4 unk" + "\n" +
164
"2 2 0 STEVE123 9 irl" + "\n" +
165
"2 2 1 NIKITOS 9 rus" + "\n" +
166
"2 2 2 SEPIA6MIRA 9 unk" + "\n" +
167
"2 2 3 INEED7X7X7 10 rus" + "\n" +
168
"2 2 4 S13 10 usa" + "\n" +
169
"2 3 0 NIKITOS 22 rus" + "\n" +
170
"2 3 1 RUBIK123 24 lit" + "\n" +
171
"2 3 2 NONAME 24 fin" + "\n" +
172
"2 3 3 ABB0 26 ind" + "\n" +
173
"2 3 4 NOYS 27 mex" + "\n" +
174
"3 0 0 JARO 1 ger" + "\n" +
175
"3 0 1 INEED7X7X7 1 rus" + "\n" +
176
"3 0 2 MDR04 1 ita" + "\n" +
177
"3 0 3 SAMW 1 uk" + "\n" +
178
"3 0 4 MOKAI88 1 den" + "\n" +
179
"3 1 0 MDR04G 3 ita" + "\n" +
180
"3 1 1 INEED7X7X7 4 rus" + "\n" +
181
"3 1 2 S13 4 usa" + "\n" +
182
"3 1 3 KARAVAN 4 rus" + "\n" +
183
"3 1 4 AWSOME897 4 usa" + "\n" +
184
"3 2 0 INEED7X7X7 8 rus" + "\n" +
185
"3 2 1 KARAVAN 8 rus" + "\n" +
186
"3 2 2 S13 9 usa" + "\n" +
187
"3 2 3 ALSOELAN 9 usa" + "\n" +
188
"3 2 4 ROBERT04 9 unk" + "\n" +
189
"3 3 0 NIKITOS 23 rus" + "\n" +
190
"3 3 1 THISNAVIS 23 usa" + "\n" +
191
"3 3 2 INEED7X7X7 24 rus" + "\n" +
192
"3 3 3 RUBIK123 30 lit" + "\n" +
193
"3 3 4 SKY16 31 usa";
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  private boolean gottaDownload()
199
    {
200
    return ((mScores.length()==0) && !mRunning);
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  static void onPause()
206
    {
207
    mRunning = false;
208
    mScores = "";
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  void abortNetworkTransaction()
214
    {
215
    mRunning = false;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  void download(Receiver receiver)
221
    {
222
    mReceiver = receiver;
223
    mMode = DOWNLOAD;
224
    mNetworkThrd = new Thread(this);
225
    mNetworkThrd.start();
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  public void run()
231
    {
232
    try
233
      {
234
      if( gottaDownload() )
235
        {
236
        mRunning = true;
237
        doDownload();
238
        fillValues();
239
        }
240
      }
241
    catch( Exception e )
242
      {
243
      android.util.Log.e("downloader", "Exception downloading records: "+e.getMessage() );
244
      }
245

    
246
    mRunning = false;
247
    mReceiver.receive(mValues);
248
    }
249
}
(6-6/11)