Skip to content

Commit 675c0a3

Browse files
committed
closes #432
2 parents 160b3f4 + 9115236 commit 675c0a3

File tree

94 files changed

+4417
-3170
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+4417
-3170
lines changed

Assets/EconomicSimulation/ClassDiagram.cd.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.23 KB
Binary file not shown.

Assets/EconomicSimulation/GeneralClasses.cd

Lines changed: 344 additions & 0 deletions
Large diffs are not rendered by default.

Assets/EconomicSimulation/GeneralClasses.cd.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/EconomicSimulation/Scripts/Logic/Agent.cs

Lines changed: 134 additions & 101 deletions
Large diffs are not rendered by default.

Assets/EconomicSimulation/Scripts/Logic/Army.cs

Lines changed: 50 additions & 153 deletions
Large diffs are not rendered by default.

Assets/EconomicSimulation/Scripts/Logic/Bank.cs

Lines changed: 175 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2,195 +2,261 @@
22
using System.Collections;
33
using System;
44
using Nashet.ValueSpace;
5+
using Nashet.Utils;
6+
57
namespace Nashet.EconomicSimulation
68
{
7-
public class Bank : Agent
9+
public class Bank : Agent, INameable
810
{
9-
Value givenLoans = new Value(0);
10-
private readonly Country country;
11-
public Bank(Country country) : base(0f, null, null)
11+
private readonly Money givenCredits = new Money(0);
12+
//private readonly Country country;
13+
14+
override public string ToString()
15+
{
16+
return FullName;
17+
}
18+
public string FullName
19+
{
20+
get { return "Bank of " + country.ShortName; }
21+
}
22+
23+
public string ShortName
24+
{
25+
get { return "Bank of " + country.ShortName; }
26+
}
27+
28+
public Bank(Country country) : base(country)
1229
{
13-
this.country = country;
30+
//this.country = country;
1431
}
1532
/// <summary>
16-
/// Returns money to bank. checks inside.
17-
/// Just wouldn't take money if giver hasn't enough money
33+
/// Gives money to bank (as deposit or loan payment). Checks inside.
34+
/// Just wouldn't take money if giver hasn't enough money.
1835
/// Don't provide variables like Cash as argument!! It would default to zero!
1936
/// </summary>
20-
internal void takeMoney(Agent giver, Value howMuchTake)
37+
internal void ReceiveMoney(Agent giver, ReadOnlyValue sum)
2138
{
22-
if (giver.pay(this, howMuchTake))
23-
if (giver.loans.isBiggerThan(Value.Zero)) //has debt (meaning has no deposits)
24-
if (howMuchTake.isBiggerOrEqual(giver.loans)) // cover debt
39+
if (giver.PayWithoutRecord(this, sum))
40+
if (giver.loans.isNotZero()) //has debt (meaning has no deposits)
41+
if (sum.isBiggerOrEqual(giver.loans)) // cover debt
2542
{
26-
Value extraMoney = howMuchTake.Copy().subtract(giver.loans);
27-
this.givenLoans.subtract(giver.loans);
28-
giver.loans.set(0f);
29-
giver.deposits.set(extraMoney);
43+
Value extraMoney = sum.Copy().Subtract(giver.loans);
44+
this.givenCredits.Subtract(giver.loans);
45+
46+
giver.loans.Set(0f); //put extra money on deposit
47+
giver.deposits.Set(extraMoney);
3048
}
31-
else// not cover debt
49+
else// not cover debt, just decrease loan
3250
{
33-
giver.loans.subtract(howMuchTake);
34-
this.givenLoans.subtract(howMuchTake);
51+
giver.loans.Subtract(sum);
52+
this.givenCredits.Subtract(sum);
3553
}
3654
else
3755
{
38-
giver.deposits.Add(howMuchTake);
56+
giver.deposits.Add(sum);
3957
}
4058
}
4159

4260
/// <summary>
43-
///checks outside
61+
/// Gives money in credit or returns deposit, if possible.
62+
/// Gives whole sum or gives nothing.
63+
/// Checks inside. Return false if didn't give credit.
4464
/// </summary>
45-
internal void giveMoney(Agent taker, Value howMuch)
65+
internal bool GiveCredit(Agent taker, ReadOnlyValue desiredCredit) // todo check
4666
{
47-
payWithoutRecord(taker, howMuch);
48-
if (taker.deposits.isBiggerThan(Value.Zero)) // has deposit (meaning, has no loans)
49-
if (howMuch.isBiggerOrEqual(taker.deposits))// loan is bigger than this deposit
67+
if (taker.deposits.isNotZero()) // has deposit (meaning, has no loans)
68+
{
69+
if (desiredCredit.isBiggerThan(taker.deposits))// loan is bigger than this deposit
5070
{
51-
Value notEnoughMoney = howMuch.Copy().subtract(taker.deposits);
52-
taker.deposits.set(0f);
53-
taker.loans.set(notEnoughMoney);
54-
this.givenLoans.Add(notEnoughMoney);
71+
ReadOnlyValue returnedDeposit = ReturnDeposit(taker, taker.deposits);
72+
if (returnedDeposit.isSmallerThan(taker.deposits))
73+
return false;// if can't return deposit than can't give credit for sure
74+
//returnedMoney = new ReadOnlyValue(0f);
75+
76+
Value restOfTheSum = desiredCredit.Copy().Subtract(returnedDeposit);
77+
if (CanGiveCredit(taker, restOfTheSum))
78+
{
79+
taker.loans.Set(restOfTheSum);//important
80+
this.givenCredits.Add(restOfTheSum);
81+
PayWithoutRecord(taker, restOfTheSum);
82+
return true;
83+
}
84+
else
85+
return false;
5586
}
56-
else // not cover
87+
else // no need for credit, just return deposit
5788
{
58-
taker.deposits.subtract(howMuch);
89+
// if can't return deposit than can't give credit for sure
90+
if (CanReturnDeposit(taker, desiredCredit))
91+
{
92+
ReturnDeposit(taker, desiredCredit);
93+
return true;
94+
}
95+
else
96+
return false;
5997
}
98+
}
6099
else
61100
{
62-
taker.loans.Add(howMuch);
63-
this.givenLoans.Add(howMuch);
101+
if (CanGiveCredit(taker, desiredCredit))
102+
{
103+
taker.loans.Add(desiredCredit);
104+
this.givenCredits.Add(desiredCredit);
105+
PayWithoutRecord(taker, desiredCredit);
106+
return true;
107+
}
108+
else
109+
return false;
64110
}
65111
}
66-
/// <summary>
67-
///checks outside
68-
/// </summary>
69-
//internal bool giveMoneyIf(Consumer taker, Value howMuch)
70-
//{
71-
//
72-
// Value needLoan = howMuch.subtractOutside(taker.cash);
73-
// if (this.canGiveMoney(taker, needLoan))
74-
// {
75-
// this.giveMoney(taker, needLoan);
76-
// return true;
77-
// }
78-
//
79-
// return false;
80-
//}
112+
81113
/// <summary>
82114
/// Gives credit. Checks inside. Just wouldn't give money if can't
83115
/// </summary>
84-
internal bool giveLackingMoney(Agent taker, ReadOnlyValue sum)
116+
internal bool GiveLackingMoneyInCredit(Agent taker, ReadOnlyValue desirableSum)
85117
{
86-
if (taker.GetCountry().Invented(Invention.Banking))// find money in bank?
118+
if (taker.Country.Invented(Invention.Banking))// find money in bank?
87119
{
88-
Value lackOfSum = sum.Copy().subtract(taker.cash);
89-
if (canGiveMoney(taker, lackOfSum))
90-
{
91-
giveMoney(taker, lackOfSum);
92-
return true;
93-
}
120+
Value lackOfSum = desirableSum.Copy().Subtract(taker.Cash);
121+
return GiveCredit(taker, lackOfSum);
94122
}
95123
return false;
96124
}
97125
/// <summary>
98-
/// Returns deposits only. As much as possible. checks inside. Just wouldn't give money if can't
99-
/// </summary>
100-
/// //todo - add some cross bank money transfer?
101-
internal void returnAllMoney(Agent agent)
126+
/// Result is how much deposit was really returned. Checks inside. Just wouldn't give money if can't
127+
/// Can return less than was prompted
128+
/// </summary>
129+
internal ReadOnlyValue ReturnDeposit(Agent toWhom, ReadOnlyValue howMuchWants)
102130
{
103-
//if (canGiveLoan(agent.deposits))
104-
// giveMoney(agent, agent.deposits);
131+
if (toWhom.Country.Invented(Invention.Banking))// find money in bank? //todo remove checks, make bank==null if uninvented
132+
{
133+
var maxReturnLimit = HowMuchDepositCanReturn(toWhom);
134+
if (maxReturnLimit.isBiggerOrEqual(howMuchWants))
135+
{
136+
ReadOnlyValue returnMoney;
137+
if (howMuchWants.isBiggerThan(maxReturnLimit))
138+
returnMoney = maxReturnLimit;
139+
else
140+
returnMoney = howMuchWants;
105141

106-
giveMoney(agent, howMuchDepositCanReturn(agent));
107-
}
142+
if (returnMoney.isNotZero())// return deposit
143+
{
144+
//giveMoney(toWhom, moneyToReturn);
145+
toWhom.deposits.Subtract(returnMoney);
146+
PayWithoutRecord(toWhom, returnMoney);
108147

109-
internal Value getGivenLoans()
148+
}
149+
return returnMoney;
150+
}
151+
}
152+
return Value.Zero;
153+
}
154+
/// <summary>
155+
/// Returns deposits only. As much as possible. checks inside. Just wouldn't give money if can't
156+
/// </summary>
157+
internal void ReturnAllDeposits(Agent toWhom)
110158
{
111-
return givenLoans;
159+
ReturnDeposit(toWhom, HowMuchDepositCanReturn(toWhom));
112160
}
113161
/// <summary>
114-
/// how much money have in cash. It's copy
115-
/// </summary>
116-
internal Value getReservs()
162+
/// includes checks for Cash and deposit. Returns copy
163+
/// </summary>
164+
internal ReadOnlyValue HowMuchDepositCanReturn(Agent agent)
117165
{
118-
return cash.Copy();
166+
var howMuchReturn = agent.deposits.Copy();//initialization
167+
168+
var wantedResrve = Cash.Copy().Subtract(GetMinimalReservs(), false); //defaults to zero if there is no money to give
169+
170+
if (howMuchReturn.isBiggerThan(wantedResrve))
171+
howMuchReturn.Set(wantedResrve);
172+
173+
return howMuchReturn;
119174
}
120175
/// <summary>
121-
/// Checks reserve limits and deposits
122-
/// </summary>
123-
internal bool canGiveMoney(Agent agent, Value loan)
176+
/// includes checks for Cash and deposit.
177+
/// </summary>
178+
internal bool CanReturnDeposit(Agent agent, ReadOnlyValue howMuch)
124179
{
125-
return howMuchCanGive(agent).isBiggerOrEqual(loan);
180+
return HowMuchDepositCanReturn(agent).isBiggerOrEqual(howMuch);
126181
}
127182

128-
private Value getMinimalReservs()
183+
internal ReadOnlyValue GetGivenCredits()
129184
{
130-
//todo improve reserves
131-
return new Value(1000f);
185+
return givenCredits;
132186
}
187+
/// <summary>
188+
/// how much money have in Cash.
189+
/// </summary>
190+
//internal ReadOnlyValue getReservs()
191+
//{
192+
// return Cash;
193+
//}
133194

134-
override public string ToString()
195+
196+
private ReadOnlyValue GetMinimalReservs()
135197
{
136-
return cash.ToString();
198+
//todo improve reserves
199+
return new Value(100f);
137200
}
138201

139-
internal void defaultLoaner(Agent agent)
202+
203+
/// <summary>
204+
/// Agent refuses to pay debt
205+
/// </summary>
206+
internal void OnLoanerRefusesToPay(Agent agent)
140207
{
141-
givenLoans.subtract(agent.loans);
142-
agent.loans.set(0);
208+
givenCredits.Subtract(agent.loans);
209+
agent.loans.Set(0);
143210
}
144211
/// <summary>
145212
/// Assuming all clients already defaulted theirs loans
146213
/// </summary>
147-
internal void add(Bank annexingBank)
214+
internal void Annex(Bank annexingBank)
148215
{
149-
annexingBank.cash.sendAll(this.cash);
150-
annexingBank.givenLoans.sendAll(this.givenLoans);
151-
}
152-
bool isItEnoughReserves(Value sum)
153-
{
154-
return cash.Copy().subtract(getMinimalReservs()).isNotZero();
216+
annexingBank.PayAllAvailableMoney(this);
217+
annexingBank.givenCredits.SendAll(this.givenCredits);
155218
}
156219

220+
157221
/// <summary>
158-
/// Checks reserve limits and deposits. Returns copy
222+
/// Checks reserve limits
159223
/// </summary>
160-
internal Value howMuchCanGive(Agent agent)
224+
internal bool CanGiveCredit(Agent whom, ReadOnlyValue desirableSum)
161225
{
162-
Value wouldGive = cash.Copy().subtract(getMinimalReservs(), false);
163-
if (agent.deposits.isBiggerThan(wouldGive))
164-
{
165-
wouldGive = agent.deposits.Copy(); // increase wouldGive to deposits size
166-
if (wouldGive.isBiggerThan(cash)) //decrease wouldGive to cash size
167-
wouldGive.set(cash);
168-
}
169-
return wouldGive;
226+
return HowBigCreditCanGive(whom).isBiggerOrEqual(desirableSum);
170227
}
171228
/// <summary>
172-
/// includes checks for cash and deposit size. Returns copy
173-
/// </summary>
174-
internal Value howMuchDepositCanReturn(Agent agent)
229+
/// How much can
230+
/// Checks reserve limits.
231+
/// </summary>
232+
internal ReadOnlyValue HowBigCreditCanGive(Agent whom)
175233
{
176-
if (cash.isBiggerOrEqual(agent.deposits))
177-
return agent.deposits.Copy();
178-
else
179-
return cash.Copy();
234+
Value maxSum = Cash.Copy().Subtract(GetMinimalReservs(), false);
235+
//if (whom.deposits.isBiggerThan(maxSum))
236+
//{
237+
// maxSum = whom.deposits.Copy(); // sets maxSum to deposits size
238+
// if (maxSum.isBiggerThan(Cash)) //decrease maxSum to Cash size
239+
// maxSum.Set(Cash);
240+
//}
241+
return maxSum;
180242
}
243+
181244
internal void destroy(Country byWhom)
182245
{
183-
cash.sendAll(byWhom.cash);
184-
givenLoans.setZero();
246+
PayAllAvailableMoney(byWhom);
247+
givenCredits.SetZero();
185248
}
186249

187250
public override void simulate()
188251
{
189252
throw new NotImplementedException();
190253
}
191-
public override Country GetCountry()
254+
public void Nationalize()
192255
{
193-
return country;
256+
country.Bank.PayAllAvailableMoney(country);
257+
country.Bank.givenCredits.SetZero();
258+
country.loans.SetZero();
259+
country.deposits.SetZero();
194260
}
195261
}
196262
}

0 commit comments

Comments
 (0)