Esempio n. 1
0
func (s *Source) NewLaunchRecord(turn int, o overpower.LaunchOrderDat, ship overpower.ShipDat) {
	lr := &LaunchRecord{
		GID:       s.GID,
		FID:       o.FID(),
		Turn:      turn,
		Source:    o.Source(),
		Target:    o.Target(),
		OrderSize: o.Size(),
	}
	if ship != nil {
		lr.Size = ship.Size()
	}
	s.M.CreateLaunchRecord(lr)
}
Esempio n. 2
0
func InternalSetLaunchOrder(gid, fid, size int, source, target hexagon.Coord) (errS, errU error) {
	manager := OPDB.NewManager()
	planets, err := manager.Planet().SelectByLocs(gid, source, target)
	if my, bad := Check(err, "internal set order failure on resource aquisition", "resource", "planets", "gid", gid, "source", source, "target", target); bad {
		return my, nil
	}
	if len(planets) != 2 {
		return nil, NewError("Planets not found for given locations")
	}
	var sPl, tPl overpower.PlanetDat
	if planets[0].Loc() == source {
		sPl = planets[0]
	} else if planets[0].Loc() == target {
		tPl = planets[0]
	}
	if planets[1].Loc() == source {
		sPl = planets[1]
	} else if planets[1].Loc() == target {
		tPl = planets[1]
	}
	if tPl == nil || sPl == nil {
		return nil, NewError("Could not sort source/target planets")
	}
	var powerType, avail int
	if sPl.PrimaryFaction() == fid {
		powerType = sPl.PrimaryPower()
	} else if sPl.SecondaryFaction() == fid {
		powerType = sPl.SecondaryPower()
	} else {
		return nil, NewError("Faction not in control of source planet")
	}
	switch powerType {
	case overpower.ANTIMATTER:
		avail = sPl.Antimatter()
	case overpower.TACHYONS:
		avail = sPl.Tachyons()
	default:
		return nil, NewError("Faction is not aligned to a power type for source planet")
	}
	orders, err := manager.LaunchOrder().SelectBySource(gid, fid, source)
	if my, bad := Check(err, "internal setorder failure on resource aquisition: select order by source", "gid", gid, "fid", fid, "source", source); bad {
		return my, nil
	}
	var used int
	var o overpower.LaunchOrderDat
	for _, test := range orders {
		if test.Target() == target {
			o = test
		} else {
			used += test.Size()
		}
	}
	if size < 1 {
		if o != nil {
			o.DELETE()
			err := manager.Close()
			if my, bad := Check(err, "internal setorder failure on save order deletion", "order", o); bad {
				return my, nil
			}
		}
		return nil, nil
	}
	if size > avail-used {
		return nil, NewError("Order size is too large for available resources")
	}
	if o != nil {
		o.SetSize(size)
		err := manager.Close()
		if my, bad := Check(err, "internal setorder failure on save order update", "order", o, "size", size); bad {
			return my, nil
		}
		return nil, nil
	}
	newO := &models.LaunchOrder{
		GID:    gid,
		FID:    fid,
		Size:   size,
		Source: source,
		Target: target,
	}
	manager.CreateLaunchOrder(newO)
	err = manager.Close()
	if my, bad := Check(err, "internal setorder failure on save order create", "order", newO); bad {
		return my, nil
	}
	return nil, nil
}