An amazing project that generates micro reports from tournament results
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pa-tournament-stats/TournamentStats/report/structures/PlayerNotableFinishes.swift

185 lines
6.2 KiB

//
// PlayerNotableFinishes.swift
// TournamentStats
//
// Created by Laurent Morvillier on 28/08/2019.
// Copyright © 2019 Stax River. All rights reserved.
//
import Foundation
import UIKit
struct PlayerNotableFinishes {
var player: Player
var wins: [Result] = []
var runnerUps: [Result] = []
var finalTables: [Result] = []
}
class PlayerNotableFinishesDataSource : NSObject, UITableViewDataSource, UITableViewDelegate {
let notableFinishes: [PlayerNotableFinishes]
var mostWins: [PlayerNotableFinishes] = []
var mostWinsCount: Int = 0
var mostRunnerUps: [PlayerNotableFinishes] = []
var mostRunnerUpsCount: Int = 0
var mostFinalTables: [PlayerNotableFinishes] = []
var mostFinalTablesCount: Int = 0
var winsDisplay: [(String, UIFont)] = []
var runnerUpsDisplay: [(String, UIFont)] = []
let finalTables: Bool
init(notableFinishes: [PlayerNotableFinishes], finalTables: Bool) {
self.notableFinishes = notableFinishes
self.finalTables = finalTables
if finalTables {
var nFinalTables = notableFinishes
nFinalTables.sort { (p1, p2) -> Bool in
return p1.finalTables.count > p2.finalTables.count
}
let finalTablesTopCount = nFinalTables.first?.finalTables.count ?? 0
self.mostFinalTables = nFinalTables.filter { $0.finalTables.count == finalTablesTopCount }
self.mostFinalTablesCount = finalTablesTopCount
} else {
var nWins = notableFinishes
nWins.sort { (p1, p2) -> Bool in
return p1.wins.count > p2.wins.count
}
let winsTopCount = nWins.first?.wins.count ?? 0
self.mostWins = nWins.filter { $0.wins.count == winsTopCount}
for win in self.mostWins {
self.winsDisplay.append((win.player.formattedName, Fonts.subTitle))
for result in win.wins {
if let tournamentName = result.tournaments.first?.formatted {
self.winsDisplay.append((" " + tournamentName, Fonts.regular))
} else {
self.winsDisplay.append(("no tournament!!!", Fonts.regular))
}
}
}
///////
var nRunnerups = notableFinishes
nRunnerups.sort { (p1, p2) -> Bool in
return p1.runnerUps.count > p2.runnerUps.count
}
let runnerUpsTopCount = nRunnerups.first?.runnerUps.count ?? 0
self.mostRunnerUps = nRunnerups.filter { $0.runnerUps.count == runnerUpsTopCount }
for runnerup in self.mostRunnerUps {
self.runnerUpsDisplay.append((runnerup.player.formattedName, Fonts.subTitle))
for result in runnerup.runnerUps {
if let tournamentName = result.tournaments.first?.formatted {
self.runnerUpsDisplay.append((" " + tournamentName, Fonts.regular))
} else {
self.runnerUpsDisplay.append(("no tournament!!!", Fonts.regular))
}
}
}
self.mostWinsCount = winsTopCount
self.mostRunnerUpsCount = runnerUpsTopCount
}
}
// MARK - Table
func numberOfSections(in tableView: UITableView) -> Int {
return self.finalTables ? 1 : 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.finalTables {
return self.mostFinalTables.count
} else {
switch section {
case 0:
return self.winsDisplay.count
case 1:
return self.runnerUpsDisplay.count
default:
return self.mostFinalTables.count
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = UIColor.clear
cell.textLabel?.textColor = UIColor.white
let font: UIFont
let text: String
if self.finalTables {
text = self.mostFinalTables[indexPath.row].player.formattedName
font = Fonts.regular
} else {
switch indexPath.section {
case 0:
text = self.winsDisplay[indexPath.row].0
font = self.winsDisplay[indexPath.row].1
case 1:
text = self.runnerUpsDisplay[indexPath.row].0
font = self.runnerUpsDisplay[indexPath.row].1
case 2:
text = self.mostFinalTables[indexPath.row].player.formattedName
font = Fonts.regular
default:
text = "problem"
font = Fonts.regular
}
}
cell.textLabel?.text = text
cell.textLabel?.font = font
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let text: String
if self.finalTables {
text = "Most final tables - \(self.mostFinalTablesCount)"
} else {
switch section {
case 0:
text = "Most wins - \(self.mostWinsCount) bracelets"
case 1:
text = "Most runner-ups - \(self.mostRunnerUpsCount)"
default:
text = "Most final tables - \(self.mostFinalTablesCount)"
}
}
return self.headerLabel(text: text)
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60.0
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40.0
}
func headerLabel(text: String) -> UILabel {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 500.0, height: 60.0))
label.textColor = UIColor.white
label.font = Fonts.subTitle
label.text = text
return label
}
}