|
1 | 1 | package failchat.goodgame |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty |
3 | 4 | import com.fasterxml.jackson.databind.JsonNode |
4 | 5 | import com.fasterxml.jackson.databind.ObjectMapper |
| 6 | +import com.fasterxml.jackson.module.kotlin.readValue |
5 | 7 | import failchat.exception.UnexpectedResponseCodeException |
6 | 8 | import failchat.exception.UnexpectedResponseException |
7 | 9 | import failchat.util.await |
@@ -68,32 +70,35 @@ class GgApiClient( |
68 | 70 | if (!matcher.find()) |
69 | 71 | throw UnexpectedResponseException("Couldn't find goodgame global emoticons array") |
70 | 72 |
|
71 | | - val emoticonsNode = objectMapper.readTree(matcher.group(1)) |
| 73 | + val emoticonDtos = objectMapper.readValue<List<EmoticonDto>>(matcher.group(1)) |
72 | 74 |
|
73 | | - return emoticonsNode.map { parseEmoticonNode(it) } |
| 75 | + return emoticonDtos.map { toGgEmoticon(it) } |
74 | 76 | } |
75 | 77 |
|
76 | 78 | private fun parseChannelEmoticons(content: String): List<GgEmoticon> { |
77 | 79 | val matcher = channelEmoticonsPattern.matcher(content) |
78 | 80 | if (!matcher.find()) |
79 | 81 | throw UnexpectedResponseException("Couldn't find goodgame channel emoticons array") |
80 | 82 |
|
81 | | - val channelEmoticonsNode = objectMapper.readTree(matcher.group(1)) |
| 83 | + val channelEmoticonsDtoMap = |
| 84 | + objectMapper.readValue<Map<String, List<EmoticonDto>>>(matcher.group(1)) |
82 | 85 |
|
83 | | - return channelEmoticonsNode.map { it }.flatMap { it }.map { parseEmoticonNode(it) } |
| 86 | + return channelEmoticonsDtoMap.map { it.value }.flatten().map { toGgEmoticon(it) } |
84 | 87 | } |
85 | 88 |
|
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 | + ) |
98 | 95 | } |
| 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 | + ) |
99 | 104 | } |
0 commit comments