From e6b68ae30714accf4beeff02cb6b8f9b571a5f8b Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 20 Oct 2023 11:45:58 +0800 Subject: [PATCH] code optimize and change debug logs on Nbnhhsh command to trace logs --- .../sukazyo/cono/morny/bot/command/Nbnhhsh.scala | 14 +++++++------- .../cc/sukazyo/cono/morny/data/NbnhhshQuery.scala | 11 +++++++---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.scala index 4c0e903..ea95cbe 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.scala @@ -45,26 +45,26 @@ class Nbnhhsh (using coeur: MornyCoeur) extends ITelegramCommand { val message = StringBuilder(NBNHHSH_RESULT_HEAD_HTML) import cc.sukazyo.cono.morny.Log.logger - logger debug s"**xx len=${queryResp.words.length}" + logger trace s"**nbnhhsh got len=${queryResp.words.length}" for (_word <- queryResp.words) { - logger debug "**exec" + logger trace s"**start for ${_word.name}" val _use_trans = (_word.trans ne null) && (_word.trans nonEmpty) val _use_inputting = (_word.inputting ne null) && (_word.inputting nonEmpty) if (_use_trans || _use_inputting) message ++= s"\n\n[[ ${h(_word.name)} ]]" - logger debug s"**used [${_word.name}]" + logger trace s"**used [${_word.name}]" if (_use_trans) for (_trans <- _word.trans) message ++= s"\n* ${h(_trans)}" - logger debug s"**used [${_word.name}] used `${_trans}``" + logger trace s"**used [${_word.name}] used `${_trans}``" if (_use_inputting) - logger debug s"**used [${_word.name}] inputting" + logger trace s"**used [${_word.name}] inputting" if (_use_trans) message += '\n' message ++= " maybe:" for (_inputting <- _word.inputting) - logger debug s"**used [${_word.name}] used-i ${_inputting}" + logger trace s"**used [${_word.name}] used-i ${_inputting}" message ++= s"\n` ${h(_inputting)}" - logger debug s"**exec as ${_word.name}" + logger trace s"**done" } coeur.account exec SendMessage( diff --git a/src/main/scala/cc/sukazyo/cono/morny/data/NbnhhshQuery.scala b/src/main/scala/cc/sukazyo/cono/morny/data/NbnhhshQuery.scala index c27bfd5..4322739 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/data/NbnhhshQuery.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/data/NbnhhshQuery.scala @@ -7,11 +7,13 @@ import sttp.model.MediaType object NbnhhshQuery { - case class Word (name: String, trans: Array[String], inputting: Array[String]) + case class Word ( + name: String, + trans: Array[String] = Array.empty, + inputting: Array[String] = Array.empty + ) case class GuessResult (words: Array[Word]) - private case class GuessRequest (text: String) - private val API_URL = uri"https://lab.magiconch.com/api/nbnhhsh/" private val API_GUESS_METHOD = uri"$API_URL/guess/" @@ -19,12 +21,13 @@ object NbnhhshQuery { @throws[HttpError[_]|SttpClientException] def sendGuess (text: String): GuessResult = { + case class GuessRequest (text: String) val http = basicRequest .body(Gson().toJson(GuessRequest(text))).contentType(MediaType.ApplicationJson) .post(API_GUESS_METHOD) .response(asString.getRight) .send(httpClient) - Gson().fromJson(s"{ 'words': ${http.body} }", classOf[GuessResult]) + GuessResult(Gson().fromJson(http.body, classOf[Array[Word]])) } }