new game, fs repo layer
This commit is contained in:
+82
-1
@@ -1,6 +1,87 @@
|
||||
package game
|
||||
|
||||
import "math"
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/iliadenisov/galaxy/pkg/generator"
|
||||
"github.com/iliadenisov/galaxy/pkg/model/game"
|
||||
)
|
||||
|
||||
type Repo interface {
|
||||
Persist(game.Game) error
|
||||
}
|
||||
|
||||
func NewGame(r Repo, races []string) (uuid.UUID, error) {
|
||||
id, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return uuid.Nil, fmt.Errorf("generate uuid: %s", err)
|
||||
}
|
||||
m, err := generator.Generate(func(ms *generator.MapSetting) {
|
||||
ms.Players = uint32(len(races))
|
||||
})
|
||||
if err != nil {
|
||||
return uuid.Nil, fmt.Errorf("generate map: %s", err)
|
||||
}
|
||||
if len(races) != len(m.HomePlanets) {
|
||||
return uuid.Nil, fmt.Errorf("generate map: wrong number of home planets: %d, expected: %d ", len(m.HomePlanets), len(races))
|
||||
}
|
||||
g := &game.Game{
|
||||
ID: id,
|
||||
Race: make([]game.Race, len(races)),
|
||||
}
|
||||
|
||||
gameMap := &game.Map{
|
||||
Width: m.Width,
|
||||
Height: m.Height,
|
||||
Planet: make([]game.Planet, 0),
|
||||
}
|
||||
for hw := range races {
|
||||
g.Race[hw] = game.Race{
|
||||
Name: races[hw],
|
||||
Votes: 1, // TODO: check with rules
|
||||
VoteFor: races[hw],
|
||||
Drive: 1,
|
||||
Weapons: 1,
|
||||
Shields: 1,
|
||||
Cargo: 1,
|
||||
}
|
||||
gameMap.Planet = append(gameMap.Planet, game.Planet{
|
||||
Owner: races[hw],
|
||||
X: m.HomePlanets[hw].HW.Position.X,
|
||||
Y: m.HomePlanets[hw].HW.Position.Y,
|
||||
Size: m.HomePlanets[hw].HW.Size,
|
||||
Resources: m.HomePlanets[hw].HW.Resources,
|
||||
Production: game.ProductionCapital.AsType(""), // TODO: check default production
|
||||
})
|
||||
for dw := range m.HomePlanets[hw].DW {
|
||||
gameMap.Planet = append(gameMap.Planet, game.Planet{
|
||||
X: m.HomePlanets[hw].DW[dw].Position.X,
|
||||
Y: m.HomePlanets[hw].DW[dw].Position.Y,
|
||||
Size: m.HomePlanets[hw].DW[dw].Size,
|
||||
Resources: m.HomePlanets[hw].DW[dw].Resources,
|
||||
Production: game.ProductionNone.AsType(""),
|
||||
})
|
||||
}
|
||||
}
|
||||
for i := range m.FreePlanets {
|
||||
gameMap.Planet = append(gameMap.Planet, game.Planet{
|
||||
X: m.FreePlanets[i].Position.X,
|
||||
Y: m.FreePlanets[i].Position.Y,
|
||||
Size: m.FreePlanets[i].Size,
|
||||
Resources: m.FreePlanets[i].Resources,
|
||||
Production: game.ProductionNone.AsType(""),
|
||||
})
|
||||
}
|
||||
|
||||
g.Map = *gameMap
|
||||
|
||||
if err := r.Persist(*g); err != nil {
|
||||
return uuid.Nil, fmt.Errorf("persist: %s", err)
|
||||
}
|
||||
return g.ID, nil
|
||||
}
|
||||
|
||||
func (r Race) FlightDistance() float64 {
|
||||
return r.Drive * 40
|
||||
|
||||
Reference in New Issue
Block a user