Example #1
0
func CreateAccount(c *echo.Context) error {
	logrus.Infof("create account")
	caRequest, err := validateCreateAccount(c.Request().Body)
	if err != nil {
		logrus.Errorf("failed create account input validation %s", err.Error())
		c.JSON(400, Response{})
		return nil
	}
	db := c.Get("db").(*mgo.Database)
	_, err = models.LoadAccount(db, caRequest.Username)
	if err == nil {
		logrus.Errorf("account taken: %s", caRequest.Username)
		c.JSON(409, Response{})
		return nil
	} else if err != models.AccountNotFound && err != nil {
		logrus.Errorf("db error in create account: %s", err.Error())
		c.JSON(500, Response{})
		return nil
	}

	a := models.Account{
		Username: caRequest.Username,
		Password: caRequest.Password,
	}
	err = models.CreateAccount(db, a)
	if err != nil {
		logrus.Errorf("failed to create account: %s", err.Error())
		c.JSON(500, Response{})
		return nil
	}

	c.JSON(200, Response{true, a})
	return nil
}
Example #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
}
Example #3
0
func Login(c *echo.Context) error {
	logrus.Infof("login")

	loginRequest, err := validateLogin(c.Request().Body)
	if err != nil {
		logrus.Errorf("failed login validation: %s", err.Error())
		c.JSON(400, Response{})
		return nil
	}

	db := c.Get("db").(*mgo.Database)
	account, err := models.LoadAccount(db, loginRequest.Username)
	if err != nil {
		logrus.Errorf("failed to load account in login: %s", err)
		c.JSON(500, Response{})
		return nil
	}

	if err := bcrypt.CompareHashAndPassword([]byte(account.Hashword), []byte(loginRequest.Password)); err != nil {
		logrus.Errorf("failed to authenticate in login: %s", err.Error())
		c.JSON(401, Response{})
		return nil
	}

	sessionID, err := account.NewSession(db)
	if err != nil {
		logrus.Errorf("failed to create new session in login: %s", err.Error())
		c.JSON(500, Response{})
		return nil
	}

	if err := models.RemovePlayerFromGames(db, account.AccountID); err != nil {
		logrus.Errorf("failed to remove player from games in login: %s", err.Error())
		c.JSON(500, Response{})
		return nil
	}

	resp := struct {
		SessionID string `json:"sessionID"`
		AccountID string `json:"accountID"`
		Username  string `json:"username"`
	}{sessionID, account.AccountID, account.Username}

	c.JSON(200, Response{
		Success: true,
		Payload: resp,
	})
	return nil
}
Example #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
}