From f4f9b862de884200f469512568261b3309dd0ebe Mon Sep 17 00:00:00 2001
From: onoderis <onoderis@users.noreply.github.com>
Date: Sun, 23 Dec 2018 15:50:13 +0300
Subject: [PATCH] Migrate goodgame to chat api v2

---
 .../kotlin/failchat/goodgame/GgChatClient.kt  | 44 +++++--------------
 src/main/resources/config/default.properties  |  2 +-
 2 files changed, 13 insertions(+), 33 deletions(-)

diff --git a/src/main/kotlin/failchat/goodgame/GgChatClient.kt b/src/main/kotlin/failchat/goodgame/GgChatClient.kt
index b4f68b80..9753545d 100644
--- a/src/main/kotlin/failchat/goodgame/GgChatClient.kt
+++ b/src/main/kotlin/failchat/goodgame/GgChatClient.kt
@@ -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(
@@ -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() {
@@ -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) {
@@ -130,7 +113,7 @@ class GgChatClient(
             when (type) {
                 "message" -> handleUserMessage(data)
                 "remove_message" -> handleModMessage(data)
-                "viewers" -> handleViewersMessage(data)
+                "channel_counters" -> handleChannelCountersMessage(data)
             }
         }
 
@@ -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()
         }
     }
 
diff --git a/src/main/resources/config/default.properties b/src/main/resources/config/default.properties
index fa6cdc43..43c19b6f 100644
--- a/src/main/resources/config/default.properties
+++ b/src/main/resources/config/default.properties
@@ -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/