예제 #1
0
파일: lobby.go 프로젝트: mikerjacobi/poker
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
}
예제 #2
0
func HandleChipRequest(msg models.Message) error {
	db, dbok := msg.Context.Get("db").(*mgo.Database)
	a, aok := msg.Context.Get("user").(models.Account)
	if !dbok || !aok {
		return fmt.Errorf("failed to load account or db.  dbok: %+v, aok: %+v", dbok, aok)
	}
	account, err := models.LoadAccount(db, a.Username)
	if err != nil {
		return fmt.Errorf("failed to load account in handle chip request: %+v", err)
	}

	req := struct {
		Amount int `json:"amount"`
	}{}
	if err := json.Unmarshal(msg.Raw, &req); err != nil {
		return fmt.Errorf("failed to unmarshal chipreq: %+v", err)
	}

	if req.Amount <= 0 {
		return fmt.Errorf("chip request <= 0: %+v", req)
	}
	account.Balance += req.Amount

	if err := account.Update(db); err != nil {
		return fmt.Errorf("failed to update account balance: %+v", err)
	}

	if err := models.Send(account.AccountID, newAccountMessage(accountLoad, account)); err != nil {
		return fmt.Errorf("failed to send in GetAccount: %+v", err)
	}
	return nil
}
예제 #3
0
파일: lobby.go 프로젝트: mikerjacobi/poker
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
}
예제 #4
0
func HandleLoadAccount(msg models.Message) error {
	db, dbok := msg.Context.Get("db").(*mgo.Database)
	a, aok := msg.Context.Get("user").(models.Account)
	if !dbok || !aok {
		return fmt.Errorf("failed to load account or db.  dbok: %+v, aok: %+v", dbok, aok)
	}
	account, err := models.LoadAccount(db, a.Username)
	if err != nil {
		return fmt.Errorf("failed to load account in handle load account: %+v", err)
	}

	if err := models.Send(account.AccountID, newAccountMessage(accountLoad, account)); err != nil {
		return fmt.Errorf("failed to send in GetAccount: %+v", err)
	}
	return nil
}