package be.kuleuven.howlongtobeat.hltb object HowLongToBeatResultParser { private val titleMatcher = """(.+) Hours""".toRegex() fun parse(html: String): List { val result = arrayListOf() val rows = html.split("\n") for(i in 0..rows.size - 1) { val matched = titleMatcher.find(rows[i]) if(matched != null) { val (title) = matched.destructured val hour = parseHoursFromRow(i, rows) result.add(Game(title, hour)) } } return result } private fun parseHoursFromRow(row: Int, rows: List): Double { var hour = -1.0 // two rows down, there should be a
6½ Hours
if (row + 3 <= rows.size) { val matchedHour = hourMatcher.find(rows[row + 3]) if (matchedHour != null) { hour = matchedHour.groupValues[1].replace("½", ".5").toDouble() } } return hour } }