ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/rent.ext
Revision: 1.2
Committed: Mon Sep 18 18:20:11 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.1: +78 -11 lines
Log Message:
*** empty log message ***

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     "/santo/dominion/sdomino/appartment" => [ 10, "santo dominion", "domino"],
8     "/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     "/brest/apartments/brest/town/house" => [30000, "brest", "brecht"],
15     );
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     unless $pl->ob->map && $pl->ob->map->path !~ /nimbus/;
37     }
38    
39 root 1.1 sub update_balance {
40     my ($pl) = @_;
41    
42     my $NOW = time;
43    
44     # 1 silver per day per level per apartment of kingdom tax
45     my $offline = (List::Util::min 30, ($NOW - $pl->{rent}{last_offline_check}) / 86400)
46     * (cf::exp_to_level $pl->ob->stats->exp)
47     * scalar keys %{ $pl->{rent}{apartment} };
48    
49     # once per hour per map rented
50 root 1.2 my $online = ($NOW - $pl->{rent}{last_online_check}) / 3600
51 root 1.1 * List::Util::sum map $apartment{$_}[0], keys %{ $pl->{rent}{apartment} };
52    
53 root 1.2 $pl->{rent}{last_offline_check} = $NOW;
54     $pl->{rent}{last_online_check} = $NOW;
55    
56     $pl->{rent}{balance} += $offline + $online;
57 root 1.1 }
58    
59     sub pay_balance {
60     my ($pl) = @_;
61    
62 root 1.2 update_balance $pl;
63    
64     return unless $pl->{rent}{balance} > 0;
65    
66     my $deduct = cf::ceil $pl->{rent}{balance};
67    
68     my $deduct_string = cf::cost_string_from_value $deduct;
69    
70     if ($deduct <= $pl->ob->{bank_balance}) {
71     $pl->ob->{bank_balance} -= $deduct;
72     $pl->{rent}{balance} -= $deduct;
73     $pl->ob->reply (undef, "Something whispers into your ear:\n"
74     . "Sir, we deducted your apartment rent ($deduct_string) from your bank account.");
75     } else {
76     $pl->ob->reply (undef, "Something whispers into your ear:\n"
77     . "Sir, we want to deduct the apartment rent ($deduct_string), but the bank informed us that they cannot perform the transaction. "
78     . "Please even out your balance so we can deduct the fees, otherwise we will be forced to shut down your access to the apartment.");
79     }
80     }
81    
82     sub check_balance {
83     my ($pl) = @_;
84    
85     pay_balance $pl if $pl->{rent}{balance} < 0;
86    
87     $pl->{rent}{balance} >= 0
88 root 1.1 }
89    
90     sub find_apartment {
91     my ($pl, $name) = @_;
92    
93 root 1.2 my $apartment = (grep $apartment{$_}[2] eq $name, keys %apartment)[0]
94     or $pl->ob->reply (undef, "Sorry, but we do not offer model '$name' for rent.");
95    
96     $apartment
97 root 1.1 }
98    
99     cf::register_script_function "rent::overview" => sub {
100     my ($pl, $types) = @_;
101    
102     while (my ($k, $v) = each %apartment) {
103     my $type = exists $pl->{rent}{apartment}{$k} ? 1 : 2;
104    
105     $pl->ob->reply (undef, "model \"$v->[2]\", situated in $v->[1] ("
106     . (cf::cost_string_from_value $v->[0]) . "/hr)")
107     if $type & $types;
108     }
109     };
110    
111     cf::register_script_function "rent::status" => sub {
112     my ($pl, $types) = @_;
113    
114     if ($pl->{rent}{balance} <= 0) {
115     $pl->ob->reply (undef, "You have no debts to pay.");
116     } else {
117     $pl->ob->reply (undef, "You owe us " . (cf::cost_string_from_value $pl->{rent}{balance}) . ".");
118     }
119     };
120    
121     cf::register_script_function "rent::rent" => sub {
122     my ($pl, $apartment) = @_;
123    
124 root 1.2 $apartment = find_apartment $pl, $apartment
125 root 1.1 or return;
126    
127     update_balance $pl;
128    
129     $pl->{rent}{apartment}{$apartment} = undef;
130 root 1.2
131     $pl->ob->reply (undef, "Wonderful decision, sir! "
132     . "We told the proprietor in $apartment{$apartment}[1] to expect you and let you in. "
133     . "We are sure you will be satisfied!");
134 root 1.1 };
135    
136     cf::register_script_function "rent::stop" => sub {
137     my ($pl, $apartment) = @_;
138    
139 root 1.2 $apartment = find_apartment $pl, $apartment
140 root 1.1 or return;
141    
142     update_balance $pl;
143    
144     delete $pl->{rent}{apartment}{$apartment};
145 root 1.2
146     $pl->ob->reply (undef, "I am sorry to hear that, we will immediately stop charging you for your apartment, of course.");
147 root 1.1 };
148    
149     cf::attach_to_players prio => 100,
150     on_login => sub {
151     return;
152     my ($pl) = @_;
153    
154     use Data::Dumper;
155     warn Dumper $pl;
156    
157     $pl->{rent}{last_offline_check} ||= time;
158    
159     if ($pl->{rent}{last_online_check}) {
160     $pl->{rent}{last_online_check} = time
161     - List::Util::min 3600,
162     $pl->ob->get_ob_key_value ("schmorplog_last_save") - $pl->{rent}{last_online_check};
163     } else {
164     $pl->{rent}{last_online_check} = time;
165     }
166    
167     update_balance $pl;
168     };
169    
170     cf::register_map_attachment rent =>
171     on_enter => sub {
172     my ($map, $pl, $x, $y) = @_;
173    
174 root 1.2 for my $path (keys %{ $pl->{rent}{apartment} }) {
175     $path =~ y/\//_/;
176     $path = sprintf "%s/%s/%s/%s",
177     cf::localdir, cf::playerdir, $pl->ob->name, $path;
178    
179     if ($map->path eq $path) {
180     if (check_balance $pl) {
181     $pl->ob->reply (undef, "Welcome to your apartment, sir!");
182     } else {
183     $pl->ob->reply (undef, "We are sorry, sir, you have to pay your rent first.");
184     reject_entry $pl;
185     }
186    
187     return;
188     }
189     }
190    
191     $pl->ob->reply (undef, "Sir, you have to rent this apartment in The Apartment Shop in Scorn first!");
192     reject_entry $pl;
193     },
194     ;
195    
196     Event->timer (after => 60, interval => 3600, cb => sub {
197     pay_balance $_ for cf::player::list;
198     });
199 root 1.1