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