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