def commitspy(spy, actions=False): if actions: actions.update({'actioned': {'action': 'set', 'amount': True}}) utils.atomic_transaction(Spy, spy.pk, actions) else: spy.actioned = True spy.save()
def instant_apply(self): for action in self.choices: data = self.choices[action] if data['model'] == Nation: pk = self.nation.pk else: pk = data['model'].objects.filter(nation=self.nation)[0].pk utils.atomic_transaction(data['model'], pk, data['actions'])
def fundopposition(nation, target, spy): img = "spy.jpg" cost = target.gdp / 30 if spy.infiltration < 10: result = "Agent do not have enough infiltration!" elif nation.budget < cost: result = "We do not have enough funds for this!" else: strength = spy.experience + spy.infiltration - target.approval spyactions = { 'actioned': { 'action': 'set', 'amount': True }, 'infiltration': { 'action': 'add', 'amount': utils.attrchange(spy.infiltration, -10) } } if spy.specialty != 'Launderer': strength /= 2 chance = random.randint(-500, 500) if strength < chance: result = "%s was caught trying to fund the opposition. The agent has been arrested." % spy.name #no news report spyactions.update({ 'arrested': { 'action': 'set', 'amount': True }, 'discovered': { 'action': 'set', 'amount': True } }) else: result = "%s has successfully funded the opposition, decreasing government approval." % spy.name spyactions = { 'experience': { 'action': 'add', 'amount': utils.attrchange(spy.experience, 5) }, 'infiltration': { 'action': 'add', 'amount': utils.attrchange(spy.infiltration, -10) } } nationactions = {'budget': {'action': 'subtract', 'amount': cost}} targetactions = { 'approval': { 'action': 'add', 'amount': utils.attrchange(target.approval, -10) }, } utils.atomic_transaction(Nation, target.pk, targetactions) utils.atomic_transaction(Nation, nation.pk, nationactions) commitspy(spy, spyactions) return {'result': result, 'img': img}
def apply_choice(self, choice): if choice not in self.choices: return False data = self.choices[choice] if data['model'] == Nation: pk = self.nation.pk else: pk = data['model'].objects.filter(nation=self.nation)[0].pk utils.atomic_transaction(data['model'], pk, data['actions']) return self.result[choice]
def armrebels(nation, target, spy): img = '' if spy.infiltration < 10: result = "Agent do not have enough infiltration!" elif nation.military.weapons < 20: result = "We do not have the weaponry for this!" else: spyactions = { 'infiltration': { 'action': 'add', 'amount': utils.attrchange(spy.infiltration, -10) } } strength = spy.experience + spy.infiltration - target.approval if spy.specialty != 'Gunrunner': strength /= 2 chance = random.randint(-750, 500) if strength < chance: result = "%s was caught arming the rebels. Agent %s has been arrested." % ( spy.name, spy.name) #no news report img = "spy.jpg" spyactions.update({ 'arrested': { 'action': 'set', 'amount': True }, 'discovered': { 'action': 'set', 'amount': True } }) else: img = "rebel.jpg" result = "%s has successfully armed the rebels." % spy.name spyactions = { 'experience': { 'action': 'add', 'amount': utils.attrchange(spy.experience, 5) }, } nationactions = {'weapons': {'action': 'subtract', 'amount': 5}} targetactions = { 'rebels': { 'action': 'add', 'amount': 2 }, } utils.atomic_transaction(Nation, target.pk, targetactions) utils.atomic_transaction(Military, nation.military.pk, nationactions) commitspy(spy, spyactions) return {'result': result, 'img': img}
def surveillancing(request, spy): nation = request.user.nation img = '' form = surveillanceform(nation, request.POST) if form.is_valid(): NSA = form.cleaned_data['spy'] cost = surveilcost(spy) if nation.budget < cost: result = "We do not have the necessary funds!" else: Spy.objects.filter(pk=spy.pk).update(surveillance=True) Spy.objects.filter(pk=NSA.pk).update(surveilling=spy) utils.atomic_transaction( Nation, nation.pk, {'budget': { 'action': 'subtract', 'amount': cost }}) result = "Agent %s assembles a small team and sets up surveillance." % NSA.name else: result = "invalid spy selected!" return {'result': result, 'img': img}
def add_budget(): #alliance members gets paid first, then regular nations for alliance in Alliance.objects.select_related('initiatives').annotate( membercount=Count('members')).filter(membercount__gte=1): for member in alliance.members.filter(vacation=False, reset=False, deleted=False).iterator(): while True: income = nation_income(member) try: utils.atomic_transaction( Nation, member.pk, { 'budget': { 'action': 'add', 'amount': income['income'] - income['tax'] } }) except OperationalError: #in case something else is using the member member.refresh_from_db() continue break for nation in Nation.objects.actives().filter(alliance=None, budget__lt=F('gdp') * 2): income = nation_income(nation) while True: try: utils.atomic_transaction( Nation, nation.pk, {'budget': { 'action': 'add', 'amount': income['income'] }}) except OperationalError: nation.refresh_from_db() continue break
def alliance_gain(): for alliance in Alliance.objects.select_related( 'bank', 'initiatives').all().iterator(): bankstats = alliance.bankstats.get_or_create( turn=ID.objects.get().turn)[0] while True: try: #first off is all incoming and outgoing #toss it into a bankstat object for easy charting (at some point) budget = alliance.bank.budget income = alliance_income(alliance) expenditures = alliance_expenditures(alliance) total_out = 0 for cost in expenditures: total_out += expenditures[cost] alliance.bank.budget += income['total'] if alliance.bank.budget >= total_out: alliance.bank.budget -= total_out else: #not enough bank to keep it all running unaffordable = [] fields = [] for initiative in expenditures: if alliance.initiatives.__dict__[initiative.split('_') [0]]: if alliance.bank.budget - expenditures[ initiative] > 0: alliance.bank.budget -= expenditures[ initiative] else: alliance.initiatives.__dict__[initiative.split( '_')[0]] = False #collecting fields to update on sav0e() fields += [ initiative, alliance.initiatives.reset_timer( initiative.split('_')[0]) ] unaffordable.append(v.initiative_loss[ initiative.split('_')[0]]) #First we calculated what the alliance in question could afford #now we insert newsitems for the relevant officers #about which initiatives got shut down from lack of funding txt = '' for init in unaffordable: txt += "%s, " % init txt = txt[:-2] alliance.initiatives.save() for officer in alliance.members.filter( Q(permissions__template__banking=True) | Q(permissions__template__founder=True)): news.initiative_recalled(officer, txt) utils.atomic_transaction( Bank, alliance.bank.pk, { 'budget': { 'action': 'add', 'amount': alliance.bank.budget - budget } }) #then we set bankstats #the reason why bankstats are set after the atomic database commit #is to avoid accidentally saving updates twice #in case an exception is thrown by the atomic transaction income.update(expenditures) #for iterability income.pop('total') #not a field in bankstats for field in income: bankstats.__dict__[field] += income[field] bankstats.save() except OperationalError: alliance.bank.refresh_from_db() continue break #end while #end for return add_budget()
def turnchange(): ID.objects.all().update(turn=F('turn') + 1) turn = ID.objects.get().turn for nation in Nation.objects.actives().select_related( 'alliance__initiatives').iterator(): create_snapshot(nation, turn) while True: try: approval = qol = growth = FI = mg = manpower = research = rebels = 0 #base material gain oil = oilgain(nation) reserveloss = oilbase(nation) rm = rmgain(nation) food = foodgain(nation) mg = mggain(nation, rm + nation.rm, oil + nation.oil) basedecay = mgbase(nation, rm + nation.rm, oil + nation.oil) oil -= basedecay rm -= basedecay ################## ## other stuff lol ################## research = researchgain(nation) approval = approvalchange(nation) gdpchange = nation.growth qol = qolgain(nation) FI = FIchanges(nation) manpower = manpowergain(nation) healthcare = healthcaredecay(nation) stability = stabilitygain(nation) soviet = sovietgain(nation) us = westerngain(nation) growth = growthchanges(nation) actions = { 'gdp': { 'action': 'add', 'amount': nation.growth }, 'growth': { 'action': 'add', 'amount': growth }, 'qol': { 'action': 'add', 'amount': utils.attrchange(nation.qol, qol) }, 'healthcare': { 'action': 'add', 'amount': utils.attrchange(nation.healthcare, healthcare) }, 'literacy': { 'action': 'add', 'amount': utils.attrchange(nation.literacy, literacydecay(nation)) }, 'stability': { 'action': 'add', 'amount': utils.attrchange(nation.stability, stability) }, 'approval': { 'action': 'add', 'amount': utils.attrchange(nation.approval, approval) }, 'soviet_points': { 'action': 'add', 'amount': utils.attrchange(nation.soviet_points, soviet, -100) }, 'us_points': { 'action': 'add', 'amount': utils.attrchange(nation.us_points, us, -100) }, 'reputation': { 'action': 'add', 'amount': utils.attrchange(nation.reputation, 5) }, 'rebels': { 'action': 'add', 'amount': utils.attrchange(nation.rebels, rebels) }, 'manpower': { 'action': 'add', 'amount': utils.attrchange(nation.manpower, manpower) }, 'research': { 'action': 'add', 'amount': research }, 'oilreserves': { 'action': 'subtract', 'amount': reserveloss }, 'FI': { 'action': 'add', 'amount': FI }, 'oil': { 'action': 'add', 'amount': oil }, 'rm': { 'action': 'add', 'amount': rm }, 'mg': { 'action': 'add', 'amount': mg }, 'food': { 'action': 'add', 'amount': food }, } utils.atomic_transaction(Nation, nation.pk, actions) except OperationalError: nation.refresh_from_db() continue eventhandler.trigger_events(nation) trade_balancing(nation) break return milturn()
def poison(nation, target, spy): img = '' cost = target.gdp / 15 if spy.infiltration < 50: result = "Agent do not have enough infiltration!" elif nation.budget < cost: result = "We do not have enough funds for this!" elif nation.military.weapons < 20: result = "We do not have the weapons to do this!" elif target.econdata.foodproduction == 0: result = "Their food production has already been decimated!" else: strength = spy.experience + spy.infiltration - target.approval spyactions = { 'actioned': { 'action': 'set', 'amount': True }, 'infiltration': { 'action': 'add', 'amount': utils.attrchange(spy.infiltration, -20) } } if spy.specialty != 'Saboteur': strength /= 2 chance = random.randint(-300, 500) if strength < chance: result = "%s was caught preparing to poison the crops. He was subsequently placed under arrest." % spy.name img = "spy.jpg" spyactions.update({ 'arrested': { 'action': 'set', 'amount': True }, 'discovered': { 'action': 'set', 'amount': True } }) else: img = "spy.jpg" result = "%s successfully poisoned the target nation's crops, decreasing their food production." % spy.name spyactions = { 'experience': { 'action': 'add', 'amount': utils.attrchange(spy.experience, 5) }, } nationactions = { 'budget': { 'action': 'subtract', 'amount': cost }, } nationmilactions = { 'weapons': { 'action': 'subtract', 'amount': 2 }, } targetactions = { 'foodproduction': { 'action': 'subtract', 'amount': (1 if target.econdata.foodproduction > 0 else 0) }, } utils.atomic_transaction(Econdata, target.econdata.pk, targetactions) utils.atomic_transaction(Nation, nation.pk, nationactions) utils.atomic_transaction(Military, nation.military.pk, nationmilactions) commitspy(spy, spyactions) return {'result': result, 'img': img}
def sabotagemine(nation, target, spy): img = '' cost = target.gdp / 30 if spy.infiltration < 20: result = "Agent do not have enough infiltration!" elif nation.budget < cost: result = "We do not have enough funds for this!" elif nation.military.weapons < 20: result = "We do not have the weapons to do this!" elif target.mines == 0: result = "They have no mines!" else: strength = spy.experience + spy.infiltration - target.approval spyactions = { 'actioned': { 'action': 'set', 'amount': True }, 'infiltration': { 'action': 'add', 'amount': utils.attrchange(spy.infiltration, -20) } } if spy.specialty != 'Saboteur': strength /= 2 chance = random.randint(-300, 500) if strength < chance: result = "Agent %s was caught sabotaging a mine. He has been placed under arrest." % spy.name news.sabotagemine(target) spyactions.update({ 'arrested': { 'action': 'set', 'amount': True }, 'discovered': { 'action': 'set', 'amount': True } }) img = "spy.jpg" else: img = "spy.jpg" result = "%s has successfully sabotaged a mine, decreasing their production." % spy.name news.sabotagedmine(target) spyactions = { 'experience': { 'action': 'add', 'amount': utils.attrchange(spy.experience, 5) }, } nationactions = { 'budget': { 'action': 'subtract', 'amount': cost }, } nationmilactions = { 'weapons': { 'action': 'subtract', 'amount': 2 }, } targetactions = { 'mines': { 'action': 'subtract', 'amount': 1 }, } utils.atomic_transaction(Nation, target.pk, targetactions) utils.atomic_transaction(Nation, nation.pk, nationactions) utils.atomic_transaction(Military, nation.military.pk, nationmilactions) commitspy(spy, spyactions) return {'result': result, 'img': img}
def terroristattack(nation, target, spy): img = '' cost = target.gdp / 15 if spy.infiltration < 50: result = "Agent do not have enough infiltration!" elif nation.budget < cost: result = "We do not have enough funds for this!" elif nation.military.weapons < 20: result = "We do not have the weapons to do this!" else: strength = spy.experience + spy.infiltration - target.approval spyactions = { 'actioned': { 'action': 'set', 'amount': True }, 'infiltration': { 'action': 'add', 'amount': utils.attrchange(spy.infiltration, -50) } } if spy.specialty != 'Terrorist': strength /= 2 chance = random.randint(-300, 500) if strength < chance: result = "%s has been caught planning a terrorist attack! The agent responsible has been detained." % spy.name news.terroristattack(target) img = "spy.jpg" spyactions.update({ 'arrested': { 'action': 'set', 'amount': True }, 'discovered': { 'action': 'set', 'amount': True } }) else: img = "spy.jpg" news.terroristattacked(target) result = "%s has successfully pulled off a terrorist attack and framed it on an internal\ faction, killing countless innocent civilians and decreasing stability in the country." % spy.name spyactions = { 'experience': { 'action': 'add', 'amount': utils.attrchange(spy.experience, 5) }, } nationactions = { 'budget': { 'action': 'subtract', 'amount': cost }, } nationmilactions = { 'weapons': { 'action': 'subtract', 'amount': 10 }, } targetactions = { 'stability': { 'action': 'add', 'amount': utils.attrchange(target.approval, -15) }, } utils.atomic_transaction(Nation, target.pk, targetactions) utils.atomic_transaction(Nation, nation.pk, nationactions) utils.atomic_transaction(Military, nation.military.pk, nationmilactions) commitspy(spy, spyactions) return {'result': result, 'img': img}
def overview(request): context = {} result = False nation = request.user.nation if request.method == 'POST': if 'train' in request.POST: cost = nation.spies.all().count() * 1000 if nation.budget < cost: result = "You cannot afford this! You need $%sk more!" % ( cost - nation.budget) else: form = newspyform(request.POST) if form.is_valid(): if nation.settings.donor and form.cleaned_data['name']: name = form.cleaned_data['name'] else: name = namegen() img = "spies/%s.png" % random.randint(1, 21) spy = nation.spies.create( name=name, specialty=form.cleaned_data['specialty'], location=nation, portrait=img) action = {'budget': {'action': 'subtract', 'amount': cost}} utils.atomic_transaction(Nation, nation.pk, action) #so it'll load the subtracted cost nation.budget -= cost request.user.nation = nation result = "Agent %s is available to serve his country proudly!" % spy.name else: result = "Your spy cannot specialize in %s" % form[ 'specialty'] elif 'accept' in request.POST or 'deny' in request.POST: vers = 'deny' if 'accept' in request.POST: vers = 'accept' if nation.pending_requests.all().filter( pk=request.POST[vers]).exists(): ext_request = nation.pending_requests.all().get( pk=request.POST[vers]) if vers == 'accept': #update spy location and insert newsitems Spy.objects.filter(pk=ext_request.spy.pk).update( location=nation) news.extraditioned(ext_request.nation, ext_request.target, 'accepted', ext_request.spy) result = "Extradition request is accepted and agent %s arrives in a secure facility" % ext_request.spy.name else: #deny the request! news.extraditioned(ext_request.nation, ext_request.target, 'denied', ext_request.spy) result = "The request is denied" ext_request.delete() spies = Spy.objects.select_related('location').filter(nation=nation) extraditions = False if nation.pending_requests.all().exists(): extraditions = nation.pending_requests.all() context.update({ 'newspy': newspyform(), 'cost': len(spies) * 1000, 'spies': spies, 'extraditions': extraditions, 'enemyspies': nation.infiltrators.filter(discovered=True), 'result': result, }) return render(request, 'nation/intelligence.html', context)