Skip to content

Commit 6bda43f

Browse files
committed
3.1.2
1 parent b143557 commit 6bda43f

File tree

10 files changed

+286
-270
lines changed

10 files changed

+286
-270
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Changelog
22

3+
## 3.1.2 (2020-01-26)
4+
5+
### Bug fixes
6+
7+
- Fix `isLive2D` parsing
8+
9+
### Misc
10+
11+
- Separate code into 2 packages: `api` and `internal`: `internal` contents are not meant to be used by users
12+
13+
## 3.1.1 (2020-01-25)
14+
15+
### Misc
16+
17+
- `reloadDatabase` explicitly throws an exception
18+
319
## 3.1.0 (2020-01-25)
420

521
### New features

src/main/kotlin/com/github/azurapi/azurapikotlin/api/Atago.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import com.github.azurapi.azurapikotlin.internal.entities.Version
66
import com.github.azurapi.azurapikotlin.internal.exceptions.ApiException
77
import com.github.azurapi.azurapikotlin.internal.exceptions.DatabaseException
88
import com.github.azurapi.azurapikotlin.internal.exceptions.ShipNotFoundException
9-
import com.github.azurapi.azurapikotlin.json.Takao
9+
import com.github.azurapi.azurapikotlin.internal.json.Takao
10+
import com.github.azurapi.azurapikotlin.internal.utils.info.AtagoInfo
1011
import java.util.stream.Collectors
1112

1213
/**

src/main/kotlin/com/github/azurapi/azurapikotlin/json/Takao.kt renamed to src/main/kotlin/com/github/azurapi/azurapikotlin/internal/json/Takao.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package com.github.azurapi.azurapikotlin.json
1+
package com.github.azurapi.azurapikotlin.internal.json
22

33
import com.github.azurapi.azurapikotlin.internal.entities.Lang
44
import com.github.azurapi.azurapikotlin.internal.entities.Ship
55
import com.github.azurapi.azurapikotlin.internal.exceptions.DatabaseException
6-
import com.github.azurapi.azurapikotlin.utils.ShipParser
6+
import com.github.azurapi.azurapikotlin.internal.utils.info.TakaoInfo
7+
import com.github.azurapi.azurapikotlin.internal.utils.parser.jsonToShip
78
import com.github.kittinunf.fuel.httpGet
89
import com.github.kittinunf.fuel.json.responseJson
910
import info.debatty.java.stringsimilarity.Cosine
@@ -60,7 +61,9 @@ class Takao {
6061
try {
6162
jsonDatabase = loadJSON(TakaoInfo.JSON_SOURCE)
6263
for (shipId in jsonDatabase.keySet()) {
63-
val ship = ShipParser.jsonToShip(jsonDatabase.getJSONObject(shipId), shipId)
64+
val ship = jsonToShip(
65+
jsonDatabase.getJSONObject(shipId), shipId
66+
)
6467
shipsById[ship.id] = ship
6568
shipsByEnName[ship.names.en] = ship
6669
shipsByCnName[if (ship.names.cn.isNotEmpty()) ship.names.cn else ship.names.en] = ship

src/main/kotlin/com/github/azurapi/azurapikotlin/internal/responses/ErrorResponse.kt

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/main/kotlin/com/github/azurapi/azurapikotlin/api/AtagoInfo.kt renamed to src/main/kotlin/com/github/azurapi/azurapikotlin/internal/utils/info/AtagoInfo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.azurapi.azurapikotlin.api
1+
package com.github.azurapi.azurapikotlin.internal.utils.info
22

33
/**
44
* API info

src/main/kotlin/com/github/azurapi/azurapikotlin/json/TakaoInfo.kt renamed to src/main/kotlin/com/github/azurapi/azurapikotlin/internal/utils/info/TakaoInfo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.azurapi.azurapikotlin.json
1+
package com.github.azurapi.azurapikotlin.internal.utils.info
22

33
/**
44
* JSON deserializer info
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
package com.github.azurapi.azurapikotlin.internal.utils.parser
2+
3+
import com.github.azurapi.azurapikotlin.internal.entities.Miscellaneous
4+
import com.github.azurapi.azurapikotlin.internal.entities.Name
5+
import com.github.azurapi.azurapikotlin.internal.entities.Ship
6+
import com.github.azurapi.azurapikotlin.internal.entities.ShipConstruction
7+
import com.github.azurapi.azurapikotlin.internal.entities.Skin
8+
import com.github.azurapi.azurapikotlin.internal.entities.SkinInfo
9+
import com.github.azurapi.azurapikotlin.internal.entities.Stars
10+
import com.github.azurapi.azurapikotlin.internal.entities.Stats
11+
import com.github.azurapi.azurapikotlin.internal.entities.StatsDetails
12+
import com.github.azurapi.azurapikotlin.internal.entities.Url
13+
import com.github.azurapi.azurapikotlin.internal.exceptions.DatabaseException
14+
import org.json.JSONArray
15+
import org.json.JSONException
16+
import org.json.JSONObject
17+
18+
private fun jsonToNames(json: JSONObject): Name {
19+
try {
20+
return Name(
21+
jp = json.optString("jp"),
22+
en = json.getString("en"),
23+
cn = json.optString("cn"),
24+
kr = json.optString("kr")
25+
)
26+
} catch (e: JSONException) {
27+
throw e
28+
}
29+
}
30+
31+
private fun jsonToSkinInfo(json: JSONObject?): SkinInfo? {
32+
if (json == null) {
33+
return null
34+
}
35+
try {
36+
return SkinInfo(
37+
enClient = json.optString("enClient"),
38+
cnClient = json.optString("cnClient"),
39+
jpClient = json.optString("jpClient"),
40+
obtainedFrom = json.optString("obtainedFrom"),
41+
cost = try {
42+
json.optString("cost").toInt()
43+
} catch (e: NumberFormatException) {
44+
0
45+
},
46+
isLive2D = json.getBoolean("live2dModel")
47+
)
48+
} catch (e: JSONException) {
49+
throw e
50+
}
51+
}
52+
53+
private fun jsonToSkins(jsonArray: JSONArray): List<Skin> {
54+
try {
55+
return jsonArray.map { skin ->
56+
skin as JSONObject
57+
Skin(
58+
name = skin.getString("name"),
59+
image = skin.getString("image"),
60+
background = skin.optString("background"),
61+
chibi = skin.optString("chibi"),
62+
info = jsonToSkinInfo(
63+
skin.optJSONObject(
64+
"info"
65+
)
66+
)
67+
)
68+
}
69+
} catch (e: JSONException) {
70+
throw e
71+
}
72+
}
73+
74+
private fun jsonToStatsDetails(json: JSONObject): StatsDetails {
75+
try {
76+
return StatsDetails(
77+
speed = json.getInt("speed"),
78+
accuracy = json.getInt("accuracy"),
79+
antiAir = json.getInt("antiair"),
80+
antiSub = json.getInt("antisubmarineWarfare"),
81+
armor = json.getString("armor"),
82+
aviation = json.getInt("aviation"),
83+
evasion = json.getInt("evasion"),
84+
firepower = json.getInt("firepower"),
85+
health = json.getInt("health"),
86+
luck = json.getInt("luck"),
87+
// FIXME: it should be defined
88+
oil = json.optInt("oilConsumption"),
89+
reload = json.getInt("reload"),
90+
torpedo = json.getInt("torpedo")
91+
)
92+
} catch (e: JSONException) {
93+
throw e
94+
}
95+
}
96+
97+
private fun jsonToStats(json: JSONObject?): Stats? {
98+
if (json == null) {
99+
return null
100+
}
101+
try {
102+
return Stats(
103+
level120 = jsonToStatsDetails(
104+
json.getJSONObject(
105+
"level120"
106+
)
107+
),
108+
level100 = jsonToStatsDetails(
109+
json.getJSONObject(
110+
"level100"
111+
)
112+
),
113+
base = jsonToStatsDetails(
114+
json.getJSONObject(
115+
"baseStats"
116+
)
117+
)
118+
)
119+
} catch (e: JSONException) {
120+
throw e
121+
}
122+
}
123+
124+
private fun jsonToStars(json: JSONObject?): Stars? {
125+
if (json == null) {
126+
return null
127+
}
128+
try {
129+
return Stars(
130+
stars = json.getString("stars"),
131+
value = json.getInt("value")
132+
)
133+
} catch (e: JSONException) {
134+
throw e
135+
}
136+
}
137+
138+
private fun jsonToUrl(json: JSONObject?): Url? {
139+
if (json == null) {
140+
return null
141+
}
142+
try {
143+
return Url(
144+
name = json.getString("name"),
145+
url = json.getString("url")
146+
)
147+
} catch (e: JSONException) {
148+
throw e
149+
}
150+
}
151+
152+
private fun jsonToConstruction(json: JSONObject?): ShipConstruction? {
153+
if (json == null) {
154+
return null
155+
}
156+
try {
157+
val availability = json.getJSONObject("availableIn")
158+
// FIXME: not always a boolean
159+
return ShipConstruction(
160+
constructionTime = json.getString("constructionTime"),
161+
light = availability.optBoolean("light", true),
162+
heavy = availability.optBoolean("heavy", true),
163+
aviation = availability.optBoolean("aviation", true),
164+
limited = availability.optBoolean("limited", true),
165+
exchange = availability.optBoolean("exchange", true)
166+
)
167+
} catch (e: JSONException) {
168+
throw e
169+
}
170+
}
171+
172+
private fun jsonToMiscellaneous(json: JSONObject?): Miscellaneous? {
173+
if (json == null) {
174+
return null
175+
}
176+
try {
177+
return Miscellaneous(
178+
artist = json.optString("artist"),
179+
web = jsonToUrl(
180+
json.optJSONObject(
181+
"web"
182+
)
183+
),
184+
pixiv = jsonToUrl(
185+
json.optJSONObject(
186+
"pixiv"
187+
)
188+
),
189+
twitter = jsonToUrl(
190+
json.optJSONObject(
191+
"twitter"
192+
)
193+
),
194+
voice = jsonToUrl(
195+
json.optJSONObject(
196+
"voice"
197+
)
198+
)
199+
)
200+
} catch (e: JSONException) {
201+
throw e
202+
}
203+
}
204+
205+
fun jsonToShip(json: JSONObject, shipId: String): Ship {
206+
try {
207+
return Ship(
208+
wikiUrl = json.getString("wikiUrl"),
209+
id = json.getString("id"),
210+
hullType = json.getString("hullType"),
211+
nationality = json.getString("nationality"),
212+
rarity = json.getString("rarity"),
213+
thumbnail = json.getString("thumbnail"),
214+
shipClass = json.optString("class"),
215+
names = jsonToNames(
216+
json.getJSONObject(
217+
"names"
218+
)
219+
),
220+
skins = jsonToSkins(
221+
json.getJSONArray(
222+
"skins"
223+
)
224+
),
225+
stats = jsonToStats(
226+
json.optJSONObject(
227+
"stats"
228+
)
229+
),
230+
stars = jsonToStars(
231+
json.optJSONObject(
232+
"stars"
233+
)
234+
),
235+
construction = jsonToConstruction(
236+
json.optJSONObject(
237+
"construction"
238+
)
239+
),
240+
misc = jsonToMiscellaneous(
241+
json.optJSONObject(
242+
"misc"
243+
)
244+
)
245+
)
246+
} catch (e: JSONException) {
247+
throw DatabaseException("Could not parse ship#$shipId: (${e.message})")
248+
}
249+
}

0 commit comments

Comments
 (0)