You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/aziis98/lupus-lite/events"
|
|
"github.com/aziis98/lupus-lite/lupus"
|
|
"github.com/aziis98/lupus-lite/model"
|
|
)
|
|
|
|
func OnPlayerJoin(partitaUid string) string {
|
|
return fmt.Sprintf("@join partita[uid=%q]", partitaUid)
|
|
}
|
|
|
|
type PartitaConfig struct {
|
|
NumGiocatori int
|
|
NumLupi int
|
|
NumFattucchiere int
|
|
NumGuardie int
|
|
NumCacciatori int
|
|
NumMedium int
|
|
NumVeggenti int
|
|
}
|
|
|
|
type PartiteHandler interface {
|
|
EventBus() *events.EventBus
|
|
|
|
Create(ownerId string, cfg PartitaConfig) (model.Partita, error)
|
|
GetPlayers(partitaUid string) ([]string, error)
|
|
Join(partitaUid, username string) error
|
|
}
|
|
|
|
type partiteHandler struct {
|
|
*server
|
|
|
|
eventBus *events.EventBus
|
|
}
|
|
|
|
func (p *partiteHandler) Create(ownerId string, cfg PartitaConfig) (model.Partita, error) {
|
|
return p.DB.CreatePartita(ownerId, model.PartitaConfig{
|
|
NumeroGiocatori: cfg.NumGiocatori,
|
|
NumeroPerRuolo: map[string]int{
|
|
lupus.Lupo.Uid: cfg.NumLupi,
|
|
lupus.Fattucchiera.Uid: cfg.NumFattucchiere,
|
|
lupus.Guardia.Uid: cfg.NumGuardie,
|
|
lupus.Cacciatore.Uid: cfg.NumCacciatori,
|
|
lupus.Medium.Uid: cfg.NumMedium,
|
|
lupus.Veggente.Uid: cfg.NumVeggenti,
|
|
},
|
|
})
|
|
}
|
|
|
|
func (p *partiteHandler) GetPlayers(partitaUid string) ([]string, error) {
|
|
partita, err := p.DB.GetPartita(partitaUid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return partita.Players, nil
|
|
}
|
|
|
|
func (p *partiteHandler) Join(partitaUid, username string) error {
|
|
partita, err := p.DB.GetPartita(partitaUid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
partita.Players = append(partita.Players, username)
|
|
|
|
if err := p.DB.UpdatePartita(partita); err != nil {
|
|
return err
|
|
}
|
|
|
|
p.eventBus.Dispatch(OnPlayerJoin(partitaUid), partita.Players)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *partiteHandler) EventBus() *events.EventBus {
|
|
return p.eventBus
|
|
}
|