use and delete items

This commit is contained in:
2025-06-24 08:25:48 -03:00
parent abda2ed46d
commit 809a9b4efa
4 changed files with 62 additions and 15 deletions

Binary file not shown.

View File

@@ -99,7 +99,8 @@ func main() {
e.GET("login", badreq)
e.GET("player", player_data)
e.GET("player/inv", inv_get)
e.GET("player/inv/:id", inv_del)
e.GET("player/inv/del/:id", inv_del)
e.GET("player/inv/use/:id", inv_use)
e.POST("player/update", player_updateInputs)
e.PATCH("player/:atb/:updown", player_update)

View File

@@ -399,3 +399,37 @@ func inv_del(c echo.Context) error {
return jsonSuccess(c, "você item deletado")
}
func inv_use(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 usar algum item")
}
itemID := c.Param("id")
var durabilidade bool
var quantidade, menos int
var item_name string
err = db.QueryRow(fmt.Sprintf("SELECT item_durabilit, quantidade, item_nome FROM inv_%s WHERE id=?;", session["playerName"]), itemID).Scan(&durabilidade, &quantidade, &item_name)
if err != nil {
return jsonError(c, http.StatusInternalServerError, "item não foi encontrado")
}
if durabilidade == false {
return jsonSuccess(c, "você usou um item sem durabilidade")
}
menos = quantidade - 1
if menos > 1 {
err = updateField(fmt.Sprintf("UPDATE inv_%s SET quantidade=? WHERE id=?;", session["playerName"]), menos, itemID)
if err != nil {
return jsonError(c, http.StatusInternalServerError, "algo deu errado ao usar seu item")
}
return jsonSuccess(c, fmt.Sprintf("você uso 1 %s", item_name))
}
err = updateField(fmt.Sprintf("DELETE FROM inv_%s WHERE id=?", session["playerName"]), itemID)
if err != nil {
return jsonError(c, http.StatusInternalServerError, "item não foi apagado, algo deu errado")
}
return jsonSuccess(c, fmt.Sprintf("você gastou seu ultimo %s", item_name))
}

View File

@@ -1,9 +1,21 @@
let img;
let update;
let invUpdate;
function delitem(id) {
alert(id);
function useInv(id, type) {
$.ajax({
url: window.api + `player/inv/${type}/${id}`,
type: "GET",
xhrFields: { withCredentials: true },
success: function (resp) {
msg(resp.Status, resp.Message);
},
error: function (resp) {
msg(
resp.responseJSON?.Status || "Erro",
resp.responseJSON?.Message || "Algo deu errado.",
);
},
});
}
function useItem(id) {
alert(id);
@@ -108,26 +120,26 @@ function invPlayer() {
$("<span>").addClass("material-symbols-outlined").text("close"),
);
$li.on("click", () => useItem(item.id));
$li.on("click", () => useInv(item.id, "use"));
$delBtn.on("click", (e) => {
e.stopPropagation();
delitem(item.id);
useInv(item.id, "del");
});
$li.append($delBtn);
return $li;
};
if (invUpdate === undefined || resp.invPeso !== invUpdate) {
$("#inv ul").empty();
resp.InvItems.forEach((item) => {
$("#inv ul").append(buildItem(item));
});
invUpdate = resp.invPeso;
}
$("#inv ul").empty();
resp.InvItems.forEach((item) => {
$("#inv ul").append(buildItem(item));
});
$("#invPA").text(resp.invPeso);
},
error: function () {
$("#inv ul").empty();
$("#invPA").text(0);
},
});
}
@@ -155,5 +167,5 @@ $(document).ready(function () {
load_ficha();
roll_load();
invPlayer();
}, 500);
}, 1000);
});