ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/reseller.ext
(Generate patch)

Comparing deliantra/server/ext/reseller.ext (file contents):
Revision 1.5 by elmex, Wed Jan 24 19:39:14 2007 UTC vs.
Revision 1.11 by root, Thu Apr 10 15:35:16 2008 UTC

1#!perl # MANDATORY 1#!perl # mandatory
2
3my %unit = (
4 silver => 1,
5 gold => 10,
6 platina => 50,
7 royalty => 5000,
8);
9
10my %aliases = (
11 platinum => 'platina',
12 royalties => 'royalty',
13);
14 2
15sub ob2info { 3sub ob2info {
16 my ($item, $rval) = @_; 4 my ($item, $rval) = @_;
17 sprintf "[%s from %s (%d:%d%s) nrof: %d uuid: %s]", 5 sprintf "[%s from %s (%d:%d%s) nrof: %d uuid: %s]",
18 $item->name, $item->get_ob_key_value ('ext_reseller_seller'), 6 $item->name, $item->get_ob_key_value ('ext_reseller_seller'),
51 find_rec $ob, sub { $_[0]->get_ob_key_value ('ext_reseller_seller') ne '' }; 39 find_rec $ob, sub { $_[0]->get_ob_key_value ('ext_reseller_seller') ne '' };
52} 40}
53 41
54cf::register_script_function "reseller::list_sells" => sub { 42cf::register_script_function "reseller::list_sells" => sub {
55 my ($who, $msg, $npc) = @_; 43 my ($who, $msg, $npc) = @_;
56 my $sells = cf::from_json $npc->get_ob_key_value ('ext_reseller_sales'); 44 my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales');
45 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
57 my $hissells = $sells->{$who->name}; 46 my $hissells = $sells->{$who->name};
58 47
59 unless (keys %{$hissells || {}}) { 48 unless (keys %{$hissells || {}}) {
60 $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); 49 $who->reply ($npc, "I'm sorry, but you sold nothing.\n");
61 return 0; 50 return 0;
71 0 60 0
72}; 61};
73 62
74cf::register_script_function "reseller::pay_player" => sub { 63cf::register_script_function "reseller::pay_player" => sub {
75 my ($who, $msg, $npc) = @_; 64 my ($who, $msg, $npc) = @_;
76 my $sells = cf::from_json $npc->get_ob_key_value ('ext_reseller_sales'); 65 my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales');
66 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
77 my $hissells = $sells->{$who->name}; 67 my $hissells = $sells->{$who->name};
78 68
79 unless (keys %{$hissells || {}}) { 69 unless (keys %{$hissells || {}}) {
80 $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); 70 $who->reply ($npc, "I'm sorry, but you sold nothing.\n");
81 return 0; 71 return 0;
89 79
90 audit_log ($who, 'collects', "$sum silver"); 80 audit_log ($who, 'collects', "$sum silver");
91 81
92 $sells->{$who->name} = {}; 82 $sells->{$who->name} = {};
93 83
94 $npc->set_ob_key_value (ext_reseller_sales => cf::to_json $sells); 84 $npc->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells)
85 if $sells;
95 86
96 0 87 0
97}; 88};
98 89
99cf::object::attachment "reseller_shopmat", 90cf::object::attachment "reseller_shopmat",
106 unless (@obs) { 97 unless (@obs) {
107 warn "Couldn't find shop keeper in " . $who->map->path . "\n"; 98 warn "Couldn't find shop keeper in " . $who->map->path . "\n";
108 return cf::override; 99 return cf::override;
109 } 100 }
110 101
111 my $sells = cf::from_json $obs[0]->get_ob_key_value ('ext_reseller_sales'); 102 my $ext_re_sales = $obs[0]->get_ob_key_value ('ext_reseller_sales');
103 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
112 104
113 my $unpaid_items = {}; 105 my $unpaid_items = {};
114 106
115 for my $item (find_unpaid ($who)) { 107 for my $item (find_unpaid ($who)) {
116 if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { 108 if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) {
117 audit_log ($who, 'removes', ob2info ($item)); 109 audit_log ($who, 'removes', ob2info ($item));
118 $item->flag (cf::FLAG_UNPAID, 0); 110 $item->flag (cf::FLAG_UNPAID, 0);
119 $item->remove; 111 $item->remove;
120 $item->insert_ob_in_ob ($who); 112 give_back ($who, $item);
121 next; 113 next;
122 } 114 }
123 115
124 my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP); 116 my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP);
125 117
151 } 143 }
152 144
153 audit_log ($who, 'removed', (join ",", @seller_noted)) 145 audit_log ($who, 'removed', (join ",", @seller_noted))
154 if @seller_noted; 146 if @seller_noted;
155 147
156 $obs[0]->set_ob_key_value (ext_reseller_sales => cf::to_json $sells); 148 $obs[0]->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells)
149 if $sells;
157 150
158 cf::override; 151 cf::override;
159 }, 152 },
160; 153;
154
155sub give_back {
156 my ($who, $what) = @_;
157 $who->insert ($what);
158 $who->esrv_send_item ($what);
159}
160
161sub give_back_with_message {
162 my ($who, $what, $msg) = @_;
163 $who->message ($msg, cf::NDI_BROWN);
164 give_back ($who, $what);
165}
161 166
162cf::object::attachment "reseller_floor", 167cf::object::attachment "reseller_floor",
163 on_drop_on => sub { 168 on_drop_on => sub {
164 my ($on, $what, $who) = @_; 169 my ($on, $what, $who) = @_;
165 my $name = $what->custom_name; 170 my $name = $what->custom_name;
166 171
167 return if $what->flag (cf::FLAG_UNPAID); 172 return if $what->flag (cf::FLAG_UNPAID);
168 173
169 if ($what->type == cf::MONEY) { 174 if ($what->type == cf::MONEY) {
175 give_back_with_message ($who, $what,
170 $who->message ("The shopkeeper says: Sorry, you can't sell money here.", cf::NDI_BROWN); 176 "The shopkeeper says: Sorry, you can't sell money here.");
171 $what->insert_ob_in_ob ($who);
172 return cf::override; 177 return cf::override;
173 } 178 }
174 179
175 if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) { 180 if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) {
181 give_back_with_message ($who, $what,
176 $who->message ("The shopkeeper says: Sorry, you can't sell unidentified stuff here.", cf::NDI_BROWN); 182 "The shopkeeper says: Sorry, you can't sell unidentified stuff here.");
177 $what->insert_ob_in_ob ($who);
178 return cf::override; 183 return cf::override;
179 } 184 }
180 185
181 my $orig_value = $what->value; 186 my $orig_value = $what->value;
182 my $value = 0; 187 my $value = 0;
183 188
184 if ($name =~ m/\S/) { 189 if ($name =~ m/\S/) {
185 unless ($name =~ m/\d+\s*\S+/) { 190 unless ($name =~ m/\d+\s*\S+/) {
186 $who->message ("The shopkeeper says: Sorry, I don't recognize '$name' as currency. Please name your item like '10 royalty' or '10 platinum 2 silver'", cf::NDI_BROWN); 191 give_back_with_message ($who, $what,
187 $what->insert_ob_in_ob ($who); 192 "The shopkeeper says: Sorry, I don't recognize '$name' as currency. "
193 . "Please name your item like '17 platinum' or '10 gold 8 silver'");
188 return cf::override; 194 return cf::override;
189 } 195 }
190 196
191 while ($name =~ s/^\s*(\d+)\s*(\S+)//) { 197 while ($name =~ s/^\s*(\d+)\s*(\S+)//) {
192 if ($aliases{lc $2} or $unit{lc $2}) { 198 my ($v, $c) = ($1, $2);
193 $value += $1 * ($unit{lc $2} ? $unit{lc $2} : $unit{$aliases{lc $2}}); 199 if (my $coin = cf::coin_from_name $c) {
200 $value += $v * $coin->value;
194 } else { 201 } else {
195 $what->insert_ob_in_ob ($who); 202 give_back_with_message ($who, $what,
196 $who->message ("The shopkeeper says: I don't know the currency '$2'", cf::NDI_BROWN); 203 "The shopkeeper says: I don't know the currency '$c', you can use one of these currencies: "
204 . (join ", ", cf::coin_names)
205 );
197 return cf::override; 206 return cf::override;
198 } 207 }
199 } 208 }
200 } else { 209 } else {
210 # commented out the following line because too many just use the
211 # reseller shop as dumpyard:
201 $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1); 212 # $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1);
213 give_back_with_message ($who, $what,
214 "Sorry, you can't just sell stuff without assigning a price to it! "
215 . "Please name your item like '17 platinum' or '10 gold 8 silver' "
216 . "and drop it again. (To rename the item use the 'rename' "
217 . "context menu item in the inventory)."
218 );
219 return cf::override;
202 } 220 }
203 221
204 if ($value < 0) { 222 if ($value < 0) {
205 $what->insert_ob_in_ob ($who); 223 give_back_with_message ($who, $what,
206 $who->message ("The shopkeeper says: You can't sell something for a negative value: $value", cf::NDI_BROWN); 224 "The shopkeeper says: You can't sell something for a negative value: $value");
207 return cf::override; 225 return cf::override;
208 } 226 }
209 227
210 my $fee = $value / 100; # 1% selling fee 228 my $fee = $value / 100; # 1% selling fee
211 229
212 unless ($who->pay_amount ($fee)) { 230 unless ($who->pay_amount ($fee)) {
213 $who->message ( 231 give_back_with_message ($who, $what,
214 "The shopkeeper says: You need " . cf::cost_string_from_value ($fee) . " to pay the 1% fee for this item", 232 "The shopkeeper says: You need " . cf::cost_string_from_value ($fee)
215 cf::NDI_BROWN 233 . " to pay the 1% fee for this item");
216 );
217 $what->insert_ob_in_ob ($who);
218 return cf::override; 234 return cf::override;
219 } else { 235 } else {
220 $who->message ( 236 $who->message (
221 "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) . " for the item", 237 "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee)
238 . " for the item",
222 cf::NDI_BROWN 239 cf::NDI_BROWN
223 ); 240 );
224 } 241 }
225 242
226 $what->value ($value); 243 $what->value ($value);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines