Skip to content

Commit

Permalink
feat: #81 #102 Move Dictionary and StandardDictionary to `kg-comm…
Browse files Browse the repository at this point in the history
…on` and convert them to Kotlin. Make fields of `Car` visible to Java.
  • Loading branch information
dmitry-weirdo committed Nov 25, 2024
1 parent e732c73 commit 607cd33
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 98 deletions.
39 changes: 9 additions & 30 deletions kgCommon/src/main/kotlin/ru/klavogonki/common/Car.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ package ru.klavogonki.common
* List of cars with ids and Russian names.
*/
@SuppressWarnings("MagicNumber")
enum class Car {
enum class Car(
@JvmField val id: Int,
@JvmField val displayName: String,
@JvmField val ownerId: Int?, // for personal autos
@JvmField val personalId: Int?, // for personal autos that have been made public
) {
// common/public autos
ZAZ_965(1, "ЗАЗ 965"),
VAZ_2104(2, "ВАЗ 2104"),
Expand Down Expand Up @@ -106,31 +111,10 @@ enum class Car {
FERRARI_F2(1024, "Ferrari F12", 320247), // http://klavogonki.ru/u/#/320247/car/
;

constructor(id: Int, displayName: String) {
this.id = id
this.displayName = displayName
this.ownerId = null
this.personalId = null
}
constructor(id: Int, displayName: String) : this(id, displayName, null, null)

constructor(id: Int, displayName: String, ownerId: Int) { // for personal cars that have not been made public
this.id = id
this.displayName = displayName
this.ownerId = ownerId
this.personalId = null
}

constructor(
id: Int,
displayName: String,
ownerId: Int,
personalId: Int
) { // for personal cars that have not been made public
this.id = id
this.displayName = displayName
this.ownerId = ownerId
this.personalId = personalId
}
// for personal cars that have not been made public
constructor(id: Int, displayName: String, ownerId: Int) : this(id, displayName, ownerId, null)

@Suppress("Unused")
fun isPersonalOnly() = (ownerId != null) && (personalId == null)
Expand All @@ -141,11 +125,6 @@ enum class Car {
@Suppress("Unused")
fun isPublic() = (id < FIRST_PERSONAL_CAR_ID)

val id: Int
val displayName: String
val ownerId: Int? // for personal autos
val personalId: Int? // for personal autos that have been made public

companion object {
fun isPersonalId(carId: Int): Boolean {
return carId >= FIRST_PERSONAL_CAR_ID
Expand Down
33 changes: 33 additions & 0 deletions kgCommon/src/main/kotlin/ru/klavogonki/common/DictionaryMode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.klavogonki.common

import ru.klavogonki.common.DictionaryUtils.isStandard

enum class DictionaryMode(

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

noerror("noerror"),

sprint("sprint"),

marathon("marathon"),
;

companion object {

@JvmStatic
fun getDictionaryMode(dictionaryCode: String): DictionaryMode {
if (!isStandard(dictionaryCode)) {
return normal
}

val standardDictionary = StandardDictionary.getByKlavogonkiName(dictionaryCode)
return standardDictionary.dictionaryMode
}
}
}
34 changes: 24 additions & 10 deletions kgCommon/src/main/kotlin/ru/klavogonki/common/StandardDictionary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ru.klavogonki.common
/**
* Энум с названиями стандартных словарей (режимов), как они используются в AJAX-API.
*/
@SuppressWarnings("MagicNumber")
@SuppressWarnings("MagicNumber", "LongParameterList")
enum class StandardDictionary(

/**
Expand Down Expand Up @@ -35,7 +35,13 @@ enum class StandardDictionary(
* Тип текста словаря.
* Для нестандартных словарей будет равен идентификатору словаря.
*/
@JvmField val textType: Int
@JvmField val textType: Int,

/**
* Режим текста словаря.
* Возвращается в поле `mode` в `/get-stats-overview -> ... -> info`.
*/
@JvmField val dictionaryMode: DictionaryMode
) {
/**
* Обычный.
Expand All @@ -46,7 +52,8 @@ enum class StandardDictionary(
displayNamePrepositional = "Обычном",
color = "#333333",
wikiPageUrl = Wiki.getUrl("%D0%9E%D0%B1%D1%8B%D1%87%D0%BD%D1%8B%D0%B9"),
textType = 0
textType = 0,
dictionaryMode = DictionaryMode.normal
),

/**
Expand All @@ -58,7 +65,8 @@ enum class StandardDictionary(
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
textType = 0,
dictionaryMode = DictionaryMode.noerror
),

/**
Expand All @@ -70,7 +78,8 @@ enum class StandardDictionary(
displayNamePrepositional = "Буквах",
color = "#B55900",
wikiPageUrl = Wiki.getUrl("%D0%91%D1%83%D0%BA%D0%B2%D1%8B"),
textType = -4
textType = -4,
dictionaryMode = DictionaryMode.normal
),

/**
Expand All @@ -82,7 +91,8 @@ enum class StandardDictionary(
displayNamePrepositional = "Марафоне",
color = "#D43E68",
wikiPageUrl = Wiki.getUrl("%D0%9C%D0%B0%D1%80%D0%B0%D1%84%D0%BE%D0%BD"),
textType = 0
textType = 0,
dictionaryMode = DictionaryMode.marathon
),

/**
Expand All @@ -94,7 +104,8 @@ enum class StandardDictionary(
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
textType = -1,
dictionaryMode = DictionaryMode.normal
),

/**
Expand All @@ -106,7 +117,8 @@ enum class StandardDictionary(
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
textType = -3,
dictionaryMode = DictionaryMode.normal
),

/**
Expand All @@ -118,7 +130,8 @@ enum class StandardDictionary(
displayNamePrepositional = "Цифрах",
color = "#777777",
wikiPageUrl = Wiki.getUrl("%D0%A6%D0%B8%D1%84%D1%80%D1%8B"),
textType = -2
textType = -2,
dictionaryMode = DictionaryMode.normal
),

/**
Expand All @@ -130,7 +143,8 @@ enum class StandardDictionary(
displayNamePrepositional = "Спринте",
color = "#833F3A",
wikiPageUrl = Wiki.getUrl("%D0%A1%D0%BF%D1%80%D0%B8%D0%BD%D1%82"),
textType = 0
textType = 0,
dictionaryMode = DictionaryMode.sprint
),
;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ru.klavogonki.common.Car;
import ru.klavogonki.common.DictionaryMode;
import ru.klavogonki.common.DictionaryUtils;
import ru.klavogonki.common.StandardDictionary;
import ru.klavogonki.kgparser.DictionaryMode;
import ru.klavogonki.kgparser.Rank;
import ru.klavogonki.openapi.model.GetIndexDataResponse;
import ru.klavogonki.openapi.model.GetIndexDataStats;
Expand Down Expand Up @@ -608,7 +608,7 @@ private static void validateInfo(final int playerId, final String statsOverviewF
DictionaryMode expectedMode = DictionaryMode.getDictionaryMode(vocabularyCode);

// todo: mapper conversion VocabularyMode -> DictionaryMode?
if (!mode.toString().equals(expectedMode.toString())) {
if (!mode.toString().equals(expectedMode.klavogonkiName)) {
throw new ParserException("Stats overview file %s: Vocabulary %s: info.mode %s is not equal to expected mode = %d.", statsOverviewFilePath, vocabularyCode, mode, expectedMode);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ru.klavogonki.statistics.entity;

import lombok.Data;
import ru.klavogonki.kgparser.DictionaryMode;
import ru.klavogonki.common.DictionaryMode;
import ru.klavogonki.kgparser.NonStandardDictionaryType;

import javax.persistence.Entity;
Expand Down Expand Up @@ -67,7 +67,7 @@ public class PlayerVocabularyStatsEntity implements Serializable {
*
* @see DictionaryMode#getDictionaryMode
*/
@Enumerated(EnumType.STRING)
@Enumerated(EnumType.STRING) // todo: save using the klavogonikiName field, NOT the enum itself
private DictionaryMode vocabularyMode;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import org.mapstruct.MappingTarget;
import org.mapstruct.ValueMapping;
import org.mapstruct.ValueMappings;
import ru.klavogonki.kgparser.DictionaryMode;
import ru.klavogonki.common.DictionaryMode;
import ru.klavogonki.kgparser.NonStandardDictionaryType;
import ru.klavogonki.statistics.entity.PlayerEntity;
import ru.klavogonki.statistics.entity.PlayerVocabularyStatsEntity;
import ru.klavogonki.statistics.util.DateUtils;
import ru.klavogonki.openapi.model.GetStatsOverviewGameType;
import ru.klavogonki.openapi.model.GetStatsOverviewResponse;
import ru.klavogonki.openapi.model.NonStandardVocabularyType;
import ru.klavogonki.openapi.model.VocabularyMode;
import ru.klavogonki.statistics.entity.PlayerEntity;
import ru.klavogonki.statistics.entity.PlayerVocabularyStatsEntity;
import ru.klavogonki.statistics.util.DateUtils;

import java.time.LocalDateTime;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void existingPlayer() {
CarAssert
.assertThat(car)
.isNotNull()
.hasCar(Car.F1.getId())
.hasCar(Car.F1.id)
.hasColor("#BF1300");

Map<String, Integer> tuning = getNonEmptyTuning(car);
Expand Down Expand Up @@ -132,7 +132,7 @@ void playerWithPersonalCarId() {
CarAssert
.assertThat(car)
.isNotNull()
.hasCar(Car.CARAVEL.getPersonalId())
.hasCar(Car.CARAVEL.personalId)
.hasColor("#000000");

assertEmptyTunning(car);
Expand Down Expand Up @@ -172,7 +172,7 @@ void brandNewPlayer() {
CarAssert
.assertThat(car)
.isNotNull()
.hasCar(Car.ZAZ_965.getId())
.hasCar(Car.ZAZ_965.id)
.hasColor("#777777");

assertEmptyTunning(car);
Expand Down Expand Up @@ -213,7 +213,7 @@ void klavoMechanicWithHiddenProfile() {
CarAssert
.assertThat(car)
.isNotNull()
.hasCar(Car.AUDI_TT.getId())
.hasCar(Car.AUDI_TT.id)
.hasColor("#893425");

Map<String, Integer> tuning = getNonEmptyTuning(car);
Expand Down

0 comments on commit 607cd33

Please sign in to comment.