1 | #!perl # mandatory |
1 | #!perl # mandatory |
2 | |
2 | |
3 | sub ob2info { |
3 | sub ob2info { |
4 | my ($item, $rval) = @_; |
4 | my ($item, $rval) = @_; |
5 | sprintf "[%s from %s (%d:%d%s) nrof: %d uuid: %s]", |
5 | sprintf "[%s from %s (%d:%d%s) nrof: %d uuid: %s]", |
6 | $item->name, $item->get_ob_key_value ('ext_reseller_seller'), |
6 | $item->name, $item->kv_get ('ext_reseller_seller'), |
7 | $item->get_ob_key_value ('ext_reseller_orig_value'), $item->value, |
7 | $item->kv_get ('ext_reseller_orig_value'), $item->value, |
8 | (defined $rval ? ":$rval" : ""), $item->nrof, $item->uuid; |
8 | (defined $rval ? ":$rval" : ""), $item->nrof, $item->uuid; |
9 | } |
9 | } |
10 | |
10 | |
11 | sub audit_log { |
11 | sub audit_log { |
12 | my ($who, $action, $info) = @_; |
12 | my ($who, $action, $info) = @_; |
… | |
… | |
34 | find_rec $ob, sub { $_[0]->flag (cf::FLAG_UNPAID) }; |
34 | find_rec $ob, sub { $_[0]->flag (cf::FLAG_UNPAID) }; |
35 | } |
35 | } |
36 | |
36 | |
37 | sub find_traded { |
37 | sub find_traded { |
38 | my ($ob) = @_; |
38 | my ($ob) = @_; |
39 | 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 '' }; |
40 | } |
40 | } |
41 | |
41 | |
42 | cf::register_script_function "reseller::list_sells" => sub { |
42 | cf::register_script_function "reseller::list_sells" => sub { |
43 | my ($who, $msg, $npc) = @_; |
43 | my ($who, $msg, $npc) = @_; |
44 | my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales'); |
44 | my $ext_re_sales = $npc->kv_get ('ext_reseller_sales'); |
45 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
45 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
46 | my $hissells = $sells->{$who->name}; |
46 | my $hissells = $sells->{$who->name}; |
47 | |
47 | |
48 | unless (keys %{$hissells || {}}) { |
48 | unless (keys %{$hissells || {}}) { |
49 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
49 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
… | |
… | |
60 | 0 |
60 | 0 |
61 | }; |
61 | }; |
62 | |
62 | |
63 | cf::register_script_function "reseller::pay_player" => sub { |
63 | cf::register_script_function "reseller::pay_player" => sub { |
64 | my ($who, $msg, $npc) = @_; |
64 | my ($who, $msg, $npc) = @_; |
65 | my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales'); |
65 | my $ext_re_sales = $npc->kv_get ('ext_reseller_sales'); |
66 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
66 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
67 | my $hissells = $sells->{$who->name}; |
67 | my $hissells = $sells->{$who->name}; |
68 | |
68 | |
69 | unless (keys %{$hissells || {}}) { |
69 | unless (keys %{$hissells || {}}) { |
70 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
70 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
… | |
… | |
79 | |
79 | |
80 | audit_log ($who, 'collects', "$sum silver"); |
80 | audit_log ($who, 'collects', "$sum silver"); |
81 | |
81 | |
82 | $sells->{$who->name} = {}; |
82 | $sells->{$who->name} = {}; |
83 | |
83 | |
84 | $npc->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells) |
84 | $npc->kv_set (ext_reseller_sales => cf::encode_json $sells) |
85 | if $sells; |
85 | if $sells; |
86 | |
86 | |
87 | 0 |
87 | 0 |
88 | }; |
88 | }; |
89 | |
89 | |
… | |
… | |
97 | unless (@obs) { |
97 | unless (@obs) { |
98 | warn "Couldn't find shop keeper in " . $who->map->path . "\n"; |
98 | warn "Couldn't find shop keeper in " . $who->map->path . "\n"; |
99 | return cf::override; |
99 | return cf::override; |
100 | } |
100 | } |
101 | |
101 | |
102 | 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'); |
103 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
103 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
104 | |
104 | |
105 | my $unpaid_items = {}; |
105 | my $unpaid_items = {}; |
106 | |
106 | |
107 | for my $item (find_unpaid ($who)) { |
107 | for my $item (find_unpaid ($who)) { |
108 | if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { |
108 | if ($item->kv_get ('ext_reseller_seller') eq $who->name) { |
109 | audit_log ($who, 'removes', ob2info ($item)); |
109 | audit_log ($who, 'removes', ob2info ($item)); |
110 | $item->flag (cf::FLAG_UNPAID, 0); |
110 | $item->flag (cf::FLAG_UNPAID, 0); |
111 | $item->remove; |
111 | $item->remove; |
112 | give_back ($who, $item); |
112 | give_back ($who, $item); |
113 | next; |
113 | next; |
… | |
… | |
115 | |
115 | |
116 | 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); |
117 | |
117 | |
118 | warn "Object " . $item->name . " bought by " . $who->name . " on map " |
118 | warn "Object " . $item->name . " bought by " . $who->name . " on map " |
119 | . $who->map->path . " for $value silver has no seller set\n" |
119 | . $who->map->path . " for $value silver has no seller set\n" |
120 | if $item->get_ob_key_value ('ext_reseller_seller') eq ''; |
120 | if $item->kv_get ('ext_reseller_seller') eq ''; |
121 | |
121 | |
122 | $unpaid_items->{$item} = [$value, $item]; |
122 | $unpaid_items->{$item} = [$value, $item]; |
123 | } |
123 | } |
124 | |
124 | |
125 | 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)) |
… | |
… | |
131 | |
131 | |
132 | for my $item (find_traded ($who)) { |
132 | for my $item (find_traded ($who)) { |
133 | next if $item->flag (cf::FLAG_UNPAID); |
133 | next if $item->flag (cf::FLAG_UNPAID); |
134 | if (my $value = $unpaid_items->{$item}[0]) { |
134 | if (my $value = $unpaid_items->{$item}[0]) { |
135 | push @seller_noted, ob2info ($item, $value)."P"; |
135 | push @seller_noted, ob2info ($item, $value)."P"; |
136 | $sells->{$item->get_ob_key_value ('ext_reseller_seller')}->{$item->name} += $value; |
136 | $sells->{$item->kv_get ('ext_reseller_seller')}->{$item->name} += $value; |
137 | } else { |
137 | } else { |
138 | push @seller_noted, ob2info ($item)."T"; |
138 | push @seller_noted, ob2info ($item)."T"; |
139 | } |
139 | } |
140 | |
140 | |
141 | $item->value ($item->get_ob_key_value ('ext_reseller_orig_value')); |
141 | $item->value ($item->kv_get ('ext_reseller_orig_value')); |
142 | $item->set_ob_key_value (ext_reseller_seller => ''); |
142 | $item->kv_del ("ext_reseller_seller"); |
143 | } |
143 | } |
144 | |
144 | |
145 | audit_log ($who, 'removed', (join ",", @seller_noted)) |
145 | audit_log ($who, 'removed', (join ",", @seller_noted)) |
146 | if @seller_noted; |
146 | if @seller_noted; |
147 | |
147 | |
148 | $obs[0]->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells) |
148 | $obs[0]->kv_set (ext_reseller_sales => cf::encode_json $sells) |
149 | if $sells; |
149 | if $sells; |
150 | |
150 | |
151 | cf::override; |
151 | cf::override; |
152 | }, |
152 | }, |
153 | ; |
153 | ; |
… | |
… | |
259 | . ($what->nrof || 1) . " " . $what->name . " to be sold for at least " |
259 | . ($what->nrof || 1) . " " . $what->name . " to be sold for at least " |
260 | . cf::cost_string_from_value ($value) |
260 | . cf::cost_string_from_value ($value) |
261 | . ($what->nrof > 1 ? " each" : ""), cf::NDI_BROWN |
261 | . ($what->nrof > 1 ? " each" : ""), cf::NDI_BROWN |
262 | ); |
262 | ); |
263 | |
263 | |
264 | $what->set_ob_key_value (ext_reseller_seller => $who->name); |
264 | $what->kv_set (ext_reseller_seller => $who->name); |
265 | $what->set_ob_key_value (ext_reseller_orig_value => $orig_value); |
265 | $what->kv_set (ext_reseller_orig_value => $orig_value); |
266 | # warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n"; |
266 | # warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n"; |
267 | $what->custom_name ($what->name . " (by " . $who->name . ")"); |
267 | $what->custom_name ($what->name . " (by " . $who->name . ")"); |
268 | $what->flag (cf::FLAG_UNPAID, 1); |
268 | $what->flag (cf::FLAG_UNPAID, 1); |
269 | $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); |
270 | |
270 | |