Skip to content

Commit e158afa

Browse files
committed
add sum-feature for cross competition scores
1 parent cc318c8 commit e158afa

File tree

8 files changed

+117
-74
lines changed

8 files changed

+117
-74
lines changed

src/main/scala/ch/seidel/kutu/data/GroupBy.scala

+13-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ sealed trait GroupBy {
2727
val groupname: String
2828
protected var next: Option[GroupBy] = None
2929
protected var isANO: Boolean = false
30+
protected var isAVG: Boolean = true
3031
protected var kind: ScoreListKind = Einzelrangliste
3132

3233
protected def allName = groupname
@@ -53,6 +54,15 @@ sealed trait GroupBy {
5354
}
5455
}
5556

57+
def isAvgOnMultipleCompetitions = isAVG
58+
59+
def setAvgOnMultipleCompetitions(value: Boolean): Unit = {
60+
traverse(value) { (gb, acc) =>
61+
gb.isAVG = acc
62+
acc
63+
}
64+
}
65+
5666
def toRestQuery: String = {
5767
val groupby = traverse("") { (gb, acc) =>
5868
acc match {
@@ -62,7 +72,7 @@ sealed trait GroupBy {
6272
acc + "," + gb.groupname
6373
}
6474
}
65-
s"groupby=${groupby}" + (if (isANO) "&alphanumeric" else "") + s"&kind=${kind}"
75+
s"groupby=${groupby}" + (if (isANO) "&alphanumeric" else "") + (if (isAVG) "&avg=true" else "&avg=false") + s"&kind=${kind}"
6676
}
6777

6878
def chainToString: String = s"$groupname (skipGrouper: $skipGrouper, $allName)" + (next match {
@@ -108,6 +118,7 @@ sealed trait GroupBy {
108118

109119
def reset: Unit = {
110120
setAlphanumericOrdered(false)
121+
setAvgOnMultipleCompetitions(true)
111122
next = None
112123
}
113124

@@ -195,7 +206,7 @@ sealed trait FilterBy extends GroupBy {
195206
}
196207
)
197208
}
198-
s"groupby=$groupby${filter.mkString}" + (if (isANO) "&alphanumeric" else "") + s"&kind=${kind}"
209+
s"groupby=$groupby${filter.mkString}" + (if (isANO) "&alphanumeric" else "") + (if (isAVG) "&avg=true" else "&avg=false") + s"&kind=${kind}"
199210
}
200211

201212
private[FilterBy] var filter: Set[DataObject] = Set.empty

src/main/scala/ch/seidel/kutu/data/GroupSection.scala

+25-19
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
8686
//val divider = if(!isDivided) 1 else groups.head._2.size
8787

8888
val gleichstandsregel = Gleichstandsregel(list.head.wettkampf)
89-
def buildColumns: List[WKCol] = {
89+
def buildColumns(isAvgOnMultipleCompetitions: Boolean = true): List[WKCol] = {
9090
val athletCols: List[WKCol] = List(
9191
WKLeafCol[ResultRow](text = "Rang", prefWidth = 20, styleClass = Seq("data"), valueMapper = gr => {
9292
if (isTeamGroup) {
@@ -126,11 +126,11 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
126126
}
127127
)
128128

129-
val (disziplinCol: List[WKCol], sumCol: List[WKCol]) = buildCommonColumns
129+
val (disziplinCol: List[WKCol], sumCol: List[WKCol]) = buildCommonColumns(isAvgOnMultipleCompetitions)
130130
athletCols ++ disziplinCol ++ sumCol
131131
}
132132

133-
def buildCommonColumns: (List[WKCol], List[WKCol]) = {
133+
def buildCommonColumns(isAvgOnMultipleCompetitions: Boolean): (List[WKCol], List[WKCol]) = {
134134
val indexer = Iterator.from(0)
135135
val disziplinCol: List[WKCol] =
136136
if (!isTeamGroup && groups.keySet.size > 1) {
@@ -170,7 +170,7 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
170170
cs.rang.formattedEnd
171171
})
172172
val cl: WKCol = WKGroupCol(
173-
text = if (anzahWettkaempfe > 1) {
173+
text = if (isAvgOnMultipleCompetitions && anzahWettkaempfe > 1) {
174174
s"ø aus " + grKey.easyprint
175175
}
176176
else {
@@ -227,7 +227,7 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
227227
lazy val clRang = WKLeafCol[ResultRow](text = "Rang", prefWidth = 60, styleClass = Seq("hintdata"), valueMapper = gr => {
228228
if (gr.resultate.size > index) f"${gr.resultate(index).rang.endnote}%3.0f" else ""
229229
})
230-
val cl = WKGroupCol(text = if (anzahWettkaempfe > 1) {
230+
val cl = WKGroupCol(text = if (isAvgOnMultipleCompetitions && anzahWettkaempfe > 1) {
231231
s"ø aus " + disziplin.name
232232
}
233233
else {
@@ -246,7 +246,7 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
246246
}
247247
val sumColAll: List[WKCol] = List(
248248
WKLeafCol[ResultRow](
249-
text = if (anzahWettkaempfe > 1) {
249+
text = if (isAvgOnMultipleCompetitions && anzahWettkaempfe > 1) {
250250
s"Total ø aus $dNoteLabel"
251251
}
252252
else {
@@ -257,7 +257,7 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
257257
}
258258
),
259259
WKLeafCol[ResultRow](
260-
text = if (anzahWettkaempfe > 1) {
260+
text = if (isAvgOnMultipleCompetitions && anzahWettkaempfe > 1) {
261261
if (!isDivided && withDNotes) {
262262
s"Total ø aus $eNoteLabel"
263263
}
@@ -290,7 +290,7 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
290290
}
291291
),
292292
WKLeafCol[ResultRow](
293-
text = if (anzahWettkaempfe > 1) {
293+
text = if (isAvgOnMultipleCompetitions && anzahWettkaempfe > 1) {
294294
s"Total ø Punkte"
295295
}
296296
else {
@@ -310,7 +310,7 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
310310
GroupSection.mapAvgRang(grp.map { d => (d._1, d._2._1, d._2._2) }).map(r => (r.groupKey.asInstanceOf[A] -> r)).toMap
311311
}
312312

313-
def mapToAvgRowSummary(athlWertungen: Iterable[WertungView] = list): (Resultat, Resultat, Iterable[(Disziplin, Long, Resultat, Resultat, Option[Int], Option[BigDecimal])], Iterable[(ProgrammView, Resultat, Resultat, Option[Int], Option[BigDecimal])], Resultat) = {
313+
def mapToAvgRowSummary(athlWertungen: Iterable[WertungView] = list, avgSumsWithMultiCompetitions: Boolean): (Resultat, Resultat, Iterable[(Disziplin, Long, Resultat, Resultat, Option[Int], Option[BigDecimal])], Iterable[(ProgrammView, Resultat, Resultat, Option[Int], Option[BigDecimal])], Resultat) = {
314314
val wks = athlWertungen.filter(_.endnote.nonEmpty).groupBy { w => w.wettkampf }
315315
val wksums = wks.map { wk => aggreateFun(wk._2.map(w => w.resultat)) }.toList
316316
val rsum = aggreateFun(wksums)
@@ -324,8 +324,9 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
324324
}) * aggreateFun.sortFactor
325325
}
326326
val gsum = aggreateFun(gwksums) //if (gwksums.nonEmpty) gwksums.reduce(_ + _) else Resultat(0, 0, 0)
327-
val avg = if (wksums.nonEmpty) rsum / wksums.size else Resultat(0, 0, 0)
328-
val gavg = if (wksums.nonEmpty) gsum / wksums.size else Resultat(0, 0, 0)
327+
val wkDivider = if(avgSumsWithMultiCompetitions) wksums.size else 1
328+
val avg = if (wksums.nonEmpty) rsum / wkDivider else Resultat(0, 0, 0)
329+
val gavg = if (wksums.nonEmpty) gsum / wkDivider else Resultat(0, 0, 0)
329330
val withAuszeichnung = anzahWettkaempfe == 1 && groups.size == 1 && wks.size == 1
330331
val auszeichnung = if (withAuszeichnung) Some(wks.head._1.auszeichnung) else None
331332
val auszeichnungEndnote = if (withAuszeichnung && wks.head._1.auszeichnungendnote > 0) Some(wks.head._1.auszeichnungendnote) else None
@@ -341,7 +342,8 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
341342
}).groupBy(_._1).map { x =>
342343
val xsum = x._2.map(_._2).reduce(_ + _)
343344
val xasum = aggreateFun(x._2.map(_._2))
344-
(x._1, xasum, xsum / x._2.size, auszeichnung, auszeichnungEndnote)
345+
val diszDivider = if(avgSumsWithMultiCompetitions) x._2.size else 1
346+
(x._1, xasum, xsum / diszDivider, auszeichnung, auszeichnungEndnote)
345347
}
346348
.toList.sortBy(d => d._1._1)
347349
.map(d => (d._1._2, d._1._3, d._2, d._3, d._4, d._5))
@@ -359,17 +361,18 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
359361
}).groupBy(_._1).map { x =>
360362
val xsum = x._2.map(_._2).reduce(_ + _)
361363
val xasum = aggreateFun(x._2.map(_._2))
362-
(x._1, xasum, xsum / x._2.size, auszeichnung, auszeichnungEndnote)
364+
val pgmDivider = if(avgSumsWithMultiCompetitions) x._2.size else 1
365+
(x._1, xasum, xsum / pgmDivider, auszeichnung, auszeichnungEndnote)
363366
}
364367
.toList.sortBy(d => d._1.ord)
365368
(rsum, avg, perDisziplinAvgs, perProgrammAvgs, gavg)
366369
}
367370

368-
def getTableData(sortAlphabetically: Boolean = false) = {
371+
def getTableData(sortAlphabetically: Boolean = false, avgSumsWithMultiCompetitions: Boolean = true) = {
369372
val avgPerAthlet = list.groupBy { x =>
370373
x.athlet
371374
}.map { x =>
372-
(x._1, mapToAvgRowSummary(x._2), x._2.head.wettkampfdisziplin.programm)
375+
(x._1, mapToAvgRowSummary(x._2, avgSumsWithMultiCompetitions), x._2.head.wettkampfdisziplin.programm)
373376
}.filter(_._2._1.endnote > 0)
374377

375378
// Beeinflusst die Total-Rangierung pro Gruppierung
@@ -408,7 +411,7 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
408411
//team.perDisciplinResults(w._1).indexOf(ww.avg) > -1)
409412
team.isRelevantResult(w._1, ww.groupKey.asInstanceOf[AthletView]))
410413
}
411-
else if(anzahWettkaempfe > 1) {
414+
else if(avgSumsWithMultiCompetitions && anzahWettkaempfe > 1) {
412415
LeafRow(w._1.name,
413416
ww.avg,
414417
rang,
@@ -434,7 +437,7 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
434437
programmResults.map{w =>
435438
val ww = avgProgrammRangMap(w._1)(athlet)
436439
// val posproz = 100d * ww.rang.endnote / avgProgrammRangMap.size
437-
if(anzahWettkaempfe > 1) {
440+
if(avgSumsWithMultiCompetitions && anzahWettkaempfe > 1) {
438441
LeafRow(w._1.easyprint,
439442
ww.avg,
440443
ww.rang,
@@ -481,8 +484,11 @@ case class GroupLeaf[GK <: DataObject](override val groupKey: GK, list: Iterable
481484
if(sortAlphabetically) {
482485
prepared.sortBy(_.athlet.easyprint)
483486
}
487+
// else if(avgSumsWithMultiCompetitions) {
488+
// }
484489
else{
485490
prepared.sortBy(_.rang.endnote)
491+
//prepared.sortBy(_.sum.endnote).reverse
486492
}
487493
}
488494
}
@@ -545,7 +551,7 @@ case class TeamSums(override val groupKey: DataObject, teamRows: List[GroupLeaf[
545551
""
546552
})
547553
)
548-
val (disziplinCol: List[WKCol], sumCol: List[WKCol]) = glGroup.buildCommonColumns
554+
val (disziplinCol: List[WKCol], sumCol: List[WKCol]) = glGroup.buildCommonColumns(true)
549555
teamCols ++ disziplinCol ++ sumCol
550556
}
551557

@@ -555,7 +561,7 @@ case class TeamSums(override val groupKey: DataObject, teamRows: List[GroupLeaf[
555561
}.map { x =>
556562
val (team, teamRow) = x
557563
val teamwertungen = team.countingWertungen.flatMap(_._2)
558-
(team, teamRow.mapToAvgRowSummary(teamwertungen))
564+
(team, teamRow.mapToAvgRowSummary(teamwertungen, avgSumsWithMultiCompetitions = true))
559565
}
560566

561567
(for (teamRuleGroup <- avgPerTeams.groupBy(_._1.rulename)) yield {

src/main/scala/ch/seidel/kutu/domain/package.scala

+1
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,7 @@ package object domain {
10341034
override def easyprint = f"PublishedScore($title - ${wettkampf.easyprint})"
10351035

10361036
def isAlphanumericOrdered = query.contains("&alphanumeric")
1037+
def isAvgOnMultipleCompetitions = query.contains("&avg=true")
10371038

10381039
def toRaw = PublishedScoreRaw(id, title, query, published, publishedDate, wettkampf.id)
10391040
}

src/main/scala/ch/seidel/kutu/http/ScoreRoutes.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ ScoreRoutes extends SprayJsonSupport with JsonSupport with AuthSupport with Rout
4545

4646
if (html) {
4747
HttpEntity(ContentTypes.`text/html(UTF-8)`, new ScoreToHtmlRenderer(){override val title: String = wettkampf}
48-
.toHTML(query.select(data).toList, athletsPerPage = 0, sortAlphabetically = alphanumeric, logofile))
48+
.toHTML(query.select(data).toList, athletsPerPage = 0, sortAlphabetically = alphanumeric, isAvgOnMultipleCompetitions = true, logofile))
4949
} else {
5050
HttpEntity(ContentTypes.`application/json`, ScoreToJsonRenderer
51-
.toJson(wettkampf, query.select(data).toList, sortAlphabetically = alphanumeric, logofile))
51+
.toJson(wettkampf, query.select(data).toList, sortAlphabetically = alphanumeric, isAvgOnMultipleCompetitions = true, logofile))
5252
}
5353
}
5454

@@ -277,10 +277,10 @@ ScoreRoutes extends SprayJsonSupport with JsonSupport with AuthSupport with Rout
277277
HttpEntity(ContentTypes.`text/html(UTF-8)`, new ScoreToHtmlRenderer() {
278278
override val title: String = wettkampf.easyprint // + " - " + score.map(_.title).getOrElse(wettkampf.easyprint)
279279
}
280-
.toHTML(query.select(publishedData).toList, athletsPerPage = 0, sortAlphabetically = score.exists(_.isAlphanumericOrdered), logofile))
280+
.toHTML(query.select(publishedData).toList, athletsPerPage = 0, sortAlphabetically = score.exists(_.isAlphanumericOrdered), isAvgOnMultipleCompetitions = score.exists(_.isAvgOnMultipleCompetitions), logofile))
281281
} else {
282282
HttpEntity(ContentTypes.`application/json`, ScoreToJsonRenderer
283-
.toJson(wettkampf.easyprint, query.select(publishedData).toList, sortAlphabetically = score.exists(_.isAlphanumericOrdered), logofile))
283+
.toJson(wettkampf.easyprint, query.select(publishedData).toList, sortAlphabetically = score.exists(_.isAlphanumericOrdered), isAvgOnMultipleCompetitions = score.exists(_.isAvgOnMultipleCompetitions), logofile))
284284
}
285285
}
286286
}

src/main/scala/ch/seidel/kutu/renderer/ScoreToHtmlRenderer.scala

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ trait ScoreToHtmlRenderer {
1313

1414
protected val title: String
1515

16-
def toHTML(gs: List[GroupSection], athletsPerPage: Int = 0, sortAlphabetically: Boolean = false, logoFile: File): String = {
17-
toHTML(gs, "", 0, athletsPerPage, sortAlphabetically, collectFilterTitles(gs, true), logoFile)
16+
def toHTML(gs: List[GroupSection], athletsPerPage: Int = 0, sortAlphabetically: Boolean = false, isAvgOnMultipleCompetitions: Boolean = true, logoFile: File): String = {
17+
toHTML(gs, "", 0, athletsPerPage, sortAlphabetically, isAvgOnMultipleCompetitions, collectFilterTitles(gs, true), logoFile)
1818
}
1919

2020
val intro = s"""<html lang="de-CH"><head>
@@ -199,7 +199,7 @@ trait ScoreToHtmlRenderer {
199199
}
200200
}
201201

202-
private def toHTML(gs: List[GroupSection], openedTitle: String, level: Int, athletsPerPage: Int, sortAlphabetically: Boolean, falsePositives: Set[String], logoFile: File): String = {
202+
private def toHTML(gs: List[GroupSection], openedTitle: String, level: Int, athletsPerPage: Int, sortAlphabetically: Boolean, isAvgOnMultipleCompetitions: Boolean, falsePositives: Set[String], logoFile: File): String = {
203203
val gsBlock = new StringBuilder()
204204

205205
if (level == 0) {
@@ -215,7 +215,7 @@ trait ScoreToHtmlRenderer {
215215
val levelText = if ((gsSize == 1 && !falsePositives.contains(escaped(c.groupKey.capsulatedprint))) || c.groupKey.isInstanceOf[NullObject]) "" else escaped(c.groupKey.capsulatedprint)
216216
c match {
217217
case gl: GroupLeaf[_] =>
218-
renderGroupLeaf(openedTitle, level, athletsPerPage, sortAlphabetically, gsBlock, levelText, gl)
218+
renderGroupLeaf(openedTitle, level, athletsPerPage, sortAlphabetically, isAvgOnMultipleCompetitions, gsBlock, levelText, gl)
219219

220220
case ts: TeamSums =>
221221
renderTeamLeaf(openedTitle, level, athletsPerPage, gsBlock, levelText, ts)
@@ -226,7 +226,7 @@ trait ScoreToHtmlRenderer {
226226
openedTitle + s"${if (levelText.isEmpty) "" else (levelText + ", ")}"
227227
else
228228
s"<h${level + 2}>${if (levelText.isEmpty) "" else (levelText + ", ")}",
229-
level + 1, athletsPerPage, sortAlphabetically, falsePositives, logoFile))
229+
level + 1, athletsPerPage, sortAlphabetically, isAvgOnMultipleCompetitions, falsePositives, logoFile))
230230

231231
case s: GroupSection =>
232232
gsBlock.append(s.easyprint)
@@ -345,7 +345,7 @@ trait ScoreToHtmlRenderer {
345345
}
346346

347347
private def renderListEnd(gsBlock: StringBuilder) = gsBlock.append(s"</tbody></table></div>\n")
348-
private def renderGroupLeaf(openedTitle: String, level: Int, athletsPerPage: Int, sortAlphabetically: Boolean, gsBlock: StringBuilder, levelText: String, gl: GroupLeaf[_]): Unit = {
348+
private def renderGroupLeaf(openedTitle: String, level: Int, athletsPerPage: Int, sortAlphabetically: Boolean, isAvgOnMultipleCompetitions: Boolean = true, gsBlock: StringBuilder, levelText: String, gl: GroupLeaf[_]): Unit = {
349349
val pretitleprint = (openedTitle + levelText).trim.reverse.dropWhile(p => p.equals(',')).reverse
350350
if (openedTitle.startsWith("<h")) {
351351
val closetag = openedTitle.substring(0, openedTitle.indexOf(">") + 1).replace("<", "</")
@@ -354,9 +354,9 @@ trait ScoreToHtmlRenderer {
354354
else {
355355
gsBlock.append(s"<h${level + 2}>$pretitleprint</h${level + 2}>")
356356
}
357-
val cols = gl.buildColumns
357+
val cols = gl.buildColumns(isAvgOnMultipleCompetitions)
358358

359-
val alldata = gl.getTableData(sortAlphabetically)
359+
val alldata = gl.getTableData(sortAlphabetically, isAvgOnMultipleCompetitions)
360360
val pagedata = if (athletsPerPage == 0) alldata.sliding(alldata.size, alldata.size)
361361
else if (firstSiteRendered.get) {
362362
alldata.sliding(athletsPerPage, athletsPerPage)
@@ -452,7 +452,7 @@ trait ScoreToHtmlRenderer {
452452
def renderTeamRows(list: List[TeamRow]) = {
453453
list.foreach { row =>
454454
val teamGroupLeaf = gl.getTeamGroupLeaf(row.team)
455-
val teamGroupCols = teamGroupLeaf.buildColumns.tail//.dropRight(2)
455+
val teamGroupCols = teamGroupLeaf.buildColumns().tail//.dropRight(2)
456456
val allMemberdata = teamGroupLeaf.getTableData()
457457
var rowspans = if (allMemberdata.size % 2 == 0) {
458458
allMemberdata.size + 1

0 commit comments

Comments
 (0)