Skip to content

Commit

Permalink
feat: #81 #102 Move Wiki and StandardDictionary to kg-common an…
Browse files Browse the repository at this point in the history
…d convert them to Kotlin.
  • Loading branch information
dmitry-weirdo committed Nov 25, 2024
1 parent 5d89dec commit 7f249c4
Show file tree
Hide file tree
Showing 29 changed files with 206 additions and 191 deletions.
165 changes: 165 additions & 0 deletions kgCommon/src/main/kotlin/ru/klavogonki/common/StandardDictionary.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package ru.klavogonki.common

/**
* Copyright 2014 [Dmitriy Popov](mailto:[email protected]).
* $HeadURL$
* $Author$
* $Revision$
* $Date:: $
* <br></br>
* <br></br>
* Энум с названиями стандартных словарей (режимов), как они используются в AJAX-API.
*/
@SuppressWarnings("MagicNumber")
enum class StandardDictionary(

/**
* Имя в API клавогонок. Все буквы в нижнем регистре.
*/
@JvmField val klavogonkiName: String,

/**
* Русское название словаря для отображения.
*/
@JvmField val displayName: String,

/**
* Русское название словаря для отображения, в предложном падеже.
*/
@JvmField val displayNamePrepositional: String,

/**
* Цвет стандартного словаря в формате #123456
*/
@JvmField val color: String,

/**
* Страница стандартного словаря в [википедии клавогонок](http://klavogonki.ru/wiki/).
*/
@JvmField val wikiPageUrl: String,

/**
* Тип текста словаря.
* Для нестандартных словарей будет равен идентификатору словаря.
*/
@JvmField val textType: Int
) {
/**
* Обычный.
*/
normal(
klavogonkiName = "normal",
displayName = "Обычный",
displayNamePrepositional = "Обычном",
color = "#333333",
wikiPageUrl = Wiki.getUrl("%D0%9E%D0%B1%D1%8B%D1%87%D0%BD%D1%8B%D0%B9"),
textType = 0
),

/**
* Безошибочный.
*/
noerror(
klavogonkiName = "noerror",
displayName = "Безошибочный",
displayNamePrepositional = "Безошибочном",
color = "#4692AA",
wikiPageUrl = Wiki.getUrl("%D0%91%D0%B5%D0%B7%D0%BE%D1%88%D0%B8%D0%B1%D0%BE%D1%87%D0%BD%D1%8B%D0%B9"),
textType = 0
),

/**
* Буквы.
*/
chars(
klavogonkiName = "chars",
displayName = "Буквы",
displayNamePrepositional = "Буквах",
color = "#B55900",
wikiPageUrl = Wiki.getUrl("%D0%91%D1%83%D0%BA%D0%B2%D1%8B"),
textType = -4
),

/**
* Марафон.
*/
marathon(
klavogonkiName = "marathon",
displayName = "Марафон",
displayNamePrepositional = "Марафоне",
color = "#D43E68",
wikiPageUrl = Wiki.getUrl("%D0%9C%D0%B0%D1%80%D0%B0%D1%84%D0%BE%D0%BD"),
textType = 0
),

/**
* Абракадабра.
*/
abra(
klavogonkiName = "abra",
displayName = "Абракадабра",
displayNamePrepositional = "Абракадабре",
color = "#3D4856",
wikiPageUrl = Wiki.getUrl("%D0%90%D0%B1%D1%80%D0%B0%D0%BA%D0%B0%D0%B4%D0%B0%D0%B1%D1%80%D0%B0"),
textType = -1
),

/**
* Яндекс.Рефераты.
*/
referats(
klavogonkiName = "referats",
displayName = "Яндекс.Рефераты",
displayNamePrepositional = "Яндекс.Рефератах",
color = "#698725",
wikiPageUrl = Wiki.getUrl("%D0%AF%D0%BD%D0%B4%D0%B5%D0%BA%D1%81.%D0%A0%D0%B5%D1%84%D0%B5%D1%80%D0%B0%D1%82%D1%8B"),
textType = -3
),

/**
* Цифры.
*/
digits(
klavogonkiName = "digits",
displayName = "Цифры",
displayNamePrepositional = "Цифрах",
color = "#777777",
wikiPageUrl = Wiki.getUrl("%D0%A6%D0%B8%D1%84%D1%80%D1%8B"),
textType = -2
),

/**
* Спринт.
*/
sprint(
klavogonkiName = "sprint",
displayName = "Спринт",
displayNamePrepositional = "Спринте",
color = "#833F3A",
wikiPageUrl = Wiki.getUrl("%D0%A1%D0%BF%D1%80%D0%B8%D0%BD%D1%82"),
textType = 0
),
;

companion object {

@JvmStatic
fun isValidStandardDictionaryCode(code: String) =
entries.any { it.klavogonkiName == code }

@JvmStatic
fun getByKlavogonkiName(code: String): StandardDictionary {
val values = entries.filter { it.klavogonkiName == code }

check(values.isNotEmpty()) {
"No standard dictionary for code = \"$code\"."
}

check(values.size == 1) {
"More than one standard dictionary for code = \"$code\"."
}

return values[0]
}
}
}
8 changes: 8 additions & 0 deletions kgCommon/src/main/kotlin/ru/klavogonki/common/Wiki.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.klavogonki.common

object Wiki {
private const val WIKI_BASE_URL = "https://klavogonki.ru/wiki/"

fun getUrl(pageName: String) =
WIKI_BASE_URL + pageName.trim()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ru.klavogonki.common.StandardDictionary;
import ru.klavogonki.kgparser.http.HttpClientTest;
import su.opencode.kefir.srv.json.Json;
import su.opencode.kefir.srv.json.JsonObject;
Expand Down Expand Up @@ -258,7 +259,7 @@ public void fillDictionariesNames() {

if (dictionary.isStandard())
{
StandardDictionary standardDictionary = StandardDictionary.valueOf(dictionaryCode);
StandardDictionary standardDictionary = StandardDictionary.getByKlavogonkiName(dictionaryCode);
codesToNames.put(dictionaryCode, standardDictionary.displayName);
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.klavogonki.kgparser;

import ru.klavogonki.common.StandardDictionary;
import ru.klavogonki.kgparser.http.UrlConstructor;
import su.opencode.kefir.srv.json.Json;
import su.opencode.kefir.util.StringUtils;
Expand Down Expand Up @@ -138,7 +139,7 @@ public static int getTextType(String code) {
return getDictionaryId(code);
}

StandardDictionary standardDictionary = StandardDictionary.valueOf(code);
StandardDictionary standardDictionary = StandardDictionary.getByKlavogonkiName(code);
return standardDictionary.textType;
}

Expand All @@ -149,7 +150,7 @@ public static int getTextType(String code) {
@Json(exclude = true)
public static String getDictionaryColor(String dictionaryCode) {
if (isStandard(dictionaryCode)) {
return StandardDictionary.valueOf(dictionaryCode).color;
return StandardDictionary.getByKlavogonkiName(dictionaryCode).color;
}

return NON_STANDARD_DICTIONARY_COLOR;
Expand All @@ -163,7 +164,7 @@ public static String getDictionaryColor(String dictionaryCode) {
*/
public static String getDictionaryPageUrl(String dictionaryCode) {
if (isStandard(dictionaryCode)) {
return StandardDictionary.valueOf(dictionaryCode).wikiPageUrl;
return StandardDictionary.getByKlavogonkiName(dictionaryCode).wikiPageUrl;
}

return UrlConstructor.dictionaryPage( getDictionaryId(dictionaryCode) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.klavogonki.kgparser;

import ru.klavogonki.common.StandardDictionary;

public enum DictionaryMode {

normal,
Expand All @@ -17,7 +19,7 @@ public static DictionaryMode getDictionaryMode(String dictionaryCode) {
return normal;
}

StandardDictionary standardDictionary = StandardDictionary.valueOf(dictionaryCode);
StandardDictionary standardDictionary = StandardDictionary.getByKlavogonkiName(dictionaryCode);

switch (standardDictionary) {
case normal:
Expand Down
2 changes: 2 additions & 0 deletions kgparserSrv/src/main/java/ru/klavogonki/kgparser/Rank.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.klavogonki.kgparser;

import ru.klavogonki.common.StandardDictionary;

import java.util.Arrays;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ru.klavogonki.common.StandardDictionary;
import su.opencode.kefir.srv.json.Json;
import su.opencode.kefir.srv.json.JsonObject;
import su.opencode.kefir.util.ObjectUtils;
Expand Down
Loading

0 comments on commit 7f249c4

Please sign in to comment.