race quit, transfer state, refactor
This commit is contained in:
@@ -118,29 +118,39 @@ func (c *Cache) DeleteShipGroup(i int) {
|
||||
}
|
||||
|
||||
func (c *Cache) DeleteKilledShipGroups() {
|
||||
keepFleet := make(map[uuid.UUID]bool, len(c.g.Fleets))
|
||||
for i := len(c.g.ShipGroups) - 1; i >= 0; i-- {
|
||||
if c.g.ShipGroups[i].FleetID != nil {
|
||||
id := *c.g.ShipGroups[i].FleetID
|
||||
keepFleet[id] = keepFleet[id] || c.g.ShipGroups[i].Number > 0
|
||||
}
|
||||
if c.g.ShipGroups[i].Number == 0 {
|
||||
c.unsafeDeleteShipGroup(i)
|
||||
}
|
||||
}
|
||||
// TODO: delete empty fleets
|
||||
}
|
||||
|
||||
func (c *Controller) JoinEqualGroups(raceName string) error {
|
||||
ri, err := c.Cache.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
for id, keep := range keepFleet {
|
||||
if keep {
|
||||
continue
|
||||
}
|
||||
c.unsafeDeleteFleet(c.MustFleetIndex(id))
|
||||
}
|
||||
c.Cache.JoinEqualGroups(ri)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) TurnMergeEqualShipGroups() {
|
||||
for i := range c.g.Race {
|
||||
for i := range c.listRaceActingIdx() {
|
||||
c.transferPendingGroups(i)
|
||||
c.JoinEqualGroups(i)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) transferPendingGroups(ri int) {
|
||||
for sg := range c.listShipGroups(ri) {
|
||||
if sg.State() == game.StateTransfer {
|
||||
sg.StateTransfer = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) JoinEqualGroups(ri int) {
|
||||
c.validateRaceIndex(ri)
|
||||
raceGroups := make([]game.ShipGroup, 0)
|
||||
@@ -183,30 +193,14 @@ func (c *Cache) JoinEqualGroups(ri int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) BreakGroup(raceName string, groupIndex, quantity uint) error {
|
||||
ri, err := c.Cache.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Cache.BreakGroup(ri, groupIndex, quantity)
|
||||
}
|
||||
|
||||
func (c *Controller) DisassembleGroup(raceName string, groupIndex, quantity uint) error {
|
||||
ri, err := c.Cache.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Cache.DisassembleGroup(ri, groupIndex, quantity)
|
||||
}
|
||||
|
||||
func (c *Cache) DisassembleGroup(ri int, groupIndex, quantity uint) error {
|
||||
sgi, ok := c.raceShipGroupIndex(ri, groupIndex)
|
||||
if !ok {
|
||||
return e.NewEntityNotExistsError("group #%d", groupIndex)
|
||||
}
|
||||
|
||||
if c.ShipGroup(sgi).State() != game.StateInOrbit {
|
||||
return e.NewShipsBusyError()
|
||||
if state := c.ShipGroup(sgi).State(); state != game.StateInOrbit {
|
||||
return e.NewShipsBusyError("state: %s", state)
|
||||
}
|
||||
|
||||
if c.ShipGroup(sgi).Number < quantity {
|
||||
@@ -254,18 +248,6 @@ func (c *Cache) DisassembleGroup(ri int, groupIndex, quantity uint) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) LoadCargo(raceName string, groupIndex uint, cargoType string, ships uint, quantity float64) error {
|
||||
ri, err := c.Cache.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ct, ok := game.CargoTypeSet[cargoType]
|
||||
if !ok {
|
||||
return e.NewCargoTypeInvalidError(cargoType)
|
||||
}
|
||||
return c.Cache.LoadCargo(ri, groupIndex, ct, ships, quantity)
|
||||
}
|
||||
|
||||
// Корабль может нести только один тип груза одновременно.
|
||||
// Возможные типы груза - это колонисты, сырье и промышленность.
|
||||
// Груз может быть доставлен на борт корабля с Вашей или не занятой планеты, на которой он имеется.
|
||||
@@ -277,8 +259,8 @@ func (c *Cache) LoadCargo(ri int, groupIndex uint, ct game.CargoType, ships uint
|
||||
if !ok {
|
||||
return e.NewEntityNotExistsError("group #%d", groupIndex)
|
||||
}
|
||||
if c.ShipGroup(sgi).State() != game.StateInOrbit {
|
||||
return e.NewShipsBusyError()
|
||||
if state := c.ShipGroup(sgi).State(); state != game.StateInOrbit {
|
||||
return e.NewShipsBusyError("state: %s", state)
|
||||
}
|
||||
p, ok := c.Planet(c.ShipGroup(sgi).Destination)
|
||||
if !ok {
|
||||
@@ -337,14 +319,6 @@ func (c *Cache) LoadCargo(ri int, groupIndex uint, ct game.CargoType, ships uint
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) UnloadCargo(raceName string, groupIndex uint, ships uint, quantity float64) error {
|
||||
ri, err := c.Cache.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Cache.UnloadCargo(ri, groupIndex, ships, quantity)
|
||||
}
|
||||
|
||||
// Промышленность и Сырье могут быть выгружены на любой планете.
|
||||
// Колонисты могут быть высажены только на планеты, принадлежащие Вам или на необитаемые планеты.
|
||||
func (c *Cache) UnloadCargo(ri int, groupIndex uint, ships uint, quantity float64) error {
|
||||
@@ -356,8 +330,8 @@ func (c *Cache) UnloadCargo(ri int, groupIndex uint, ships uint, quantity float6
|
||||
if !ok {
|
||||
return e.NewEntityNotExistsError("group #%d", groupIndex)
|
||||
}
|
||||
if c.ShipGroup(sgi).State() != game.StateInOrbit {
|
||||
return e.NewShipsBusyError()
|
||||
if state := c.ShipGroup(sgi).State(); state != game.StateInOrbit {
|
||||
return e.NewShipsBusyError("state: %s", state)
|
||||
}
|
||||
st := c.ShipGroupShipClass(sgi)
|
||||
if st.Cargo < 1 {
|
||||
@@ -429,19 +403,7 @@ func (c *Cache) unsafeUnloadCargo(sgi int, q float64) {
|
||||
p.UnpackCapital()
|
||||
}
|
||||
|
||||
func (c *Controller) GiveawayGroup(raceName, raceAcceptor string, groupIndex, quantity uint) error {
|
||||
ri, err := c.Cache.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
riAccept, err := c.Cache.raceIndex(raceAcceptor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Cache.GiveawayGroup(ri, riAccept, groupIndex, quantity)
|
||||
}
|
||||
|
||||
func (c *Cache) GiveawayGroup(ri, riAccept int, groupIndex, quantity uint) (err error) {
|
||||
func (c *Cache) TransferGroup(ri, riAccept int, groupIndex, quantity uint) (err error) {
|
||||
if ri == riAccept {
|
||||
return e.NewSameRaceError(c.g.Race[riAccept].Name)
|
||||
}
|
||||
@@ -449,8 +411,13 @@ func (c *Cache) GiveawayGroup(ri, riAccept int, groupIndex, quantity uint) (err
|
||||
if !ok {
|
||||
return e.NewEntityNotExistsError("group #%d", groupIndex)
|
||||
}
|
||||
if c.ShipGroup(sgi).Number < quantity {
|
||||
return e.NewBeakGroupNumberNotEnoughError("%d<%d", c.ShipGroup(sgi).Number, quantity)
|
||||
sg := c.ShipGroup(sgi)
|
||||
state := sg.State()
|
||||
if state == game.StateTransfer {
|
||||
return e.NewShipsBusyError("state: %s", state)
|
||||
}
|
||||
if sg.Number < quantity {
|
||||
return e.NewBeakGroupNumberNotEnoughError("%d<%d", sg.Number, quantity)
|
||||
}
|
||||
|
||||
st := c.ShipGroupShipClass(sgi)
|
||||
@@ -474,18 +441,22 @@ func (c *Cache) GiveawayGroup(ri, riAccept int, groupIndex, quantity uint) (err
|
||||
stAcc = len(c.g.Race[riAccept].ShipTypes) - 1
|
||||
}
|
||||
|
||||
sg := *(c.ShipGroup(sgi))
|
||||
sg.TypeID = c.g.Race[riAccept].ShipTypes[stAcc].ID
|
||||
sg.Number = uint(quantity)
|
||||
sg.Tech = maps.Clone(sg.Tech)
|
||||
c.appendShipGroup(riAccept, &sg)
|
||||
newGroup := *(sg)
|
||||
newGroup.TypeID = c.g.Race[riAccept].ShipTypes[stAcc].ID
|
||||
newGroup.Tech = maps.Clone(sg.Tech)
|
||||
if state == game.StateLaunched {
|
||||
newGroup.StateTransfer = true
|
||||
}
|
||||
|
||||
if quantity == 0 || quantity == c.ShipGroup(sgi).Number {
|
||||
if quantity == 0 || quantity == sg.Number {
|
||||
c.unsafeDeleteShipGroup(sgi)
|
||||
} else {
|
||||
c.ShipGroup(sgi).Number -= quantity
|
||||
newGroup.Number = quantity
|
||||
sg.Number -= quantity
|
||||
}
|
||||
|
||||
c.appendShipGroup(riAccept, &newGroup)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -502,7 +473,7 @@ func (c *Cache) BreakGroup(ri int, groupIndex, quantity uint) error {
|
||||
return e.NewEntityNotExistsError("group #%d", groupIndex)
|
||||
}
|
||||
|
||||
if c.ShipGroup(sgi).State() != game.StateInOrbit {
|
||||
if state := c.ShipGroup(sgi).State(); state != game.StateInOrbit {
|
||||
return e.NewShipsBusyError()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user