|
| 1 | +package cc.modlabs |
| 2 | + |
| 3 | +import kotlinx.serialization.SerialName |
| 4 | +import kotlinx.serialization.Serializable |
| 5 | +import kotlinx.serialization.json.Json |
| 6 | +import java.nio.file.Files |
| 7 | +import java.nio.file.Path |
| 8 | + |
| 9 | +@Serializable |
| 10 | +data class HytlPluginConfig( |
| 11 | + val serverId: String = "", |
| 12 | + val serverSecret: String = "", |
| 13 | + val backendBaseUrl: String = "https://hytl.dev/api/plugin", |
| 14 | + val heartbeatSeconds: Int = 10, |
| 15 | + val votePollSeconds: Int = 15, |
| 16 | + val privacy: PrivacyConfig = PrivacyConfig(), |
| 17 | + val voteRewards: VoteRewardsConfig = VoteRewardsConfig(), |
| 18 | + val httpTimeoutMs: Long = 3_000, |
| 19 | + val maxBackoffSeconds: Int = 60, |
| 20 | + // Optional convenience: override the public host/port shown in HYTL. |
| 21 | + val publicHost: String? = null, |
| 22 | + val publicPort: Int? = null, |
| 23 | + // Optional convenience: override server version/patchline reported to HYTL. |
| 24 | + val gameVersion: String? = null, |
| 25 | + val patchline: String? = null |
| 26 | +) |
| 27 | + |
| 28 | +@Serializable |
| 29 | +data class PrivacyConfig( |
| 30 | + val shareMods: Boolean = true, |
| 31 | + val sharePlayerCount: Boolean = true, |
| 32 | + val sharePlayerNames: Boolean = true, |
| 33 | + val sharePlayerUUIDs: Boolean = false |
| 34 | +) |
| 35 | + |
| 36 | +@Serializable |
| 37 | +data class VoteRewardsConfig( |
| 38 | + /** |
| 39 | + * LOG_ONLY: logs what would be rewarded and ACKs votes (default). |
| 40 | + * DISABLED: does not reward and does not ACK (votes stay pending). |
| 41 | + */ |
| 42 | + val mode: String = "LOG_ONLY", |
| 43 | + val baseAmount: Int = 1, |
| 44 | + /** |
| 45 | + * Tier thresholds (inclusive) by streak. Example default tiers: |
| 46 | + * 1 -> tier 1, 3 -> tier 2, 7 -> tier 3, 14 -> tier 4, 30 -> tier 5. |
| 47 | + */ |
| 48 | + val tierStreakThresholds: List<Int> = listOf(1, 3, 7, 14, 30), |
| 49 | + /** |
| 50 | + * Amount multipliers per tier index (1-based). Default: [1,2,3,4,5]. |
| 51 | + */ |
| 52 | + val tierMultipliers: List<Int> = listOf(1, 2, 3, 4, 5) |
| 53 | +) |
| 54 | + |
| 55 | +object HytlConfigIO { |
| 56 | + private val json = Json { |
| 57 | + prettyPrint = true |
| 58 | + encodeDefaults = true |
| 59 | + ignoreUnknownKeys = true |
| 60 | + explicitNulls = false |
| 61 | + } |
| 62 | + |
| 63 | + const val CONFIG_FILE_NAME: String = "config.json" |
| 64 | + |
| 65 | + fun configPath(dataDir: Path): Path = dataDir.resolve(CONFIG_FILE_NAME) |
| 66 | + |
| 67 | + /** |
| 68 | + * Loads config from the plugin data directory. If missing, writes a starter file and returns defaults. |
| 69 | + */ |
| 70 | + fun loadOrCreate(dataDir: Path): HytlPluginConfig { |
| 71 | + Files.createDirectories(dataDir) |
| 72 | + val path = configPath(dataDir) |
| 73 | + if (!Files.exists(path)) { |
| 74 | + val starter = HytlPluginConfig() |
| 75 | + Files.writeString(path, json.encodeToString(HytlPluginConfig.serializer(), starter)) |
| 76 | + return starter |
| 77 | + } |
| 78 | + val raw = Files.readString(path) |
| 79 | + return json.decodeFromString(HytlPluginConfig.serializer(), raw) |
| 80 | + } |
| 81 | + |
| 82 | + fun save(dataDir: Path, cfg: HytlPluginConfig) { |
| 83 | + Files.createDirectories(dataDir) |
| 84 | + val path = configPath(dataDir) |
| 85 | + Files.writeString(path, json.encodeToString(HytlPluginConfig.serializer(), cfg)) |
| 86 | + } |
| 87 | +} |
| 88 | + |
0 commit comments