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.
181 lines
6.0 KiB
181 lines
6.0 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)] = []
|
|
|
|
init(notableFinishes: [PlayerNotableFinishes]) {
|
|
self.notableFinishes = notableFinishes
|
|
|
|
var nFinalTables = notableFinishes
|
|
nFinalTables.sort { (p1, p2) -> Bool in
|
|
return p1.finalTables.count > p2.finalTables.count
|
|
}
|
|
self.mostFinalTables = nFinalTables
|
|
|
|
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.bold))
|
|
let sortedWins = win.wins.sorted(by: { $0.tournaments.first!.number < $1.tournaments.first!.number })
|
|
for result in sortedWins {
|
|
if let tournamentName = result.tournaments.first?.formatted {
|
|
self.winsDisplay.append((" " + tournamentName, Fonts.small))
|
|
} else {
|
|
self.winsDisplay.append(("no tournament!!!", Fonts.small))
|
|
}
|
|
}
|
|
}
|
|
|
|
///////
|
|
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.bold))
|
|
let sortedRunnerUps = runnerup.runnerUps.sorted(by: { $0.tournaments.first!.number < $1.tournaments.first!.number })
|
|
for result in sortedRunnerUps {
|
|
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
|
|
|
|
}
|
|
|
|
func finalTablesCumulated() -> [CumulatedResults] {
|
|
var cumulatedResults: [CumulatedResults] = []
|
|
for finalTablist in self.mostFinalTables {
|
|
let cr = CumulatedResults(player: finalTablist.player, results: finalTablist.finalTables)
|
|
cumulatedResults.append(cr)
|
|
}
|
|
cumulatedResults.sort(by: { (c1, c2) -> Bool in
|
|
if c1.numberOfCashes == c2.numberOfCashes {
|
|
return c1.total > c2.total
|
|
}
|
|
return c1.numberOfCashes > c2.numberOfCashes
|
|
})
|
|
|
|
return cumulatedResults
|
|
}
|
|
|
|
// MARK: - Table
|
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
|
return 1
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
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 = Fonts.color
|
|
let font: UIFont
|
|
let text: String
|
|
|
|
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
|
|
switch section {
|
|
case 0:
|
|
text = "Most bracelets"
|
|
case 1:
|
|
text = "Most runner-ups"
|
|
default:
|
|
text = "Most final tables"
|
|
}
|
|
return self.headerLabel(text: text)
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
return 32.0
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
return 60.0
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
|
|
return 30.0
|
|
}
|
|
|
|
func headerLabel(text: String) -> UILabel {
|
|
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 500.0, height: 60.0))
|
|
label.textColor = Fonts.titleColor
|
|
label.font = Fonts.title
|
|
label.text = text
|
|
return label
|
|
}
|
|
|
|
}
|
|
|