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.

87 lines
2.7 KiB

//
// CountryCashes.swift
// TournamentStats
//
// Created by Laurent Morvillier on 09/06/2019.
// Copyright © 2019 Stax River. All rights reserved.
//
import Foundation
import Charts
final class CountryCounter : HTMLRepresentable, Aggregeable {
static func aggregate(table: [CountryCounter], param: String) -> [CountryCounter] {
if let indexOfParam = table.firstIndex(where: { $0.country == param }) {
let paramCounter = table[indexOfParam]
var copy = table
copy.remove(at: indexOfParam)
let count = copy.reduce(0) { $0 + $1.counter }
return [paramCounter, CountryCounter(country: "Others", counter: count)]
}
let count = table.reduce(0) { $0 + $1.counter }
return [CountryCounter(country: "All", counter: count)]
}
var country: String
var counter: Int
init(country: String, counter: Int) {
self.country = country
self.counter = counter
}
func increment() {
self.counter += 1
}
static func columnDescriptors() -> [ColumnDescriptor] {
return [ColumnDescriptor(header: "Country", number: false, widthWeight: 1.0),
ColumnDescriptor(header: "Cashes", number: true, widthWeight: 1.0)]
}
func cellValues() -> [String] {
let formattedCountry: String
if let flagoji = Locale.current.flagoji(from: self.country) {
formattedCountry = "\(flagoji) \(self.country)"
} else {
formattedCountry = self.country
}
var strings: [String] = []
strings.append(formattedCountry)
strings.append("\(counter)")
return strings
}
var pieChartDataEntry: PieChartDataEntry {
return PieChartDataEntry(value: Double(self.counter), label: self.country)
}
// static func htmlHeaders() -> String {
// var strings: [String] = []
// strings.append("Country")
// strings.append("Cashes")
// let all = strings.joined(separator: "</td><td>")
// return "<tr class=\"table-header\"><td>\(all)</td></tr>"
// }
//
// func html() -> String {
//
// let formattedCountry: String
// if let flagoji = Locale.current.flagoji(from: self.country) {
// formattedCountry = "\(flagoji) \(self.country)"
// } else {
// formattedCountry = self.country
// }
//
// var strings: [String] = []
// strings.append(formattedCountry)
// strings.append("\(counter)")
// let all = strings.joined(separator: "</td><td>")
// return "<tr><td>\(all)</td></tr>"
// }
}