Last active
January 18, 2021 22:48
-
-
Save davidseek/9d8aba84a209d6a83940fd8be1003c8f to your computer and use it in GitHub Desktop.
Runner.swift
This file contains hidden or 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
var body: some View { | |
VStack { | |
... | |
} | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
// We need to start our runner within the body | |
// So we are able to update our immutable view attributes | |
.onAppear(perform: self.run) | |
} | |
} | |
// This is our runner that updates our view and kicks off new calculations | |
private func run() { | |
// For the current state of our POC, | |
// let's update once per second | |
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in | |
// First let's get a new capturing of the poker table | |
guard let canvas = Canvas() else { | |
// MARK: - TODO, Handle appropriatly | |
return | |
} | |
// The image to display for us so we see what's going on | |
self.image = canvas.image | |
// The CGImage we want to use for further calculations | |
self.screenshot = canvas.screenshot | |
self.setWholeCards(in: self.screenshot!) | |
self.findDealer(in: self.screenshot!) | |
} | |
} | |
// We're using the screenshot to now analyze the table | |
private func setWholeCards(in frame: CGImage) { | |
// We're getting our left card. | |
// We're passing our hard-coded coordinates of the face and the suite. | |
// The face is the letter/number of our hole card. | |
// For example A/K/T/9/7/6/etc | |
// The suite is the picture of the card | |
// For example diamond or heart | |
// getLeftWholeCardFace and getLeftWholeCardSuite | |
// once again each just return hard-coded CGRects | |
getCard( | |
face: Rect.getLeftWholeCardFace(in: frame), | |
suite: Rect.getLeftWholeCardSuite(in: frame)) { (face, suite) in | |
// Once we have the face and the suite, we'll attach it to our game model | |
// I'll show the game model in a minute | |
game.setLeftWholeCard(face, suite) | |
} | |
// Repeat for the right card | |
getCard( | |
face: Rect.getRightWholeCardFace(in: frame), | |
suite: Rect.getRightWholeCardSuite(in: frame)) { (face, suite) in | |
// Same as above | |
... | |
} | |
} | |
// Type alias for better readability | |
typealias CardCompletion = ((face: String, suite: String)) -> Void | |
// This function wraps the getFace and getSuite method to provide a simple API | |
private func getCard(face facePosition: CGRect, | |
suite suitePosition: CGRect, | |
onCompletion: @escaping CardCompletion) { | |
// getFace and getSuite will be explained shortly | |
getFace(at: facePosition) { face in | |
getSuite(at: suitePosition) { suite in | |
onCompletion((face, suite)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment