Revision d8aa4ba8
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/magic/RubikScoresDownloader.java | ||
---|---|---|
25 | 25 |
{ |
26 | 26 |
interface Receiver |
27 | 27 |
{ |
28 |
void receive(String scores); |
|
28 |
void receive(String[][][][] scores);
|
|
29 | 29 |
} |
30 | 30 |
|
31 |
static final int MAX_PLACES = 10; |
|
32 |
|
|
31 | 33 |
private static final int DOWNLOAD = 0; |
32 | 34 |
private static final int SUBMIT = 1; |
33 | 35 |
private static final int IDLE = 2; |
... | ... | |
40 | 42 |
private static Receiver mReceiver; |
41 | 43 |
|
42 | 44 |
private static String mScores = ""; |
45 |
private static String[][][][] mValues = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES][3]; |
|
43 | 46 |
|
44 | 47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
45 | 48 |
|
46 |
static void onPause()
|
|
49 |
private void fillValues()
|
|
47 | 50 |
{ |
48 |
mRunning = false; |
|
49 |
mScores = ""; |
|
50 |
} |
|
51 |
int begin=-1 ,end, len = mScores.length(); |
|
51 | 52 |
|
52 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
53 |
|
|
54 |
private boolean gottaDownload() |
|
55 |
{ |
|
56 |
return ((mScores.length()==0) && !mRunning); |
|
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 |
} |
|
57 | 60 |
} |
58 | 61 |
|
59 | 62 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
60 | 63 |
|
61 |
void abortNetworkTransaction()
|
|
64 |
private void fillRow(String row)
|
|
62 | 65 |
{ |
63 |
mRunning = false; |
|
64 |
} |
|
65 |
|
|
66 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
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) ); |
|
67 | 76 |
|
68 |
void download(Receiver receiver) |
|
69 |
{ |
|
70 |
mReceiver = receiver; |
|
71 |
mMode = DOWNLOAD; |
|
72 |
mNetworkThrd = new Thread(this); |
|
73 |
mNetworkThrd.start(); |
|
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 |
} |
|
74 | 106 |
} |
75 | 107 |
|
76 | 108 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
77 | 109 |
|
78 |
public void run()
|
|
110 |
private void doDownload()
|
|
79 | 111 |
{ |
80 |
try |
|
81 |
{ |
|
82 |
if( gottaDownload() ) |
|
83 |
{ |
|
84 |
mRunning = true; |
|
85 |
|
|
86 |
Thread.sleep(2000); |
|
87 |
|
|
88 | 112 |
mScores = |
89 | 113 |
|
90 | 114 |
"0 0 0 INEED7X7X7 1 rus" + "\n" + |
... | ... | |
167 | 191 |
"3 3 2 INEED7X7X7 24 rus" + "\n" + |
168 | 192 |
"3 3 3 RUBIK123 30 lit" + "\n" + |
169 | 193 |
"3 3 4 SKY16 31 usa"; |
170 |
} |
|
171 | 194 |
} |
172 |
catch( Exception e ) |
|
195 |
|
|
196 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
197 |
|
|
198 |
private boolean gottaDownload() |
|
173 | 199 |
{ |
174 |
android.util.Log.e("downloader", "Exception downloading records: "+e.getMessage() );
|
|
200 |
return ((mScores.length()==0) && !mRunning);
|
|
175 | 201 |
} |
176 | 202 |
|
177 |
mRunning = false; |
|
178 |
mReceiver.receive(mScores); |
|
179 |
} |
|
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 |
} |
|
180 | 249 |
} |
Also available in: Unified diff
Downloading High Scores: optimizations