feat: store battles and bombings
This commit is contained in:
@@ -1,46 +1,80 @@
|
||||
package controller
|
||||
|
||||
import "github.com/iliadenisov/galaxy/internal/model/game"
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
"github.com/iliadenisov/galaxy/internal/model/report"
|
||||
)
|
||||
|
||||
func TransformBattle(c *Cache, b *Battle) *game.BattleReport {
|
||||
r := &game.BattleReport{
|
||||
func TransformBattle(c *Cache, b *Battle) *report.BattleReport {
|
||||
r := &report.BattleReport{
|
||||
ID: b.ID,
|
||||
Planet: b.Planet,
|
||||
PlanetName: c.MustPlanet(b.Planet).Name,
|
||||
Races: make(map[int]string),
|
||||
Ships: make(map[int]string),
|
||||
Protocol: make([]game.BattleActionReport, len(b.Protocol)),
|
||||
Races: make(map[int]uuid.UUID),
|
||||
Ships: make(map[int]report.BattleReportGroup),
|
||||
Protocol: make([]report.BattleActionReport, len(b.Protocol)),
|
||||
}
|
||||
|
||||
cacheShipClass := make(map[string]int)
|
||||
cacheRaceName := make(map[string]int)
|
||||
cacheShipClass := make(map[uuid.UUID]int)
|
||||
cacheRaceName := make(map[uuid.UUID]int)
|
||||
|
||||
cacher := func(shipClass string, cache map[string]int) int {
|
||||
if v, ok := cache[shipClass]; ok {
|
||||
addShipGroup := func(groupId int, inBattle bool) int {
|
||||
shipClass := c.ShipGroupShipClass(groupId)
|
||||
sg := c.ShipGroup(groupId)
|
||||
itemNumber := len(r.Ships)
|
||||
r.Ships[itemNumber] = report.BattleReportGroup{
|
||||
OwnerID: sg.OwnerID,
|
||||
InBattle: inBattle,
|
||||
Number: b.initialNumbers[groupId],
|
||||
NumberLeft: sg.Number,
|
||||
ClassName: shipClass.Name,
|
||||
LoadType: sg.CargoString(),
|
||||
LoadQuantity: sg.Load,
|
||||
Drive: sg.TechLevel(game.TechDrive),
|
||||
Weapons: sg.TechLevel(game.TechWeapons),
|
||||
Shields: sg.TechLevel(game.TechShields),
|
||||
Cargo: sg.TechLevel(game.TechCargo),
|
||||
}
|
||||
cacheShipClass[shipClass.ID] = itemNumber
|
||||
return itemNumber
|
||||
}
|
||||
|
||||
ship := func(groupId int) int {
|
||||
shipClass := c.ShipGroupShipClass(groupId)
|
||||
if v, ok := cacheShipClass[shipClass.ID]; ok {
|
||||
return v
|
||||
} else {
|
||||
itemNumber := len(r.Ships)
|
||||
r.Ships[itemNumber] = shipClass
|
||||
cache[shipClass] = itemNumber
|
||||
return addShipGroup(groupId, true)
|
||||
}
|
||||
}
|
||||
|
||||
race := func(groupId int) int {
|
||||
race := c.ShipGroupOwnerRace(groupId)
|
||||
if v, ok := cacheRaceName[race.ID]; ok {
|
||||
return v
|
||||
} else {
|
||||
itemNumber := len(r.Races)
|
||||
r.Races[itemNumber] = race.ID
|
||||
cacheRaceName[race.ID] = itemNumber
|
||||
return itemNumber
|
||||
}
|
||||
}
|
||||
|
||||
for i := range b.Protocol {
|
||||
r.Protocol[i] = game.BattleActionReport{
|
||||
Attacker: cacher(c.ShipGroupOwnerRace(b.Protocol[i].Attacker).Name, cacheRaceName),
|
||||
AttackerShipClass: cacher(c.ShipGroupShipClass(b.Protocol[i].Attacker).Name, cacheShipClass),
|
||||
Defender: cacher(c.ShipGroupOwnerRace(b.Protocol[i].Defenter).Name, cacheRaceName),
|
||||
DefenderShipClass: cacher(c.ShipGroupShipClass(b.Protocol[i].Defenter).Name, cacheShipClass),
|
||||
r.Protocol[i] = report.BattleActionReport{
|
||||
Attacker: race(b.Protocol[i].Attacker),
|
||||
AttackerShipClass: ship(b.Protocol[i].Attacker),
|
||||
Defender: race(b.Protocol[i].Defender),
|
||||
DefenderShipClass: ship(b.Protocol[i].Defender),
|
||||
Destroyed: b.Protocol[i].Destroyed,
|
||||
}
|
||||
}
|
||||
|
||||
for name, index := range cacheRaceName {
|
||||
r.Races[index] = name
|
||||
}
|
||||
for name, index := range cacheShipClass {
|
||||
r.Ships[index] = name
|
||||
for sgi, inBattle := range b.observerGroups {
|
||||
if !inBattle {
|
||||
addShipGroup(sgi, false)
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
|
||||
Reference in New Issue
Block a user