48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package controller
|
|
|
|
import "github.com/iliadenisov/galaxy/internal/model/game"
|
|
|
|
func TransformBattle(c *Cache, b *Battle) *game.BattleReport {
|
|
r := &game.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)),
|
|
}
|
|
|
|
cacheShipClass := make(map[string]int)
|
|
cacheRaceName := make(map[string]int)
|
|
|
|
cacher := func(shipClass string, cache map[string]int) int {
|
|
if v, ok := cache[shipClass]; ok {
|
|
return v
|
|
} else {
|
|
itemNumber := len(r.Ships)
|
|
r.Ships[itemNumber] = shipClass
|
|
cache[shipClass] = 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),
|
|
Destroyed: b.Protocol[i].Destroyed,
|
|
}
|
|
}
|
|
|
|
for name, index := range cacheRaceName {
|
|
r.Races[index] = name
|
|
}
|
|
for name, index := range cacheShipClass {
|
|
r.Ships[index] = name
|
|
}
|
|
|
|
return r
|
|
}
|