Skip to content

Instantly share code, notes, and snippets.

@mollietaylor
Created February 21, 2015 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mollietaylor/556392c5a9e6517640e0 to your computer and use it in GitHub Desktop.
Save mollietaylor/556392c5a9e6517640e0 to your computer and use it in GitHub Desktop.
newGame()
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