terminei o inventario, falta o de habilidades/poderes '-'
This commit is contained in:
@@ -31,6 +31,7 @@ type logEntry struct {
|
||||
type resp_json struct {
|
||||
Status string `json:"Status"`
|
||||
Message string `json:"Message,omitempty"`
|
||||
PlayerID *int64 `json:"PlayerID,omitempty"`
|
||||
Logs []logEntry `json:"Logs,omitempty"`
|
||||
InvPeso int `json:"invPeso,omitempty"`
|
||||
InvItems []plyInv `json:"InvItems,omitempty"`
|
||||
|
||||
@@ -99,6 +99,7 @@ func main() {
|
||||
e.GET("login", badreq)
|
||||
e.GET("player", player_data)
|
||||
e.GET("player/inv", inv_get)
|
||||
e.POST("player/inv", inv_add)
|
||||
e.GET("player/inv/del/:id", inv_del)
|
||||
e.GET("player/inv/use/:id", inv_use)
|
||||
e.POST("player/update", player_updateInputs)
|
||||
|
||||
@@ -197,13 +197,13 @@ func player_data(c echo.Context) error {
|
||||
}
|
||||
|
||||
var foto, nome, vida, descricao string
|
||||
var conhecimento, level, idade, vd, vdm, destreza, inteligencia, forca, constituicao, carisma, inventario int64
|
||||
var id, conhecimento, level, idade, vd, vdm, destreza, inteligencia, forca, constituicao, carisma, inventario int64
|
||||
var altura float64
|
||||
|
||||
err = db.QueryRow(`SELECT foto, nome, conhecimento, level, idade, altura, vida, vida_maxima,
|
||||
err = db.QueryRow(`SELECT id, foto, nome, conhecimento, level, idade, altura, vida, vida_maxima,
|
||||
destresa, inteligencia, força, constituição, cárisma, inventario, descrição
|
||||
FROM players WHERE id=? LIMIT 1;`, session["user_id"]).
|
||||
Scan(&foto, &nome, &conhecimento, &level, &idade, &altura, &vd, &vdm,
|
||||
Scan(&id, &foto, &nome, &conhecimento, &level, &idade, &altura, &vd, &vdm,
|
||||
&destreza, &inteligencia, &forca, &constituicao, &carisma, &inventario, &descricao)
|
||||
|
||||
if err != nil {
|
||||
@@ -215,6 +215,7 @@ func player_data(c echo.Context) error {
|
||||
|
||||
return c.JSON(http.StatusOK, resp_json{
|
||||
Status: "Success",
|
||||
PlayerID: &id,
|
||||
Foto: foto,
|
||||
PlayerName: nome,
|
||||
Conhecimento: &conhecimento,
|
||||
@@ -263,12 +264,31 @@ func player_update(c echo.Context) error {
|
||||
if !ok {
|
||||
return jsonError(c, http.StatusBadRequest, "você passou um parametro invalido, tente novamente com um parametro correto")
|
||||
}
|
||||
if atb != "vida" {
|
||||
query := fmt.Sprintf("UPDATE players SET %s = %s %s 1 WHERE id=?;", field, field, sinal)
|
||||
if err := updateField(query, session["user_id"]); err != nil {
|
||||
return jsonError(c, http.StatusInternalServerError, "algo deu errado ao atualizar sua ficha")
|
||||
}
|
||||
return jsonSuccess(c, sinal+"1 em "+atb)
|
||||
}
|
||||
var life, life_max int
|
||||
err = db.QueryRow("SELECT vida, vida_maxima FROM players WHERE id=?", session["user_id"]).Scan(&life, &life_max)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return jsonError(c, http.StatusInternalServerError, "algo deu errado ao atualizar sua ficha")
|
||||
}
|
||||
|
||||
if life <= 0 && sinal != "+" {
|
||||
return jsonError(c, http.StatusBadRequest, "sua vida não pode ficar a baixo de zero")
|
||||
}
|
||||
if life >= life_max && sinal != "-" {
|
||||
return jsonError(c, http.StatusBadRequest, fmt.Sprintf("sua vida maxima é %v, você não pode estar com a vida a cima da maxima", life_max))
|
||||
}
|
||||
|
||||
query := fmt.Sprintf("UPDATE players SET %s = %s %s 1 WHERE id=?;", field, field, sinal)
|
||||
if err := updateField(query, session["user_id"]); err != nil {
|
||||
return jsonError(c, http.StatusInternalServerError, "algo deu errado ao atualizar sua ficha")
|
||||
}
|
||||
|
||||
return jsonSuccess(c, sinal+"1 em "+atb)
|
||||
}
|
||||
|
||||
@@ -433,3 +453,62 @@ func inv_use(c echo.Context) error {
|
||||
}
|
||||
return jsonSuccess(c, fmt.Sprintf("você gastou seu ultimo %s", item_name))
|
||||
}
|
||||
|
||||
func inv_add(c echo.Context) error {
|
||||
session, err := requireSession(c)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if session["playerName"] == "" {
|
||||
return jsonError(c, http.StatusUnauthorized, "Você precisa estar logado para adicionar itens ao inventário.")
|
||||
}
|
||||
|
||||
nome := c.FormValue("item_name")
|
||||
durabiliteStr := c.FormValue("durabilite")
|
||||
peso := c.FormValue("peso")
|
||||
quantidade := c.FormValue("quantidade")
|
||||
|
||||
if anyEmpty(nome, durabiliteStr, peso, quantidade) {
|
||||
return jsonError(c, http.StatusBadRequest, "Algum parâmetro está faltando ou está vazio. Tente novamente.")
|
||||
}
|
||||
|
||||
durabilite, err := strconv.ParseBool(durabiliteStr)
|
||||
if err != nil {
|
||||
return jsonError(c, http.StatusBadRequest, "A durabilidade deve ser 'true' ou 'false'.")
|
||||
}
|
||||
|
||||
row, err := db.Query(fmt.Sprintf("SELECT item_peso FROM inv_%s;", session["playerName"]))
|
||||
if err != nil {
|
||||
return jsonError(c, http.StatusInternalServerError, "Erro ao consultar itens do inventário.")
|
||||
}
|
||||
defer row.Close()
|
||||
|
||||
var total, atualpeso int
|
||||
for row.Next() {
|
||||
var itempeso int
|
||||
if err := row.Scan(&itempeso); err != nil {
|
||||
return jsonError(c, http.StatusInternalServerError, "Erro ao ler itens do inventário.")
|
||||
}
|
||||
total += itempeso
|
||||
}
|
||||
|
||||
err = db.QueryRow("SELECT inventario FROM players WHERE id=?;", session["user_id"]).Scan(&atualpeso)
|
||||
ps, _ := strconv.Atoi(peso)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return jsonError(c, http.StatusInternalServerError, "Algo deu errado ao verificar o tamanho do seu inventário.")
|
||||
}
|
||||
|
||||
total += ps
|
||||
if total > atualpeso {
|
||||
return jsonError(c, http.StatusInternalServerError, fmt.Sprintf("%s é muito pesado para colocar no seu inventário.", nome))
|
||||
}
|
||||
|
||||
_, err = db.Exec(fmt.Sprintf("INSERT INTO inv_%s (item_nome, quantidade, item_peso, item_durabilit) VALUE (?, ?, ?, ?);", session["playerName"]), nome, quantidade, peso, durabilite)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return jsonError(c, http.StatusInternalServerError, fmt.Sprintf("Algo deu errado ao adicionar %s ao seu inventário.", nome))
|
||||
}
|
||||
|
||||
return jsonSuccess(c, fmt.Sprintf("%s %ss foram colocados no seu inventário.", quantidade, nome))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user