1<?php require_once 'engine/init.php'; theme_open();
2
3$server = $config['shop']['imageServer'];
4$imageType = $config['shop']['imageType'];
5$items = getItemList();
6$compare = &$_GET['compare'];
7
8$marketLoadError = '';
9$marketMode = 'list';
10$offers = array();
11$historyOffers = array();
12$activeSellOffers = array();
13$buylist = false;
14$itemname = '';
15
16// If we failed to load items.xml, a string is returned (not an array) with
17// the attempted loaded file path.
18if (is_array($items) === false) {
19 $marketLoadError = (string)$items;
20} elseif (!$compare) {
21 // If you are not comparing any items, present the list.
22 $cache = new Cache('engine/cache/market');
23 $cache->setExpiration(60);
24 if ($cache->hasExpired()) {
25 $offers = array(
26 'wts' => db()->fetchAll("SELECT `mo`.`id`, `mo`.`itemtype` AS `item_id`, `mo`.`amount`, `mo`.`price`, `mo`.`created`, `mo`.`anonymous`, `p`.`name` AS `player_name` FROM `market_offers` AS `mo` INNER JOIN `players` AS `p` ON `mo`.`player_id`=`p`.`id` WHERE `mo`.`sale` = '1' ORDER BY `mo`.`created` DESC;"),
27 'wtb' => db()->fetchAll("SELECT `mo`.`id`, `mo`.`itemtype` AS `item_id`, `mo`.`amount`, `mo`.`price`, `mo`.`created`, `mo`.`anonymous`, `p`.`name` AS `player_name` FROM `market_offers` AS `mo` INNER JOIN `players` AS `p` ON `mo`.`player_id`=`p`.`id` WHERE `mo`.`sale` = '0' ORDER BY `mo`.`created` DESC;")
28 );
29 $cache->setContent($offers);
30 $cache->save();
31 } else {
32 $offers = $cache->load();
33 }
34} else {
35 // Else you want to compare price.
36 $marketMode = 'compare';
37 $compare = ((int)$compare > 0) ? (int)$compare : getValue($compare);
38
39 $conditionSql = '`itemtype` = ?';
40 $conditionParams = [$compare];
41
42 if (is_string($compare)) {
43 $query = array();
44 foreach ($items as $id => $name) {
45 if (strpos(strtolower($name), stripslashes(strtolower($compare))) !== false) {
46 $query[] = (int)$id;
47 }
48 }
49 if (!empty($query)) {
50 $conditionSql = '`itemtype` IN (' . implode(',', array_fill(0, count($query), '?')) . ')';
51 $conditionParams = $query;
52 } else {
53 $conditionSql = false;
54 }
55 }
56
57 // First list active bids.
58 if ($conditionSql !== false) {
59 $offers = db()->fetchAll("SELECT `mo`.`id`, `mo`.`sale`, `mo`.`itemtype` AS `item_id`, `mo`.`amount`, `mo`.`price`, `mo`.`created`, `mo`.`anonymous`, `p`.`name` AS `player_name` FROM `market_offers` AS `mo` INNER JOIN `players` AS `p` ON `mo`.`player_id`=`p`.`id` WHERE `mo`.{$conditionSql} ORDER BY `mo`.`price` ASC;", $conditionParams);
60 $historyOffers = db()->fetchAll("SELECT `id`, `itemtype` AS `item_id`, `amount`, `price`, `inserted`, `expires_at` FROM `market_history` WHERE {$conditionSql} AND `state`='255' ORDER BY `price` ASC;", $conditionParams);
61 }
62
63 $itemname = (isset($items[$compare])) ? $items[$compare] : $compare;
64
65 // Split active offers into sell offers and the want-to-buy list.
66 foreach (($offers ? $offers : array()) as $o) {
67 if ($o['sale'] == 0) {
68 if ($buylist === false) $buylist = array();
69 $buylist[] = $o;
70 } else {
71 $activeSellOffers[] = $o;
72 }
73 }
74}
75
76view('market');
77theme_close();
78