Skip to content

Commit

Permalink
sonarlint improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Galarzaa90 committed Mar 16, 2024
1 parent cf2c19e commit 09072e5
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 Allan Galarza
* Copyright © 2024 Allan Galarza
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,7 @@ import com.galarzaa.tibiakt.core.models.bazaar.Mounts
import com.galarzaa.tibiakt.core.models.bazaar.OutfitEntry
import com.galarzaa.tibiakt.core.models.bazaar.OutfitImage
import com.galarzaa.tibiakt.core.models.bazaar.Outfits
import com.galarzaa.tibiakt.core.utils.TABLE_SELECTOR
import com.galarzaa.tibiakt.core.utils.cellsText
import com.galarzaa.tibiakt.core.utils.clean
import com.galarzaa.tibiakt.core.utils.cleanText
Expand All @@ -57,6 +58,10 @@ import java.net.URL
public object AuctionParser : Parser<Auction?> {
private const val PERCENTAGE = 100f

@PublishedApi
internal const val ICON_CSS_SELECTOR: String = "div.CVIcon"
internal const val PAGINATOR_SELECTOR: String = "div.BlockPageNavigationRow"

private val charInfoRegex = Regex("""Level: (\d+) \| Vocation: ([\w\s]+)\| (\w+) \| World: (\w+)""")
private val idAddonsRegex = Regex("""/(\d+)_(\d+)""")
private val amountRegex = Regex("""([\d,]+)x """)
Expand Down Expand Up @@ -99,7 +104,7 @@ public object AuctionParser : Parser<Auction?> {
}

private fun AuctionBuilder.AuctionDetailsBuilder.parseCharmsTable(table: Element) {
for (row in table.selectFirst("table.TableContent").rows().offsetStart(1)) {
for (row in table.selectFirst(TABLE_SELECTOR).rows().offsetStart(1)) {
val colsText = row.cellsText()
if (colsText.size != 2) continue
val (cost, name) = colsText
Expand All @@ -108,7 +113,7 @@ public object AuctionParser : Parser<Auction?> {
}

private fun AuctionBuilder.AuctionDetailsBuilder.parseAchievementsTable(table: Element) {
for (row in table.selectFirst("table.TableContent").rows().offsetStart(1)) {
for (row in table.selectFirst(TABLE_SELECTOR).rows().offsetStart(1)) {
val text = row.cleanText()
if (text.contains("more entries")) continue
val isSecret = row.selectFirst("img") != null
Expand All @@ -117,7 +122,7 @@ public object AuctionParser : Parser<Auction?> {
}

private fun AuctionBuilder.AuctionDetailsBuilder.parseBestiaryTable(table: Element, bosstiary: Boolean = false) {
for (row in table.selectFirst("table.TableContent").rows().offsetStart(1)) {
for (row in table.selectFirst(TABLE_SELECTOR).rows().offsetStart(1)) {
val columnsText = row.cellsText()
if (columnsText.size != 3) continue
val (step, kills, name) = columnsText
Expand All @@ -127,7 +132,7 @@ public object AuctionParser : Parser<Auction?> {
}

private fun AuctionBuilder.AuctionDetailsBuilder.parseBlessingsTable(table: Element) {
for (row in table.selectFirst("table.TableContent").rows().offsetStart(1)) {
for (row in table.selectFirst(TABLE_SELECTOR).rows().offsetStart(1)) {
val colsText = row.cellsText()
if (colsText.size != 2) continue
val (amount, name) = colsText
Expand All @@ -136,7 +141,7 @@ public object AuctionParser : Parser<Auction?> {
}

private fun parseSingleColumnTable(table: Element): List<String> {
val innerTable = table.select("table.TableContent").last()
val innerTable = table.select(TABLE_SELECTOR).last()
val values = mutableListOf<String>()
for (row in innerTable.rows().offsetStart(1)) {
val text = row.selectFirst("td")?.cleanText().orEmpty()
Expand All @@ -147,12 +152,13 @@ public object AuctionParser : Parser<Auction?> {
}

private fun parseFamiliarsTable(table: Element): Familiars {
val paginationData = table.selectFirst("div.BlockPageNavigationRow")?.parsePagination() ?: return Familiars(1,
val paginationData = table.selectFirst(PAGINATOR_SELECTOR)?.parsePagination() ?: return Familiars(
1,
0,
0,
emptyList(),
false)
val familiarBoxes = table.select("div.CVIcon")
val familiarBoxes = table.select(ICON_CSS_SELECTOR)
val familiars = mutableListOf<FamiliarEntry>()
for (mountBox in familiarBoxes) {
val name = mountBox.attr("title").split("(").first().clean()
Expand All @@ -168,12 +174,13 @@ public object AuctionParser : Parser<Auction?> {
}

private fun parseOutfitsTable(table: Element): Outfits {
val paginationData = table.selectFirst("div.BlockPageNavigationRow")?.parsePagination() ?: return Outfits(1,
val paginationData = table.selectFirst(PAGINATOR_SELECTOR)?.parsePagination() ?: return Outfits(
1,
0,
0,
emptyList(),
false)
val outfitBoxes = table.select("div.CVIcon")
val outfitBoxes = table.select(ICON_CSS_SELECTOR)
val outfits = outfitBoxes.map { parseDisplayedOutfit(it) }
return Outfits(paginationData.currentPage,
paginationData.totalPages,
Expand All @@ -184,24 +191,26 @@ public object AuctionParser : Parser<Auction?> {

private fun parseMountsTable(mountsTable: Element): Mounts {
val paginationData =
mountsTable.selectFirst("div.BlockPageNavigationRow")?.parsePagination() ?: return Mounts(1,
mountsTable.selectFirst(PAGINATOR_SELECTOR)?.parsePagination() ?: return Mounts(
1,
0,
0,
emptyList(),
false)
val mountsBoxes = mountsTable.select("div.CVIcon")
val mountsBoxes = mountsTable.select(ICON_CSS_SELECTOR)
val mounts = mountsBoxes.map { parseDisplayedMount(it) }
return Mounts(paginationData.currentPage, paginationData.totalPages, paginationData.resultsCount, mounts, false)
}

private fun parseItemsTable(itemsTable: Element): ItemSummary {
val paginationData =
itemsTable.selectFirst("div.BlockPageNavigationRow")?.parsePagination() ?: return ItemSummary(1,
itemsTable.selectFirst(PAGINATOR_SELECTOR)?.parsePagination() ?: return ItemSummary(
1,
0,
0,
emptyList(),
false)
val itemBoxes = itemsTable.select("div.CVIcon")
val itemBoxes = itemsTable.select(ICON_CSS_SELECTOR)
val items = mutableListOf<ItemEntry>()
for (itemBox in itemBoxes) {
parseDisplayedItem(itemBox)?.run { items.add(this) }
Expand All @@ -221,8 +230,8 @@ public object AuctionParser : Parser<Auction?> {
}

private fun AuctionBuilder.AuctionDetailsBuilder.parseGeneralTable(table: Element) {
val contentContainers = table.select("table.TableContent")
for (row in contentContainers[0].rows()) {
val contentContainers = table.select(TABLE_SELECTOR)
contentContainers[0].rows().forEach { row ->
val (field, value) = getAuctionTableFieldValue(row)
when (field) {
"Hit Points" -> hitPoints = value.parseInteger()
Expand All @@ -236,7 +245,7 @@ public object AuctionParser : Parser<Auction?> {
}
}
val skillsMap = mutableMapOf<String, Double>().withDefault { 0.0 }
for (row in contentContainers[1].rows()) {
contentContainers[1].rows().forEach { row ->
val (name, level, progress) = row.cellsText()
skillsMap[name] = level.parseInteger() + (progress.remove("%").toDouble() / PERCENTAGE)
}
Expand All @@ -250,7 +259,7 @@ public object AuctionParser : Parser<Auction?> {
fistFighting = skillsMap.getValue("Fist Fighting"),
shielding = skillsMap.getValue("Shielding"),
)
for (row in contentContainers[2].rows()) {
contentContainers[2].rows().forEach { row ->
val (field, value) = getAuctionTableFieldValue(row)
when (field) {
"Creation Date" -> creationDate = parseTibiaDateTime(value)
Expand All @@ -265,7 +274,7 @@ public object AuctionParser : Parser<Auction?> {
}
}

for (row in contentContainers[4].rows()) {
contentContainers[4].rows().forEach { row ->
val (field, value) = getAuctionTableFieldValue(row)
when (field) {
"Charm Expansion" -> hasCharmExpansion = value.contains("yes")
Expand All @@ -274,7 +283,7 @@ public object AuctionParser : Parser<Auction?> {
}
}
dailyRewardStreak = contentContainers[5].selectFirst("div")?.cleanText()?.parseInteger() ?: 0
for (row in contentContainers[6].rows()) {
contentContainers[6].rows().forEach { row ->
val (field, value) = getAuctionTableFieldValue(row)
when (field) {
"Hunting Task Points" -> huntingTaskPoints = value.parseInteger()
Expand All @@ -284,7 +293,7 @@ public object AuctionParser : Parser<Auction?> {
}
}

for (row in contentContainers[7].rows()) {
contentContainers[7].rows().forEach { row ->
val (field, value) = getAuctionTableFieldValue(row)
when (field) {
"Hirelings" -> hirelings = value.parseInteger()
Expand All @@ -293,7 +302,7 @@ public object AuctionParser : Parser<Auction?> {
}
}

for (row in contentContainers.getOrNull(8)?.rows().orEmpty()) {
contentContainers.getOrNull(8)?.rows().orEmpty().forEach { row ->
val (field, value) = getAuctionTableFieldValue(row)
when (field) {
"Exalted Dust" -> {
Expand All @@ -304,13 +313,13 @@ public object AuctionParser : Parser<Auction?> {
}
}

for (row in contentContainers.getOrNull(9)?.rows().orEmpty()) {
contentContainers.getOrNull(9)?.rows().orEmpty().forEach { row ->
val (field, value) = getAuctionTableFieldValue(row)
when (field) {
"Boss Points" -> bossPoints = value.parseInteger()
}
}
for (row in contentContainers.getOrNull(10)?.rows().orEmpty()) {
contentContainers.getOrNull(10)?.rows().orEmpty().forEach { row ->
val (field, value) = getAuctionTableFieldValue(row)
when (field) {
"Bonus Promotion Points" -> bonusPromotionPoints = value.parseInteger()
Expand Down Expand Up @@ -422,7 +431,7 @@ public object AuctionParser : Parser<Auction?> {

public inline fun <reified T> parsePageItems(content: String): List<T> {
val document = Jsoup.parse(content)
val cvIcon = document.select("div.CVIcon")
val cvIcon = document.select(ICON_CSS_SELECTOR)
return when (T::class) {
ItemEntry::class -> cvIcon.mapNotNull { parseDisplayedItem(it) as T }
OutfitEntry::class -> cvIcon.mapNotNull { parseDisplayedOutfit(it) as T }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2022 Allan Galarza
* Copyright © 2024 Allan Galarza
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@ import com.galarzaa.tibiakt.core.enums.ThreadStatus
import com.galarzaa.tibiakt.core.exceptions.ParsingException
import com.galarzaa.tibiakt.core.models.forums.ForumBoard
import com.galarzaa.tibiakt.core.models.forums.ForumEmoticon
import com.galarzaa.tibiakt.core.utils.TABLE_SELECTOR
import com.galarzaa.tibiakt.core.utils.cells
import com.galarzaa.tibiakt.core.utils.cleanText
import com.galarzaa.tibiakt.core.utils.formData
Expand All @@ -35,7 +36,7 @@ import org.jsoup.nodes.Element

/** Parser for forum boards. */
public object ForumBoardParser : Parser<ForumBoard?> {
private val fileNameRegex = Regex("""([\w_]+.gif)""")
private val fileNameRegex = Regex("""(\w+.gif)""")
override fun fromContent(content: String): ForumBoard? {
val boxContent = boxContent(content, org.jsoup.parser.Parser.xmlParser())
val forumBreadcrumbs = boxContent.selectFirst("div.ForumBreadCrumbs")
Expand All @@ -58,7 +59,7 @@ public object ForumBoardParser : Parser<ForumBoard?> {
threadAge = form.values["threadage"]?.toInt() ?: return@forumBoard

val table = boxContent.selectFirst("table.Table3") ?: throw ParsingException("No board tables found.")
val contentTables = table.select("table.TableContent")
val contentTables = table.select(TABLE_SELECTOR)
if (contentTables.size >= 2) parseAnnouncements(contentTables.first()!!)
parseThreadsTable(contentTables.last()!!)
val paginationData = boxContent.selectFirst("td > small")!!.parsePagination()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2022 Allan Galarza
* Copyright © 2024 Allan Galarza
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ package com.galarzaa.tibiakt.core.parsers
import com.galarzaa.tibiakt.core.builders.forumsSection
import com.galarzaa.tibiakt.core.exceptions.ParsingException
import com.galarzaa.tibiakt.core.models.forums.ForumSection
import com.galarzaa.tibiakt.core.utils.TABLE_SELECTOR
import com.galarzaa.tibiakt.core.utils.cells
import com.galarzaa.tibiakt.core.utils.cleanText
import com.galarzaa.tibiakt.core.utils.getLinkInformation
Expand All @@ -38,7 +39,7 @@ public object ForumSectionParser : Parser<ForumSection> {
if ("Boards" !in tables) throw ParsingException("Boards table not found")

return forumsSection {
val boardRows = tables["Boards"]!!.selectFirst("table.TableContent")?.select("tr:not(.LabelH)")!!
val boardRows = tables["Boards"]!!.selectFirst(TABLE_SELECTOR)?.select("tr:not(.LabelH)")!!
for (row in boardRows) {
val columns = row.cells()
if (columns.size != 6) continue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2022 Allan Galarza
* Copyright © 2024 Allan Galarza
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@ import com.galarzaa.tibiakt.core.builders.forumThread
import com.galarzaa.tibiakt.core.exceptions.ParsingException
import com.galarzaa.tibiakt.core.models.forums.ForumEmoticon
import com.galarzaa.tibiakt.core.models.forums.ForumThread
import com.galarzaa.tibiakt.core.utils.TABLE_SELECTOR
import com.galarzaa.tibiakt.core.utils.cleanText
import com.galarzaa.tibiakt.core.utils.getLinkInformation
import com.galarzaa.tibiakt.core.utils.parseAuthorTable
Expand Down Expand Up @@ -67,7 +68,7 @@ public object ForumThreadParser : Parser<ForumThread?> {
hasGoldenFrame = "gold" in border.attr("style")
title = forumTitleContainer.cleanText()

val postTable = boxContent.selectFirst("table.TableContent")!!
val postTable = boxContent.selectFirst(TABLE_SELECTOR)!!
val threadInfoContainer = postTable.selectFirst("div.ForumPostHeaderText")!!
val (threadNumber, navigationContainer) = threadInfoContainer.childNodes()

Expand Down Expand Up @@ -108,12 +109,11 @@ public object ForumThreadParser : Parser<ForumThread?> {
while (true) {
val child = contentContainer.child(0)
child.remove()
if (child.tagName() == "img")
emoticonTag = child
if (child.tagName() == "b")
titleTag = child
if (child.tagName() == "div")
break
when (child.tagName()) {
"img" -> emoticonTag = child
"b" -> titleTag = child
"div" -> break
}
}
contentContainer.selectFirst("br")?.remove()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.galarzaa.tibiakt.core.parsers
import com.galarzaa.tibiakt.core.builders.guildsSection
import com.galarzaa.tibiakt.core.exceptions.ParsingException
import com.galarzaa.tibiakt.core.models.guild.GuildsSection
import com.galarzaa.tibiakt.core.utils.TABLE_SELECTOR
import com.galarzaa.tibiakt.core.utils.boxContent
import com.galarzaa.tibiakt.core.utils.cleanText
import com.galarzaa.tibiakt.core.utils.offsetStart
Expand All @@ -30,7 +31,7 @@ public object GuildsSectionParser : Parser<GuildsSection?> {
override fun fromContent(content: String): GuildsSection? {
val document: Document = Jsoup.parse(content, "")
val boxContent = document.boxContent()
val tables = boxContent.select("table.TableContent")
val tables = boxContent.select(TABLE_SELECTOR)
return guildsSection {
val selectedWorld = boxContent.selectFirst("select[name=world]")?.selectFirst("option[selected]")
?: throw ParsingException("Could not find selected world")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 Allan Galarza
* Copyright © 2024 Allan Galarza
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@ import com.galarzaa.tibiakt.core.enums.StringEnum
import com.galarzaa.tibiakt.core.exceptions.ParsingException
import com.galarzaa.tibiakt.core.models.highscores.Highscores
import com.galarzaa.tibiakt.core.utils.FormData
import com.galarzaa.tibiakt.core.utils.TABLE_SELECTOR
import com.galarzaa.tibiakt.core.utils.cellsText
import com.galarzaa.tibiakt.core.utils.cleanText
import com.galarzaa.tibiakt.core.utils.formData
Expand Down Expand Up @@ -91,7 +92,7 @@ public object HighscoresParser : Parser<Highscores?> {
}

private fun HighscoresBuilder.parseHighscoresTable(element: Element) {
val entriesTable = element.selectFirst("table.TableContent")
val entriesTable = element.selectFirst(TABLE_SELECTOR)
for (row in entriesTable.rows().offsetStart(1)) {
val columns = row.cellsText()
if (columns.size < 6) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2022 Allan Galarza
* Copyright © 2024 Allan Galarza
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@ import com.galarzaa.tibiakt.core.builders.KillStatisticsBuilder
import com.galarzaa.tibiakt.core.builders.killStatistics
import com.galarzaa.tibiakt.core.exceptions.ParsingException
import com.galarzaa.tibiakt.core.models.KillStatistics
import com.galarzaa.tibiakt.core.utils.TABLE_SELECTOR
import com.galarzaa.tibiakt.core.utils.cellsText
import com.galarzaa.tibiakt.core.utils.formData
import com.galarzaa.tibiakt.core.utils.offsetStart
Expand All @@ -46,7 +47,7 @@ public object KillStatisticsParser : Parser<KillStatistics?> {
}

private fun KillStatisticsBuilder.parseKillStatisticsTable(table: Element) {
val innerTable = table.selectFirst("table.TableContent")
val innerTable = table.selectFirst(TABLE_SELECTOR)
for (row in innerTable.rows().offsetStart(2)) {
val columnns = row.cellsText()
if (columnns[0] == "Total") {
Expand Down
Loading

0 comments on commit 09072e5

Please sign in to comment.