11package com.cornellappdev.score.util
22
3+ import android.util.Log
34import com.cornellappdev.score.model.GameData
45import com.cornellappdev.score.model.TeamBoxScore
56import com.cornellappdev.score.model.TeamScore
@@ -17,7 +18,7 @@ import com.cornellappdev.score.model.TeamScore
1718 * @return a pair where the first value is a list of parsed period scores and the second is the total score (or null if invalid)
1819 */
1920// TODO: ASK ABOUT OT. Other sports might be added.
20- fun convertScores (scoreList : List <String ?>? , sport : String ): Pair <List <Int >, Int?> {
21+ fun convertScores (scoreList : List <String ?>? , sport : String , result : String ): Pair <List <Int >, Int?> {
2122 if (scoreList == null || scoreList.size < 2 ) return Pair (emptyList(), null )
2223
2324 var scoresByPeriod = scoreList
@@ -31,7 +32,12 @@ fun convertScores(scoreList: List<String?>?, sport: String): Pair<List<Int>, Int
3132 }
3233
3334 if (sport.lowercase() == " baseball" ) {
34- scoresByPeriod = scoresByPeriod.take(9 )
35+ val scoreParsed = result.split(" (" )
36+ scoresByPeriod = if (scoreParsed.size > 1 ) {
37+ scoresByPeriod.take(6 )
38+ } else {
39+ scoresByPeriod.take(9 )
40+ }
3541 val totalScore = scoresByPeriod.sum()
3642 return Pair (scoresByPeriod, totalScore)
3743 }
@@ -56,14 +62,15 @@ fun toGameData(
5662 scoreBreakdown : List <List <String ?>? >? ,
5763 team1 : TeamBoxScore ,
5864 team2 : TeamBoxScore ,
59- sport : String
65+ sport : String ,
66+ result : String ,
6067): GameData {
6168 val (team1Scores, team1Total) = scoreBreakdown?.getOrNull(0 )?.let {
62- convertScores(it, sport)
69+ convertScores(it, sport, result )
6370 } ? : (emptyList<Int >() to null )
6471
6572 val (team2Scores, team2Total) = scoreBreakdown?.getOrNull(1 )?.let {
66- convertScores(it, sport)
73+ convertScores(it, sport, result )
6774 } ? : (emptyList<Int >() to null )
6875
6976 val team1Score =
@@ -90,11 +97,12 @@ fun parseResultScore(result: String?): Pair<Int, Int>? {
9097 if (parts.size != 2 ) return null
9198
9299 val scorePart = parts[1 ].split(" -" )
100+ val secondScorePartEdge = scorePart[1 ].split(" (" )
93101 if (scorePart.size != 2 ) return null
94102
95103 val homeScore = scorePart[0 ].toIntOrNull()
96- val oppScore = scorePart[ 1 ].toIntOrNull()
97-
104+ val oppScore = secondScorePartEdge[ 0 ].toIntOrNull()
105+ Log .d( " HIHI " , oppScore.toString())
98106 if (homeScore != null && oppScore != null ) {
99107 return Pair (homeScore, oppScore)
100108 } else {
0 commit comments