Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelvin Lau authored and Kelvin Lau committed Jun 25, 2017
1 parent 8c7ce1e commit 34bbc50
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 35 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion FaceMatch/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class AppDelegate: UIResponder {
extension AppDelegate: UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = GameViewController.instantiate(service: FakeWebservice())
window?.rootViewController = GameViewController.instantiate(service: Webservice())
window?.makeKeyAndVisible()
return true
}
Expand Down
21 changes: 21 additions & 0 deletions FaceMatch/Assets.xcassets/angryface.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "angryface.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions FaceMatch/Assets.xcassets/fearface.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "fearface.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions FaceMatch/Assets.xcassets/neutralface.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "sticker,375x360.u2.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions FaceMatch/Assets.xcassets/surprisedface.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "1f632.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
12 changes: 9 additions & 3 deletions FaceMatch/Emotion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ enum Emotion: String {
return #imageLiteral(resourceName: "happyface")
case .sad:
return #imageLiteral(resourceName: "sadface")
default:
fatalError()
case .angry:
return #imageLiteral(resourceName: "angryface")
case .fear:
return #imageLiteral(resourceName: "fearface")
case .neutral:
return #imageLiteral(resourceName: "neutralface")
case .surprise:
return #imageLiteral(resourceName: "surprisedface")
}
}

static var allValues: [Emotion] {
return [.happy, .sad]
return [.happy, .sad, .angry, .fear, .neutral, .surprise]
}

static var random: Emotion {
Expand Down
4 changes: 2 additions & 2 deletions FaceMatch/FakeWebservice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ extension FakeWebservice: HighscoresAPI {
completion(scores)
}

func postHighscore(name: String, score: Int, completion: @escaping (Result<Score>) -> ()) {
completion(.success(Score(name: name, score: score)))
func postHighscore(name: String, score: Int, completion: @escaping ([Score]) -> ()) {
completion([Score(name: name, score: score)])
}
}

Expand Down
2 changes: 1 addition & 1 deletion FaceMatch/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ extension GameViewController {
func spawnEmotion() {
guard numberOfSpawns < maxSpawns else {
let accuracy = Int((Double(correct) / Double(maxSpawns)) * 100)
let highscoresVC = HighScoresViewController.instantiate(score: score, accuracy: accuracy, webservice: FakeWebservice(), onComplete: { [weak self] vc in
let highscoresVC = HighScoresViewController.instantiate(score: score, accuracy: accuracy, webservice: Webservice(), onComplete: { [weak self] vc in
self?.resetGame()
vc.dismiss(animated: true, completion: nil)
})
Expand Down
6 changes: 3 additions & 3 deletions FaceMatch/HighScoresViewController.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="FAb-85-FKd" customClass="RoundableView" customModule="FaceMatch" customModuleProvider="target">
<rect key="frame" x="38" y="100.5" width="300" height="466.5"/>
<subviews>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" keyboardDismissMode="onDrag" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="zfY-xL-yvh">
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" keyboardDismissMode="onDrag" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="zfY-xL-yvh">
<rect key="frame" x="0.0" y="44" width="300" height="422.5"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="sDa-Wn-kkQ">
Expand Down Expand Up @@ -144,8 +144,8 @@
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="7Pk-4I-XvC">
<rect key="frame" x="0.0" y="136" width="300" height="44"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="LEADERBOARDS" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="LaG-2h-PzQ">
<rect key="frame" x="74.5" y="11" width="151.5" height="21"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="LEADERBOARD" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="LaG-2h-PzQ">
<rect key="frame" x="80" y="11" width="141" height="21"/>
<fontDescription key="fontDescription" name="Baskerville-Bold" family="Baskerville" pointSize="18"/>
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
Expand Down
10 changes: 2 additions & 8 deletions FaceMatch/HighScoresViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,12 @@ extension HighScoresViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HighScoresReusableView.reuseIdentifier, for: indexPath) as? HighScoresReusableView else { fatalError() }
header.configure(score: score, accuracy: accuracy, onSubmit: { [weak self] name, score in
self?.webservice.postHighscore(name: name, score: score) { [weak self] result in
guard case .success(let score) = result else { fatalError("Wat") }
self?.add(score: score)
self?.webservice.postHighscore(name: name, score: score) { [weak self] scores in
self?.highscores = scores
}
})
return header
}

// update the ui with the new score
func add(score: Score) {

}
}

// MARK: - UICollectionViewDelegate
Expand Down
2 changes: 1 addition & 1 deletion FaceMatch/HighscoresAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

protocol HighscoresAPI {
func getHighscores(completion: @escaping ([Score]) -> ())
func postHighscore(name: String, score: Int, completion: @escaping (Result<Score>) -> ())
func postHighscore(name: String, score: Int, completion: @escaping ([Score]) -> ())
}
29 changes: 13 additions & 16 deletions FaceMatch/Webservice+Highscores.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,17 @@ extension Webservice: HighscoresAPI {
func getHighscores(completion: @escaping ([Score]) -> ()) {

// TODO: - add endpoint for highscores
let url = URL(string: "")!
let url = URL(string: "http://192.168.25.109:8080/leaderboard")!

URLSession.shared.dataTask(with: url) { data, _, _ in
var scoresArray: [Score] = []
defer {
DispatchQueue.main.async {
completion(scoresArray)
}
DispatchQueue.main.async {
completion(self.scores(from: data))
}

let dict = self.response(from: data)
guard let scoresDictionaries = dict["scores"] as? [[String: Any]] else { return }
scoresArray = scoresDictionaries.map(Score.init)
}.resume()
}

func postHighscore(name: String, score: Int, completion: @escaping (Result<Score>) -> ()) {
let url = URL(string: "")!
func postHighscore(name: String, score: Int, completion: @escaping ([Score]) -> ()) {
let url = URL(string: "http://192.168.25.109:8080/leaderboard")!

var request = URLRequest(url: url)
request.httpMethod = HttpMethod.post.rawValue
Expand All @@ -50,12 +43,16 @@ extension Webservice: HighscoresAPI {
request.httpBody = try! JSONSerialization.data(withJSONObject: params, options: [])

URLSession.shared.dataTask(with: request) { data, _, _ in
let dict = self.response(from: data)
let score = Score(dict: dict)

DispatchQueue.main.async {
completion(.success(score))
completion(self.scores(from: data))
}
}.resume()
}

private func scores(from data: Data?) -> [Score] {
guard let data = data else { return [] }
guard let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) else { return [] }
guard let dicts = jsonObject as? [[String: Any]] else { return [] }
return dicts.map(Score.init)
}
}

0 comments on commit 34bbc50

Please sign in to comment.