1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2022 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.network;
|
21
|
|
22
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23
|
|
24
|
import org.distorted.objects.RubikObjectList;
|
25
|
|
26
|
import java.util.ArrayList;
|
27
|
|
28
|
public class RubikUpdates
|
29
|
{
|
30
|
public static class UpdateInfo
|
31
|
{
|
32
|
public final String mObjectShortName;
|
33
|
public final String mObjectLongName;
|
34
|
public final String mDescription;
|
35
|
public final int mObjectMinorVersion;
|
36
|
public final int mExtrasMinorVersion;
|
37
|
public final int mPercent;
|
38
|
public final boolean mUpdateObject;
|
39
|
public final boolean mUpdateExtras;
|
40
|
|
41
|
public UpdateInfo(String shortName, String longName, String description, int objectMinor,
|
42
|
int extrasMinor, int percent, boolean updateO, boolean updateE)
|
43
|
{
|
44
|
mObjectShortName = shortName;
|
45
|
mObjectLongName = longName;
|
46
|
mDescription = description;
|
47
|
mObjectMinorVersion = objectMinor;
|
48
|
mExtrasMinorVersion = extrasMinor;
|
49
|
mPercent = percent;
|
50
|
mUpdateObject = updateO;
|
51
|
mUpdateExtras = updateE;
|
52
|
}
|
53
|
}
|
54
|
|
55
|
private String mUrl;
|
56
|
private final ArrayList<UpdateInfo> mCompleted, mStarted;
|
57
|
|
58
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59
|
|
60
|
public RubikUpdates()
|
61
|
{
|
62
|
mCompleted = new ArrayList<>();
|
63
|
mStarted = new ArrayList<>();
|
64
|
}
|
65
|
|
66
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67
|
|
68
|
private String debug(ArrayList<UpdateInfo> list)
|
69
|
{
|
70
|
String ret = "";
|
71
|
|
72
|
for( UpdateInfo info : list)
|
73
|
{
|
74
|
ret += (info.mObjectShortName+" "+info.mObjectLongName+" "+info.mDescription+" ");
|
75
|
ret += (info.mObjectMinorVersion+" "+info.mExtrasMinorVersion+" "+info.mUpdateObject+" "+info.mUpdateExtras+" , ");
|
76
|
}
|
77
|
|
78
|
return ret;
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
private void parseLine(String[] elements)
|
84
|
{
|
85
|
String shortName = elements[0].trim();
|
86
|
String objMinor = elements[1].trim();
|
87
|
String extMinor = elements[2].trim();
|
88
|
String percent = elements[3].trim();
|
89
|
String longName = elements[4];
|
90
|
String description = elements[5];
|
91
|
int oMinor, eMinor, oPercent;
|
92
|
|
93
|
try { oMinor = Integer.parseInt(objMinor); }
|
94
|
catch (NumberFormatException ex) { oMinor = -1; }
|
95
|
try { eMinor = Integer.parseInt(extMinor); }
|
96
|
catch (NumberFormatException ex) { eMinor = -1; }
|
97
|
try { oPercent = Integer.parseInt(percent); }
|
98
|
catch (NumberFormatException ex) { oPercent = -1; }
|
99
|
|
100
|
if( oMinor>=0 && eMinor>=0 && oPercent>=0 )
|
101
|
{
|
102
|
int objOrdinal = RubikObjectList.getOrdinal(shortName.toUpperCase());
|
103
|
boolean updateO=true, updateE=true;
|
104
|
|
105
|
if( objOrdinal>=0 )
|
106
|
{
|
107
|
int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
|
108
|
int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
|
109
|
updateO = localObjectMinor<oMinor;
|
110
|
updateE = localExtrasMinor<eMinor;
|
111
|
}
|
112
|
if( updateO || updateE )
|
113
|
{
|
114
|
UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,oPercent,updateO,updateE);
|
115
|
if(oPercent>=100) mCompleted.add(info);
|
116
|
else mStarted.add(info);
|
117
|
}
|
118
|
}
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
|
123
|
void parse(String updates)
|
124
|
{
|
125
|
android.util.Log.e("D", updates);
|
126
|
|
127
|
mCompleted.clear();
|
128
|
mStarted.clear();
|
129
|
|
130
|
String[] lines = updates.split("\n");
|
131
|
int numLines = lines.length;
|
132
|
|
133
|
if( numLines>=1 )
|
134
|
{
|
135
|
mUrl = lines[0];
|
136
|
for(int line=1; line<numLines; line++)
|
137
|
{
|
138
|
String[] elements = lines[line].split(",");
|
139
|
if( elements.length>=6 ) parseLine(elements);
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
|
144
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145
|
|
146
|
public void updateDone(String shortName)
|
147
|
{
|
148
|
for( UpdateInfo info : mCompleted)
|
149
|
{
|
150
|
if( info.mObjectShortName.equals(shortName) )
|
151
|
{
|
152
|
mCompleted.remove(info);
|
153
|
return;
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public UpdateInfo getCompletedUpdate(int ordinal)
|
161
|
{
|
162
|
return mCompleted.get(ordinal);
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
public UpdateInfo getStartedUpdate(int ordinal)
|
168
|
{
|
169
|
return mStarted.get(ordinal);
|
170
|
}
|
171
|
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
public int getCompletedNumber()
|
175
|
{
|
176
|
return mCompleted.size();
|
177
|
}
|
178
|
|
179
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180
|
|
181
|
public int getStartedNumber()
|
182
|
{
|
183
|
return mStarted.size();
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
public void showDebug()
|
189
|
{
|
190
|
android.util.Log.e("D", "url: "+mUrl);
|
191
|
android.util.Log.e("D", "ready objects: "+debug(mCompleted));
|
192
|
android.util.Log.e("D", "next objects: "+debug(mStarted));
|
193
|
}
|
194
|
}
|