Aggiunta creazione partite
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { render } from 'preact'
|
||||
import { useState, useEffect } from 'preact/hooks'
|
||||
|
||||
const clamp = (min, value, max) => (value > max ? max : value < min ? min : value)
|
||||
|
||||
const InputNumero = ({ name, id, value, setValue }) => (
|
||||
<div class="compound-input">
|
||||
<input
|
||||
@@ -8,13 +10,22 @@ const InputNumero = ({ name, id, value, setValue }) => (
|
||||
type="number"
|
||||
name={name}
|
||||
id={id}
|
||||
min="0"
|
||||
value={value}
|
||||
onInput={e => setValue(e.target.value)}
|
||||
onInput={e => setValue(parseInt(e.target.value))}
|
||||
/>
|
||||
<button type="button" class="square" onClick={() => setValue(value => value + 1)}>
|
||||
<button
|
||||
type="button"
|
||||
class="square"
|
||||
onClick={() => setValue(value => clamp(0, value + 1, Infinity))}
|
||||
>
|
||||
<span class="material-symbols-outlined">add</span>
|
||||
</button>
|
||||
<button type="button" class="square" onClick={() => setValue(value => value - 1)}>
|
||||
<button
|
||||
type="button"
|
||||
class="square"
|
||||
onClick={() => setValue(value => clamp(0, value - 1, Infinity))}
|
||||
>
|
||||
<span class="material-symbols-outlined">remove</span>
|
||||
</button>
|
||||
</div>
|
||||
@@ -28,6 +39,8 @@ const App = () => {
|
||||
.then(res => {
|
||||
if (res.ok) {
|
||||
return res.json()
|
||||
} else {
|
||||
location.href = '/login'
|
||||
}
|
||||
})
|
||||
.then(user => setUser(user))
|
||||
|
||||
+1
-58
@@ -2,11 +2,9 @@ package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/aziis98/lupus-lite/model"
|
||||
"github.com/aziis98/lupus-lite/util"
|
||||
)
|
||||
|
||||
type Database interface {
|
||||
@@ -14,15 +12,10 @@ type Database interface {
|
||||
GetUsers() ([]model.User, error)
|
||||
GetUser(username string) (model.User, error)
|
||||
|
||||
CreatePartita(partitaConfig model.PartitaConfig) (model.Partita, error)
|
||||
CreatePartita(ownerUid string, partitaConfig model.PartitaConfig) (model.Partita, error)
|
||||
GetPartita(uid string) (model.Partita, error)
|
||||
}
|
||||
|
||||
type memDB struct {
|
||||
Users map[string]model.User `json:"users"`
|
||||
Partite map[string]model.Partita `json:"partite"`
|
||||
}
|
||||
|
||||
func NewInMemoryDB() (Database, error) {
|
||||
var db memDB
|
||||
|
||||
@@ -37,53 +30,3 @@ func NewInMemoryDB() (Database, error) {
|
||||
|
||||
return &db, nil
|
||||
}
|
||||
|
||||
func (db *memDB) GetUsers() ([]model.User, error) {
|
||||
users := make([]model.User, 0, len(db.Users))
|
||||
for _, u := range db.Users {
|
||||
users = append(users, u)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (db *memDB) GetUser(username string) (model.User, error) {
|
||||
user, ok := db.Users[username]
|
||||
if !ok {
|
||||
return model.User{}, fmt.Errorf(`no user with username %q`, username)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (db *memDB) CreateUser(user model.User) error {
|
||||
if _, ok := db.Users[user.Username]; ok {
|
||||
return fmt.Errorf(`user with username %q already exists`, user.Username)
|
||||
}
|
||||
|
||||
db.Users[user.Username] = user
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *memDB) CreatePartita(partitaConfig model.PartitaConfig) (model.Partita, error) {
|
||||
uid := util.GenerateRandomString(12)
|
||||
partita := model.Partita{
|
||||
Uid: uid,
|
||||
Config: partitaConfig,
|
||||
Players: []string{},
|
||||
Iniziata: false,
|
||||
}
|
||||
|
||||
db.Partite[uid] = partita
|
||||
|
||||
return partita, nil
|
||||
}
|
||||
|
||||
func (db *memDB) GetPartita(uid string) (model.Partita, error) {
|
||||
partita, ok := db.Partite[uid]
|
||||
if !ok {
|
||||
return model.Partita{}, fmt.Errorf(`nessuna partita con uid %q`, uid)
|
||||
}
|
||||
|
||||
return partita, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/aziis98/lupus-lite/model"
|
||||
"github.com/aziis98/lupus-lite/util"
|
||||
)
|
||||
|
||||
type memDB struct {
|
||||
Users map[string]model.User `json:"users"`
|
||||
Partite map[string]model.Partita `json:"partite"`
|
||||
}
|
||||
|
||||
func (db *memDB) GetUsers() ([]model.User, error) {
|
||||
users := make([]model.User, 0, len(db.Users))
|
||||
for _, u := range db.Users {
|
||||
users = append(users, u)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (db *memDB) GetUser(username string) (model.User, error) {
|
||||
user, ok := db.Users[username]
|
||||
if !ok {
|
||||
return model.User{}, fmt.Errorf(`no user with username %q`, username)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (db *memDB) CreateUser(user model.User) error {
|
||||
if _, ok := db.Users[user.Username]; ok {
|
||||
return fmt.Errorf(`user with username %q already exists`, user.Username)
|
||||
}
|
||||
|
||||
db.Users[user.Username] = user
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *memDB) CreatePartita(ownerUid string, partitaConfig model.PartitaConfig) (model.Partita, error) {
|
||||
uid := util.GenerateRandomString(16)
|
||||
partita := model.Partita{
|
||||
Uid: uid,
|
||||
OwnerUid: ownerUid,
|
||||
Config: partitaConfig,
|
||||
Players: []string{},
|
||||
Iniziata: false,
|
||||
}
|
||||
|
||||
db.Partite[uid] = partita
|
||||
|
||||
return partita, nil
|
||||
}
|
||||
|
||||
func (db *memDB) GetPartita(uid string) (model.Partita, error) {
|
||||
partita, ok := db.Partite[uid]
|
||||
if !ok {
|
||||
return model.Partita{}, fmt.Errorf(`nessuna partita con uid %q`, uid)
|
||||
}
|
||||
|
||||
return partita, nil
|
||||
}
|
||||
@@ -8,6 +8,7 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/alecthomas/repr v0.1.0 // indirect
|
||||
github.com/andybalholm/brotli v1.0.4 // indirect
|
||||
github.com/klauspost/compress v1.15.0 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
|
||||
github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
|
||||
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
|
||||
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||
github.com/gofiber/fiber/v2 v2.34.1 h1:C6saXB7385HvtXX+XMzc5Dqj5S/aEXOfKCW7JNep4rA=
|
||||
|
||||
+8
-9
@@ -18,16 +18,15 @@ func (u User) PublicUser() PublicUser {
|
||||
}
|
||||
|
||||
type PartitaConfig struct {
|
||||
// NumeroGiocatori indica il numero totale di giocatori (per capire quanto tutti sono entrati nella partita)
|
||||
NumeroGiocatori int
|
||||
|
||||
// NumeroPerRuolo indica quanti giocatori di un certo ruolo ci dovrebbero essere
|
||||
NumeroPerRuolo map[string]int
|
||||
NumeroGiocatori int `json:"numeroGiocatori"` // NumeroGiocatori indica il numero totale di giocatori (per capire quanto tutti sono entrati nella partita)
|
||||
NumeroPerRuolo map[string]int `json:"numeroPerRuolo"` // NumeroPerRuolo indica quanti giocatori di un certo ruolo ci dovrebbero essere
|
||||
}
|
||||
|
||||
type Partita struct {
|
||||
Uid string // Uid è il codice identificativo della partita.
|
||||
Config PartitaConfig // Config è la configurazione iniziale della partita.
|
||||
Players []string // Players è la lista di giocatori che sono entrati in questa partita.
|
||||
Iniziata bool // Iniziata indica se la partita è iniziata, quando lo è c'è una tabella corrispondente StatoPartita con le informazioni sui vari giocatori e sullo stato attuale del gioco.
|
||||
Uid string `json:"uid"`
|
||||
OwnerUid string `json:"ownerUid"` // OwnerUid è l'uid dell'utente che ha creato la partita.
|
||||
|
||||
Config PartitaConfig `json:"config"` // Config è la configurazione iniziale della partita.
|
||||
Players []string `json:"players"` // Players è la lista di giocatori che sono entrati fino ad ora nella partita.
|
||||
Iniziata bool `json:"iniziata"` // Iniziata indica se la partita è iniziata, quando lo è c'è una tabella corrispondente.
|
||||
}
|
||||
|
||||
@@ -3,10 +3,14 @@ package routes
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/alecthomas/repr"
|
||||
"github.com/aziis98/lupus-lite/auth"
|
||||
"github.com/aziis98/lupus-lite/database"
|
||||
"github.com/aziis98/lupus-lite/lupus"
|
||||
"github.com/aziis98/lupus-lite/model"
|
||||
"github.com/aziis98/lupus-lite/util"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
@@ -93,6 +97,60 @@ func Api(api fiber.Router) {
|
||||
return c.Redirect("/login")
|
||||
})
|
||||
|
||||
api.Post("/crea-partita", requireLogged, func(c *fiber.Ctx) error {
|
||||
user := requestUser(c)
|
||||
|
||||
var form struct {
|
||||
NumGiocatori string `form:"numero-giocatori"`
|
||||
NumLupi string `form:"numero-lupi"`
|
||||
NumFattucchiere string `form:"numero-fattucchiere"`
|
||||
NumGuardie string `form:"numero-guardie"`
|
||||
NumCacciatori string `form:"numero-cacciatori"`
|
||||
NumMedium string `form:"numero-medium"`
|
||||
NumVeggenti string `form:"numero-veggenti"`
|
||||
}
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
repr.Println(form)
|
||||
|
||||
cfg := model.PartitaConfig{
|
||||
NumeroPerRuolo: map[string]int{},
|
||||
}
|
||||
|
||||
if util.AtoiInto(form.NumGiocatori, &cfg.NumeroGiocatori); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Questo codice magico scorre su questa "tabella" con colonne ruolo e stringa del ruolo nel form e popola la mappa NumeroPerRuolo da uid ruolo a numero per ruolo.
|
||||
for _, r := range []struct {
|
||||
ruolo lupus.Ruolo
|
||||
num string
|
||||
}{
|
||||
{lupus.Lupo, form.NumLupi},
|
||||
{lupus.Fattucchiera, form.NumFattucchiere},
|
||||
{lupus.Guardia, form.NumGuardie},
|
||||
{lupus.Cacciatore, form.NumCacciatori},
|
||||
{lupus.Medium, form.NumMedium},
|
||||
{lupus.Veggente, form.NumVeggenti},
|
||||
} {
|
||||
num, err := strconv.Atoi(r.num)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg.NumeroPerRuolo[r.ruolo.Uid] = num
|
||||
}
|
||||
|
||||
partita, err := db.CreatePartita(user.Username, cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(partita)
|
||||
})
|
||||
|
||||
api.Get("/user", requireLogged, func(c *fiber.Ctx) error {
|
||||
return c.JSON(requestUser(c).PublicUser())
|
||||
})
|
||||
|
||||
@@ -2,6 +2,7 @@ package util
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
@@ -31,3 +32,13 @@ func IsWhitespaceFree(s string) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func AtoiInto(s string, into *int) error {
|
||||
num, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*into = num
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user