Project

General

Profile

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

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

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 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
      {
42
      mObjectShortName    = shortName;
43
      mObjectLongName     = longName;
44
      mDescription        = description;
45
      mObjectMinorVersion = objectMinor;
46
      mExtrasMinorVersion = extrasMinor;
47
      mUpdateObject       = updateO;
48
      mUpdateExtras       = updateE;
49
      }
50
    }
51

    
52
  private String mUrl;
53
  private final ArrayList<UpdateInfo> mNew, mUpd;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  public RubikUpdates()
58
    {
59
    mNew = new ArrayList<>();
60
    mUpd = new ArrayList<>();
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  private String debug(ArrayList<UpdateInfo> list)
66
    {
67
    String ret = "";
68

    
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
    return ret;
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  private void parseLine(String[] elements)
81
    {
82
    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
    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
    if( oMinor>=0 && eMinor>=0 )
95
      {
96
      int objOrdinal = RubikObjectList.getOrdinal(shortName.toUpperCase());
97

    
98
      if( objOrdinal>=0 )
99
        {
100
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
101
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
102
        boolean updateO = localObjectMinor<oMinor;
103
        boolean updateE = localExtrasMinor<eMinor;
104

    
105
        if( updateO || updateE )
106
          {
107
          UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,updateO,updateE);
108
          mUpd.add(info);
109
          }
110
        }
111
      else
112
        {
113
        UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,true,true);
114
        mNew.add(info);
115
        }
116
      }
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  void parse(String updates)
122
    {
123
    android.util.Log.e("D", updates);
124

    
125
    mNew.clear();
126
    mUpd.clear();
127

    
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
        String[] elements = lines[line].split(",");
137
        if( elements.length>=3 ) parseLine(elements);
138
        }
139
      }
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  public int getNumberOfUpdates()
168
    {
169
    return mNew.size()+mUpd.size();
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  public void showDebug()
175
    {
176
    android.util.Log.e("D", "url: "+mUrl);
177
    android.util.Log.e("D", "new objects: "+debug(mNew));
178
    android.util.Log.e("D", "upd objects: "+debug(mUpd));
179
    }
180
}
(3-3/3)