ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/rent.ext
Revision: 1.15
Committed: Sat Oct 24 11:08:14 2009 UTC (14 years, 7 months ago) by root
Branch: MAIN
Changes since 1.14: +2 -1 lines
Log Message:
add apartment

File Contents

# Content
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 "/celvear_port/tower2/tower2" => [ 100, "celvear port", "kevlar"],
10 "/navar_city/apartments/apartment" => [ 250, "navar", "navar"],
11 "/azumauindo/ranbounagisatoshi/apartments/sapartment" => [ 100, "乱暴渚都市", "benjo"],
12 "/azumauindo/suno-yamatoshi/apartments/lapartment1" => [ 1000, "スノー大和島根", "sama"],
13 "/elmex/jeweler/jeweler_inn_upper" => [ 500, "jeweler town", "jewelor"],
14 "/pup_land/nurnberg/apartment/main" => [ 300, "nürnberg", "sauerkraut"],
15 "/pup_land/lone_town/apartment/groundfloor" => [50000, "lone town", "looney"],
16 "/brest/apartments/brest_town_house" => [30000, "brest", "brecht"],
17 );
18
19 # we have to special case some special cases :)
20 sub reject_entry {
21 my ($pl) = @_;
22
23 my $prev_pos = $pl->ob->{_prev_pos};
24 $pl->ob->goto ($prev_pos ? @$prev_pos : ("/world/world_105_115", 2, 34));
25
26 cf::override;
27 }
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 cf::cede_to_tick;
53
54 update_balance $pl;
55
56 return unless $pl->{rent}{balance} > 0;
57
58 my $deduct = cf::ceil $pl->{rent}{balance};
59
60 my $deduct_string = cf::cost_string_from_value $deduct;
61
62 if ($deduct <= $pl->ob->{bank_balance}) {
63 cf::db_put rent => balance => $deduct + cf::db_get rent => "balance";
64 $pl->ob->{bank_balance} -= $deduct;
65 $pl->{rent}{balance} -= $deduct;
66 $pl->ob->reply (undef, "Something whispers into your ear: "
67 . "Your highness, we deducted your apartment rent ($deduct_string) from your bank account.");
68 } else {
69 $pl->ob->reply (undef, "Something whispers into your ear: "
70 . "Your highness, we want to deduct the apartment rent ($deduct_string), but the bank informed us that they cannot perform the transaction. "
71 . "Please even out your balance so we can deduct the fees, otherwise we will be forced to shut down your access to the apartment.");
72 }
73 }
74
75 sub check_balance {
76 my ($pl) = @_;
77
78 pay_balance $pl if $pl->{rent}{balance} > 0;
79
80 $pl->{rent}{balance} <= 0
81 }
82
83 sub find_apartment {
84 my ($pl, $name) = @_;
85
86 my $apartment = (grep $apartment{$_}[2] eq $name, keys %apartment)[0]
87 or $pl->ob->reply (undef, "Sorry, but we do not offer model '$name' for rent.");
88
89 $apartment
90 }
91
92 cf::register_script_function "rent::overview" => sub {
93 my ($pl, $types) = @_;
94
95 while (my ($k, $v) = each %apartment) {
96 my $type = exists $pl->{rent}{apartment}{$k} ? 1 : 2;
97
98 $pl->ob->reply (undef, "model \"$v->[2]\", situated in $v->[1] ("
99 . (cf::cost_string_from_value $v->[0]) . "/hr)\n")
100 if $type & $types;
101 }
102 };
103
104 cf::register_script_function "rent::status" => sub {
105 my ($pl, $types) = @_;
106
107 if ($pl->{rent}{balance} <= 0) {
108 $pl->ob->reply (undef, "You have no debts to pay.");
109 } else {
110 $pl->ob->reply (undef, "You owe us " . (cf::cost_string_from_value $pl->{rent}{balance}) . ".");
111 }
112 };
113
114 cf::register_script_function "rent::rent" => sub {
115 my ($pl, $apartment) = @_;
116
117 $apartment = find_apartment $pl, $apartment
118 or return;
119
120 update_balance $pl;
121
122 $pl->{rent}{apartment}{$apartment} = undef;
123
124 $pl->ob->reply (undef, "Wonderful decision, your highness! "
125 . "We told the proprietor in $apartment{$apartment}[1] to expect you and let you in. "
126 . "We are sure you will be satisfied!");
127 };
128
129 cf::register_script_function "rent::stop" => sub {
130 my ($pl, $apartment) = @_;
131
132 $apartment = find_apartment $pl, $apartment
133 or return;
134
135 update_balance $pl;
136
137 delete $pl->{rent}{apartment}{$apartment};
138
139 $pl->ob->reply (undef, "I am sorry to hear that, we will immediately stop charging you for your apartment, of course.");
140 };
141
142 cf::player->attach (
143 prio => 100,
144 on_login => sub {
145 my ($pl) = @_;
146
147 $pl->{rent}{last_offline_check} ||= time;
148
149 if ($pl->{rent}{last_online_check}) {
150 $pl->{rent}{last_online_check} = time
151 - List::Util::min 3600,
152 $pl->ob->kv_get ("schmorplog_last_save") - $pl->{rent}{last_online_check};
153 } else {
154 $pl->{rent}{last_online_check} = time;
155 }
156
157 update_balance $pl;
158 },
159 );
160
161 cf::map::attachment rent =>
162 on_enter => sub {
163 my ($map, $pl, $x, $y) = @_;
164
165 my $pfx = sprintf "~%s/", $pl->ob->name;
166
167 # only do something if entering ones own apartment
168 if ($pfx eq substr $map->path, 0, length $pfx) {
169 for my $path (keys %{ $pl->{rent}{apartment} }) {
170 $path = sprintf "~%s%s", $pl->ob->name, $path;
171
172 if ($map->path eq $path) {
173 if (check_balance $pl) {
174 $pl->ob->reply (undef, "Welcome to your apartment, your highness!");
175 } else {
176 $pl->ob->reply (undef, "We are sorry, your highness, you have to pay your rent first.");
177 reject_entry $pl;
178 }
179
180 return;
181 }
182 }
183
184 $pl->ob->reply (undef, "Your highness, you have to rent this apartment in The Apartment Shop in Scorn or other apartment shops first!");
185 reject_entry $pl;
186 }
187 },
188 ;
189
190 our $RENT_TIMER = cf::periodic 3600, Coro::unblock_sub {
191 pay_balance $_ for cf::player::list;
192 };
193