Created
February 21, 2015 22:10
-
-
Save mollietaylor/556392c5a9e6517640e0 to your computer and use it in GitHub Desktop.
newGame()
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
func newGame(token: String) { | |
let joinOptions: [String:AnyObject] = [ | |
"endpoint" : "games/id", | |
"method" : "POST", | |
"body" : [ | |
"authentication_token" : token | |
] | |
] | |
let createOptions: [String:AnyObject] = [ | |
"endpoint" : "games", | |
"method" : "POST", | |
"body" : [ | |
"authentication_token" : token | |
] | |
] | |
APIRequest.requestWithOptions(joinOptions, andCompletion: { (responseInfo, error) -> () in | |
if error != nil { | |
// create a game | |
// TODO: see what error this prints. be more specific about in what case(s) we should move on to create a game | |
println(error) | |
APIRequest.requestWithOptions(createOptions, andCompletion: { (responseInfo, error) -> () in | |
if error != nil { | |
println("Error != nil") | |
} else { | |
if let dataInfo: AnyObject = responseInfo!["game"] { | |
if let board = dataInfo["board"] as? [[Int]] { | |
// TODO: This probably should work differently, especially once we have multiple games to keep track of at once | |
GameModel().boardSquares = board | |
} | |
if let id = dataInfo["id"] as? Int { | |
GameModel().id = id | |
} | |
} | |
} | |
}) | |
} else { | |
// you can join a game | |
if let dataInfo: AnyObject = responseInfo!["game"] { | |
if let board = dataInfo["board"] as? [[Int]] { | |
// TODO: This probably should work differently, especially once we have multiple games to keep track of at once | |
GameModel().boardSquares = board | |
} | |
if let id = dataInfo["id"] as? Int { | |
GameModel().id = id | |
} | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment