1 | <?php |
---|
2 | /** |
---|
3 | * Verify that the EVE central tracking table exists. |
---|
4 | */ |
---|
5 | function verify_sync_table() { |
---|
6 | $query = new DBQuery(); |
---|
7 | |
---|
8 | $query->execute("SHOW TABLES LIKE 'kb3_eve_central'"); |
---|
9 | if ($query->recordCount() == 0) { |
---|
10 | $query->execute("CREATE TABLE kb3_eve_central (item_id int unsigned not null, item_price varchar(20), last_updated timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, primary key (item_id)) engine=myisam comment='EVE central sync tracker'"); |
---|
11 | } |
---|
12 | } |
---|
13 | |
---|
14 | /** |
---|
15 | * Retrieve the item value from EVE Central |
---|
16 | */ |
---|
17 | function ec_get_value($item_id) { |
---|
18 | $query = new DBQuery(); |
---|
19 | |
---|
20 | $query->execute("SELECT item_price, unix_timestamp(last_updated) last_upd FROM kb3_eve_central WHERE item_id=$item_id"); |
---|
21 | // If there's 1 record, then we have an archived value. |
---|
22 | if ($query->recordcount() == 1) { |
---|
23 | // Is it recent enough? |
---|
24 | $data = $query->getrow(); |
---|
25 | if (48*3600 > (date('U') - $data['last_upd'])) { |
---|
26 | file_put_contents(KB_CACHEDIR . '/ec', "Handling from cache.\n", FILE_APPEND); |
---|
27 | return $data['item_price']; |
---|
28 | } else { |
---|
29 | // Not recent enough, interrogate EVE Central |
---|
30 | return ask_eve_central($item_id); |
---|
31 | } |
---|
32 | } else { |
---|
33 | return ask_eve_central($item_id); |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * Query EVE Central's XML feed. |
---|
39 | */ |
---|
40 | function ask_eve_central($item_id) { |
---|
41 | file_put_contents(KB_CACHEDIR.'/ec', "Handling from live.\n", FILE_APPEND); |
---|
42 | $query = new DBQuery(); |
---|
43 | $file = fopen("http://eve-central.com/home/marketstat_xml.html?typeid=$item_id", 'r'); |
---|
44 | if (! $file) { |
---|
45 | return -99; |
---|
46 | } |
---|
47 | $content = stream_get_contents($file); |
---|
48 | fclose($file); |
---|
49 | file_put_contents(KB_CACHEDIR.'/ec', "$content\n", FILE_APPEND); |
---|
50 | if (!preg_match('/market_stat/', $content)) { |
---|
51 | return -99; |
---|
52 | } |
---|
53 | if (preg_match('/avg_price>None/', $content)) { |
---|
54 | return -99; |
---|
55 | } |
---|
56 | preg_match('/<avg_sell_price>(\d+\.\d{2})\d+</', $content, $match); |
---|
57 | $sell = $match[1]; |
---|
58 | preg_match('/<avg_buy_price>(\d+\.\d{2})\d+</', $content, $match); |
---|
59 | $buy = $match[1]; |
---|
60 | $weighted_average = round(((1.6 * $buy + 0.8 * $sell) / 2),2); |
---|
61 | if (0 == $weighted_average) { |
---|
62 | return -99; |
---|
63 | } |
---|
64 | $query->execute("REPLACE INTO kb3_eve_central (item_id, item_price) VALUES ($item_id, '$weighted_average')"); |
---|
65 | return $weighted_average; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Wrapper to do all to work. Updates the items table based on the cached or live data. |
---|
70 | */ |
---|
71 | function ec_update_value($item_id) { |
---|
72 | $query = new DBQuery(); |
---|
73 | |
---|
74 | // Don't try if we can't open URLs with fopen. |
---|
75 | if (1 != ini_get('allow_url_fopen')) { |
---|
76 | return; |
---|
77 | } |
---|
78 | // Don't try if the item id isn't an integer. |
---|
79 | if (!is_numeric($item_id)) { |
---|
80 | return; |
---|
81 | } |
---|
82 | // Verify we have a sync table to use. |
---|
83 | verify_sync_table(); |
---|
84 | // The destroyed items etc feed in the -internal- killboard item ID. |
---|
85 | // EVE Central needs the external ID if we have it. |
---|
86 | $query->execute("SELECT itm_externalid FROM kb3_items WHERE itm_id=$item_id"); |
---|
87 | $data = $query->getRow(); |
---|
88 | $e_item_id = $data['itm_externalid']; |
---|
89 | // Don't try if the item id isn't an integer or it's 0. |
---|
90 | if (!is_numeric($e_item_id) OR 0 == $e_item_id) { |
---|
91 | return; |
---|
92 | } |
---|
93 | file_put_contents(KB_CACHEDIR.'/ec', "Request for $item_id -> $e_item_id\n", FILE_APPEND); |
---|
94 | |
---|
95 | $value = ec_get_value($e_item_id); |
---|
96 | if (-99 != $value) { |
---|
97 | $query->execute("update kb3_items set itm_value='$value' WHERE itm_id=$item_id"); |
---|
98 | return true; |
---|
99 | } else { |
---|
100 | file_put_contents(KB_CACHEDIR.'/ec', "Failed to find it.\n", FILE_APPEND); |
---|
101 | } |
---|
102 | return false; |
---|
103 | } |
---|
104 | ?> |
---|