func HandleLeaveGame(msg models.Message) error { userFailMsg := "failed to leave game" lg, err := validateLeaveGame(msg) if err != nil { models.SendError(msg.Sender.AccountID, userFailMsg) return fmt.Errorf("validate %s: %+v", userFailMsg, err.Error()) } db := msg.Context.Get("db").(*mgo.Database) game, err := models.LeaveGame(db, lg.ID, msg.Sender.AccountID) if err != nil { models.SendError(msg.Sender.AccountID, userFailMsg) return fmt.Errorf("leavegame %s: %+v", userFailMsg, err.Error()) } //notify all clients that someone left this game if err := models.SendAll(newLobbyMessage(gameLeaveAlert, game)); err != nil { return fmt.Errorf("sendall error in handleLeaveGame: %+v", err) } //notify this client to leave the game if err := models.Send(msg.Sender.AccountID, newLobbyMessage(gameLeave, game)); err != nil { return fmt.Errorf("send error in handleLeaveGame: %+v", err) } return nil }
func HandleJoinGame(msg models.Message) error { userFailMsg := "failed to join game" jg, err := validateJoinGame(msg) if err != nil { models.SendError(msg.Sender.AccountID, userFailMsg) return fmt.Errorf("fail validate join game. %s: %+v ", userFailMsg, err) } db := msg.Context.Get("db").(*mgo.Database) game, err := models.JoinGame(db, jg.Game.ID, msg.Sender.AccountID, jg.BuyinAmount) if err != nil { models.SendError(msg.Sender.AccountID, userFailMsg) return fmt.Errorf("fail join game. %s: %+v ", userFailMsg, err) } //notify all clients that someone joined this game if err := models.SendAll(newLobbyMessage(gameJoinAlert, game)); err != nil { return fmt.Errorf("sendall error in handleJoinGame: %+v", err) } //notify this client to enter the game; this ultimately redirs the user into the game if err := models.Send(msg.Sender.AccountID, newLobbyMessage(gameJoin, game)); err != nil { return fmt.Errorf("send error in handleJoinGame: %+v", err) } return nil }
func HandleSqrt(msg models.Message) error { db := msg.Context.Get("db").(*mgo.Database) count, err := models.SqrtCounter(db) if err != nil { models.SendError(msg.Sender.AccountID, "sqrt error") return fmt.Errorf("failed to sqrt: %+v", err) } if err := models.SendAll(newMathMessage(msg.Type, count)); err != nil { return fmt.Errorf("sendall error: %+v", err) } return nil }
func HandleCreateGame(msg models.Message) error { userFailMsg := "failed to create game" cg, err := validateCreateGame(msg) if err != nil { models.SendError(msg.Sender.AccountID, userFailMsg) return fmt.Errorf("failed to validate create game: %+v", err) } db := msg.Context.Get("db").(*mgo.Database) game, err := models.CreateGame(db, cg.Name, cg.Type) if err != nil { models.SendError(msg.Sender.AccountID, userFailMsg) return fmt.Errorf("failed to create game: %+v", err) } if err := models.SendAll(newLobbyMessage(msg.Type, game)); err != nil { return fmt.Errorf("sendall error: %+v", err) } return nil }