Project

General

Profile

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

magiccube / src / main / java / org / distorted / network / RubikUpdates.java @ 903c7bbc

1 fcf7320f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c99db493 Leszek Koltunski
    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 boolean mUpdateObject;
38
    public final boolean mUpdateExtras;
39
40
    public UpdateInfo(String shortName, String longName, String description, int objectMinor, int extrasMinor, boolean updateO, boolean updateE)
41 fcf7320f Leszek Koltunski
      {
42 c99db493 Leszek Koltunski
      mObjectShortName    = shortName;
43
      mObjectLongName     = longName;
44
      mDescription        = description;
45
      mObjectMinorVersion = objectMinor;
46
      mExtrasMinorVersion = extrasMinor;
47
      mUpdateObject       = updateO;
48
      mUpdateExtras       = updateE;
49 fcf7320f Leszek Koltunski
      }
50
    }
51
52
  private String mUrl;
53 c99db493 Leszek Koltunski
  private final ArrayList<UpdateInfo> mNew, mUpd;
54 fcf7320f Leszek Koltunski
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57
  public RubikUpdates()
58
    {
59 c99db493 Leszek Koltunski
    mNew = new ArrayList<>();
60
    mUpd = new ArrayList<>();
61 fcf7320f Leszek Koltunski
    }
62
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
  private String debug(ArrayList<UpdateInfo> list)
66
    {
67
    String ret = "";
68 c99db493 Leszek Koltunski
69
    for( UpdateInfo info : list)
70
      {
71
      ret += (info.mObjectShortName+" "+info.mObjectLongName+" "+info.mDescription+" ");
72
      ret += (info.mObjectMinorVersion+" "+info.mExtrasMinorVersion+" "+info.mUpdateObject+" "+info.mUpdateExtras+" , ");
73
      }
74
75 fcf7320f Leszek Koltunski
    return ret;
76
    }
77
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80
  private void parseLine(String[] elements)
81
    {
82 c99db493 Leszek Koltunski
    String shortName   = elements[0];
83
    String objMinor    = elements[1];
84
    String extMinor    = elements[2];
85
    String longName    = elements[3];
86
    String description = elements[4];
87 fcf7320f Leszek Koltunski
    int oMinor, eMinor;
88
89
    try { oMinor = Integer.parseInt(objMinor); }
90
    catch (NumberFormatException ex) { oMinor = -1; }
91
    try { eMinor = Integer.parseInt(extMinor); }
92
    catch (NumberFormatException ex) { eMinor = -1; }
93
94 c99db493 Leszek Koltunski
    if( oMinor>=0 && eMinor>=0 )
95 fcf7320f Leszek Koltunski
      {
96 c99db493 Leszek Koltunski
      int objOrdinal = RubikObjectList.getOrdinal(shortName.toUpperCase());
97 fcf7320f Leszek Koltunski
98
      if( objOrdinal>=0 )
99
        {
100 c99db493 Leszek Koltunski
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
101 fcf7320f Leszek Koltunski
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
102 c99db493 Leszek Koltunski
        boolean updateO = localObjectMinor<oMinor;
103
        boolean updateE = localExtrasMinor<eMinor;
104
105
        if( updateO || updateE )
106 fcf7320f Leszek Koltunski
          {
107 c99db493 Leszek Koltunski
          UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,updateO,updateE);
108
          mUpd.add(info);
109 fcf7320f Leszek Koltunski
          }
110
        }
111
      else
112
        {
113 c99db493 Leszek Koltunski
        UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,true,true);
114
        mNew.add(info);
115 fcf7320f Leszek Koltunski
        }
116
      }
117
    }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121
  void parse(String updates)
122
    {
123
    android.util.Log.e("D", updates);
124
125 c99db493 Leszek Koltunski
    mNew.clear();
126
    mUpd.clear();
127 fcf7320f Leszek Koltunski
128
    String[] lines = updates.split("\n");
129
    int numLines = lines.length;
130
131
    if( numLines>=1 )
132
      {
133
      mUrl = lines[0];
134
      for(int line=1; line<numLines; line++)
135
        {
136 c99db493 Leszek Koltunski
        String[] elements = lines[line].split(",");
137 fcf7320f Leszek Koltunski
        if( elements.length>=3 ) parseLine(elements);
138
        }
139
      }
140 63dd19c4 Leszek Koltunski
    }
141
142 903c7bbc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144
  public void updateDone(String shortName)
145
    {
146
    for( UpdateInfo info : mNew)
147
      {
148
      if( info.mObjectShortName.equals(shortName) )
149
        {
150
        mNew.remove(info);
151
        return;
152
        }
153
      }
154
155
    for( UpdateInfo info : mUpd)
156
      {
157
      if( info.mObjectShortName.equals(shortName) )
158
        {
159
        mUpd.remove(info);
160
        return;
161
        }
162
      }
163
    }
164
165 10373dc7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
  public int getNumberOfUpdates()
168
    {
169 903c7bbc Leszek Koltunski
    return mNew.size()+mUpd.size();
170 10373dc7 Leszek Koltunski
    }
171
172 63dd19c4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173 fcf7320f Leszek Koltunski
174 63dd19c4 Leszek Koltunski
  public void showDebug()
175
    {
176 fcf7320f Leszek Koltunski
    android.util.Log.e("D", "url: "+mUrl);
177 c99db493 Leszek Koltunski
    android.util.Log.e("D", "new objects: "+debug(mNew));
178
    android.util.Log.e("D", "upd objects: "+debug(mUpd));
179 fcf7320f Leszek Koltunski
    }
180
}