Project

General

Profile

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

magiccube / src / main / java / org / distorted / network / RubikUpdates.java @ c99db493

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