Skip to content

Commit 13490fb

Browse files
committed
Add attacktype and usespells. Optimise getting creatures one by one.
1 parent c5654af commit 13490fb

5 files changed

Lines changed: 22 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.wikia.tibia.enums
2+
3+
enum class AttackType {
4+
Melee, Distance, None
5+
}

src/main/kotlin/com/wikia/tibia/jackson/Parser.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,13 @@ object Parser {
9494
""
9595
}
9696
}
97+
98+
fun <T> mapToType(type: Class<T>, json: Any, mapper: ObjectMapper = defaultObjectMapper): T? {
99+
return try {
100+
mapper.convertValue(json, type)
101+
} catch (e: Exception) {
102+
logger.error("Unable to construct object of type '$type' from the following json: '$json' because of error: $e")
103+
null
104+
}
105+
}
97106
}

src/main/kotlin/com/wikia/tibia/objects/Creature.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.wikia.tibia.objects
22

33
import com.fasterxml.jackson.annotation.JsonGetter
44
import com.wikia.tibia.enums.Article
5+
import com.wikia.tibia.enums.AttackType
56
import com.wikia.tibia.enums.BestiaryClass
67
import com.wikia.tibia.enums.BestiaryLevel
78
import com.wikia.tibia.enums.BestiaryOccurrence
@@ -33,6 +34,8 @@ data class Creature(
3334
val bestiaryclass: BestiaryClass? = null,
3435
val bestiarylevel: BestiaryLevel? = null,
3536
val occurrence: BestiaryOccurrence? = null,
37+
val attacktype: AttackType? = null,
38+
val usespells: YesNo? = null,
3639
val spawntype: List<Spawntype>? = emptyList(),
3740
val isboss: YesNo? = null,
3841
val isarenaboss: YesNo? = null,

src/main/kotlin/com/wikia/tibia/v2/adapters/creature/CreatureRepositoryImpl.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.wikia.tibia.v2.adapters.creature
22

33
import com.github.benmanes.caffeine.cache.AsyncLoadingCache
44
import com.github.benmanes.caffeine.cache.Caffeine
5+
import com.wikia.tibia.jackson.Parser
56
import com.wikia.tibia.objects.Creature
67
import com.wikia.tibia.v2.adapters.tibiawiki.TibiaWikiApiClientFactory
78
import com.wikia.tibia.v2.domain.creature.CreatureRepository
@@ -70,7 +71,9 @@ class CreatureRepositoryImpl : CreatureRepository {
7071
logger.info("Getting all creatures")
7172
val response = client.getCreatures()
7273
if (response.isSuccessful) {
73-
response.body() ?: emptyList()
74+
response.body()
75+
?.mapNotNull { Parser.mapToType(type = Creature::class.java, json = it) }
76+
?: emptyList()
7477
} else {
7578
logger.error("Could not get list of creatures for key $key because: ${response.errorBody() ?: response.message()}")
7679
emptyList()

src/main/kotlin/com/wikia/tibia/v2/adapters/tibiawiki/TibiaWikiApiClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import retrofit2.http.Query
1414
interface TibiaWikiApiClient {
1515

1616
@GET("/api/creatures")
17-
suspend fun getCreatures(@Query("expand") expand: Boolean? = true): Response<List<Creature>>
17+
suspend fun getCreatures(@Query("expand") expand: Boolean? = true): Response<List<Any>>
1818

1919
@GET("/api/creatures")
2020
suspend fun getCreatureNames(@Query("expand") expand: Boolean? = false): Response<List<String>>

0 commit comments

Comments
 (0)