Пример #1
0
def radar(request, pp):
    '''needs a presence in 'pp' '''
    if pp.owner != getAccount(request): return HttpResponse('ACCOUNT')

    radarlevel = 0
    for i in xrange(0, pp.province.slots):
        otype, olvl = pp.getPair(i)
        if otype == 4:
            if olvl > radarlevel: radarlevel = olvl

    if radarlevel == 0: return HttpResponse('NORADAR')

    ms = Mother.objects.filter(orbiting=pp.province.planet)
    mros_i = MotherRelocationOrder.objects.filter(loc_to=pp.province.planet)
    mros_o = MotherRelocationOrder.objects.filter(loc_from=pp.province.planet)

    sms = filter(lambda x: (not x in mros_i) and (not x in mros_o), ms)

    srl = RadarReportv01(pp.province, radarlevel)

    if radarlevel >= 1:   # radar lv 1
        for mum in sms:
            srl.appendStationary(mum)
    if radarlevel >= 2:   # radar lv 2
        for mum in mros_i:
            srl.appendInbound(mum)
    if radarlevel >= 3:   # radar lv 3
        for mum in mros_o:
            srl.appendOutbound(mum)

    rp = makeReport(srl)
    sendTo(rp, getAccount(request), u'Raport radaru z '+pp.province.name)

    return HttpResponse('OK')
Пример #2
0
def doScan(account, mother, province):
    '''Exception on failure. Will use Exceptions unicode() to generate error message in HTML.
    Returns a report'''
    attacker_spy_level = account.technology.o_13        # Racial tech, attacker is always Magnuss

#    if ResourceIndex(titan=5000, pluton=2000, men=50) >= mother.owner.resources.stateUpdate():
#        raise ScanException(u'Nie stać cię! Skan wymaga 5000 tytanu, 2000 plutonu i 50 manpower!')

#    mother.owner.resources -= ResourceIndex(titan=5000, pluton=2000, men=50)
#    mother.owner.resources.save()
#    note(mother.owner.id, ResourceIndex(titan=5000, pluton=2000, men=50), 'scans')


    rep = ScanReportv01()
    rep.setProvince(province)

    try:
        province.presence
    except:
        rep.setFree(True)
        gamma = attacker_spy_level + choice((-1, 0, 1))
        if gamma > 4:
            rep.setResourceCoefficients(province.town_coef, province.titan_coef, province.pluton_coef)

    else:
        rep.setFree(False)

        if province.presence.owner == account:
            # If scanning self!
            attacker_spy_level = 10000
            defender_counterspy_level = 0

        defender_counterspy_level = province.presence.owner.technology.o_5
        gamma = attacker_spy_level - defender_counterspy_level + choice((-1, 0, 1))

        if gamma > -65535:
            rep.setOwner(province.presence.owner)
        if gamma > -3:
            rep.setNaturalDefenseLevel(province.natural_defense_level)
        if gamma > -1:
            rep.setBuildings(province.presence)
        if gamma > 1:
            rep.setGarrison(province.presence.owner, province.presence.garrison)
        if gamma > 3:
            for reinf in province.presence.reinforcement_set.all():
                rep.setGarrison(reinf.owner, reinf.garrison)
        if gamma > 4:
            rep.setResourceCoefficients(province.town_coef, province.titan_coef, province.pluton_coef)

    mrp = makeReport(rep, 'Raport szpiegowski z '+province.name)
    sendTo(mrp, account, True)
Пример #3
0
def doLandarmyPlanetaryStrikeOrder(entry):
    '''
        If order was to reinforce, signal Exception
    '''
    lpso = LandarmyPlanetaryStrikeOrder.objects.get(got=entry)

    wtd = whatToDo(lpso.mother.owner, lpso.province, lpso.designation)

    if wtd == 'REINFORCE':
        if lpso.mother.owner == lpso.province.presence.owner:      # Reinforce own province
            lpso.province.presence.garrison += lpso.garrison
            lpso.province.presence.garrison.save()
            lpso.garrison.delete()
            lpso.delete()
            return
                                                             # Reinforce somebody's else province
        try:
            reinforcement = lpso.province.presence.reinforcement_set.get(owner=lpso.mother.owner)
        except:                          # I have no reinforcements in this province
            Reinforcement(None, lpso.mother.owner.id, lpso.province.presence.id, lpso.garrison.id, lpso.orders).save()
            lpso.delete()
            return
        else:                            # I do have reinforcements on this province
            reinforcement.garrison += lpso.garrison
            reinforcement.garrison.save()
            lpso.garrison.delete()
            lpso.delete()
            return
    elif wtd == 'FALLBACK':
        lmpo = LandarmyMotherPickupOrder(None, lpso.got.id, lpso.mother.id, lpso.province.id, lpso.garrison.id)
        lmpo.save()
        lpso.got.ordertype = 8
        lpso.got.to_be_completed += timedelta(0, int(pmmovelen(lpso.province, lpso.mother, lpso.garrison)))
        lpso.got.save()
        lpso.delete()
        raise DontRemoveGOT
    elif wtd == 'ASSAULT':
        try:
            lpso.province.presence          # If uninhabited?
        except:
            pres = ProvintionalPresence(id=None,
                                        garrison=lpso.garrison,
                                        owner=lpso.mother.owner,
                                        province=lpso.province,
                                        garrison_orders=lpso.orders)
            pres.save()
            lpso.delete()
            return

        report = WarfareReport()
        report.initializeEnvironment(lpso.mother, lpso.province, datetime.now())
        report.initializeParties(lpso.mother.owner, lpso.province.presence.owner)
        attacker_army, defender_army = prepare(lpso)
        attacker_won = perform(attacker_army, defender_army, report, is_drop=True)
        cleanup(attacker_army, defender_army, report, attacker_won)

        rep = makeReport(report, u'Raport wojenny z '+lpso.province.name)
        sendTo(rep, lpso.mother.owner, False)
        sendTo(rep, lpso.province.presence.owner, False)
        for reinf in lpso.province.presence.reinforcement_set.all():
            sendTo(rep, reinf.owner, False)

        note(None, LX_DROP_COMBAT_LAND, attacker_id=lpso.mother.owner,
                                        defender_id=lpso.province.presence.owner,
                                        province_id=lpso.province,
                                        attacker_won=attacker_won)

        if attacker_won:
            defender = lpso.province.presence.owner
                    # Elliminate all reinforcements
            reinfs = Reinforcement.objects.filter(presence=lpso.province.presence)
            for reinf in reinfs:
                reinf.garrison.delete()
                reinf.delete()

            lpso.province.presence.unsettle_dueToAssault()

            lpso.province.presence.garrison.clone(lpso.garrison)# Copy to presence's garrison info about garrison
            lpso.province.presence.garrison.save()

            lpso.province.presence.owner = lpso.mother.owner  # Change owner
            lpso.province.presence.save()

            lpso.garrison.delete()

            from bellum.common.fixtures.resources import recalc
            recalc(defender)
            recalc(lpso.mother.owner)
        if attacker_won in (False, None):       # save garrisons
            # Save all reinforcement garrisons, delete if necessary
            for reinf in lpso.province.presence.reinforcement_set.all():
                if reinf.garrison.isZero():
                    reinf.garrison.delete()
                    reinf.delete()
                else:
                    reinf.garrison.save()

            lpso.province.presence.garrison.save()
            if attacker_won == False: lpso.garrison.delete()
        if attacker_won == None:
            lpso.garrison.save()
            lpso.fallback()
            raise DontRemoveGOT

        lpso.delete()    
Пример #4
0
def doLandarmyProvintionalStrikeOrder(entry, lpso=None):
    '''
        If order was to reinforce, signal Exception
    '''

    if lpso == None: lpso = LandarmyProvintionalStrikeOrder.objects.get(got=entry)

    wtd = whatToDo(lpso.attacker, lpso.dstprovince, lpso.designation)

    if wtd == 'REINFORCE':
        if lpso.attacker == lpso.dstprovince.presence.owner:      # Reinforce own province
            lpso.dstprovince.presence.garrison  # make sure it's ready
            lpso.dstprovince.presence.garrison += lpso.garrison
            lpso.garrison.delete()
            lpso.dstprovince.presence.garrison.save()
            lpso.delete()
            return
        try:
            reinforcement = lpso.dstprovince.presence.reinforcement_set.get(owner=lpso.attacker)
        except: # Was not previously reinforced, need to make new classes
            Reinforcement(None, lpso.attacker.id, lpso.dstprovince.presence.id, lpso.garrison.id, lpso.orders).save()
            lpso.delete()
            return
        else:   # Reinforce already reinforced troops
            reinforcement.garrison += lpso.garrison
            reinforcement.garrison.save()
            lpso.garrison.delete()
            lpso.delete()
            return
    elif wtd == 'FALLBACK':
        lpso.directiveFallback(0)
        raise DontRemoveGOT
    elif wtd == 'ASSAULT':
        try:
            lpso.dstprovince.presence             # If uninhabited?
        except:
            pres = ProvintionalPresence(id=None,
                                        garrison=lpso.garrison,
                                        owner=lpso.attacker,
                                        province=lpso.dstprovince,
                                        garrison_orders=0)
            pres.save()
            lpso.delete()
            return

        # War!
        # lpso.attacker WITH HIS lpso.garrison IS ATTACKING lpso.dstprovince

        report = WarfareReport()
        report.initializeEnvironment(lpso.srcprovince, lpso.dstprovince, datetime.now())
        report.initializeParties(lpso.attacker, lpso.dstprovince.presence.owner)
        attacker_army, defender_army = prepare(lpso)
        attacker_won = perform(attacker_army, defender_army, report)
        cleanup(attacker_army, defender_army, report, attacker_won)

        rep = makeReport(report, u'Raport wojenny z '+lpso.dstprovince.name)
        sendTo(rep, lpso.attacker, False)
        sendTo(rep, lpso.dstprovince.presence.owner, False)
        for reinf in lpso.dstprovince.presence.reinforcement_set.all():
            sendTo(rep, reinf.owner, False)


        note(None, LX_PROVINCE_COMBAT_LAND, attacker_id=lpso.attacker.id,
                                                defender_id=lpso.dstprovince.presence.owner.id,
                                                source_pid=lpso.srcprovince.id,
                                                target_pid=lpso.dstprovince.id,
                                                attacker_won=attacker_won)

        if attacker_won == True:
            defender = lpso.dstprovince.presence.owner

            lpso.dstprovince.presence.unsettle_dueToAssault()

            lpso.dstprovince.presence.garrison.clone(lpso.garrison)
            lpso.dstprovince.presence.garrison.save()
            lpso.dstprovince.presence.owner = lpso.attacker
            lpso.dstprovince.presence.save()
            lpso.garrison.delete()

            # Slay all reinforcement garrisons
            for reinf in lpso.dstprovince.presence.reinforcement_set.all():
                reinf.garrison.delete()
                reinf.delete()                            
                
            from bellum.common.fixtures.resources import recalc
            recalc(defender)
            recalc(lpso.attacker)
        else: # if (attacker_won == False) or (attacker_won == None):
            # Save all reinforcement garrisons, delete if necessary
            for reinf in lpso.dstprovince.presence.reinforcement_set.all():
                if reinf.garrison.isZero():
                    reinf.garrison.delete()
                    reinf.delete()
                else:
                    reinf.garrison.save()

            lpso.dstprovince.presence.garrison.save()
            if attacker_won == False: lpso.garrison.delete()
        if attacker_won == None:  # stalemate
            # pack attackers bags and send him home
            lpso.garrison.save()
            lpso.directiveFallback()
            raise DontRemoveGOT
    
        lpso.delete()