Skip to content

Commit

Permalink
migrate welcome info list from json to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Dec 6, 2023
1 parent 5d80bf7 commit 1eb63c9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 126 deletions.
4 changes: 0 additions & 4 deletions main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ tasks.jar {
archiveBaseName.set("CyberIO")
includeEmptyDirs = false
exclude("**/**/*.java")

from("$rootDir/meta") {
include("*.json")
}
}

java {
Expand Down
45 changes: 45 additions & 0 deletions main/src/net/liplum/welcome/Scenes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.liplum.welcome

object Scenes {
val common = WelcomeTipPack(
scenes = listOf(
"UpdateECHO",
"TheSHIP",
"ECHO",
"NewBrains",
"Discord",
"AdBlock",
"404NotFound",
"OhNo",
"RateUs",
"SetOutErekir",
"CyberionForErekir"
)
)
val v2 = WelcomeTipPack(
default = "EmploymentVerification",
scenes = listOf(
"EmploymentVerification"
)
)
val v3 = common.inherit(
default = "ResearchProject",
scenes = listOf(
"EmploymentVerification"
)
)
val v4 = common.inherit(
default = "ErekirDetected",
scenes = listOf(
"ErekirDetected",
"SetOutErekir"
)
)
val v4_1 = v4.inherit(
scenes = listOf(
"CyberionForErekir"
)
)
val v5 = v4_1.inherit()
val v5_1 = v5.inherit()
}
68 changes: 11 additions & 57 deletions main/src/net/liplum/welcome/Welcome.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package net.liplum.welcome

import arc.Core
import arc.Events
import arc.struct.ObjectMap
import arc.util.ArcRuntimeException
import arc.util.Time
import arc.util.serialization.JsonValue
import mindustry.game.EventType.Trigger
import mindustry.io.JsonIO
import net.liplum.CioMod
import net.liplum.Meta
import net.liplum.Settings.CioVersion
Expand All @@ -20,7 +16,6 @@ import net.liplum.annotations.Only
import net.liplum.annotations.SubscribeEvent
import net.liplum.blocks.tmtrainer.RandomName
import net.liplum.cio
import net.liplum.common.Res
import net.liplum.common.util.ReferBundleWrapper
import net.liplum.common.util.allMaxBy
import net.liplum.common.util.randomExcept
Expand All @@ -33,10 +28,11 @@ import plumy.dsl.sprite
@ClientOnly
object Welcome {
var bundle = ReferBundleWrapper.create()
private var info = Info()
fun genEntity() = Entity(bundle, info)
private var version = Scenes.v5_1
fun genEntity() = Entity(bundle, version)
private var entity = genEntity()
private var showWelcome = false

@JvmStatic
@ClientOnly
fun showWelcomeDialog() {
Expand All @@ -50,9 +46,10 @@ object Welcome {
tip?.condition?.canShow(tip)*/
//entity.showTipByID("SetOutErekir")
}

@JvmStatic
fun judgeWelcome() {
val allTips = info.scenes.map { Welcomes[it] }.distinct().toList()
val allTips = version.scenes.map { Welcomes[it] }.distinct().toList()
val tipsCanShow = allTips.filter { it.condition.canShow(it) }
val allCandidates = tipsCanShow.allMaxBy { it.condition.priority(it) }
if (allCandidates.isEmpty()) {
Expand All @@ -79,6 +76,7 @@ object Welcome {
showWelcome = true
}
}

@JvmStatic
@SubscribeEvent(CioInitEvent::class, Only.client)
fun modifierModInfo() {
Expand All @@ -90,6 +88,7 @@ object Welcome {
}
}
}

@JvmStatic
fun checkLastVersion() {
val lastVersion = CioVersion
Expand All @@ -101,20 +100,22 @@ object Welcome {
}
CioVersion = Meta.Version
}

@JvmStatic
fun recordClick() {
ClickWelcomeTimes += 1
}

@JvmStatic
@ClientOnly
fun load() {
loadBundle()
loadInfo()
//To load all templates and actions
Templates
Actions
Conditions
}

@JvmStatic
fun loadBundle() {
bundle.loadMoreFrom("welcomes")
Expand All @@ -123,61 +124,14 @@ object Welcome {
}
}

lateinit var infoJson: ObjectMap<String, JsonValue>
@Suppress("UNCHECKED_CAST")
@JvmStatic
fun loadInfo() {
val json = Res("WelcomeInfo.json").readAllText()
infoJson = JsonIO.json.fromJson(ObjectMap::class.java, json) as ObjectMap<String, JsonValue>
val curInfo = infoJson.get(Meta.Version)
?: throw ArcRuntimeException("The welcome message information of Cyber IO ${Meta.Version} not found.")
val default = curInfo.get("Default")?.asString() ?: "Default"
val scenes = curInfo.get("Scene")?.asStringArray() ?: emptyArray()
val parent: String? = curInfo.get("Parent")?.asString()
info.default = default
val allScenes = HashSet<String>()
allScenes.addAll(scenes)
fun loadParent(parent: String) {
val parentInfo = infoJson.get(parent)
val parentScenes = parentInfo.get("Scene").asStringArray()
val parentParent: String? = parentInfo.get("Parent")?.asString()
allScenes.addAll(parentScenes)
if (parentParent != null)
loadParent(parentParent)
}
if (parent != null) {
loadParent(parent)
}
info.scenes = allScenes.toList()
}

class Info {
var default = "Default"
var scenes: List<String> = emptyList()
val sceneSize: Int
get() = scenes.size

operator fun get(index: Int) =
if (index !in scenes.indices)
default
else
scenes[index]

fun indexOf(tipID: String): Int =
scenes.indexOf(tipID)

val defaultIndex: Int
get() = indexOf(default)
}

fun String.handleTrRefer(): TR =
if (startsWith('@'))
removePrefix("@").sprite
else cio.sprite

class Entity(
val bundle: ReferBundleWrapper,
val info: Info,
val info: WelcomeTipPack,
) {
var tip: WelcomeTip = WelcomeTip.Default
operator fun get(key: String) =
Expand Down
27 changes: 27 additions & 0 deletions main/src/net/liplum/welcome/WelcomeTip.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,30 @@ class WelcomeTip(
const val DefaultIconPath = "icon"
}
}

class WelcomeTipPack(
var default: String = "Default",
var scenes: List<String> = emptyList(),
) {
operator fun get(index: Int) =
if (index !in scenes.indices)
default
else
scenes[index]

fun indexOf(tipID: String): Int =
scenes.indexOf(tipID)

val defaultIndex: Int
get() = indexOf(default)

fun inherit(
default: String? = null,
scenes: List<String> = emptyList(),
): WelcomeTipPack {
return WelcomeTipPack(
default = default ?: this.default,
scenes = this.scenes + scenes,
)
}
}
65 changes: 0 additions & 65 deletions meta/WelcomeInfo.json

This file was deleted.

0 comments on commit 1eb63c9

Please sign in to comment.