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.6 by elmex, Wed Jun 6 17:41:17 2007 UTC vs.
Revision 1.12 by root, Sat May 3 11:14:50 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->kv_get ('ext_reseller_seller'),
19 $item->get_ob_key_value ('ext_reseller_orig_value'), $item->value, 7 $item->kv_get ('ext_reseller_orig_value'), $item->value,
20 (defined $rval ? ":$rval" : ""), $item->nrof, $item->uuid; 8 (defined $rval ? ":$rval" : ""), $item->nrof, $item->uuid;
21} 9}
22 10
23sub audit_log { 11sub audit_log {
24 my ($who, $action, $info) = @_; 12 my ($who, $action, $info) = @_;
46 find_rec $ob, sub { $_[0]->flag (cf::FLAG_UNPAID) }; 34 find_rec $ob, sub { $_[0]->flag (cf::FLAG_UNPAID) };
47} 35}
48 36
49sub find_traded { 37sub find_traded {
50 my ($ob) = @_; 38 my ($ob) = @_;
51 find_rec $ob, sub { $_[0]->get_ob_key_value ('ext_reseller_seller') ne '' }; 39 find_rec $ob, sub { $_[0]->kv_get ('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 $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales'); 44 my $ext_re_sales = $npc->kv_get ('ext_reseller_sales');
57 my $sells = $ext_re_sales && cf::from_json $ext_re_sales; 45 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
58 my $hissells = $sells->{$who->name}; 46 my $hissells = $sells->{$who->name};
59 47
60 unless (keys %{$hissells || {}}) { 48 unless (keys %{$hissells || {}}) {
61 $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); 49 $who->reply ($npc, "I'm sorry, but you sold nothing.\n");
62 return 0; 50 return 0;
72 0 60 0
73}; 61};
74 62
75cf::register_script_function "reseller::pay_player" => sub { 63cf::register_script_function "reseller::pay_player" => sub {
76 my ($who, $msg, $npc) = @_; 64 my ($who, $msg, $npc) = @_;
77 my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales'); 65 my $ext_re_sales = $npc->kv_get ('ext_reseller_sales');
78 my $sells = $ext_re_sales && cf::from_json $ext_re_sales; 66 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
79 my $hissells = $sells->{$who->name}; 67 my $hissells = $sells->{$who->name};
80 68
81 unless (keys %{$hissells || {}}) { 69 unless (keys %{$hissells || {}}) {
82 $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); 70 $who->reply ($npc, "I'm sorry, but you sold nothing.\n");
83 return 0; 71 return 0;
91 79
92 audit_log ($who, 'collects', "$sum silver"); 80 audit_log ($who, 'collects', "$sum silver");
93 81
94 $sells->{$who->name} = {}; 82 $sells->{$who->name} = {};
95 83
96 $npc->set_ob_key_value (ext_reseller_sales => cf::to_json $sells) 84 $npc->kv_set (ext_reseller_sales => cf::encode_json $sells)
97 if $sells; 85 if $sells;
98 86
99 0 87 0
100}; 88};
101 89
109 unless (@obs) { 97 unless (@obs) {
110 warn "Couldn't find shop keeper in " . $who->map->path . "\n"; 98 warn "Couldn't find shop keeper in " . $who->map->path . "\n";
111 return cf::override; 99 return cf::override;
112 } 100 }
113 101
114 my $ext_re_sales = $obs[0]->get_ob_key_value ('ext_reseller_sales'); 102 my $ext_re_sales = $obs[0]->kv_get ('ext_reseller_sales');
115 my $sells = $ext_re_sales && cf::from_json $ext_re_sales; 103 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
116 104
117 my $unpaid_items = {}; 105 my $unpaid_items = {};
118 106
119 for my $item (find_unpaid ($who)) { 107 for my $item (find_unpaid ($who)) {
120 if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { 108 if ($item->kv_get ('ext_reseller_seller') eq $who->name) {
121 audit_log ($who, 'removes', ob2info ($item)); 109 audit_log ($who, 'removes', ob2info ($item));
122 $item->flag (cf::FLAG_UNPAID, 0); 110 $item->flag (cf::FLAG_UNPAID, 0);
123 $item->remove; 111 $item->remove;
124 $item->insert_ob_in_ob ($who); 112 give_back ($who, $item);
125 next; 113 next;
126 } 114 }
127 115
128 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);
129 117
130 warn "Object " . $item->name . " bought by " . $who->name . " on map " 118 warn "Object " . $item->name . " bought by " . $who->name . " on map "
131 . $who->map->path . " for $value silver has no seller set\n" 119 . $who->map->path . " for $value silver has no seller set\n"
132 if $item->get_ob_key_value ('ext_reseller_seller') eq ''; 120 if $item->kv_get ('ext_reseller_seller') eq '';
133 121
134 $unpaid_items->{$item} = [$value, $item]; 122 $unpaid_items->{$item} = [$value, $item];
135 } 123 }
136 124
137 audit_log ($who, 'wants', (join ",", map { ob2info ($_->[1], $_->[0]) } values %$unpaid_items)) 125 audit_log ($who, 'wants', (join ",", map { ob2info ($_->[1], $_->[0]) } values %$unpaid_items))
143 131
144 for my $item (find_traded ($who)) { 132 for my $item (find_traded ($who)) {
145 next if $item->flag (cf::FLAG_UNPAID); 133 next if $item->flag (cf::FLAG_UNPAID);
146 if (my $value = $unpaid_items->{$item}[0]) { 134 if (my $value = $unpaid_items->{$item}[0]) {
147 push @seller_noted, ob2info ($item, $value)."P"; 135 push @seller_noted, ob2info ($item, $value)."P";
148 $sells->{$item->get_ob_key_value ('ext_reseller_seller')}->{$item->name} += $value; 136 $sells->{$item->kv_get ('ext_reseller_seller')}->{$item->name} += $value;
149 } else { 137 } else {
150 push @seller_noted, ob2info ($item)."T"; 138 push @seller_noted, ob2info ($item)."T";
151 } 139 }
152 140
153 $item->value ($item->get_ob_key_value ('ext_reseller_orig_value')); 141 $item->value ($item->kv_get ('ext_reseller_orig_value'));
154 $item->set_ob_key_value (ext_reseller_seller => ''); 142 $item->kv_del ("ext_reseller_seller");
155 } 143 }
156 144
157 audit_log ($who, 'removed', (join ",", @seller_noted)) 145 audit_log ($who, 'removed', (join ",", @seller_noted))
158 if @seller_noted; 146 if @seller_noted;
159 147
160 $obs[0]->set_ob_key_value (ext_reseller_sales => cf::to_json $sells) 148 $obs[0]->kv_set (ext_reseller_sales => cf::encode_json $sells)
161 if $sells; 149 if $sells;
162 150
163 cf::override; 151 cf::override;
164 }, 152 },
165; 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}
166 166
167cf::object::attachment "reseller_floor", 167cf::object::attachment "reseller_floor",
168 on_drop_on => sub { 168 on_drop_on => sub {
169 my ($on, $what, $who) = @_; 169 my ($on, $what, $who) = @_;
170 my $name = $what->custom_name; 170 my $name = $what->custom_name;
171 171
172 return if $what->flag (cf::FLAG_UNPAID); 172 return if $what->flag (cf::FLAG_UNPAID);
173 173
174 if ($what->type == cf::MONEY) { 174 if ($what->type == cf::MONEY) {
175 give_back_with_message ($who, $what,
175 $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.");
176 $what->insert_ob_in_ob ($who);
177 return cf::override; 177 return cf::override;
178 } 178 }
179 179
180 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,
181 $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.");
182 $what->insert_ob_in_ob ($who);
183 return cf::override; 183 return cf::override;
184 } 184 }
185 185
186 my $orig_value = $what->value; 186 my $orig_value = $what->value;
187 my $value = 0; 187 my $value = 0;
188 188
189 if ($name =~ m/\S/) { 189 if ($name =~ m/\S/) {
190 unless ($name =~ m/\d+\s*\S+/) { 190 unless ($name =~ m/\d+\s*\S+/) {
191 $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,
192 $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'");
193 return cf::override; 194 return cf::override;
194 } 195 }
195 196
196 while ($name =~ s/^\s*(\d+)\s*(\S+)//) { 197 while ($name =~ s/^\s*(\d+)\s*(\S+)//) {
197 if ($aliases{lc $2} or $unit{lc $2}) { 198 my ($v, $c) = ($1, $2);
198 $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;
199 } else { 201 } else {
200 $what->insert_ob_in_ob ($who); 202 give_back_with_message ($who, $what,
201 $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 );
202 return cf::override; 206 return cf::override;
203 } 207 }
204 } 208 }
205 } else { 209 } else {
210 # commented out the following line because too many just use the
211 # reseller shop as dumpyard:
206 $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;
207 } 220 }
208 221
209 if ($value < 0) { 222 if ($value < 0) {
210 $what->insert_ob_in_ob ($who); 223 give_back_with_message ($who, $what,
211 $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");
212 return cf::override; 225 return cf::override;
213 } 226 }
214 227
215 my $fee = $value / 100; # 1% selling fee 228 my $fee = $value / 100; # 1% selling fee
216 229
217 unless ($who->pay_amount ($fee)) { 230 unless ($who->pay_amount ($fee)) {
218 $who->message ( 231 give_back_with_message ($who, $what,
219 "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)
220 cf::NDI_BROWN 233 . " to pay the 1% fee for this item");
221 );
222 $what->insert_ob_in_ob ($who);
223 return cf::override; 234 return cf::override;
224 } else { 235 } else {
225 $who->message ( 236 $who->message (
226 "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",
227 cf::NDI_BROWN 239 cf::NDI_BROWN
228 ); 240 );
229 } 241 }
230 242
231 $what->value ($value); 243 $what->value ($value);
247 . ($what->nrof || 1) . " " . $what->name . " to be sold for at least " 259 . ($what->nrof || 1) . " " . $what->name . " to be sold for at least "
248 . cf::cost_string_from_value ($value) 260 . cf::cost_string_from_value ($value)
249 . ($what->nrof > 1 ? " each" : ""), cf::NDI_BROWN 261 . ($what->nrof > 1 ? " each" : ""), cf::NDI_BROWN
250 ); 262 );
251 263
252 $what->set_ob_key_value (ext_reseller_seller => $who->name); 264 $what->kv_set (ext_reseller_seller => $who->name);
253 $what->set_ob_key_value (ext_reseller_orig_value => $orig_value); 265 $what->kv_set (ext_reseller_orig_value => $orig_value);
254# warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n"; 266# warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n";
255 $what->custom_name ($what->name . " (by " . $who->name . ")"); 267 $what->custom_name ($what->name . " (by " . $who->name . ")");
256 $what->flag (cf::FLAG_UNPAID, 1); 268 $what->flag (cf::FLAG_UNPAID, 1);
257 $what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y); 269 $what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y);
258 270

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines