ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/rent.ext
Revision: 1.5
Committed: Fri Jan 5 01:06:27 2007 UTC (17 years, 4 months ago) by root
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
rename goto_map to goto

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