ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/bank.ext
(Generate patch)

Comparing deliantra/maps/perl/bank.ext (file contents):
Revision 1.4 by pippijn, Mon May 22 17:57:55 2006 UTC vs.
Revision 1.8 by pippijn, Mon May 22 18:54:12 2006 UTC

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()
15# - Add a DM command to view any user's balance.
14 16
15# 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.
16my %unit = ( 18my %unit = (
17 silver => 1, 19 silver => 1,
18 gold => 10, 20 gold => 10,
19 platina => 50, 21 platina => 50,
20 imperial => 10000, 22 imperial => 10000,
21); 23);
22 24
23# Subroutine just to calculate exchange rates. 25# Subroutine just to calculate exchange rates.
24sub calc_exchange_rate { 26sub calc_exchange_rate {
25 my ($value, $from_cur, $to_cur) = @_; 27 my ($value, $from_cur, $to_cur) = @_;
26 28
27 if ($unit{$from_cur} and $unit{$to_cur}) {
28 return $value * $unit{$from_cur} / $unit{$to_cur}; 29 $value * $unit{$from_cur} / $unit{$to_cur}
29 }
30} 30}
31 31
32# Calculates and returns how much money someone has starting with the 32# Calculates and returns how much money someone has starting with the
33# 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
34# 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
35# seconds since 1970-01-01 00:00:00 UTC. 35# seconds since 1970-01-01 00:00:00 UTC.
36sub balance { 36sub balance {
37 my ($who) = @_; 37 my ($who) = @_;
38 my $balance = $who->{bank_balance}; 38 my $balance = $who->{bank_balance};
39 39
40 if (!$balance or int($balance) == 0) {
41 return "Sorry, you have no balance."; 40 return "Sorry, you have no balance."
42 } else { 41 unless $balance;
43 42
44 my $imperials = calc_exchange_rate($balance, "silver", "imperial"); 43 my $imperials = calc_exchange_rate $balance, "silver", "imperial";
45 my $platinum = calc_exchange_rate($imperials - int ($imperials), "imperial", "platina"); 44 my $platinum = calc_exchange_rate $imperials - (int $imperials), "imperial", "platina";
46 my $gold = calc_exchange_rate($platinum - int ($platinum), "platina", "gold"); 45 my $gold = calc_exchange_rate $platinum - (int $platinum), "platina", "gold";
47 my $silver = calc_exchange_rate($gold - int ($gold), "gold", "silver"); 46 my $silver = calc_exchange_rate $gold - (int $gold), "gold", "silver";
48 47
49 return "You have ".int($imperials)." imperials, ".int($platinum)." platinum, ".int($gold)." gold and about ".int($silver)." silver coins."; 48 return "You have "
50 } 49 . (int $imperials) . " imperials, "
50 . (int $platinum) . " platinum, "
51 . (int $gold) . " gold and about "
52 . (int $silver) ." silver coins.";
51} 53}
52 54
53sub transaction { 55sub transaction {
54 my ($who, $action, $amount, $currency) = @_; 56 my ($who, $action, $amount, $currency) = @_;
55 my $pay = $amount * $unit{$currency}; 57 my $pay = $amount * $unit{$currency};
56 58
57 if (!$unit{$currency}) {
58 return "We don't trade in that currency."; 59 return "We don't trade in that currency."
59 } 60 unless exists $unit{$currency};
61
62 return "You can not $action fractions."
63 unless $amount =~ /^\d+$/;
60 64
61 # First check for possible overflow and user stupidity 65 # First check for possible overflow and user stupidity
62 if ($amount > 10000) { 66 if ($amount > 10000) {
63 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.".";
64 } elsif ($amount == 0) { 68 } elsif ($amount == 0) {
77 } 81 }
78 } elsif ($action eq "withdraw") { 82 } elsif ($action eq "withdraw") {
79 if ($who->{bank_balance} - $pay > 0) { 83 if ($who->{bank_balance} - $pay > 0) {
80 $who->{bank_balance} -= $pay; 84 $who->{bank_balance} -= $pay;
81 if ($currency eq "imperial") { 85 if ($currency eq "imperial") {
82 my $money = cf::object::new ("imperial"); 86 my $money = cf::object::new "imperial";
83 $money->set_property (1, 24, $amount); 87 $money->set_property (1, 24, $amount);
84 $money->insert_in_ob ($who); 88 $money->insert_in_ob ($who);
85 } else { 89 } else {
86 my $money = cf::object::new ($currency."coin"); 90 my $money = cf::object::new $currency . "coin";
87 $money->set_property (1, 24, $amount); 91 $money->set_property (1, 24, $amount);
88 $money->insert_in_ob ($who); 92 $money->insert_in_ob ($who);
89 } 93 }
90 return "Withdrew $amount $currency"; 94 return "Withdrew $amount $currency";
91 } 95 }
94 98
95sub on_say { 99sub on_say {
96 my ($event, $ob, $who, $msg) = @_; 100 my ($event, $ob, $who, $msg) = @_;
97 my ($cmd, $arguments) = split /\s+/, $msg, 2; 101 my ($cmd, $arguments) = split /\s+/, $msg, 2;
98 my ($amount, $currency) = split /\s+/, $arguments, 2; 102 my ($amount, $currency) = split /\s+/, $arguments, 2;
103 my $name = $ob->name;
99 my $service_charge = 5; 104 my $service_charge = 5;
100 my @units = sort keys %unit; 105 my @units = sort keys %unit;
101 my $units = join ", ", @units; 106 my $units = join ", ", @units;
102 my $fees = - ($service_charge / 100) + 1; 107 my $fees = - ($service_charge / 100) + 1;
103 108
104 if ($cmd eq "balance") { 109 if ($cmd eq "balance") {
105 $who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); 110 $who->message ("$name says: ".balance ($who), cf::NDI_WHITE);
106 }
107 111
108 elsif ($cmd eq "deposit" and $currency) { 112 } elsif ($cmd eq "deposit" and $currency) {
109 $who->message ($ob->name." says: ".transaction($who, "deposit", $amount, $currency), cf::NDI_WHITE); 113 $who->message ("$name says: " . (transaction $who, "deposit", $amount, $currency), cf::NDI_WHITE);
110 $who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); 114 $who->message ("$name says: " . (balance $who), cf::NDI_WHITE);
115
111 } elsif ($cmd eq "deposit" and $amount and $currency eq "") { 116 } elsif ($cmd eq "deposit" and $amount and $currency eq "") {
112 $who->message ($ob->name." says: Deposit $amount of what: ".$units.".", cf::NDI_WHITE); 117 $who->message ("$name says: Deposit $amount of what: $units", cf::NDI_WHITE);
118
113 } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { 119 } 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); 120 $who->message ("$name says: How much do you want to deposit?", cf::NDI_WHITE);
115 }
116 121
117 elsif ($cmd eq "withdraw" and $currency) { 122 } elsif ($cmd eq "withdraw" and $currency) {
118 $who->message ($ob->name." says: ".transaction($who, "withdraw", $amount, $currency), cf::NDI_WHITE); 123 $who->message ("$name says: " . (transaction $who, "withdraw", $amount, $currency), cf::NDI_WHITE);
119 $who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); 124 $who->message ("$name says: " . (balance $who), cf::NDI_WHITE);
125
120 } elsif ($cmd eq "withdraw" and $amount and $currency eq "") { 126 } elsif ($cmd eq "withdraw" and $amount and $currency eq "") {
121 $who->message ($ob->name." says: Withdraw $amount of what: ".$units.".", cf::NDI_WHITE); 127 $who->message ("$name says: Withdraw $amount of what: $units", cf::NDI_WHITE);
128
122 } elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") { 129 } 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); 130 $who->message ("$name says: How much do you want to withdraw?", cf::NDI_WHITE);
124 }
125 131
126 elsif ($cmd eq "help" or $cmd eq "yes") { 132 } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments eq "") {
127 $who->message ($ob->name." says: ".help($arguments), cf::NDI_WHITE); 133 $who->message ("$name says: " . (help $arguments), cf::NDI_WHITE);
128 }
129 134
135 } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments ne "") {
136 $who->message ("$name says: " . (help), cf::NDI_WHITE);
137
130 else { 138 } else {
131 $who->message ($ob->name." says: Do you need help?", cf::NDI_WHITE); 139 $who->message ("$name says: Do you need help?", cf::NDI_WHITE);
132 } 140 }
133} 141}
134 142
135sub help { 143sub help {
136 my ($command) = @_; 144 my ($command) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines