Skip to content

Commit

Permalink
Add comments to StringXmlParser and change to proper name
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcell committed Aug 17, 2023
1 parent 9226820 commit cf70b3b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
44 changes: 41 additions & 3 deletions src/main/kotlin/tools/StringXmlParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,52 @@ import server.life.LifeFactory
import webapi.controller.SearchType
import java.io.File

/**
* To search resources in game by name or id, we need to parse String.wz and store them in some places.
* This will be store string data, and provide search function. This can be used in GM command, or Web API.
* Open to non-GM users are not recommended. Each search function require additional computing power from the server.
* Please check out AdminSearch controller before use.
*
* @see webapi.controller.adminSearch
* @author Seungyeon Choi
*/
object StringXmlParser : KLoggable {
override val logger = logger()

val itemData = mutableMapOf<Int, String>()
val mapData = addMapEntry()
val mapData = getMapEntry()
val mobData = LifeFactory.getStringData(1)
val npcData = LifeFactory.getStringData(2)
val skillData = mutableMapOf<Int, String>()

/**
* Function to add items. When server in starting process, all items are going to load once, use this function to fill
*
* @param loaded List of a pair of id and name. Items are actually separated by inventory type or category.
* This will be the one set of list by type.
*/
fun addItemEntry(loaded: List<Pair<Int, String>>) {
loaded.forEach { pair ->
itemData[pair.first] = pair.second
}
}

/**
* Function to add skills. When server in starting process, all skills are going to load once, use this function to fill
*
* @param id Id of skill
* @param name Name of skill
*/
fun addSkillEntry(id: Int, name: String) {
skillData[id] = name
}

private fun addMapEntry(): Map<Int, String> {
/**
* Parse String.wz\Map.img.xml and get map of code and name
*
* @return Map of code and name. name will be combined as "StreetName - MapName"
*/
private fun getMapEntry(): Map<Int, String> {
val map = mutableMapOf<Int, String>()
val stringWz =
DataProviderFactory.getDataProvider(File("${ServerJSON.settings.wzPath}/String.wz")).getData("Map.img")
Expand All @@ -44,7 +70,16 @@ object StringXmlParser : KLoggable {
return map
}

fun findMap(type: SearchType, code: Int? = null, name: String? = null): Map<Int, String> {
/**
* Find something using type, code or Id, name. Search by string contains
*
* @param type type of search. check SearchType
* @see webapi.controller.SearchType
* @param code Integer value of code
* @param name query string of name
* @return Map of code and name
*/
fun find(type: SearchType, code: Int? = null, name: String? = null): Map<Int, String> {
if (code == null && name == null) return emptyMap()
return when (type) {
SearchType.MAP -> mapData
Expand All @@ -58,6 +93,9 @@ object StringXmlParser : KLoggable {
}
}

/**
* Test function to print loaded data to Log. For test
*/
fun test() {
logger.debug { "Testing String data holder - Map(100000000) - Stored Name: ${mapData[100000000]}" }
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/webapi/controller/AdminSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun Route.adminSearch() {
val name = principal.payload.getClaim("name").asString()
if (!isAdmin || name == null) return@post
val request = call.receive<SearchRequest>()
val result = StringXmlParser.findMap(request.type, request.byId, request.byName)
val result = StringXmlParser.find(request.type, request.byId, request.byName)
call.respond(ApiResponse(true, ResponseMessage.SUCCESS, Json.encodeToJsonElement(result)))
}
}
Expand Down

0 comments on commit cf70b3b

Please sign in to comment.