back_end e front_end, motoes de editar, tela de login e os caralho a4 '-'
This commit is contained in:
Binary file not shown.
@@ -30,15 +30,32 @@ func sessionCookie(id int, player string) (*http.Cookie, error) {
|
||||
}
|
||||
|
||||
cookie := &http.Cookie{
|
||||
Name: cookieName,
|
||||
Value: sessionID,
|
||||
Path: "/*",
|
||||
HttpOnly: true,
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
Name: cookieName,
|
||||
Value: sessionID,
|
||||
Path: "/",
|
||||
//HttpOnly: true,
|
||||
//Secure: true,
|
||||
//SameSite: http.SameSiteNoneMode,
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
}
|
||||
return cookie, nil
|
||||
}
|
||||
|
||||
func delCookie(c echo.Context, sessionID string) error {
|
||||
cookie := &http.Cookie{
|
||||
Name: cookieName,
|
||||
Value: "",
|
||||
Path: "/",
|
||||
MaxAge: -1,
|
||||
}
|
||||
err := redisCL.Del(ctx, sessionID).Err()
|
||||
if err != nil {
|
||||
log.Println(sessionID + "não era um cookie valido")
|
||||
}
|
||||
c.SetCookie(cookie)
|
||||
return nil
|
||||
}
|
||||
|
||||
func verifyTabeles(tbex []string, tb string) bool {
|
||||
for _, a := range tbex {
|
||||
if tb == a {
|
||||
@@ -112,6 +129,7 @@ func ValidSession(c echo.Context) (map[string]string, error) {
|
||||
sessionID := cookie.Value
|
||||
dataSession, err := redisCL.HGetAll(ctx, sessionID).Result()
|
||||
if err != nil {
|
||||
delCookie(c, sessionID)
|
||||
return nil, fmt.Errorf("sessão invalida ou expirada.")
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ var (
|
||||
redisCL *redis.Client
|
||||
ctx = context.Background()
|
||||
table_list = map[string]string{
|
||||
"players": "CREATE TABLE players (`id` INT NOT NULL AUTO_INCREMENT,`foto` MEDIUMTEXT NOT NULL,`player` VARCHAR(15) NOT NULL,`password` TEXT NOT NULL,`nome` VARCHAR(30) NOT NULL, `conhecimento` INT NOT NULL DEFAULT '0',`level` INT NOT NULL DEFAULT '0',`idade` INT NOT NULL DEFAULT '18',`altura` FLOAT NOT NULL DEFAULT '1.40',`vida` INT NOT NULL DEFAULT '10',`vida_maxima` INT NOT NULL DEFAULT '10',`destresa` INT NOT NULL DEFAULT '0',`inteligencia` INT NOT NULL DEFAULT '0',`força` INT NOT NULL DEFAULT '0',`constituição` INT NOT NULL DEFAULT '0',`cárisma` INT NOT NULL DEFAULT '0',`inventario` INT NOT NULL DEFAULT '10',`descrição` TEXT NOT NULL,PRIMARY KEY (`id`)) ENGINE = InnoDB;",
|
||||
"players": "CREATE TABLE players (`id` INT NOT NULL AUTO_INCREMENT,`foto` LONGTEXT NOT NULL,`player` VARCHAR(15) NOT NULL,`password` TEXT NOT NULL,`nome` VARCHAR(30) NOT NULL, `conhecimento` INT NOT NULL DEFAULT '0',`level` INT NOT NULL DEFAULT '0',`idade` INT NOT NULL DEFAULT '18',`altura` FLOAT NOT NULL DEFAULT '1.40',`vida` INT NOT NULL DEFAULT '10',`vida_maxima` INT NOT NULL DEFAULT '10',`destresa` INT NOT NULL DEFAULT '0',`inteligencia` INT NOT NULL DEFAULT '0',`força` INT NOT NULL DEFAULT '0',`constituição` INT NOT NULL DEFAULT '0',`cárisma` INT NOT NULL DEFAULT '0',`inventario` INT NOT NULL DEFAULT '10',`descrição` TEXT NOT NULL,PRIMARY KEY (`id`)) ENGINE = InnoDB;",
|
||||
"roll_logs": "CREATE TABLE roll_logs (`id` INT NOT NULL AUTO_INCREMENT , `log` TEXT NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;",
|
||||
}
|
||||
formatFILES = map[string]bool{
|
||||
@@ -50,5 +50,5 @@ var (
|
||||
".jpeg": true,
|
||||
".gif": true,
|
||||
}
|
||||
cookieName string = "PlayerSession"
|
||||
cookieName string = "playerSession"
|
||||
)
|
||||
|
||||
@@ -84,7 +84,7 @@ func main() {
|
||||
log.Println("concluido, back_end rodando")
|
||||
|
||||
/* rotas */
|
||||
|
||||
e.GET("logout", logout)
|
||||
e.GET("/", online)
|
||||
e.GET("roll", roll_get)
|
||||
e.POST("roll", roll)
|
||||
@@ -93,6 +93,8 @@ func main() {
|
||||
e.POST("login", login, timeoutMiddleware, rateLimit)
|
||||
e.GET("login", badreq)
|
||||
e.GET("player", player_data)
|
||||
e.POST("player/update", player_updateInputs)
|
||||
e.PATCH("player/:atb/:updown", player_update)
|
||||
|
||||
/* log */
|
||||
e.Logger.Fatal(e.Start(fmt.Sprintf(":%s", load_data["port"])))
|
||||
|
||||
@@ -17,6 +17,25 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func logout(c echo.Context) error {
|
||||
_, err := ValidSession(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusUnauthorized, resp_json{
|
||||
Status: "Error",
|
||||
Message: "para fazer logout você deve estar logado seu cabaço",
|
||||
})
|
||||
}
|
||||
cookie, _ := c.Cookie(cookieName)
|
||||
|
||||
sessionID := cookie.Value
|
||||
delCookie(c, sessionID)
|
||||
|
||||
return c.JSON(http.StatusOK, resp_json{
|
||||
Status: "Success",
|
||||
Message: "Você fez foi deslogado.",
|
||||
})
|
||||
}
|
||||
|
||||
func online(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, resp_json{
|
||||
Status: "Success",
|
||||
@@ -91,10 +110,10 @@ func registrar(c echo.Context) error {
|
||||
})
|
||||
}
|
||||
defer openfile.Close()
|
||||
if ImageFile.Size > 3*1024*1024 {
|
||||
if ImageFile.Size > 25*1024*1024 {
|
||||
return c.JSON(http.StatusBadRequest, resp_json{
|
||||
Status: "Error",
|
||||
Message: "Sua imagem deve ter no maximo 3MB",
|
||||
Message: "Sua imagem deve ter no maximo 25MB",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -157,7 +176,7 @@ func registrar(c echo.Context) error {
|
||||
Message: "sua conta foi criada com sucesso",
|
||||
})
|
||||
}
|
||||
|
||||
log.Println(err)
|
||||
return c.JSON(http.StatusInternalServerError, resp_json{
|
||||
Status: "Error",
|
||||
Message: "Erro ao criar sua conta, tente novamente",
|
||||
@@ -327,3 +346,136 @@ func player_data(c echo.Context) error {
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func player_update(c echo.Context) error {
|
||||
userSession, err := ValidSession(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusUnauthorized, resp_json{
|
||||
Status: "Error",
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
updown := c.Param("updown")
|
||||
atb := c.Param("atb")
|
||||
var sinal string
|
||||
|
||||
switch updown {
|
||||
case "up":
|
||||
sinal = "+"
|
||||
case "down":
|
||||
sinal = "-"
|
||||
default:
|
||||
return c.JSON(http.StatusBadRequest, resp_json{
|
||||
Status: "Error",
|
||||
Message: "você passou um parametro invalido em updown, use up ou down.",
|
||||
})
|
||||
}
|
||||
|
||||
var updateList = map[string]string{
|
||||
"vida": fmt.Sprintf("UPDATE players SET vida = vida %s 1 WHERE id=?;", sinal),
|
||||
"level": fmt.Sprintf("UPDATE players SET level = level %s 1 WHERE id=?;", sinal),
|
||||
"idade": fmt.Sprintf("UPDATE players SET idade = idade %s 1 WHERE id=?;", sinal),
|
||||
"conhecimento": fmt.Sprintf("UPDATE players SET conhecimento = conhecimento %s 1 WHERE id=?;", sinal),
|
||||
"força": fmt.Sprintf("UPDATE players SET força = força %s 1 WHERE id=?;", sinal),
|
||||
"destresa": fmt.Sprintf("UPDATE players SET destresa = destresa %s 1 WHERE id=?;", sinal),
|
||||
"constituição": fmt.Sprintf("UPDATE players SET constituição = constituição %s 1 WHERE id=?;", sinal),
|
||||
"inteligencia": fmt.Sprintf("UPDATE players SET inteligencia = inteligencia %s 1 WHERE id=?;", sinal),
|
||||
"cárisma": fmt.Sprintf("UPDATE players SET cárisma = cárisma %s 1 WHERE id=?;", sinal),
|
||||
}
|
||||
|
||||
query, ok := updateList[atb]
|
||||
if !ok {
|
||||
return c.JSON(http.StatusBadRequest, resp_json{
|
||||
Status: "Error",
|
||||
Message: "você passou um parametro invalido, tente novamente com um parametro correto",
|
||||
})
|
||||
}
|
||||
_, err = db.Exec(query, userSession["user_id"])
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, resp_json{
|
||||
Status: "Error",
|
||||
Message: "algo deu errado a atualizar sua ficha",
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, resp_json{
|
||||
Status: "Success",
|
||||
Message: sinal + "1 em " + atb,
|
||||
})
|
||||
}
|
||||
|
||||
func player_updateInputs(c echo.Context) error {
|
||||
userSession, err := ValidSession(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusUnauthorized, resp_json{
|
||||
Status: "Error",
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
var lista []string
|
||||
|
||||
vida := c.FormValue("vida")
|
||||
nome := c.FormValue("nome")
|
||||
altura := c.FormValue("altura")
|
||||
inv := c.FormValue("inventario")
|
||||
|
||||
if vida != "" {
|
||||
_, err = db.Exec("UPDATE players SET vida=?, vida_maxima=? WHERE id=?;", vida, vida, userSession["user_id"])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return c.JSON(http.StatusInternalServerError, resp_json{
|
||||
Status: "Error",
|
||||
Message: "algo deu errado ao alterar sua vida.",
|
||||
})
|
||||
}
|
||||
lista = append(lista, "vida")
|
||||
}
|
||||
|
||||
if nome != "" {
|
||||
_, err = db.Exec("UPDATE players SET nome=? WHERE id=?;", nome, userSession["user_id"])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return c.JSON(http.StatusInternalServerError, resp_json{
|
||||
Status: "Error",
|
||||
Message: "algo deu errado ao alterar seu nome.",
|
||||
})
|
||||
}
|
||||
lista = append(lista, "nome")
|
||||
}
|
||||
|
||||
if altura != "" {
|
||||
_, err = db.Exec("UPDATE players SET altura=? WHERE id=?;", altura, userSession["user_id"])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return c.JSON(http.StatusInternalServerError, resp_json{
|
||||
Status: "Error",
|
||||
Message: "algo deu errado ao alterar sua altura.",
|
||||
})
|
||||
}
|
||||
lista = append(lista, "altura")
|
||||
}
|
||||
|
||||
if inv != "" {
|
||||
_, err = db.Exec("UPDATE players SET inventario=? WHERE id=?;", inv, userSession["user_id"])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return c.JSON(http.StatusInternalServerError, resp_json{
|
||||
Status: "Error",
|
||||
Message: "algo deu errado ao alterar o tamanho do seu inventario.",
|
||||
})
|
||||
}
|
||||
lista = append(lista, "inventario")
|
||||
}
|
||||
|
||||
if len(lista) <= 0 {
|
||||
return c.JSON(http.StatusUnauthorized, resp_json{
|
||||
Status: "Error",
|
||||
Message: "você é obrigado a alguma informação seu cabaço.",
|
||||
})
|
||||
}
|
||||
return c.JSON(http.StatusOK, resp_json{
|
||||
Status: "Success",
|
||||
Message: fmt.Sprintf("você mudou as seguintes informaçôes: %s", lista),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user