1 |
#! perl |
2 |
|
3 |
# The international bank script. There are five commands one can |
4 |
# give the banker in-game: |
5 |
# - help, will show the usage information |
6 |
# - deposit, can deposit a certain amount of a given currency unit |
7 |
# - withdraw, can give out a certain amount of a given currency unit |
8 |
# provided, the bank account has this amount of money |
9 |
# - exchange, will exchange between imperials and platinum coins |
10 |
# - balance, simply shows how much money one has on his/her account. |
11 |
# |
12 |
# TODO: |
13 |
# - Comment more steps |
14 |
# - Implement exchange() |
15 |
|
16 |
# The units are being used in each sub so they are declared globally. |
17 |
my %unit = ( |
18 |
silver => 1, |
19 |
gold => 10, |
20 |
platina => 50, |
21 |
imperial => 10000, |
22 |
); |
23 |
|
24 |
# Subroutine just to calculate exchange rates. |
25 |
sub calc_exchange_rate { |
26 |
my ($value, $from_cur, $to_cur) = @_; |
27 |
|
28 |
if ($unit{$from_cur} and $unit{$to_cur}) { |
29 |
return $value * $unit{$from_cur} / $unit{$to_cur}; |
30 |
} |
31 |
} |
32 |
|
33 |
# Calculates and returns how much money someone has starting with the |
34 |
# highest value currency going down. Think of it like having the date |
35 |
# and time in the format of 2006-05-19 03:21:31 UTC instead of 1148001691 |
36 |
# seconds since 1970-01-01 00:00:00 UTC. |
37 |
sub balance { |
38 |
my ($who) = @_; |
39 |
my $balance = $who->{bank_balance}; |
40 |
|
41 |
if (!$balance or int($balance) == 0) { |
42 |
return "Sorry, you have no balance."; |
43 |
} else { |
44 |
|
45 |
my $imperials = calc_exchange_rate($balance, "silver", "imperial"); |
46 |
my $platinum = calc_exchange_rate($imperials - int ($imperials), "imperial", "platina"); |
47 |
my $gold = calc_exchange_rate($platinum - int ($platinum), "platina", "gold"); |
48 |
my $silver = calc_exchange_rate($gold - int ($gold), "gold", "silver"); |
49 |
|
50 |
return "You have ".int($imperials)." imperials, ".int($platinum)." platinum, ".int($gold)." gold and about ".int($silver)." silver coins."; |
51 |
} |
52 |
} |
53 |
|
54 |
sub transaction { |
55 |
my ($who, $action, $amount, $currency) = @_; |
56 |
my $pay = $amount * $unit{$currency}; |
57 |
|
58 |
if (!$unit{$currency}) { |
59 |
return "We don't trade in that currency."; |
60 |
} |
61 |
|
62 |
if ($amount !=~ /\d+/) { |
63 |
return "You can not $action fractions."; |
64 |
} |
65 |
|
66 |
# First check for possible overflow and user stupidity |
67 |
if ($amount > 10000) { |
68 |
return "Sorry, we do not handle more than 10000 units for one ".$action."."; |
69 |
} elsif ($amount == 0) { |
70 |
return "You can not ".$action." nothing."; |
71 |
} elsif ($amount < 0) { |
72 |
return "You can not ".$action." negative values."; |
73 |
} |
74 |
|
75 |
# Here we handle the transactions |
76 |
if ($action eq "deposit") { |
77 |
if ($who->pay_amount ($pay)) { |
78 |
$who->{bank_balance} += $pay; |
79 |
return "$amount $currency received."; |
80 |
} else { |
81 |
return "You don't have enough money with you."; |
82 |
} |
83 |
} elsif ($action eq "withdraw") { |
84 |
if ($who->{bank_balance} - $pay > 0) { |
85 |
$who->{bank_balance} -= $pay; |
86 |
if ($currency eq "imperial") { |
87 |
my $money = cf::object::new ("imperial"); |
88 |
$money->set_property (1, 24, $amount); |
89 |
$money->insert_in_ob ($who); |
90 |
} else { |
91 |
my $money = cf::object::new ($currency."coin"); |
92 |
$money->set_property (1, 24, $amount); |
93 |
$money->insert_in_ob ($who); |
94 |
} |
95 |
return "Withdrew $amount $currency"; |
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
sub on_say { |
101 |
my ($event, $ob, $who, $msg) = @_; |
102 |
my ($cmd, $arguments) = split /\s+/, $msg, 2; |
103 |
my ($amount, $currency) = split /\s+/, $arguments, 2; |
104 |
my $service_charge = 5; |
105 |
my @units = sort keys %unit; |
106 |
my $units = join ", ", @units; |
107 |
my $fees = - ($service_charge / 100) + 1; |
108 |
|
109 |
if ($cmd eq "balance") { |
110 |
$who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); |
111 |
} |
112 |
|
113 |
elsif ($cmd eq "deposit" and $currency) { |
114 |
$who->message ($ob->name." says: ".transaction($who, "deposit", $amount, $currency), cf::NDI_WHITE); |
115 |
$who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); |
116 |
} elsif ($cmd eq "deposit" and $amount and $currency eq "") { |
117 |
$who->message ($ob->name." says: Deposit $amount of what: ".$units.".", cf::NDI_WHITE); |
118 |
} elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { |
119 |
$who->message ($ob->name." says: How much do you want to deposit?", cf::NDI_WHITE); |
120 |
} |
121 |
|
122 |
elsif ($cmd eq "withdraw" and $currency) { |
123 |
$who->message ($ob->name." says: ".transaction($who, "withdraw", $amount, $currency), cf::NDI_WHITE); |
124 |
$who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); |
125 |
} elsif ($cmd eq "withdraw" and $amount and $currency eq "") { |
126 |
$who->message ($ob->name." says: Withdraw $amount of what: ".$units.".", cf::NDI_WHITE); |
127 |
} elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") { |
128 |
$who->message ($ob->name." says: How much do you want to withdraw?", cf::NDI_WHITE); |
129 |
} |
130 |
|
131 |
elsif ($cmd eq "help" or $cmd eq "yes") { |
132 |
$who->message ($ob->name." says: ".help($arguments), cf::NDI_WHITE); |
133 |
} |
134 |
|
135 |
else { |
136 |
$who->message ($ob->name." says: Do you need help?", cf::NDI_WHITE); |
137 |
} |
138 |
} |
139 |
|
140 |
sub help { |
141 |
my ($command) = @_; |
142 |
|
143 |
if ($command eq "") { |
144 |
return "You can: |
145 |
deposit <amount> <currency> |
146 |
withdraw <amount> <currency> |
147 |
balance |
148 |
exchange <amount> <direction> |
149 |
help <topic>"; |
150 |
} elsif ($command eq "deposit") { |
151 |
return "You can deposit some of the money you don't currently use in our bank. To do this, ask me to: |
152 |
deposit <amount> <currency>\nSee help exchange for our currencies."; |
153 |
} elsif ($command eq "withdraw") { |
154 |
return "To get the money you stored in our system back, just say: |
155 |
withdraw <amount> <currency>"; |
156 |
} elsif ($command eq "balance") { |
157 |
return "I can tell you how much money you are saving in our bank, just ask me about your balance."; |
158 |
} elsif ($command eq "exchange") { |
159 |
return "We exchange between imperial notes and platinum coins at a very low price. To change from platinum to imperials, say: |
160 |
exchange <amount> platina\nTo change from imperials to platinum, say: |
161 |
exchange <amount> imperial\nWe currently only exchange between these two currencies. If you want to know more about exchange rates, ask me: |
162 |
help exchange rates\nThanks for using our bank."; |
163 |
} elsif ($command eq "exchange rates") { |
164 |
my @units = sort { $unit{$b} <=> $unit{$a} } keys %unit; |
165 |
my $exchange_rates = "Here is a list of exchange rates:\n"; |
166 |
|
167 |
for my $from_cur_nr (0 .. $#units) { |
168 |
for my $to_cur_nr ($from_cur_nr+1 .. $#units) { |
169 |
$exchange_rates = $exchange_rates."One $units[$from_cur_nr] is ".calc_exchange_rate(1, $units[$from_cur_nr], $units[$to_cur_nr])." $units[$to_cur_nr]\n"; |
170 |
} |
171 |
} |
172 |
return $exchange_rates."This may change a bit with the stock markets but don't expect it to be much."; |
173 |
} |
174 |
} |