ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/rent.ext
Revision: 1.26
Committed: Fri Mar 19 22:11:56 2010 UTC (14 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.25: +3 -2 lines
Log Message:
new apartment.

File Contents

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