ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/rent.ext
Revision: 1.14
Committed: Thu Jul 17 09:13:09 2008 UTC (15 years, 10 months ago) by elmex
Branch: MAIN
CVS Tags: rel-2_82, rel-2_81, rel-2_80, rel-2_6, rel-2_7, rel-2_72, rel-2_73, rel-2_71, rel-2_76, rel-2_77, rel-2_74, rel-2_75, rel-2_79, rel-2_78, rel-2_61
Changes since 1.13: +2 -1 lines
Log Message:
added new apartment

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