aboutsummaryrefslogtreecommitdiff
path: root/src/org/happysanta/gd/API/LevelsResponse.java
blob: b62a6e6a402d22dcec7a54842e8853f954f584a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package org.happysanta.gd.API;

import org.happysanta.gd.Storage.Level;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Vector;

public class LevelsResponse {

	protected Level levels[] = null;
	protected int totalCount = 0;

	public LevelsResponse(Response response) {
		parse(response);
	}

	protected void parse(Response response) {
		JSONArray json = response.getJSON();
		try {
			JSONObject object = json.getJSONObject(1);
			Vector<Level> levels = new Vector<>();
			totalCount = object.getInt("count");
			JSONArray items = object.getJSONArray("items");

			try {
				JSONObject item;
				JSONArray tracks;
				for (int i = 0; i < items.length(); i++) {
					item = items.getJSONObject(i);
					tracks = item.getJSONArray("tracks");

					levels.addElement(new Level(
							0,
							item.getString("name"),
							item.getJSONObject("author").getString("name"),
							tracks.getInt(0),
							tracks.getInt(1),
							tracks.getInt(2),
							item.getInt("added"),
							item.getInt("size"),
							item.getInt("id")
					));
				}
			} catch (JSONException e) {
				e.printStackTrace();
			} finally {
				this.levels = levels.toArray(new Level[0]);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public int getTotalCount() {
		return totalCount;
	}

	public Level[] getLevels() {
		return levels;
	}

}