Skip to content

Commit

Permalink
Migrate goodgame to chat api v2
Browse files Browse the repository at this point in the history
  • Loading branch information
onoderis committed Dec 23, 2018
1 parent c9d2d61 commit f4f9b86
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 33 deletions.
44 changes: 12 additions & 32 deletions src/main/kotlin/failchat/goodgame/GgChatClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import failchat.chat.OriginStatus.DISCONNECTED
import failchat.chat.StatusMessage
import failchat.chat.handlers.CommaHighlightHandler
import failchat.chat.handlers.ElementLabelEscaper
import failchat.exception.ChannelOfflineException
import failchat.util.completedFuture
import failchat.util.exceptionalFuture
import failchat.util.objectMapper
import failchat.util.synchronized
import failchat.util.whileNotNull
import failchat.viewers.ViewersCountLoader
import failchat.ws.client.WsClient
import mu.KLogging
import org.java_websocket.handshake.ServerHandshake
import java.net.URI
import java.util.Queue
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicReference

class GgChatClient(
Expand Down Expand Up @@ -58,7 +58,9 @@ class GgChatClient(
)

private val history = EvictingQueue.create<GgMessage>(50).synchronized()
private val viewersCountFutures: Queue<CompletableFuture<Int>> = ConcurrentLinkedQueue()

@Volatile
private var viewersCount: Int? = null


override fun start() {
Expand All @@ -75,29 +77,10 @@ class GgChatClient(
}

override fun loadViewersCount(): CompletableFuture<Int> {
/*
* Undocumented api
* request: {"type":"get_all_viewers","data":{"channel":"21506"}}
* response: {"type":"viewers","data":{"channel_id":"21506","count":173}}
* Ответ приходит 1 раз. Если канал оффлайн - значение 0.
* */
val getAllViewersMessage = objectMapper.createObjectNode().apply {
put("type", "get_all_viewers")
putObject("data").apply {
put("channel", channelId.toString())
}
}

val countFuture = CompletableFuture<Int>()
try {
wsClient.send(getAllViewersMessage.toString())
} catch (e: Exception) {
countFuture.completeExceptionally(e)
}
if (!countFuture.isCompletedExceptionally) {
viewersCountFutures.offer(countFuture)
viewersCount?.let {
return completedFuture(it)
}
return countFuture
return exceptionalFuture(ChannelOfflineException(Origin.GOODGAME, channelName))
}

private inner class GgWsClient(uri: URI) : WsClient(uri) {
Expand Down Expand Up @@ -130,7 +113,7 @@ class GgChatClient(
when (type) {
"message" -> handleUserMessage(data)
"remove_message" -> handleModMessage(data)
"viewers" -> handleViewersMessage(data)
"channel_counters" -> handleChannelCountersMessage(data)
}
}

Expand Down Expand Up @@ -173,11 +156,8 @@ class GgChatClient(
foundMessage?.let { onChatMessageDeleted?.invoke(it) }
}

private fun handleViewersMessage(dataNode: JsonNode) {
val count = dataNode.get("count").asInt()
whileNotNull({ viewersCountFutures.poll() }) {
it.complete(count)
}
private fun handleChannelCountersMessage(dataNode: JsonNode) {
viewersCount = dataNode.get("clients_in_channel").asInt()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ twitch.emoticon-url-suffix = /1.0
twitch.badge-api-url = https://badges.twitch.tv/v1/badges/
twitch.twitchemotes-api-url = https://twitchemotes.com
bttv.api-url = https://api.betterttv.net/
goodgame.ws-url = ws://chat.goodgame.ru:8081/chat/websocket
goodgame.ws-url = wss://chat-1.goodgame.ru/chat2/
goodgame.api-url = http://goodgame.ru/api/
goodgame.emoticon-js-url = https://goodgame.ru/js/minified/global.js
goodgame.badge-url = https://static.goodgame.ru/files/icons/
Expand Down

0 comments on commit f4f9b86

Please sign in to comment.