… | |
… | |
9 | # - exchange, will exchange between imperials and platinum coins |
9 | # - exchange, will exchange between imperials and platinum coins |
10 | # - balance, simply shows how much money one has on his/her account. |
10 | # - balance, simply shows how much money one has on his/her account. |
11 | # |
11 | # |
12 | # TODO: |
12 | # TODO: |
13 | # - Comment more steps |
13 | # - Comment more steps |
|
|
14 | # - Implement exchange() |
14 | |
15 | |
15 | # The units are being used in each sub so they are declared globally. |
16 | # The units are being used in each sub so they are declared globally. |
16 | my %unit = ( |
17 | my %unit = ( |
17 | silver => 1, |
18 | silver => 1, |
18 | gold => 10, |
19 | gold => 10, |
19 | platina => 50, |
20 | platina => 50, |
20 | imperial => 10000, |
21 | imperial => 10000, |
21 | ); |
22 | ); |
22 | |
23 | |
23 | # Subroutine just to calculate exchange rates. |
24 | # Subroutine just to calculate exchange rates. |
24 | sub calc_exchange_rate { |
25 | sub calc_exchange_rate { |
25 | my ($value, $from_cur, $to_cur) = @_; |
26 | my ($value, $from_cur, $to_cur) = @_; |
26 | |
27 | |
27 | if ($unit{$from_cur} and $unit{$to_cur}) { |
|
|
28 | return $value * $unit{$from_cur} / $unit{$to_cur}; |
28 | $value * $unit{$from_cur} / $unit{$to_cur} |
29 | } |
|
|
30 | } |
29 | } |
31 | |
30 | |
32 | # Calculates and returns how much money someone has starting with the |
31 | # Calculates and returns how much money someone has starting with the |
33 | # highest value currency going down. Think of it like having the date |
32 | # highest value currency going down. Think of it like having the date |
34 | # and time in the format of 2006-05-19 03:21:31 UTC instead of 1148001691 |
33 | # and time in the format of 2006-05-19 03:21:31 UTC instead of 1148001691 |
35 | # seconds since 1970-01-01 00:00:00 UTC. |
34 | # seconds since 1970-01-01 00:00:00 UTC. |
36 | sub balance { |
35 | sub balance { |
37 | my ($who) = @_; |
36 | my ($who, $is_wiz) = @_; |
38 | my $balance = $who->{bank_balance}; |
37 | my $balance = $who->{bank_balance}; |
39 | |
38 | |
40 | if (!$balance or int($balance) == 0) { |
|
|
41 | return "Sorry, you have no balance."; |
39 | return "Sorry, you have no balance." |
42 | } else { |
40 | unless $balance; |
43 | |
41 | |
44 | my $imperials = calc_exchange_rate($balance, "silver", "imperial"); |
42 | my $imperials = calc_exchange_rate $balance, "silver", "imperial"; |
45 | my $platinum = calc_exchange_rate($imperials - int ($imperials), "imperial", "platina"); |
43 | my $platinum = calc_exchange_rate $imperials - (int $imperials), "imperial", "platina"; |
46 | my $gold = calc_exchange_rate($platinum - int ($platinum), "platina", "gold"); |
44 | my $gold = calc_exchange_rate $platinum - (int $platinum), "platina", "gold"; |
47 | my $silver = calc_exchange_rate($gold - int ($gold), "gold", "silver"); |
45 | my $silver = calc_exchange_rate $gold - (int $gold), "gold", "silver"; |
48 | |
46 | |
49 | return "You have ".int($imperials)." imperials, ".int($platinum)." platinum, ".int($gold)." gold and about ".int($silver)." silver coins."; |
47 | return "You have " |
50 | } |
48 | . (int $imperials) . " imperials, " |
|
|
49 | . (int $platinum) . " platinum, " |
|
|
50 | . (int $gold) . " gold and about " |
|
|
51 | . (int $silver) ." silver coins."; |
51 | } |
52 | } |
52 | |
53 | |
53 | sub transaction { |
54 | sub transaction { |
54 | my ($who, $action, $amount, $currency) = @_; |
55 | my ($who, $action, $amount, $currency) = @_; |
55 | my $pay = $amount * $unit{$currency}; |
56 | my $pay = $amount * $unit{$currency}; |
56 | |
57 | |
57 | if (!$unit{$currency}) { |
|
|
58 | return "We don't trade in that currency."; |
58 | return "We don't trade in that currency." |
59 | } |
59 | unless exists $unit{$currency}; |
|
|
60 | |
|
|
61 | return "You can not $action fractions." |
|
|
62 | unless $amount =~ /^\d+$/; |
60 | |
63 | |
61 | # First check for possible overflow and user stupidity |
64 | # First check for possible overflow and user stupidity |
62 | if ($amount > 10000) { |
65 | if ($amount > 10000) { |
63 | return "Sorry, we do not handle more than 10000 units for one ".$action."."; |
66 | return "Sorry, we do not handle more than 10000 units for one ".$action."."; |
64 | } elsif ($amount == 0) { |
67 | } elsif ($amount == 0) { |
… | |
… | |
77 | } |
80 | } |
78 | } elsif ($action eq "withdraw") { |
81 | } elsif ($action eq "withdraw") { |
79 | if ($who->{bank_balance} - $pay > 0) { |
82 | if ($who->{bank_balance} - $pay > 0) { |
80 | $who->{bank_balance} -= $pay; |
83 | $who->{bank_balance} -= $pay; |
81 | if ($currency eq "imperial") { |
84 | if ($currency eq "imperial") { |
82 | my $money = cf::object::new ("imperial"); |
85 | my $money = cf::object::new "imperial"; |
83 | $money->set_property (1, 24, $amount); |
86 | $money->set_property (1, 24, $amount); |
84 | $money->insert_in_ob ($who); |
87 | $money->insert_in_ob ($who); |
85 | } else { |
88 | } else { |
86 | my $money = cf::object::new ($currency."coin"); |
89 | my $money = cf::object::new $currency . "coin"; |
87 | $money->set_property (1, 24, $amount); |
90 | $money->set_property (1, 24, $amount); |
88 | $money->insert_in_ob ($who); |
91 | $money->insert_in_ob ($who); |
89 | } |
92 | } |
90 | return "Withdrew $amount $currency"; |
93 | return "Withdrew $amount $currency"; |
91 | } |
94 | } |
92 | } |
|
|
93 | } |
|
|
94 | |
|
|
95 | sub on_say { |
|
|
96 | my ($event, $ob, $who, $msg) = @_; |
|
|
97 | my ($cmd, $arguments) = split /\s+/, $msg, 2; |
|
|
98 | my ($amount, $currency) = split /\s+/, $arguments, 2; |
|
|
99 | my $service_charge = 5; |
|
|
100 | my @units = sort keys %unit; |
|
|
101 | my $units = join ", ", @units; |
|
|
102 | my $fees = - ($service_charge / 100) + 1; |
|
|
103 | |
|
|
104 | if ($cmd eq "balance") { |
|
|
105 | $who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); |
|
|
106 | } |
|
|
107 | |
|
|
108 | elsif ($cmd eq "deposit" and $currency) { |
|
|
109 | $who->message ($ob->name." says: ".transaction($who, "deposit", $amount, $currency), cf::NDI_WHITE); |
|
|
110 | $who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); |
|
|
111 | } elsif ($cmd eq "deposit" and $amount and $currency eq "") { |
|
|
112 | $who->message ($ob->name." says: Deposit $amount of what: ".$units.".", cf::NDI_WHITE); |
|
|
113 | } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { |
|
|
114 | $who->message ($ob->name." says: How much do you want to deposit?", cf::NDI_WHITE); |
|
|
115 | } |
|
|
116 | |
|
|
117 | elsif ($cmd eq "withdraw" and $currency) { |
|
|
118 | $who->message ($ob->name." says: ".transaction($who, "withdraw", $amount, $currency), cf::NDI_WHITE); |
|
|
119 | $who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); |
|
|
120 | } elsif ($cmd eq "withdraw" and $amount and $currency eq "") { |
|
|
121 | $who->message ($ob->name." says: Withdraw $amount of what: ".$units.".", cf::NDI_WHITE); |
|
|
122 | } elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") { |
|
|
123 | $who->message ($ob->name." says: How much do you want to withdraw?", cf::NDI_WHITE); |
|
|
124 | } |
|
|
125 | |
|
|
126 | elsif ($cmd eq "help" or $cmd eq "yes") { |
|
|
127 | $who->message ($ob->name." says: ".help($arguments), cf::NDI_WHITE); |
|
|
128 | } |
|
|
129 | |
|
|
130 | else { |
|
|
131 | $who->message ($ob->name." says: Do you need help?", cf::NDI_WHITE); |
|
|
132 | } |
95 | } |
133 | } |
96 | } |
134 | |
97 | |
135 | sub help { |
98 | sub help { |
136 | my ($command) = @_; |
99 | my ($command) = @_; |
… | |
… | |
165 | } |
128 | } |
166 | } |
129 | } |
167 | return $exchange_rates."This may change a bit with the stock markets but don't expect it to be much."; |
130 | return $exchange_rates."This may change a bit with the stock markets but don't expect it to be much."; |
168 | } |
131 | } |
169 | } |
132 | } |
|
|
133 | |
|
|
134 | sub on_say { |
|
|
135 | my ($event, $ob, $who, $msg) = @_; |
|
|
136 | my ($cmd, $arguments) = split /\s+/, $msg, 2; |
|
|
137 | my ($amount, $currency) = split /\s+/, $arguments, 2; |
|
|
138 | my $name = $ob->name; |
|
|
139 | my $service_charge = 5; |
|
|
140 | my @units = sort keys %unit; |
|
|
141 | my $units = join ", ", @units; |
|
|
142 | my $fees = - ($service_charge / 100) + 1; |
|
|
143 | |
|
|
144 | if ($cmd eq "balance") { |
|
|
145 | if ($who->flag (cf::FLAG_WIZ)) { |
|
|
146 | my $player = cf::player::find $arguments; |
|
|
147 | $who->message ("$name says: ".balance ($player->ob, 1), cf::NDI_WHITE); |
|
|
148 | } else { |
|
|
149 | $who->message ("$name says: ".balance ($who, 0), cf::NDI_WHITE); |
|
|
150 | } |
|
|
151 | |
|
|
152 | } elsif ($cmd eq "deposit" and $currency) { |
|
|
153 | $who->message ("$name says: " . (transaction $who, "deposit", $amount, $currency), cf::NDI_WHITE); |
|
|
154 | $who->message ("$name says: " . (balance $who), cf::NDI_WHITE); |
|
|
155 | |
|
|
156 | } elsif ($cmd eq "deposit" and $amount and $currency eq "") { |
|
|
157 | $who->message ("$name says: Deposit $amount of what: $units", cf::NDI_WHITE); |
|
|
158 | |
|
|
159 | } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { |
|
|
160 | $who->message ("$name says: How much do you want to deposit?", cf::NDI_WHITE); |
|
|
161 | |
|
|
162 | } elsif ($cmd eq "withdraw" and $currency) { |
|
|
163 | $who->message ("$name says: " . (transaction $who, "withdraw", $amount, $currency), cf::NDI_WHITE); |
|
|
164 | $who->message ("$name says: " . (balance $who), cf::NDI_WHITE); |
|
|
165 | |
|
|
166 | } elsif ($cmd eq "withdraw" and $amount and $currency eq "") { |
|
|
167 | $who->message ("$name says: Withdraw $amount of what: $units", cf::NDI_WHITE); |
|
|
168 | |
|
|
169 | } elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") { |
|
|
170 | $who->message ("$name says: How much do you want to withdraw?", cf::NDI_WHITE); |
|
|
171 | |
|
|
172 | } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments eq "") { |
|
|
173 | $who->message ("$name says: " . (help $arguments), cf::NDI_WHITE); |
|
|
174 | |
|
|
175 | } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments ne "") { |
|
|
176 | $who->message ("$name says: " . (help), cf::NDI_WHITE); |
|
|
177 | |
|
|
178 | } else { |
|
|
179 | $who->message ("$name says: Do you need help?", cf::NDI_WHITE); |
|
|
180 | } |
|
|
181 | } |
|
|
182 | |