ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/rent.ext
Revision: 1.5
Committed: Tue Sep 19 18:51:38 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.4: +4 -1 lines
Log Message:
hack to avoid the worst

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3     use List::Util;
4    
5     my %apartment = (
6     "/scorn/apartment/apartments" => [ 1, "scorn", "skorn"],
7 root 1.4 "/santo_dominion/sdomino_appartment" => [ 10, "santo dominion", "domino"],
8 root 1.1 "/darcap/darcap/apartment" => [ 30, "darcap", "thecap"],
9     "/navar_city/apartments/apartment" => [ 250, "navar", "navar"],
10     "/azumauindo/ranbounagisatoshi/apartments/sapartment" => [ 100, "乱暴渚都市", "benjo"],
11     "/azumauindo/suno-yamatoshi/apartments/lapartment1" => [ 1000, "スノー大和島根", "sama"],
12     "/pup_land/nurnberg/apartment/main" => [ 300, "nürnberg", "sauerkraut"],
13     "/pup_land/lone_town/apartment/groundfloor" => [50000, "lone town", "looney"],
14 root 1.4 "/brest/apartments/brest_town_house" => [30000, "brest", "brecht"],
15 root 1.1 );
16    
17     sub teleport {
18     my ($pl, $map, $x, $y) = @_;
19    
20     my $portal = cf::object::new "exit";
21    
22     $portal->slaying ($map);
23     $portal->stats->hp ($x);
24     $portal->stats->sp ($y);
25     $portal->apply ($pl->ob);
26     $portal->free;
27     }
28    
29 root 1.2 # we have to special case some special cases :)
30     sub reject_entry {
31     my ($pl) = @_;
32    
33     cf::override;
34    
35     teleport $pl, "/world/world_105_115", 2, 34
36 root 1.5 unless
37     $pl->ob->map
38     && $pl->ob->map->path !~ /nimbus/
39     && $pl->ob->map->path !~ m%/var/crossfire/players/%;
40 root 1.2 }
41    
42 root 1.1 sub update_balance {
43     my ($pl) = @_;
44    
45     my $NOW = time;
46    
47     # 1 silver per day per level per apartment of kingdom tax
48     my $offline = (List::Util::min 30, ($NOW - $pl->{rent}{last_offline_check}) / 86400)
49     * (cf::exp_to_level $pl->ob->stats->exp)
50     * scalar keys %{ $pl->{rent}{apartment} };
51    
52     # once per hour per map rented
53 root 1.2 my $online = ($NOW - $pl->{rent}{last_online_check}) / 3600
54 root 1.1 * List::Util::sum map $apartment{$_}[0], keys %{ $pl->{rent}{apartment} };
55    
56 root 1.2 $pl->{rent}{last_offline_check} = $NOW;
57     $pl->{rent}{last_online_check} = $NOW;
58    
59     $pl->{rent}{balance} += $offline + $online;
60 root 1.1 }
61    
62     sub pay_balance {
63     my ($pl) = @_;
64    
65 root 1.2 update_balance $pl;
66    
67     return unless $pl->{rent}{balance} > 0;
68    
69     my $deduct = cf::ceil $pl->{rent}{balance};
70    
71     my $deduct_string = cf::cost_string_from_value $deduct;
72    
73     if ($deduct <= $pl->ob->{bank_balance}) {
74     $pl->ob->{bank_balance} -= $deduct;
75     $pl->{rent}{balance} -= $deduct;
76     $pl->ob->reply (undef, "Something whispers into your ear:\n"
77     . "Sir, we deducted your apartment rent ($deduct_string) from your bank account.");
78     } else {
79     $pl->ob->reply (undef, "Something whispers into your ear:\n"
80     . "Sir, we want to deduct the apartment rent ($deduct_string), but the bank informed us that they cannot perform the transaction. "
81     . "Please even out your balance so we can deduct the fees, otherwise we will be forced to shut down your access to the apartment.");
82     }
83     }
84    
85     sub check_balance {
86     my ($pl) = @_;
87    
88 root 1.3 pay_balance $pl if $pl->{rent}{balance} > 0;
89 root 1.2
90 root 1.3 $pl->{rent}{balance} <= 0
91 root 1.1 }
92    
93     sub find_apartment {
94     my ($pl, $name) = @_;
95    
96 root 1.2 my $apartment = (grep $apartment{$_}[2] eq $name, keys %apartment)[0]
97     or $pl->ob->reply (undef, "Sorry, but we do not offer model '$name' for rent.");
98    
99     $apartment
100 root 1.1 }
101    
102     cf::register_script_function "rent::overview" => sub {
103     my ($pl, $types) = @_;
104    
105     while (my ($k, $v) = each %apartment) {
106     my $type = exists $pl->{rent}{apartment}{$k} ? 1 : 2;
107    
108     $pl->ob->reply (undef, "model \"$v->[2]\", situated in $v->[1] ("
109     . (cf::cost_string_from_value $v->[0]) . "/hr)")
110     if $type & $types;
111     }
112     };
113    
114     cf::register_script_function "rent::status" => sub {
115     my ($pl, $types) = @_;
116    
117     if ($pl->{rent}{balance} <= 0) {
118     $pl->ob->reply (undef, "You have no debts to pay.");
119     } else {
120     $pl->ob->reply (undef, "You owe us " . (cf::cost_string_from_value $pl->{rent}{balance}) . ".");
121     }
122     };
123    
124     cf::register_script_function "rent::rent" => sub {
125     my ($pl, $apartment) = @_;
126    
127 root 1.2 $apartment = find_apartment $pl, $apartment
128 root 1.1 or return;
129    
130     update_balance $pl;
131    
132     $pl->{rent}{apartment}{$apartment} = undef;
133 root 1.2
134     $pl->ob->reply (undef, "Wonderful decision, sir! "
135     . "We told the proprietor in $apartment{$apartment}[1] to expect you and let you in. "
136     . "We are sure you will be satisfied!");
137 root 1.1 };
138    
139     cf::register_script_function "rent::stop" => sub {
140     my ($pl, $apartment) = @_;
141    
142 root 1.2 $apartment = find_apartment $pl, $apartment
143 root 1.1 or return;
144    
145     update_balance $pl;
146    
147     delete $pl->{rent}{apartment}{$apartment};
148 root 1.2
149     $pl->ob->reply (undef, "I am sorry to hear that, we will immediately stop charging you for your apartment, of course.");
150 root 1.1 };
151    
152     cf::attach_to_players prio => 100,
153     on_login => sub {
154     return;
155     my ($pl) = @_;
156    
157     use Data::Dumper;
158     warn Dumper $pl;
159    
160     $pl->{rent}{last_offline_check} ||= time;
161    
162     if ($pl->{rent}{last_online_check}) {
163     $pl->{rent}{last_online_check} = time
164     - List::Util::min 3600,
165     $pl->ob->get_ob_key_value ("schmorplog_last_save") - $pl->{rent}{last_online_check};
166     } else {
167     $pl->{rent}{last_online_check} = time;
168     }
169    
170     update_balance $pl;
171     };
172    
173     cf::register_map_attachment rent =>
174     on_enter => sub {
175     my ($map, $pl, $x, $y) = @_;
176    
177 root 1.2 for my $path (keys %{ $pl->{rent}{apartment} }) {
178     $path =~ y/\//_/;
179     $path = sprintf "%s/%s/%s/%s",
180     cf::localdir, cf::playerdir, $pl->ob->name, $path;
181    
182     if ($map->path eq $path) {
183     if (check_balance $pl) {
184     $pl->ob->reply (undef, "Welcome to your apartment, sir!");
185     } else {
186     $pl->ob->reply (undef, "We are sorry, sir, you have to pay your rent first.");
187     reject_entry $pl;
188     }
189    
190     return;
191     }
192     }
193    
194     $pl->ob->reply (undef, "Sir, you have to rent this apartment in The Apartment Shop in Scorn first!");
195     reject_entry $pl;
196     },
197     ;
198    
199     Event->timer (after => 60, interval => 3600, cb => sub {
200     pay_balance $_ for cf::player::list;
201     });
202 root 1.1