Exemple #1
0
 def locationreset(self):
     self.location = self.owner
     self.inteltime = v.now()
     self.resinteltime = v.now()
     self.timespent = 0
     self.nextaction = v.now()
     self.save()
Exemple #2
0
 def locationreset(self):
     self.location = self.owner
     self.inteltime = v.now()
     self.resinteltime = v.now()
     self.timespent = 0
     self.nextaction = v.now()
     self.save()
Exemple #3
0
def warstatus(world, target):
    try:
        War.objects.get(attacker=world, defender=target)
        atwar = True
    except:
        try:
            War.objects.get(attacker=target, defender=world)
            atwar = True
        except:
            atwar = False

    ownpower = targetpower = 0
    powerok = False
    for sector in v.sectors:
        for f in world.fleets.all().filter(sector=sector):
            ownpower += f.powermodifiers()
        for f in target.fleets.all().filter(sector=sector):
            targetpower += f.powermodifiers()
        if ownpower < 0.1*targetpower:
            powerok = True
            break
        ownpower = targetpower = 0

    if world == target:
        status = warnotok('You cannot declare war on yourself.')
    elif target.preferences.vacation:
        status = warnotok('This world is in vacation mode.')
    elif atwar:
        status = warnotok('You\'re already at war with this world.')
    elif target in world.declaredwars.all():
        status = warnotok('You\'ve recently declared war on this world.')
    elif world.warsperturn == 3:
        status = warnotok('You\'ve declared too many wars this turn.')
    elif v.now() < (target.creationtime + time.timedelta(weeks=1)) and target.noobprotect:
        status = warnotok('You cannot declare war on such a new world.')
    elif v.now() < target.warprotection:
        status = warnotok('This world is in war protection.')
    elif (target.gdp < 0.75*(world.gdp)) and (v.now() > target.abovegdpprotection):
        status = warnotok('This world is too small to declare war on.')
    elif target.wardefender.count() == 3:
        status = warnotok('This world already has the max quota of defensive wars.')
    elif world.warattacker.count() == 3:
        status = warnotok('You already have the max quota of offensive wars.')
    elif powerok:
        status = warnotok('You do not have enough fleet power to declare war on this world.')
    else:
        title = ''
        if v.now() < world.warprotection:
            title += 'You are currently in war protection. '

        if target.gdp > 3 * world.gdp:
            title += 'This world is above your GDP cap.'

        if title != '':
            status = warwarning(title)
        else:
            status = warok('You can declare war on this world.')

    return mark_safe(status)
Exemple #4
0
def warcleanup():
    for war in War.objects.all().iterator():
        attacktimeout = False
        if v.now() > war.lastattack + datetime.timedelta(hours=36) and v.now() > war.lastattack + datetime.timedelta(hours=36):
            attacktimeout = True
        if v.now() > (war.starttime + datetime.timedelta(weeks=1)) or attacktimeout:
            update.wartimeout(war.attacker, war.defender)
            war.delete()
Exemple #5
0
def warcleanup():
    for war in War.objects.all().iterator():
        attacktimeout = False
        if v.now() > war.lastattack + datetime.timedelta(hours=36) and v.now(
        ) > war.lastattack + datetime.timedelta(hours=36):
            attacktimeout = True
        if v.now() > (war.starttime +
                      datetime.timedelta(weeks=1)) or attacktimeout:
            update.wartimeout(war.attacker, war.defender)
            war.delete()
def nextspyaction(spy):

    if spy.nextaction > v.now():
        timediff = spy.nextaction - v.now()
        hours, minutes, seconds = utilities.timedeltadivide(timediff)
        timetonextaction = '%s:%s:%s until your next spy action.' % (
            hours, minutes, seconds)
    else:
        timetonextaction = '<span style="color:green;">Your spy is available for actions.</span>'

    return mark_safe(timetonextaction)
def warstatus(worldpk, targetpk):

    world = World.objects.get(pk=worldpk)
    target = World.objects.get(pk=targetpk)
    try:
        War.objects.get(attacker=world, defender=target)
        atwar = True
    except:
        try:
            War.objects.get(attacker=target, defender=world)
            atwar = True
        except:
            atwar = False

    ownpower = utilities.powerallmodifiers(world, target.region)
    targetpower = utilities.powerallmodifiers(target, target.region) + utilities.powerallmodifiers(target, 'S')
    if world == target:
        status = warnotok('You cannot declare war on yourself.')
    elif target.vacation:
        status = warnotok('This world is in vacation mode.')
    elif atwar:
        status = warnotok('You\'re already at war with this world.')
    elif target in world.declaredwars.all():
        status = warnotok('You\'ve recently declared war on this world.')
    elif world.warsperturn == 3:
        status = warnotok('You\'ve declared too many wars this turn.')
    elif v.now() < (target.creationtime + time.timedelta(weeks=1)) and target.noobprotect:
        status = warnotok('You cannot declare war on such a new world.')
    elif v.now() < target.warprotection:
        status = warnotok('This world is in war protection.')
    elif (target.gdp < 0.75*(world.gdp)) and (v.now() > target.abovegdpprotection):
        status = warnotok('This world is too small to declare war on.')
    elif target.wardefender.count() == 3:
        status = warnotok('This world already has the max quota of defensive wars.')
    elif world.warattacker.count() == 3:
        status = warnotok('You already have the max quota of offensive wars.')
    elif ownpower < 0.1*targetpower:
        status = warnotok('You do not have enough fleet power to declare war on this world.')
    else:
        title = ''
        if v.now() < world.warprotection:
            title += 'You are currently in war protection. '

        if target.gdp > 3 * world.gdp:
            title += 'This world is above your GDP cap.'

        if title != '':
            status = warwarning(title)
        else:
            status = warok('You can declare war on this world.')

    return mark_safe(status)
Exemple #8
0
 def wrap(request, *args, **kwargs):
     world = World.objects.get(user=request.user)
     if ActionNewsItem.objects.filter(target=world, datetime__lt=v.now()-time.timedelta(days=1)).exists():
         from wawmembers.views import main
         return main(request, message='You have events more than a day old that need to be addressed! <br> Go to the news page.')
     else:
         return f(request, *args, **kwargs)
Exemple #9
0
def cleanups():
    for anno in Announcement.objects.all().iterator():
        if v.now() > (anno.datetime + datetime.timedelta(days=5)):
            anno.delete()
    for trade in Trade.objects.all().iterator():
        if v.now() > (trade.datetime + datetime.timedelta(days=2)):
            update.tradetimeout(trade)
            trade.delete()
    for actionnews in ActionNewsItem.objects.all().iterator():
        if v.now() > (actionnews.datetime + datetime.timedelta(weeks=1)):
            actionnews.delete()
    for mytask in Task.objects.all().iterator():
        if v.now() > (mytask.datetime + datetime.timedelta(weeks=4)):
            mytask.delete()
    for mynews in NewsItem.objects.all().iterator():
        if v.now() > (mynews.datetime + datetime.timedelta(weeks=4)):
            mynews.delete()
Exemple #10
0
def cleanups():
    for anno in Announcement.objects.all().iterator():
        if v.now() > (anno.datetime + datetime.timedelta(days=5)):
            anno.delete()
    for trade in Trade.objects.all().iterator():
        if v.now() > (trade.datetime + datetime.timedelta(days=2)):
            update.tradetimeout(trade)
            trade.delete()
    for actionnews in ActionNewsItem.objects.all().iterator():
        if v.now() > (actionnews.datetime + datetime.timedelta(weeks=1)):
            actionnews.delete()
    for mytask in Task.objects.all().iterator():
        if v.now() > (mytask.datetime + datetime.timedelta(weeks=4)):
            mytask.delete()
    for mynews in NewsItem.objects.all().iterator():
        if v.now() > (mynews.datetime + datetime.timedelta(weeks=4)):
            mynews.delete()
Exemple #11
0
def spyinterceptsend(world, target, resname, resamount):
    'Calculates if sent trade or aid is intercepted.'
    from wawmembers.newsgenerator import notifyspyinterceptsend
    spies = Spy.objects.filter(location=world, inteltime__gt=v.now()).exclude(owner=target)
    for spy in spies:
        chance = random.randint(1, 100)
        if chance <= 2*spy.intelligence:
            htmldata = notifyspyinterceptsend(world, target, resname, resamount)
            NewsItem.objects.create(target=spy.owner, content=htmldata)
Exemple #12
0
def spyintercept(target, sender, resname, resamount):
    'Calculates if received trade or aid is intercepted.'
    from wawmembers.newsgenerator import notifyspyintercept
    spies = Spy.objects.filter(location=target,
                               inteltime__gt=v.now()).exclude(owner=sender)
    for spy in spies:
        chance = random.randint(1, 100)
        if chance <= 2 * spy.intelligence:
            htmldata = notifyspyintercept(target, sender, resname, resamount)
            NewsItem.objects.create(target=spy.owner, content=htmldata)
Exemple #13
0
def rumsoddiumhandout():
    'Hands out rumsoddium to active worlds.'
    activeworlds = list(World.objects.filter(lastloggedintime__gte=v.now()-datetime.timedelta(days=5)))
    for i in xrange(4):
        luckyworld = random.choice(activeworlds)
        activeworlds.remove(luckyworld)
        luckyworld.rumsoddium = 1
        luckyworld.save(update_fields=['rumsoddium'])
        NewsItem.objects.create(target=luckyworld, content='On a routine patrol, one of your ships encountered a small piece of green \
            substance floating through space. Upon bringing it back for inspection, you scientists confirmed it was a rare piece \
            of rumsoddium! What luck!')
def lastonline(worldid):

    try:
        world = World.objects.get(worldid=worldid)
    except:
        return ''

    lastonline = world.lastloggedintime
    delta = v.now() - lastonline
    toreturn = longtimeformat(delta)

    return mark_safe(toreturn)
Exemple #15
0
def lastonline(worldid):

    try:
        world = World.objects.get(worldid=worldid)
    except:
        return ''

    lastonline = world.lastloggedintime
    delta = v.now() - lastonline
    toreturn = longtimeformat(delta)

    return mark_safe(toreturn)
Exemple #16
0
 def wrap(request, *args, **kwargs):
     world = World.objects.get(user=request.user)
     if ActionNewsItem.objects.filter(target=world,
                                      datetime__lt=v.now() -
                                      time.timedelta(days=1)).exists():
         from wawmembers.views import main
         return main(
             request,
             message=
             'You have events more than a day old that need to be addressed! <br> Go to the news page.'
         )
     else:
         return f(request, *args, **kwargs)
Exemple #17
0
def rumsoddiumhandout():
    'Hands out rumsoddium to active worlds.'
    activeworlds = list(
        World.objects.filter(lastloggedintime__gte=v.now() -
                             datetime.timedelta(days=5)))
    for i in xrange(4):
        luckyworld = random.choice(activeworlds)
        activeworlds.remove(luckyworld)
        luckyworld.rumsoddium = 1
        luckyworld.save(update_fields=['rumsoddium'])
        NewsItem.objects.create(
            target=luckyworld,
            content=
            'On a routine patrol, one of your ships encountered a small piece of green \
            substance floating through space. Upon bringing it back for inspection, you scientists confirmed it was a rare piece \
            of rumsoddium! What luck!')
Exemple #18
0
def servertime():
    return v.now().strftime('%H:%M:%S')
Exemple #19
0
def now():
    return v.now().strftime('%d %b %H:%M:%S')
Exemple #20
0
def lastonline(world):
    lastonline = world.lastloggedintime
    delta = v.now() - lastonline
    return longtimeformat(delta)
Exemple #21
0
def now():
    return v.now().strftime('%d %b %H:%M:%S')
Exemple #22
0
def lastonline(world):
    lastonline = world.lastloggedintime
    delta = v.now() - lastonline
    return longtimeformat(delta)
Exemple #23
0
def warstatus(world, target):
    try:
        War.objects.get(attacker=world, defender=target)
        atwar = True
    except:
        try:
            War.objects.get(attacker=target, defender=world)
            atwar = True
        except:
            atwar = False

    ownpower = targetpower = 0
    powerok = False
    for sector in v.sectors:
        for f in world.fleets.all().filter(sector=sector):
            ownpower += f.powermodifiers()
        for f in target.fleets.all().filter(sector=sector):
            targetpower += f.powermodifiers()
        if ownpower < 0.1 * targetpower:
            powerok = True
            break
        ownpower = targetpower = 0

    if world == target:
        status = warnotok('You cannot declare war on yourself.')
    elif target.preferences.vacation:
        status = warnotok('This world is in vacation mode.')
    elif atwar:
        status = warnotok('You\'re already at war with this world.')
    elif target in world.declaredwars.all():
        status = warnotok('You\'ve recently declared war on this world.')
    elif world.warsperturn == 3:
        status = warnotok('You\'ve declared too many wars this turn.')
    elif v.now() < (target.creationtime +
                    time.timedelta(weeks=1)) and target.noobprotect:
        status = warnotok('You cannot declare war on such a new world.')
    elif v.now() < target.warprotection:
        status = warnotok('This world is in war protection.')
    elif (target.gdp < 0.75 *
          (world.gdp)) and (v.now() > target.abovegdpprotection):
        status = warnotok('This world is too small to declare war on.')
    elif target.wardefender.count() == 3:
        status = warnotok(
            'This world already has the max quota of defensive wars.')
    elif world.warattacker.count() == 3:
        status = warnotok('You already have the max quota of offensive wars.')
    elif powerok:
        status = warnotok(
            'You do not have enough fleet power to declare war on this world.')
    else:
        title = ''
        if v.now() < world.warprotection:
            title += 'You are currently in war protection. '

        if target.gdp > 3 * world.gdp:
            title += 'This world is above your GDP cap.'

        if title != '':
            status = warwarning(title)
        else:
            status = warok('You can declare war on this world.')

    return mark_safe(status)
Exemple #24
0
def policies_military(request):

    # variable setup, stuff needed to process POST data
    world = request.user.world
    result = indefwar = rumsodmsg = None
    shipdata = v.shipcosts(world.sector)
    rebelfuelcost = 0
    if request.method == 'POST':
        form = request.POST
        actions = False  #if the n***a wants to build ships
        if 'build' in form:
            shiptype = form['build']
            form = shipbuildform(world, data={shiptype: form[shiptype]})
            if form.is_valid():
                amount = form.cleaned_data[shiptype]
                ship = (shiptype if amount > 1 else shiptype[:-1]
                        )  #shipname, if >1 then plural
                if amount == 0:
                    result = "Can't build 0 %s!" % ship.replace('_', ' ')
                else:
                    #make sure he can (or she) can afford it
                    results = utilities.costcheck(world, form.cleaned_data)
                    if results['status'] is True:  #player can afford it
                        actions = {
                            'budget': {
                                'action': 'subtract',
                                'amount': D(results['cost']['geu'])
                            },
                            'duranium': {
                                'action': 'subtract',
                                'amount': results['cost']['duranium']
                            },
                            'tritanium': {
                                'action': 'subtract',
                                'amount': results['cost']['tritanium']
                            },
                            'adamantium': {
                                'action': 'subtract',
                                'amount': results['cost']['adamantium']
                            },
                            'productionpoints': {
                                'action': 'subtract',
                                'amount': results['cost']['productionpoints']
                            }
                        }
                        #queue merges with build fleet at turn change
                        queue = shipqueue(world=world,
                                          fleet=world.preferences.buildfleet)
                        queue.__dict__[shiptype] = amount
                        current_time = v.now()
                        if current_time.hour > 12:
                            hours = 24 - current_time.hour - 1
                            minutes = 60 - current_time.minute
                        else:
                            hours = 12 - current_time.hour - 1
                            minutes = 60 - current_time.minute
                        outcometime = current_time + time.timedelta(
                            hours=hours, minutes=minutes)
                        ship = ship.replace('_', ' ')
                        task = Task.objects.create(target=world,
                                                   content=taskdata.buildship(
                                                       ship, amount),
                                                   datetime=outcometime)
                        queue.task = task
                        queue.save()
                        result = "%s %s are building" % (amount, ship)
                    else:
                        result = "You can't afford to build %s %s!" % (
                            amount, ship.replace('_', ' '))

        if "research" in form:
            form = ResearchForm(request.POST)
            if form.is_valid():
                next_tier = v.tiers[v.tiers.index(world.techlevel) + 1]
                rdcost = shipdata[next_tier.replace(' ', '_').lower() +
                                  's']['research']
                durcost = rdcost['duranium']
                try:
                    tritcost = rdcost['tritanium']
                except:
                    tritcost = 0
                try:
                    adamcost = rdcost['adamantium']
                except:
                    adamcost = 0
                data = form.cleaned_data
                amount = data['researchamount']
                if amount < 0:
                    result = 'Enter a positive integer.'
                elif world.techlevel == "dreadnought":
                    result = 'You have researched all ship types already!'
                elif world.budget < amount:
                    result = outcomes.nomoney()
                elif durcost > world.duranium:
                    result = outcomes.research('NoDur')
                elif tritcost > world.tritanium:
                    result = outcomes.research('NoTrit')
                elif adamcost > world.adamantium:
                    result = outcomes.research('NoAdam')
                elif world.turnresearched:
                    result = outcomes.research('TooSoon')
                elif amount > 3 * world.gdp:
                    result = outcomes.research('TooMuch')
                else:
                    actions = {
                        'budget': {
                            'action': 'subtract',
                            'amount': amount
                        },
                        'duranium': {
                            'action': 'subtract',
                            'amount': durcost
                        },
                        'tritanium': {
                            'action': 'subtract',
                            'amount': tritcost
                        },
                        'adamantium': {
                            'action': 'subtract',
                            'amount': adamcost
                        },
                        'turnresearched': {
                            'action': 'set',
                            'amount': True
                        },
                        'budget': {
                            'action': 'subtract',
                            'amount': amount
                        },
                    }
                    if world.sector == 'draco':
                        amount = int(amount * 1.25)
                    message = outcomes.researchtext(world, amount)
                    actions.update(
                        {'millevel': {
                            'action': 'add',
                            'amount': amount
                        }})
                    result = outcomes.research('Success', result=message)

        if "moveship" in form:
            form = fleetwarpform(world, request.POST)
            if form.is_valid(
            ):  #form checks practically everything but warpcost
                warpfleet = fleet.objects.get(pk=form.cleaned_data['fleet'])
                if warpfleet.enoughfuel():
                    tgtsector = form.cleaned_data['destination']
                    actions = {
                        'warpfuel': {
                            'action': 'subtract',
                            'amount': warpfleet.fuelcost()
                        }
                    }
                    taskcontent = taskdata.warpfleet(warpfleet.name,
                                                     warpfleet.sector,
                                                     tgtsector)
                    delay = (4 if tgtsector == warpfleet.sector else 8)
                    outcometime = v.now() + time.timedelta(minutes=2)
                    task = Task.objects.create(target=world,
                                               content=taskcontent,
                                               datetime=outcometime)
                    newtask.fleet_warp.apply_async(args=(warpfleet.pk,
                                                         warpfleet.name,
                                                         tgtsector, task.pk),
                                                   eta=outcometime)
                    result = "%s bugs out and is warping to %s" % (
                        warpfleet.name, tgtsector)
                    #actually altering data goes last
                    utilities.atomic_fleet(warpfleet.pk, ['warp'])
                else:
                    result = "Not enough warpfuel, %s needs %s more warpfuel to warp!" % \
                        (warpfleet.name, (warpfleet.fuelcost() - world.warpfuel))

        if "train" in form:
            form = trainfleetform(world, form)
            if form.is_valid():
                targetfleet = form.cleaned_data['fleet']
                if world.budget < targetfleet.trainingcost():
                    result = outcomes.nomoney()
                elif targetfleet.training >= targetfleet.maxtraining():
                    result = outcomes.training('AtMax')
                else:
                    actions = {
                        'budget': {
                            'action': 'subtract',
                            'amount': targetfleet.trainingcost()
                        },
                    }
                    utilities.atomic_fleet(targetfleet.pk, {'train': True})

        if "buildshipyard" in form:
            if world.budget < 500:
                result = outcomes.nomoney()
            elif world.duranium < 5:
                result = outcomes.notenoughduranium()
            else:
                actions = {
                    'budget': {
                        'action': 'subtract',
                        'amount': 500
                    },
                    'duranium': {
                        'action': 'subtract',
                        'amount': 1
                    },
                    'shipyards': {
                        'action': 'add',
                        'amount': 1
                    },
                }
                result = outcomes.shipyards('Success')

        if "attackrebels" in form:
            fleets = world.controlled_fleets.all()
            worldpower = utilities.militarypower(world, world.sector)
            fuelcost = int(0.1 * utilities.warpfuelcost(worldlist))
            if world.budget < 10:
                result = outcomes.nomoney()
            elif world.rebels == 0:
                result = outcomes.norebels()
            elif world.warpfuel < fuelcost:
                result = 'You do not have enough warpfuel to hunt down the rebels!'
            elif worldpower == 0:
                result = 'You do not have a fleet to attack the rebels with!'
            else:
                world.budget = F('budget') - 10
                world.warpfuel = F('warpfuel') - fuelcost

                totworldpower = utilities.powerallmodifiers(
                    world, world.sector)

                rebelpower = 0
                for f in fleets:
                    rebelpower += f.basepower()  # sum of total fleet power

                outcome = random.randint(1, 100)
                if (1 <= outcome < 5) or (30 < outcome <= 100):
                    if 1 <= world.rebels < 20:
                        rebelpower = 0.01 * rebelpower
                    elif 20 <= world.rebels < 40:
                        rebelpower = 0.02 * rebelpower
                    elif 40 <= world.rebels < 60:
                        rebelpower = 0.03 * rebelpower
                    elif 60 <= world.rebels < 80:
                        rebelpower = 0.04 * rebelpower
                    elif world.rebels >= 80:
                        rebelpower = 0.05 * rebelpower
                    dmg = utilities.war_result(rebelpower, totworldpower,
                                               worldpower)
                    utilities.warloss_byregion(world, world.sector, deflosses)

                if 30 < outcome <= 100:
                    utilities.rebelschange(world, -5)
                elif 1 <= outcome <= 5:
                    utilities.rebelschange(world, 5)

                utilities.wearinesschange(world, world.sector, -2)

                world.save(update_fields=['budget', 'warpfuel'])

                result = news.rebelresult(outcome, deflosses)

        if "rumsodmil" in form:
            targetname = form['targetname']
            try:
                target = World.objects.get(world_name=targetname)
            except ObjectDoesNotExist:
                result = 'There is no such world by that name!'
            else:
                if world.rumsoddium != 4:
                    result = 'You do not have enough rumsoddium for the ritual!'
                else:
                    target.gdp = F('gdp') - int(target.gdp / 2)
                    target.budget = F('budget') - D(target.budget / 2)
                    target.warpfuel = F('warpfuel') - int(target.warpfuel / 2)
                    target.duranium = F('duranium') - int(target.duranium / 2)
                    target.tritanium = F('tritanium') - int(
                        target.tritanium / 2)
                    target.adamantium = F('adamantium') - int(
                        target.adamantium / 2)
                    world.rumsoddium = 0
                    world.save(update_fields=['rumsoddium'])
                    target.save(update_fields=[
                        'gdp', 'budget', 'warpfuel', 'duranium', 'tritanium',
                        'adamantium'
                    ])
                    rumsodmsg = v.rumsodmilitary
                    htmldata = news.rumsodmilitaryreceive(world)
                    NewsItem.objects.create(target=target, content=htmldata)
                    utilities.rumsoddiumhandout()

        if "buildflagship" in form:
            form = PersonalShipForm(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                shiptype = data['shiptype']
                if world.flagshiptype != 0:
                    result = outcomes.personalship('Already')
                elif shiptype not in [1, 2, 3]:
                    result = 'Invalid shiptype selected!'
                elif world.budget < 500:
                    result = outcomes.nomoney()
                elif (world.shipyards - world.shipyardsinuse) < 5:
                    result = outcomes.notenoughshipyards()
                elif world.duranium < 20:
                    result = outcomes.notenoughduranium()
                else:
                    current_time = v.now()
                    if current_time.hour > 12:
                        hours = 24 - current_time.hour - 1
                        minutes = 60 - current_time.minute
                    else:
                        hours = 12 - current_time - 1
                        minutes = 60 - current_time.minute
                    outcometime = v.now() + time.timedelta(hours=hours,
                                                           minutes=minutes)
                    world.budget = F('budget') - D(500)
                    world.duranium = F('duranium') - 20
                    world.productionpoints = F('shipyardsinuse') + 25
                    world.flagshipbuild = True
                    world.save(update_fields=[
                        'budget', 'duranium', 'productionpoints',
                        'flagshipbuild'
                    ])
                    taskdetails = taskdata.buildpersonalship(shiptype)
                    task = Task(target=world,
                                content=taskdetails,
                                datetime=outcometime)
                    task.save()

                    newtask.buildpersonalship.apply_async(args=(world.pk,
                                                                task.pk,
                                                                shiptype),
                                                          eta=outcometime)
                    result = outcomes.personalship('Success', shiptype)

        if "scuttleflagship" in form:
            if world.flagshiptype == 0:
                result = 'You do not have a flagship to scuttle!'
            else:
                utilities.flagshipreset(world)
                result = 'You scuttled your flagship.'

        elif "setfleetprefs" in form:
            form = buildlocationform(world, request.POST)
            if form.is_valid():
                prefs = world.preferences
                prefs.buildfleet = form.cleaned_data['buildchoice']
                prefs.recievefleet = form.cleaned_data['recievechoice']
                prefs.save()
                result = "Fleet preferences successfully updated"

        if actions:
            utilities.atomic_world(world.pk, actions)
            world.refresh_from_db()
    if world.wardefender.count() > 0:
        indefwar = True
    #create context dictionary with needed variables instead of fuckhuge return dict
    costs = []
    ids = []
    for f in world.fleets.all().filter(sector=world.sector):
        costs.append(f.trainingcost())
        ids.append(f.pk)

    context = {
        'costs':
        costs,
        'ids':
        ids,
        'result':
        result,
        'researchform':
        ResearchForm(),
        'buildtoform':
        buildlocationform(world,
                          initial={
                              'buildchoice': world.preferences.buildfleet.pk,
                              'recievechoice':
                              world.preferences.recievefleet.pk
                          }),
        'moveform':
        fleetwarpform(world),
        'prefs':
        world.shipsortprefs,
        'buildlist':
        display.milpolicydisplay(world),
        'rumpolicy': (True if world.rumsoddium == 4 else None),
        'rumsodmsg':
        rumsodmsg,
        'indefwar': (True if world.wardefender.count() > 0 else False),
        'rebelfuelcost':
        rebelfuelcost,
        'displayresearch':
        (True if v.tiers.index(world.techlevel) is len(v.tiers) else False),
        'result':
        result,
        'rumsodmsg':
        rumsodmsg,
        'world':
        world,
        'trainform':
        trainfleetform(world),
    }
    if world.techlevel != "Dreadnought":
        next_tier = v.tiers[v.tiers.index(world.techlevel) + 1]
        rdcost = shipdata[next_tier.replace(' ', '_').lower() +
                          's']['research']
        context.update({'costmsg': rdcost, 'shiptext': next_tier})

    return render(request, 'policies_military.html', context)
Exemple #25
0
def policies_diplomacy(request):

    # variable setup
    world = request.user.world
    result = None
    if Spy.objects.filter(owner=world, location=world).count() == 0:
        spyform = None
    else:
        spyform = SelectSpyForm(world)

    if request.method == 'POST':
        form = request.POST
        actions = False
        if "createfederation" in form:
            if world.alliance != None:
                result = outcomes.createfederation('AlreadyAllied')
            elif world.alliancepaid:
                return redirect('new_alliance')
            else:
                if world.budget < 200:
                    result = outcomes.nomoney()
                else:
                    actions = {
                        'budget': {
                            'action': 'subtract',
                            'amount': 200
                        },
                        'alliancepaid': {
                            'action': 'set',
                            'amount': True
                        }
                    }
                    utilities.atomic_world(world.pk, actions)
                    return redirect('new_alliance')

        if "trainspy" in form:
            name = form['spyname']
            if world.budget < 500:
                result = outcomes.nomoney()
            elif Spy.objects.filter(owner=world).count() >= 5:
                result = outcomes.trainspy('TooMany')
            elif len(name) > 10:
                result = outcomes.trainspy('TooLong')
            else:
                actions = {'budget': {'action': 'subtract', 'amount': 500}}
                spy = Spy.objects.create(owner=world,
                                         location=world,
                                         name=name)
                if world.sector == 'draco':
                    if world.millevel >= v.millevel('dre'):
                        spy.infiltration = spy.propaganda = spy.gunrunning = spy.intelligence = spy.sabotage = spy.counterint = 4
                    else:
                        spy.infiltration = spy.propaganda = spy.gunrunning = spy.intelligence = spy.sabotage = spy.counterint = 2
                spy.nextaction = v.now() + time.timedelta(hours=4)
                spy.save()
                result = outcomes.trainspy('Success')

        if "counterintel" in form:
            form = SelectSpyForm(world, request.POST)
            if form.is_valid():
                data = form.cleaned_data
                spyid = data['spyselect']
                try:
                    spy = Spy.objects.get(pk=spyid)
                except ObjectDoesNotExist:
                    result = "There is no such spy!"
                else:
                    if world.budget < 100:
                        result = outcomes.nomoney()
                    elif spy.owner != world:
                        result = "This spy does not belong to your intelligence services!"
                    elif spy.location != world:
                        result = "This spy is not at your home world!"
                    elif spy.nextaction > v.now():
                        result = "This spy is currently recovering and unavailable for missions."
                    else:
                        actions = {
                            'budget': {
                                'action': 'subtract',
                                'amount': 100
                            }
                        }
                        killed = 0
                        listkilled = []
                        enemyspies = list(
                            Spy.objects.filter(location=world).exclude(
                                owner=world))
                        for enspy in enemyspies:
                            chance = random.randint(1, 100)
                            if 20 + spy.counterint - enspy.timespent >= chance:
                                killed += 1
                                listkilled.append(enspy)
                                reveal, x = utilities.reveal(enspy)

                                htmldata = news.counterintkilled(enspy, world)
                                NewsItem.objects.create(target=enspy.owner,
                                                        content=htmldata)

                                enspy.delete()

                        spy.nextaction = v.now() + time.timedelta(hours=8)
                        if killed > 0:
                            spy.counterint += 1
                        spy.save()

                        result = outcomes.counterintel(listkilled)
            else:
                result = "You have no spies available!"
        if actions:
            utilities.atomic_world(world.pk, actions)
            world.refresh_from_db()
    money = world.budget
    return render(request, 'policies_diplomacy.html', {
        'result': result,
        'money': money,
        'spyform': spyform
    })
Exemple #26
0
def policies_domestic(request):

    # variable setup
    world = request.user.world
    gdp = world.gdp
    moregdp = 1.5 * world.gdp

    if world.econsystem == 1:
        scaling = 2
    elif world.econsystem == 0:
        scaling = 1.5
    elif world.econsystem == -1:
        scaling = 1

    citycost = (50 * scaling if world.gdp <= 500 else
                (world.gdp / 10) * scaling)

    result = None

    if request.method == 'POST':
        form = request.POST
        actions = False
        if "arrest" in form:
            if world.budget < 50:
                result = outcomes.nomoney()
            elif world.polsystem <= -80:
                result = outcomes.arrest('ArrestedAll')
            else:
                outcome = random.randint(1, 100)
                stab = utilities.attrchange(world.stability, 10)
                cont = utilities.attrchange(world.contentment, -10)
                polsys = utilities.attrchange(world.polsystem, -10)
                actions = {
                    'budget': {
                        'action': 'subtract',
                        'amount': 50
                    },
                    'stability': {
                        'action': 'add',
                        'amount': stab
                    },
                    'contentment': {
                        'action': 'add',
                        'amount': cont
                    },
                    'polsystem': {
                        'action': 'add',
                        'amount': polsys
                    },
                }
                if 95 < outcome <= 100:
                    rebels = utilities.attrchange(world.rebels, -5, zero=True)
                    actions.update(
                        {'rebels': {
                            'action': 'subtract',
                            'amount': rebels
                        }})
                result = outcomes.arrest(outcome)

        if "free" in form:
            if world.budget < 50:
                result = outcomes.nomoney()
            elif world.polsystem >= 60:
                result = outcomes.free('FreedAll')
            else:
                outcome = random.randint(1, 100)
                stab = utilities.attrchange(world.stability, -10)
                cont = utilities.attrchange(world.contentment, 10)
                polsys = utilities.attrchange(world.polsystem, 10)
                actions = {
                    'budget': {
                        'action': 'subtract',
                        'amount': 50
                    },
                    'stability': {
                        'action': 'add',
                        'amount': stab
                    },
                    'contentment': {
                        'action': 'add',
                        'amount': cont
                    },
                    'polsystem': {
                        'action': 'add',
                        'amount': polsys
                    },
                }
                if 80 < outcome <= 100:
                    rebels = utilities.attrchange(world.rebels, 5)
                    actions.update(
                        {'rebels': {
                            'action': 'subtract',
                            'amount': rebels
                        }})
                result = outcomes.free(outcome)

        if "martiallaw" in form:
            offwars = world.warattacker.all()
            defwars = world.wardefender.all()
            om = offwars.aggregate(m=Min('starttime'))
            dm = defwars.aggregate(m=Min('starttime'))
            omw = (v.now() if om['m'] is None else om['m'])
            dmw = (v.now() if dm['m'] is None else dm['m'])
            maxtime = (omw if omw < dmw else dmw)
            nexttime = v.now() + time.timedelta(days=5)

            if world.budget < 500:
                result = outcomes.nomoney()
            elif offwars.count() == 0 and defwars.count() == 0:
                result = outcomes.martiallaw('NotAtWar')
            elif world.polsystem <= -60:
                result = outcomes.martiallaw('Dictator')
            elif -60 < world.polsystem < -20:
                result = outcomes.martiallaw('AlreadyAdmiralty')
            elif world.timetonextadmiralty > v.now():
                timediff = world.timetonextadmiralty - v.now()
                result = outcomes.martiallaw('TooSoon', timediff)
            elif maxtime > v.now() - time.timedelta(days=1):
                timediff = maxtime - (v.now() - time.timedelta(days=1))
                result = outcomes.martiallaw('UnderTime', timediff)
            else:
                world.timetonextadmiralty = nexttime
                stab = utilities.attrchange(world.stability, 10)
                cont = utilities.attrchange(world.contentment, -20)
                actions = {
                    'budget': {
                        'action': 'subtract',
                        'amount': 500
                    },
                    'stability': {
                        'action': 'subtract',
                        'amount': stab
                    },
                    'contentment': {
                        'action': 'add',
                        'amount': cont
                    },
                    'polsystem': {
                        'action': 'set',
                        'amount': -40
                    },
                    'timetonextadmiralty': {
                        'action': 'set',
                        'amount': nexttime
                    }
                }
                #utilities.martiallawadd(world)
                result = outcomes.martiallaw('Success')

        if "citybuilding" in form:
            if world.budget < citycost:
                result = outcomes.nomoney()
            else:
                outcome = random.randint(1, 100)
                cont = utilities.attrchange(world.contentment, 10)
                actions = {
                    'budget': {
                        'action': 'subtract',
                        'amount': D(citycost)
                    },
                    'contentment': {
                        'action': 'add',
                        'amount': cont
                    },
                }
                if 95 < outcome <= 100:
                    qol = utilities.attrchange(world.qol, 4)
                    actions.update({'qol': {'action': 'add', 'amount': qol}})
                result = outcomes.citybuilding(outcome)

        if "literacy" in form:
            if world.budget < world.gdp:
                result = outcomes.nomoney()
            elif world.qol == 100:
                result = outcomes.maxqol()
            else:
                qol = utilities.attrchange(world.qol, 20)
                contentment = utilities.attrchange(world.contentment, 5)
                actions = {
                    'contentment': {
                        'action': 'add',
                        'amount': contentment
                    },
                    'qol': {
                        'action': 'add',
                        'amount': qol
                    },
                    'budget': {
                        'action': 'subtract',
                        'amount': world.gdp
                    },
                }
                result = outcomes.literacy()

        if "healthcare" in form:
            if world.budget < ((world.gdp) * 1.5):
                result = outcomes.nomoney()
            elif world.qol == 100:
                result = outcomes.maxqol()
            else:
                qol = utilities.attrchange(world.qol, 40)
                contentment = utilities.attrchange(world.contentment, 10)
                actions = {
                    'contentment': {
                        'action': 'add',
                        'amount': contentment
                    },
                    'qol': {
                        'action': 'add',
                        'amount': qol
                    },
                    'budget': {
                        'action': 'subtract',
                        'amount': D(world.gdp * 1.5)
                    },
                }
                result = outcomes.healthcare()
        if actions:
            utilities.atomic_world(world.pk, actions)
            world.refresh_from_db()

    money = world.budget
    return render(
        request, 'policies_domestic.html', {
            'result': result,
            'GDP': gdp,
            'moreGDP': moregdp,
            'citycost': citycost,
            'money': money
        })