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 |
my $prev_pos = $pl->ob->{_prev_pos}; |
22 |
$pl->ob->goto ($prev_pos ? @$prev_pos : ("/world/world_105_115", 2, 34)); |
23 |
|
24 |
cf::override; |
25 |
} |
26 |
|
27 |
sub update_balance { |
28 |
my ($pl) = @_; |
29 |
|
30 |
my $NOW = time; |
31 |
|
32 |
# 1 silver per day per level per apartment of kingdom tax |
33 |
my $offline = (List::Util::min 30, ($NOW - $pl->{rent}{last_offline_check}) / 86400) |
34 |
* (cf::exp_to_level $pl->ob->stats->exp) |
35 |
* scalar keys %{ $pl->{rent}{apartment} }; |
36 |
|
37 |
# once per hour per map rented |
38 |
my $online = ($NOW - $pl->{rent}{last_online_check}) / 3600 |
39 |
* List::Util::sum map $apartment{$_}[0], keys %{ $pl->{rent}{apartment} }; |
40 |
|
41 |
$pl->{rent}{last_offline_check} = $NOW; |
42 |
$pl->{rent}{last_online_check} = $NOW; |
43 |
|
44 |
$pl->{rent}{balance} += $offline + $online; |
45 |
} |
46 |
|
47 |
sub pay_balance { |
48 |
my ($pl) = @_; |
49 |
|
50 |
cf::cede_to_tick; |
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: " |
65 |
. "Your highness, we deducted your apartment rent ($deduct_string) from your bank account."); |
66 |
} else { |
67 |
$pl->ob->reply (undef, "Something whispers into your ear: " |
68 |
. "Your highness, 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)\n") |
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, your highness! " |
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 |
cf::player->attach ( |
141 |
prio => 100, |
142 |
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->kv_get ("schmorplog_last_save") - $pl->{rent}{last_online_check}; |
151 |
} else { |
152 |
$pl->{rent}{last_online_check} = time; |
153 |
} |
154 |
|
155 |
update_balance $pl; |
156 |
}, |
157 |
); |
158 |
|
159 |
cf::map::attachment rent => |
160 |
on_enter => sub { |
161 |
my ($map, $pl, $x, $y) = @_; |
162 |
|
163 |
my $pfx = sprintf "~%s/", $pl->ob->name; |
164 |
|
165 |
# only do something if entering ones own apartment |
166 |
if ($pfx eq substr $map->path, 0, length $pfx) { |
167 |
for my $path (keys %{ $pl->{rent}{apartment} }) { |
168 |
$path = sprintf "~%s%s", $pl->ob->name, $path; |
169 |
|
170 |
if ($map->path eq $path) { |
171 |
if (check_balance $pl) { |
172 |
$pl->ob->reply (undef, "Welcome to your apartment, your highness!"); |
173 |
} else { |
174 |
$pl->ob->reply (undef, "We are sorry, your highness, you have to pay your rent first."); |
175 |
reject_entry $pl; |
176 |
} |
177 |
|
178 |
return; |
179 |
} |
180 |
} |
181 |
|
182 |
$pl->ob->reply (undef, "Your highness, you have to rent this apartment in The Apartment Shop in Scorn first!"); |
183 |
reject_entry $pl; |
184 |
} |
185 |
}, |
186 |
; |
187 |
|
188 |
our $RENT_TIMER = cf::periodic 3600, Coro::unblock_sub { |
189 |
pay_balance $_ for cf::player::list; |
190 |
}; |
191 |
|