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.

125 lines
4.6 KiB

//
// TournamentStats.swift
// TournamentStats
//
// Created by Laurent Morvillier on 10/06/2019.
// Copyright © 2019 Stax River. All rights reserved.
//
import Foundation
import UIKit
class TournamentStats : NSObject, HTMLRepresentable, UITableViewDelegate, UITableViewDataSource {
var entries: Double
var buyin: Double
var prizepool: Double
var itmValue: Double
init(entries: Double, buyin: Double, prizepool: Double, itmValue: Double) {
self.entries = entries
self.buyin = buyin
self.prizepool = prizepool
self.itmValue = itmValue
super.init()
}
static func columnDescriptors() -> [ColumnDescriptor] {
return []
}
func cellValues() -> [String] {
return []
}
static func htmlHeaders() -> String {
return ""
}
func html() -> String {
var strings: [(String, String)] = []
strings.append(("Entries", NumberFormatter().string(from: NSNumber(value: self.entries))!))
strings.append(("Buy-in", Formatter.currency.string(from: NSNumber(value: self.buyin))!))
strings.append(("Prizepool", Formatter.currency.string(from: NSNumber(value: self.prizepool))!))
strings.append(("Earnings", Formatter.currency.string(from: NSNumber(value: self.itmValue))!))
var all = ""
strings.forEach { (tuple) in
let table = [tuple.0, tuple.1]
let joined = table.joined(separator: "</td><td>")
all.append("<tr><td>")
all.append(joined)
all.append("</td></tr>\n")
}
return all
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
fileprivate let FONTSIZE: CGFloat = 18.0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = UIColor.clear
var cells: (String, String)
switch indexPath.row {
case 0:
cells = ("Entries", NumberFormatter().string(from: NSNumber(value: self.entries))!)
case 1:
cells = ("Buy-in", Formatter.currency.string(from: NSNumber(value: self.buyin))!)
case 2:
cells = ("Prizepool", Formatter.currency.string(from: NSNumber(value: self.prizepool))!)
default:
cells = ("Earnings", Formatter.currency.string(from: NSNumber(value: self.itmValue))!)
}
var leftAnchor = cell.contentView.leftAnchor
for i in 0..<2 {
let labelContainer: UIView = UIView()
cell.contentView.addSubview(labelContainer)
labelContainer.translatesAutoresizingMaskIntoConstraints = false
labelContainer.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true
labelContainer.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor).isActive = true
let leftPadding: CGFloat = (i == 0) ? 0.0 : 0.0 //16.0
labelContainer.leftAnchor.constraint(equalTo: leftAnchor, constant: leftPadding).isActive = true
leftAnchor = labelContainer.rightAnchor
labelContainer.widthAnchor.constraint(equalTo: cell.contentView.widthAnchor, multiplier: 0.5).isActive = true
let label: UILabel = UILabel(frame: CGRect.zero)
label.textColor = UIColor.white
label.backgroundColor = UIColor.clear
label.text = (i == 0) ? cells.0 : cells.1
label.font = (i == 0) ? UIFont.boldSystemFont(ofSize: FONTSIZE) : UIFont.systemFont(ofSize: FONTSIZE)
label.textAlignment = (i == 1) ? .right : .left
labelContainer.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.topAnchor.constraint(equalTo: labelContainer.topAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: labelContainer.bottomAnchor).isActive = true
label.leftAnchor.constraint(equalTo: labelContainer.leftAnchor, constant: 8.0).isActive = true
label.rightAnchor.constraint(equalTo: labelContainer.rightAnchor, constant: -8.0).isActive = true
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 36.0
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.1
}
}