Skip to content

Commit 7b65970

Browse files
committed
Change parsing of goodgame emoticons
1 parent 40f50bf commit 7b65970

3 files changed

Lines changed: 23 additions & 23 deletions

File tree

src/main/kotlin/failchat/goodgame/GgApiClient.kt

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package failchat.goodgame
22

3+
import com.fasterxml.jackson.annotation.JsonProperty
34
import com.fasterxml.jackson.databind.JsonNode
45
import com.fasterxml.jackson.databind.ObjectMapper
6+
import com.fasterxml.jackson.module.kotlin.readValue
57
import failchat.exception.UnexpectedResponseCodeException
68
import failchat.exception.UnexpectedResponseException
79
import failchat.util.await
@@ -68,32 +70,35 @@ class GgApiClient(
6870
if (!matcher.find())
6971
throw UnexpectedResponseException("Couldn't find goodgame global emoticons array")
7072

71-
val emoticonsNode = objectMapper.readTree(matcher.group(1))
73+
val emoticonDtos = objectMapper.readValue<List<EmoticonDto>>(matcher.group(1))
7274

73-
return emoticonsNode.map { parseEmoticonNode(it) }
75+
return emoticonDtos.map { toGgEmoticon(it) }
7476
}
7577

7678
private fun parseChannelEmoticons(content: String): List<GgEmoticon> {
7779
val matcher = channelEmoticonsPattern.matcher(content)
7880
if (!matcher.find())
7981
throw UnexpectedResponseException("Couldn't find goodgame channel emoticons array")
8082

81-
val channelEmoticonsNode = objectMapper.readTree(matcher.group(1))
83+
val channelEmoticonsDtoMap =
84+
objectMapper.readValue<Map<String, List<EmoticonDto>>>(matcher.group(1))
8285

83-
return channelEmoticonsNode.map { it }.flatMap { it }.map { parseEmoticonNode(it) }
86+
return channelEmoticonsDtoMap.map { it.value }.flatten().map { toGgEmoticon(it) }
8487
}
8588

86-
private fun parseEmoticonNode(node: JsonNode): GgEmoticon {
87-
val code = node.get("name").asText()
88-
val id = node.get("id").asLong()
89-
90-
val emoticon = GgEmoticon(code = code, url = node.get("img_big").asText(), ggId = id)
91-
92-
if (node.get("animated").booleanValue()) {
93-
emoticon.animatedInstance =
94-
GgEmoticon(code = code, url = node.get("img_gif").asText(), ggId = id)
95-
}
96-
97-
return emoticon
89+
private fun toGgEmoticon(dto: EmoticonDto): GgEmoticon {
90+
return GgEmoticon(
91+
code = dto.name,
92+
url = if (dto.animated) dto.imgGif else dto.imgBig,
93+
ggId = dto.id,
94+
)
9895
}
96+
97+
private data class EmoticonDto(
98+
val id: Long,
99+
val name: String,
100+
val animated: Boolean,
101+
@JsonProperty("img_big") val imgBig: String,
102+
@JsonProperty("img_gif") val imgGif: String,
103+
)
99104
}

src/main/kotlin/failchat/goodgame/GgEmoticon.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ import failchat.chat.ImageFormat.RASTER
55
import failchat.emoticon.Emoticon
66

77
class GgEmoticon(code: String, override val url: String, val ggId: Long) :
8-
Emoticon(Origin.GOODGAME, code, RASTER) {
9-
var animatedInstance: GgEmoticon? = null
10-
}
8+
Emoticon(Origin.GOODGAME, code, RASTER)

src/main/kotlin/failchat/goodgame/GgEmoticonHandler.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ class GgEmoticonHandler(private val emoticonFinder: EmoticonFinder) : MessageHan
1414
emoticonFinder.findByCode(GOODGAME, code) as? GgEmoticon
1515
?: return@process ReplaceDecision.Skip
1616

17-
// prefer animated emoticons even for non-premium users
18-
val emoticonToAdd = emoticon.animatedInstance ?: emoticon
19-
20-
val label = message.addElement(emoticonToAdd)
17+
val label = message.addElement(emoticon)
2118
return@process ReplaceDecision.Replace(label)
2219
}
2320
}

0 commit comments

Comments
 (0)