示例#1
0
func (s *Source) NewPowerOrder(fid int, planet overpower.PlanetDat) overpower.PowerOrderDat {
	po := &PowerOrder{
		GID: s.GID,
		FID: fid,
		Loc: planet.Loc(),
	}
	s.M.CreatePowerOrder(po)
	return po.Intf()
}
示例#2
0
func (s *Source) NewBattleRecord(ship overpower.ShipDat, fid, turn,
	initPrimaryFac, initPrPres,
	initSecondaryFac, initSePres int,
	result overpower.PlanetDat,
	betrayals [][2]int,
) {
	btr := make([]int, 0, len(betrayals)*2)
	for _, pt := range betrayals {
		btr = append(btr, pt[0], pt[1])
	}

	br := &BattleRecord{
		GID:       s.GID,
		FID:       fid,
		Turn:      turn,
		Loc:       result.Loc(),
		Betrayals: btr,

		InitPrimaryPresence:   initPrPres,
		InitSecondaryPresence: initSePres,
		PrimaryPresence:       result.PrimaryPresence(),
		SecondaryPresence:     result.SecondaryPresence(),
	}
	if ship != nil {
		br.ShipFaction = sql.NullInt64{Valid: true, Int64: int64(ship.FID())}
		br.ShipSize = ship.Size()
	}
	if initPrimaryFac != 0 {
		br.InitPrimaryFaction = sql.NullInt64{Valid: true, Int64: int64(initPrimaryFac)}
	}
	if initSecondaryFac != 0 {
		br.InitSecondaryFaction = sql.NullInt64{Valid: true, Int64: int64(initSecondaryFac)}
	}

	if resPrFac := result.PrimaryFaction(); resPrFac != 0 {
		br.PrimaryFaction = sql.NullInt64{Valid: true, Int64: int64(resPrFac)}
	}
	if resSeFac := result.SecondaryFaction(); resSeFac != 0 {
		br.SecondaryFaction = sql.NullInt64{Valid: true, Int64: int64(resSeFac)}
	}

	s.M.CreateBattleRecord(br)
}
示例#3
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
}
示例#4
0
func (s *Source) UpdatePlanetView(fid, turn int, planet overpower.PlanetDat) overpower.PlanetViewDat {
	pv := &PlanetView{
		GID:  s.GID,
		FID:  fid,
		Turn: turn,
		//
		Name:              planet.Name(),
		Loc:               planet.Loc(),
		PrimaryPresence:   planet.PrimaryPresence(),
		PrimaryPower:      planet.PrimaryPower(),
		SecondaryPresence: planet.SecondaryPresence(),
		SecondaryPower:    planet.SecondaryPower(),
		Antimatter:        planet.Antimatter(),
		Tachyons:          planet.Tachyons(),
	}
	pF := planet.PrimaryFaction()
	sF := planet.SecondaryFaction()
	if pF != 0 {
		pv.PrimaryFaction = sql.NullInt64{Valid: true, Int64: int64(pF)}
	}
	if sF != 0 {
		pv.SecondaryFaction = sql.NullInt64{Valid: true, Int64: int64(sF)}
	}
	pv.sql.UPDATE = true
	//
	pt := [3]int{pv.Loc[0], pv.Loc[1], fid}
	sess := s.M.PlanetView()
	if i, ok := s.updatedPVs[pt]; ok {
		sess.List[i] = pv
	} else {
		s.updatedPVs[pt] = len(sess.List)
		sess.List = append(sess.List, pv)
	}
	sess.List = append(sess.List, pv)
	return pv.Intf()
}
示例#5
0
func (s *Source) NewPlanetView(fid int, pl overpower.PlanetDat, exodus bool) overpower.PlanetViewDat {
	pv := &PlanetView{
		GID:  s.GID,
		FID:  fid,
		Loc:  pl.Loc(),
		Name: pl.Name(),
	}

	if pl.PrimaryFaction() == fid || pl.SecondaryFaction() == fid {
		pv.Turn = 1
		pF := pl.PrimaryFaction()
		sF := pl.SecondaryFaction()
		if pF != 0 {
			pv.PrimaryFaction = sql.NullInt64{Valid: true, Int64: int64(pF)}
		}
		if sF != 0 {
			pv.SecondaryFaction = sql.NullInt64{Valid: true, Int64: int64(sF)}
		}
		pv.PrimaryPresence = pl.PrimaryPresence()
		pv.PrimaryPower = pl.PrimaryPower()
		pv.SecondaryPresence = pl.SecondaryPresence()
		pv.SecondaryPower = pl.SecondaryPower()
		pv.Antimatter = pl.Antimatter()
		pv.Tachyons = pl.Tachyons()
	}
	s.M.CreatePlanetView(pv)
	return pv.Intf()
}