// HandleMoveRequest makes the move and saves the new state to the db. // // Requests are of the form libgame.Move. The index must be included (but is // ignored) for "stock" and "waste" piles. // // We respond just like a /state request func HandleMoveRequest(w http.ResponseWriter, r *http.Request) { gameState, err := getGameState(w, r) if err != nil { libhttp.HandleErrorJson(w, fmt.Errorf("Can't get game state: %v.", err)) return } // Parse the request from json err = r.ParseForm() if err != nil { libhttp.HandleErrorJson( w, fmt.Errorf("failure to decode move request: %v", err)) return } var moveRequest libgame.MoveRequest err = decoder.Decode(&moveRequest, r.PostForm) if err != nil { libhttp.HandleErrorJson( w, fmt.Errorf("failure to decode move request: %v. form values: %v", err, r.PostForm)) return } log.Printf("Handling move request from %s-%d to %s-%d\n", moveRequest.FromPile, moveRequest.FromIndex, moveRequest.ToPile, moveRequest.ToIndex) // Move the card err = gameState.MoveCard(moveRequest) if err != nil { libhttp.HandleErrorJson(w, fmt.Errorf("invalid move: %v", err)) return } saveGameStateAndRespond(w, r, *gameState) }
// PostLogin performs login. func PostLogin(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") db := context.Get(r, "db").(*sqlx.DB) cookieStore := context.Get(r, "cookieStore").(*sessions.CookieStore) email := r.FormValue("Email") password := r.FormValue("Password") u := dal.NewUser(db) user, err := u.GetUserByEmailAndPassword(nil, email, password) if err != nil { libhttp.HandleErrorJson(w, err) return } session, _ := cookieStore.Get(r, "forty-thieves-session") session.Values["user"] = user err = session.Save(r, w) if err != nil { libhttp.HandleErrorJson(w, err) return } http.Redirect(w, r, "/", 302) }
// saveGameStateAndRespond saves GameState to the DB, replies with the new state. // // Sends a json response with the new state using the /state route. func saveGameStateAndRespond( w http.ResponseWriter, r *http.Request, gameState libgame.GameState) { gameStateDB, currentUser, err := databaseParams(w, r) if err != nil { libhttp.HandleErrorJson(w, err) return } err = gameStateDB.SaveGameState(nil, *currentUser, gameState) if err != nil { libhttp.HandleErrorJson(w, fmt.Errorf("error saving gamestate: %v", err)) return } HandleStateRequest(w, r) }
func HandleStateRequest(w http.ResponseWriter, r *http.Request) { gameState, err := getGameState(w, r) if err != nil { libhttp.HandleErrorJson(w, fmt.Errorf("Can't get game state: %v.", err)) return } data, err := json.Marshal(&gameState) if err != nil { libhttp.HandleErrorJson(w, err) return } w.Header().Set("Content-Type", "text/json") fmt.Fprint(w, string(data)) }
// HandleUndoMove deletes the latest move for the current user. // // If no error, responds with the gamestate for the new latest move (after // deletion). func HandleUndoMove(w http.ResponseWriter, r *http.Request) { gameStateDB, currentUser, err := databaseParams(w, r) if err != nil { libhttp.HandleErrorJson(w, err) return } err = gameStateDB.DeleteLatestGameState(nil, *currentUser) if err != nil { libhttp.HandleErrorJson(w, fmt.Errorf("Undo failed: %v", err)) return } HandleStateRequest(w, r) }
func HandleFlipStockRequest(w http.ResponseWriter, r *http.Request) { gameState, err := getGameState(w, r) if err != nil { libhttp.HandleErrorJson(w, fmt.Errorf("Can't get game state: %v.", err)) return } err = gameState.FlipStock() if err != nil { libhttp.HandleErrorJson(w, fmt.Errorf("can't flip stock: %v", err)) return } saveGameStateAndRespond(w, r, *gameState) }
func PutUsersID(w http.ResponseWriter, r *http.Request) { userId, err := getIdFromPath(w, r) if err != nil { libhttp.HandleErrorJson(w, err) return } db := context.Get(r, "db").(*sqlx.DB) cookieStore := context.Get(r, "cookieStore").(*sessions.CookieStore) session, _ := cookieStore.Get(r, "forty-thieves-session") currentUser := session.Values["user"].(*dal.UserRow) if currentUser.ID != userId { err := errors.New("Modifying other user is not allowed.") libhttp.HandleErrorJson(w, err) return } email := r.FormValue("Email") password := r.FormValue("Password") passwordAgain := r.FormValue("PasswordAgain") u := dal.NewUser(db) currentUser, err = u.UpdateEmailAndPasswordById(nil, currentUser.ID, email, password, passwordAgain) if err != nil { libhttp.HandleErrorJson(w, err) return } // Update currentUser stored in session. session.Values["user"] = currentUser err = session.Save(r, w) if err != nil { libhttp.HandleErrorJson(w, err) return } http.Redirect(w, r, "/", 302) }
func GetLoginWithoutSession(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") tmpl, err := template.ParseFiles("templates/users/login-signup-parent.html.tmpl", "templates/users/login.html.tmpl") if err != nil { libhttp.HandleErrorJson(w, err) return } tmpl.Execute(w, nil) }
func PostSignup(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") db := context.Get(r, "db").(*sqlx.DB) email := r.FormValue("Email") password := r.FormValue("Password") passwordAgain := r.FormValue("PasswordAgain") _, err := dal.NewUser(db).Signup(nil, email, password, passwordAgain) if err != nil { libhttp.HandleErrorJson(w, err) return } // Perform login PostLogin(w, r) }
func GetHome(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") currentUser, exists := getCurrentUser(w, r) if !exists { http.Redirect(w, r, "/logout", 302) return } data := struct { CurrentUser *dal.UserRow }{ currentUser, } tmpl, err := template.ParseFiles("templates/dashboard.html.tmpl", "templates/home.html.tmpl") if err != nil { libhttp.HandleErrorJson(w, err) return } tmpl.Execute(w, data) }
func DeleteUsersID(w http.ResponseWriter, r *http.Request) { err := errors.New("DELETE method is not implemented.") libhttp.HandleErrorJson(w, err) return }