Created
December 2, 2022 21:13
-
-
Save mossprescott/fb36ea104bfcf5f8fad0fe82ff93f064 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PlaygroundSupport | |
import SwiftUI | |
struct Team: Comparable { | |
var name: String | |
var symbol: String | |
static func < (lhs: Team, rhs: Team) -> Bool { | |
return lhs.name < rhs.name | |
} | |
} | |
struct Status: Comparable, Identifiable { | |
var team: Team | |
var points: Int | |
var gd: Int | |
var id: String { team.name } | |
static func < (lhs: Status, rhs: Status) -> Bool { | |
if lhs.points < rhs.points { | |
return true | |
} | |
else if lhs.points == rhs.points { | |
if lhs.gd < rhs.gd { | |
return true | |
} else if lhs.gd == rhs.gd { | |
return lhs.team.name < rhs.team.name | |
} | |
} | |
return false | |
} | |
} | |
struct ResultCell: View { | |
var table: [Status] | |
var body: some View { | |
let first = table[0] | |
let second = table[1] | |
let third = table[2] | |
return VStack(alignment: .leading) { | |
Text("\(first.team.symbol) \(first.points)") | |
if second.points > third.points { | |
Text("\(second.team.symbol) \(second.points)") | |
} | |
else { | |
Text("\(second.team.symbol) \(second.points) (\(second.gd))") | |
Text("\(third.team.symbol) \(third.points) (\(third.gd))") | |
} | |
} | |
.frame(width: 75, height: 75) | |
} | |
} | |
struct GroupWinners: View { | |
var competition: String | |
var group: String | |
var match1: (Status, Status) | |
var match2: (Status, Status) | |
var body: some View { | |
let win1 = result(match1, score: (1, 0)) | |
let draw1 = result(match1, score: (0, 0)) | |
let loss1 = result(match1, score: (0, 1)) | |
let win2 = result(match2, score: (1, 0)) | |
let draw2 = result(match2, score: (0, 0)) | |
let loss2 = result(match2, score: (0, 1)) | |
return VStack { | |
Text(competition).font(.title) | |
Text(group).font(.title2) | |
Spacer() | |
ZStack { | |
ZStack { | |
// G | |
span(x: 1, y: 1, width: 2, height: 2) | |
span(x: 1, y: 0, width: 2) | |
span(x: 0, y: 2, width: 3) | |
// // H | |
// span(x: 0, y: 1, width: 2, height: 2) | |
// span(x: 0, y: 0, height: 2) | |
// span(x: 1, y: 0, width: 2) | |
// span(x: 2, y: 1, height: 2) | |
} | |
.frame(width: 75*3, height: 75*3) | |
.position(x: 75 + 3*75/2, y: 30 + 3*75/2) // ??? | |
VStack(spacing: -1) { | |
HStack(spacing: -1) { | |
Spacer(minLength: 75) | |
header(team: match1.0.team) | |
.frame(height: 30) | |
Text("-") | |
.font(.title3) | |
.frame(width: 75, height: 30) | |
header(team: match1.1.team) | |
.frame(height: 30) | |
} | |
HStack(spacing: -1) { | |
header(team: match2.0.team) | |
ResultCell(table: table(win1, win2)) | |
ResultCell(table: table(draw1, win2)) | |
ResultCell(table: table(loss1, win2)) | |
} | |
HStack(spacing: -1) { | |
Text("-") | |
.font(.title3) | |
.frame(width: 75, height: 75) | |
ResultCell(table: table(win1, draw2)) | |
ResultCell(table: table(draw1, draw2)) | |
ResultCell(table: table(loss1, draw2)) | |
} | |
HStack(spacing: -1) { | |
header(team: match2.1.team) | |
ResultCell(table: table(win1, loss2)) | |
ResultCell(table: table(draw1, loss2)) | |
ResultCell(table: table(loss1, loss2)) | |
} | |
} | |
} | |
} | |
.padding() | |
.background(Color.white) | |
} | |
func header(team: Team) -> some View { | |
HStack { | |
Text(team.name) | |
Text(team.symbol) | |
} | |
.font(.title3) | |
.frame(width: 75, height: 75) | |
} | |
func result(_ statuses: (Status, Status), score: (Int, Int)) -> (Status, Status) { | |
func update(_ status: Status, gd: Int) -> Status { | |
let pts = gd > 0 ? 3 : gd == 0 ? 1 : 0 | |
return (Status(team: status.team, | |
points: status.points + pts, | |
gd: status.gd + gd)) | |
} | |
return ( | |
update(statuses.0, gd: score.0 - score.1), | |
update(statuses.1, gd: score.1 - score.0) | |
) | |
} | |
func table(_ pair1: (Status, Status), _ pair2: (Status, Status)) -> [Status] { | |
let ss: [Status] = [pair1.0, pair1.1, pair2.0, pair2.1] | |
return ss.sorted().reversed() | |
} | |
func span(x: Int, y: Int, width: Int = 1, height: Int = 1) -> some View { | |
let inset = 2 | |
let cellSize = 75 | |
return RoundedRectangle(cornerRadius: 5) | |
// .stroke(lineWidth: 1) | |
// .foregroundColor(.black) | |
.fill() | |
.foregroundColor(Color(red: 0.95, green: 0.95, blue: 0.95)) | |
.frame(width: CGFloat(width*cellSize - inset*2), | |
height: CGFloat(height*cellSize - inset*2)) | |
.position(x: CGFloat(x*cellSize + width*cellSize/2), | |
y: CGFloat(y*cellSize + height*cellSize/2)) | |
} | |
} | |
let pol = Status(team: Team(name: "Pol", symbol: "π΅π±"), points: 4, gd: +2) | |
let arg = Status(team: Team(name: "Arg", symbol: "π¦π·"), points: 3, gd: +1) | |
let ksa = Status(team: Team(name: "KSA", symbol: "πΈπ¦"), points: 3, gd: -1) | |
let mex = Status(team: Team(name: "Mex", symbol: "π²π½"), points: 1, gd: -2) | |
let groupC = GroupWinners( | |
competition: "World Cup 2022", | |
group: "Group C", | |
match1: (pol, arg), | |
match2: (ksa, mex)) | |
let aus = Status(team: Team(name: "Aus", symbol: "π¦πΊ"), points: 1, gd: -2) | |
let den = Status(team: Team(name: "Den", symbol: "π©π°"), points: 2, gd: -1) | |
let tun = Status(team: Team(name: "Tun", symbol: "πΉπ³"), points: 2, gd: -1) | |
let fra = Status(team: Team(name: "Fra", symbol: "π«π·"), points: 4, gd: +4) | |
let groupD = GroupWinners( | |
competition: "World Cup 2022", | |
group: "Group D", | |
match1: (aus, den), | |
match2: (tun, fra)) | |
let crc = Status(team: Team(name: "CR", symbol: "π¨π·"), points: 3, gd: -6) | |
let ger = Status(team: Team(name: "Ger", symbol: "π©πͺ"), points: 1, gd: -1) | |
let jpn = Status(team: Team(name: "Jpn", symbol: "π―π΅"), points: 3, gd: 0) | |
let esp = Status(team: Team(name: "Esp", symbol: "πͺπΈ"), points: 4, gd: +7) | |
let groupE = GroupWinners( | |
competition: "World Cup 2022", | |
group: "Group E", | |
match1: (crc, ger), | |
match2: (jpn, esp)) | |
let can = Status(team: Team(name: "Can", symbol: "π¨π¦"), points: 0, gd: -4) | |
let mor = Status(team: Team(name: "Mor", symbol: "π²π¦"), points: 4, gd: +2) | |
let cro = Status(team: Team(name: "Cro", symbol: "ππ·"), points: 4, gd: +3) | |
let bel = Status(team: Team(name: "Bel", symbol: "π§πͺ"), points: 3, gd: -1) | |
let groupF = GroupWinners( | |
competition: "World Cup 2022", | |
group: "Group F", | |
match1: (can, mor), | |
match2: (cro, bel)) | |
let cam = Status(team: Team(name: "Cam", symbol: "π¨π²"), points: 1, gd: -1) | |
let bra = Status(team: Team(name: "Bra", symbol: "π§π·"), points: 6, gd: +3) | |
let srb = Status(team: Team(name: "Srb", symbol: "π·πΈ"), points: 1, gd: -2) | |
let sch = Status(team: Team(name: "Sch", symbol: "π¨π"), points: 3, gd: 0) | |
let groupG = GroupWinners( | |
competition: "World Cup 2022", | |
group: "Group G", | |
match1: (cam, bra), | |
match2: (srb, sch)) | |
let gha = Status(team: Team(name: "Gha", symbol: "π¬π"), points: 3, gd: 0) | |
let uru = Status(team: Team(name: "Uru", symbol: "πΊπΎ"), points: 1, gd: -2) | |
let kor = Status(team: Team(name: "Kor", symbol: "π°π·"), points: 1, gd: -1) | |
let por = Status(team: Team(name: "Por", symbol: "π΅πΉ"), points: 6, gd: +3) | |
let groupH = GroupWinners( | |
competition: "World Cup 2022", | |
group: "Group H", | |
match1: (gha, uru), | |
match2: (kor, por)) | |
//PlaygroundPage.current.setLiveView(groupC) | |
//PlaygroundPage.current.setLiveView(groupD) | |
//PlaygroundPage.current.setLiveView(groupE) | |
//PlaygroundPage.current.setLiveView(groupF) | |
PlaygroundPage.current.setLiveView(groupG) | |
//PlaygroundPage.current.setLiveView(groupH) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment