1
<?php
2
/**
3
 *   Funkcje pliku:
4
 *   Minerals market
5
 *
6
 *   @name                 : pmarket.php                            
7
 *   @copyright            : (C) 2004,2005,2006,2007 Vallheru Team based on Gamers-Fusion ver 2.5
8
 *   @author               : thindil <thindil@users.sourceforge.net>
9
 *   @author               : eyescream <tduda@users.sourceforge.net>
10
 *   @version              : 1.3
11
 *   @since                : 07.02.2007
12
 *
13
 */
14
15
//
16
//
17
//       This program is free software; you can redistribute it and/or modify
18
//   it under the terms of the GNU General Public License as published by
19
//   the Free Software Foundation; either version 2 of the License, or
20
//   (at your option) any later version.
21
//
22
//   This program is distributed in the hope that it will be useful,
23
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
24
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
//   GNU General Public License for more details.
26
//
27
//   You should have received a copy of the GNU General Public License
28
//   along with this program; if not, write to the Free Software
29
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
//
31
// $Id: pmarket.php 882 2007-02-07 19:16:13Z thindil $
32
33
$title = "Rynek minerałów";
34
require_once("includes/head.php");
35
36
/**
37
* Get the localization for game
38
*/
39
require_once("languages/".$player -> lang."/pmarket.php");
40
41
if ($player -> location != 'Altara' && $player -> location != 'Ardulith') 
42
{
43
    error (ERROR);
44
}
45
46
$objMinerals = $db -> Execute("SELECT `owner`, `copperore`, `zincore`, `tinore`, `ironore`, `coal`, `copper`, `bronze`, `brass`, `iron`, `steel`, `pine`, `hazel`, `yew`, `elm`, `crystal`, `adamantium`, `meteor` FROM `minerals` WHERE `owner`=".$player -> id);
47
$arrMinerals = array(MIN1, MIN15, MIN16, MIN17, MIN18, MIN2, MIN9, MIN10, MIN3, MIN11, MIN4, MIN5, MIN6, MIN7, MIN8, MIN12, MIN13, MIN14);
48
49
/**
50
* Assign variables to template
51
*/
52
$smarty -> assign(array("Message" => '', 
53
                        "Previous" => '', 
54
                        "Next" => ''));
55
56
/**
57
 * Main menu
58
 */
59
if (!isset($_GET['view']) && !isset($_GET['buy']) && !isset($_GET['wyc']))
60
{
61
    $smarty -> assign(array("Minfo" => M_INFO,
62
                            "Aview" => A_VIEW,
63
                            "Asearch" => A_SEARCH,
64
                            "Adelete" => A_DELETE,
65
                            "Aadd" => A_ADD,
66
                            "Alist" => A_LIST,
67
                            "Aback2" => A_BACK2));
68
}
69
70
/**
71
 * Search menu
72
 */
73
if (isset ($_GET['view']) && $_GET['view'] == 'szukaj') 
74
{
75
    $smarty -> assign(array("Sinfo" => S_INFO,
76
                            "Sinfo2" => S_INFO2,
77
                            "Asearch" => A_SEARCH,
78
                            "Mineral" => MINERAL));
79
}
80
81
/**
82
 * List of offerts
83
 */
84
if (isset ($_GET['view']) && $_GET['view'] == 'market') 
85
{
86
    if (empty($_POST['szukany'])) 
87
    {
88
        $msel = $db -> Execute("SELECT id FROM pmarket");
89
        $_POST['szukany'] = '';
90
    } 
91
        else 
92
    {
93
        $_POST['szukany'] = strip_tags($_POST['szukany']);
94
        $_POST['szukany'] = str_replace("*","%", $_POST['szukany']);
95
        $strSearch = $db -> qstr($_POST['szukany'], get_magic_quotes_gpc());
96
        $msel = $db -> Execute("SELECT id FROM pmarket WHERE nazwa LIKE ".$strSearch);
97
    }
98
    $oferty = $msel -> RecordCount();
99
    $msel -> Close();
100
    if ($oferty == 0) 
101
    {
102
        error (NO_OFERTS);
103
    }
104
    if (!isset($_GET['limit'])) 
105
    {
106
        $_GET['limit'] = 0;
107
    }
108
    if (!isset($_GET['lista'])) 
109
    {
110
        $_GET['lista'] = 'id';
111
    }
112
    $smarty -> assign(array("Mineral" => MINERAL,
113
                            "Viewinfo" => VIEW_INFO,
114
                            "Tamount" => T_AMOUNT,
115
                            "Tcost" => T_COST,
116
                            "Tseller" => T_SELLER,
117
                            "Toptions" => T_OPTIONS));
118
    if ($_GET['limit'] < $oferty) 
119
    {
120
        if (empty($_POST['szukany'])) 
121
        {
122
            $pm = $db -> SelectLimit("SELECT * FROM pmarket ORDER BY ".$_GET['lista']." DESC", 30, $_GET['limit']);
123
        } 
124
            else 
125
        {
126
            $pm = $db -> SelectLimit("SELECT * FROM pmarket WHERE nazwa LIKE ".$strSearch." ORDER BY ".$_GET['lista']." DESC", 30, $_GET['limit']);
127
        }
128
        $arrname = array();
129
        $arramount = array();
130
        $arrcost = array();
131
        $arrseller = array();
132
        $arrlink = array();
133
        $arruser = array();
134
        $i = 0;
135
        while (!$pm -> EOF) 
136
        {
137
            $arrname[$i] = $pm -> fields['nazwa'];
138
            $arramount[$i] = $pm -> fields['ilosc'];
139
            $arrcost[$i] = $pm -> fields['cost'];
140
            $arrseller[$i] = $pm -> fields['seller'];
141
            $seller = $db -> Execute("SELECT user FROM players WHERE id=".$pm -> fields['seller']);
142
            $arruser[$i] = $seller -> fields['user'];
143
            $seller -> Close();
144
            if ($player -> id == $pm -> fields['seller']) 
145
            {
146
                $arrlink[$i] = "<td>- <a href=pmarket.php?wyc=".$pm -> fields['id'].">".A_DELETE."</a></td></tr>";
147
            } 
148
                else 
149
            {
150
                $arrlink[$i] = "<td>- <a href=pmarket.php?buy=".$pm -> fields['id'].">".A_BUY."</a></td></tr>";
151
            }
152
            $pm -> MoveNext();
153
            $i = $i + 1;
154
        }
155
        $pm -> Close();
156
        $smarty -> assign(array("Name" => $arrname, 
157
            "Amount" => $arramount, 
158
            "Cost" => $arrcost, 
159
            "Seller" => $arrseller, 
160
            "Link" => $arrlink, 
161
            "User" => $arruser));
162
        if ($_GET['limit'] >= 30) 
163
        {
164
            $lim = $_GET['limit'] - 30;
165
            $smarty -> assign ("Previous", "<form method=\"post\" action=\"pmarket.php?view=market&limit=".$lim."&lista=".$_GET['lista']."\"><input type=\"hidden\" name=\"szukany\" value=\"".$_POST['szukany']."\"><input type=\"submit\" value=\"".A_PREVIOUS."\"></form> ");
166
        }
167
        $_GET['limit'] = $_GET['limit'] + 30;
168
        if ($oferty > 30 && $_GET['limit'] < $oferty) 
169
        {
170
            $smarty -> assign ("Next", " <form method=\"post\" action=\"pmarket.php?view=market&limit=".$_GET['limit']."&lista=".$_GET['lista']."\"><input type=\"hidden\" name=\"szukany\" value=\"".$_POST['szukany']."\"><input type=\"submit\" value=\"".A_NEXT."\"></form>");
171
        }
172
    }
173
}
174
175
/**
176
 * Add ofert to market
177
 */
178
if (isset ($_GET['view']) && $_GET['view'] == 'add') 
179
{
180
    $arrAmount = array($player -> platinum, $objMinerals -> fields['copperore'], $objMinerals -> fields['zincore'], $objMinerals -> fields['tinore'], $objMinerals -> fields['ironore'], $objMinerals -> fields['copper'], $objMinerals -> fields['bronze'], $objMinerals -> fields['brass'], $objMinerals -> fields['iron'], $objMinerals -> fields['steel'], $objMinerals -> fields['coal'], $objMinerals -> fields['adamantium'], $objMinerals -> fields['meteor'], $objMinerals -> fields['crystal'], $objMinerals -> fields['pine'], $objMinerals -> fields['hazel'], $objMinerals -> fields['yew'], $objMinerals -> fields['elm']);
181
    $smarty -> assign(array("Addinfo" => ADD_INFO,
182
                            "Mamount" => M_AMOUNT,
183
                            "Mcost" => M_COST,
184
                            "Aadd" => A_ADD,
185
                            "Minerals" => $arrMinerals,
186
                            "Mineralsamount" => $arrAmount,
187
                            "Mineral" => MINERAL,
188
                            "Tamount" => T_AMOUNT,
189
                            "Addofert" => 0));
190
    if (isset ($_GET['step']) && $_GET['step'] == 'add') 
191
    {
192
        if (!isset($_POST['mineral']) || !ereg("^[0-9]*$", $_POST['mineral'])) 
193
        {
194
            error(ERROR);
195
        }
196
        $arrSqlname = array('', 'copperore', 'zincore', 'tinore', 'ironore', 'copper', 'bronze', 'brass', 'iron', 'steel', 'coal', 'adamantium', 'meteor', 'crystal', 'pine', 'hazel', 'yew', 'elm');
197
        $arrName = array(MITH, COPPERORE, ZINCORE, TINORE, IRONORE, COPPER, BRONZE, BRASS, IRON, STEEL, COAL, ADAMANTIUM, METEOR, CRYSTAL, PINE, HAZEL, YEW, ELM);
198
        $strMineral = $arrSqlname[$_POST['mineral']];
199
        $strName = $arrName[$_POST['mineral']];
200
        $intAmount = $arrAmount[$_POST['mineral']];
201
        $strSqlname = $arrMinerals[$_POST['mineral']];
202
        if (!isset($_POST['amount']))
203
        {
204
            error(ERROR);
205
        }
206
        if ($_POST['amount'] > $intAmount)
207
        {
208
            error(NO_AMOUNT.$strName);
209
        }
210
        if (!ereg("^[1-9][0-9]*$", $_POST['amount']) || !ereg("^[1-9][0-9]*$", $_POST['cost'])) 
211
        {
212
            error (ERROR);
213
        }
214
        $objTest = $db -> Execute("SELECT `id` FROM `pmarket` WHERE `seller`=".$player -> id." AND `nazwa`='".$strSqlname."'");
215
        if (!$objTest -> fields['id'])
216
        {
217
            if ($_POST['mineral'] == 0) 
218
            {
219
                $db -> Execute("UPDATE players SET platinum=platinum-".$_POST['amount']." WHERE id=".$player -> id);
220
            } 
221
                else 
222
            {
223
                $db -> Execute("UPDATE minerals SET ".$strMineral."=".$strMineral."-".$_POST['amount']." WHERE owner=".$player -> id);
224
            }
225
            $db -> Execute("INSERT INTO pmarket (seller, ilosc, cost, nazwa, lang) VALUES(".$player -> id.",".$_POST['amount'].",".$_POST['cost'].",'".$strSqlname."', '".$player -> lang."')");
226
            $smarty -> assign("Message", YOU_ADD.$_POST['amount']."</b> ".$strName.ON_MARKET.$_POST['cost'].FOR_GOLDS." <a href=\"pmarket.php?view=add\">".A_REFRESH."</a>");
227
        }
228
            else
229
        {
230
            $smarty -> assign(array("Addofert" => $objTest -> fields['id'],
231
                                    "Youwant" => YOU_WANT,
232
                                    "Ayes" => YES,
233
                                    "Mineralname" => $_POST['mineral'],
234
                                    "Mineralamount" => $_POST['amount'],
235
                                    "Mineralcost" => $_POST['cost']));
236
            if (isset($_POST['ofert']))
237
            {
238
                if (!ereg("^[1-9][0-9]*$", $_POST['ofert'])) 
239
                {
240
                    error(ERROR);
241
                }
242
                require_once('includes/marketaddto.php');
243
                addtomin($_POST['ofert'], $strMineral, $_POST['mineral'], $player -> id);
244
                $smarty -> assign("Message", YOU_ADD.$_POST['amount']."</b> ".$strName.ON_MARKET2." <a href=\"pmarket.php?view=add\">".A_REFRESH."</a>");
245
            }
246
        }
247
        $objTest -> Close();
248
    }
249
}
250
251
/**
252
 * Delete all oferts from market
253
 */
254
if (isset ($_GET['view']) && $_GET['view'] == 'del') 
255
{
256
    require_once('includes/marketdelall.php');
257
    deleteallmin($player -> id, $arrMinerals);
258
    $smarty -> assign("Message", YOU_DELETE." (<a href=\"pmarket.php\">".A_BACK."</a>)");
259
}
260
261
/**
262
 * Buy minerals from market
263
 */
264
if (isset($_GET['buy'])) 
265
{
266
    if (!ereg("^[1-9][0-9]*$", $_GET['buy'])) 
267
    {
268
        error (ERROR);
269
    }
270
    $buy = $db -> Execute("SELECT * FROM pmarket WHERE id=".$_GET['buy']);
271
    if (!$buy -> fields['id']) 
272
    {
273
        error (NO_OFERTS);
274
    }
275
    if ($buy -> fields['seller'] == $player -> id) 
276
    {
277
        error (ITS_YOUR);
278
    }
279
    $seller = $db -> Execute("SELECT user FROM players WHERE id=".$buy -> fields['seller']);
280
    $smarty -> assign(array("Name" => $buy -> fields['nazwa'], 
281
        "Amount1" => $buy -> fields['ilosc'], 
282
        "Itemid" => $buy -> fields['id'], 
283
        "Cost" => $buy -> fields['cost'], 
284
        "Seller" => $seller -> fields['user'],
285
        "Sellerid" => $buy -> fields['seller'],
286
        "Buyinfo" => BUY_INFO,
287
        "Oamount" => O_AMOUNT,
288
        "Mcost" => M_COST,
289
        "Mseller" => M_SELLER,
290
        "Bamount" => B_AMOUNT,
291
        "Abuy" => A_BUY,
292
        "Mineral" => MINERAL));
293
    $buy -> Close();
294
    if (isset($_GET['step']) && $_GET['step'] == 'buy') 
295
    {
296
        if (!isset($_POST['amount']))
297
        {
298
            error(ERROR);
299
        }
300
        if (!ereg("^[1-9][0-9]*$", $_POST['amount'])) 
301
        {
302
            error (ERROR);
303
        }
304
        $buy = $db -> Execute("SELECT * FROM pmarket WHERE id=".$_GET['buy']);
305
        $price = $_POST['amount'] * $buy -> fields['cost'];
306
        if ($price > $player -> credits) 
307
        {
308
            error (NO_MONEY);
309
        }
310
        if ($_POST['amount'] > $buy -> fields['ilosc']) 
311
        {
312
            error(NO_AMOUNT.$buy -> fields['nazwa'].ON_MARKET);
313
        }
314
        $arrSqlname = array('', 'copperore', 'zincore', 'tinore', 'ironore', 'copper', 'bronze', 'brass', 'iron', 'steel', 'coal', 'adamantium', 'meteor', 'crystal', 'pine', 'hazel', 'yew', 'elm');
315
        $intKey = array_search($buy -> fields['nazwa'], $arrMinerals);
316
        $strMineral = $arrSqlname[$intKey];
317
        $db -> Execute("UPDATE players SET bank=bank+".$price." WHERE id=".$buy -> fields['seller']);
318
        $db -> Execute("UPDATE players SET credits=credits-".$price." WHERE id=".$player -> id);
319
        if ($buy -> fields['nazwa'] == 'Mithril') 
320
        {
321
            $db -> Execute("UPDATE players SET platinum=platinum+".$_POST['amount']." where id=".$player -> id);
322
        } 
323
            else 
324
        {
325
            if (!$objMinerals -> fields['owner']) 
326
            {
327
                $db -> Execute("INSERT INTO minerals (owner, ".$strMineral.") VALUES(".$player -> id.",".$_POST['amount'].")");
328
            } 
329
                else 
330
            {
331
                $db -> Execute("UPDATE minerals SET ".$strMineral."=".$strMineral."+".$_POST['amount']." WHERE owner=".$player -> id);
332
            }
333
        }
334
        if ($_POST['amount'] == $buy -> fields['ilosc']) 
335
        {
336
            $db -> Execute("DELETE FROM pmarket WHERE id=".$buy -> fields['id']);
337
        } 
338
            else 
339
        {
340
            $db -> Execute("UPDATE pmarket set ilosc=ilosc-".$_POST['amount']." WHERE id=".$buy -> fields['id']);
341
        }
342
        $strDate = $db -> DBDate($newdate);
343
        $db -> Execute("INSERT INTO `log` (`owner`, `log`, `czas`) VALUES(".$buy -> fields['seller'].",'<b><a href=view.php?view=".$player -> id.">".$player -> user.L_ACCEPT.$player -> id.L_ACCEPT2.$_POST['amount'].L_AMOUNT.$buy -> fields['nazwa'].YOU_GET.$price.TO_BANK."', ".$strDate.")");
344
        $smarty -> assign("Message", YOU_BUY.$_POST['amount'].I_AMOUNT.$buy -> fields['nazwa'].FOR_A.$price.GOLD_COINS);
345
    }
346
}
347
348
/**
349
 * Delete selected offert from market
350
 */
351
if (isset($_GET['wyc'])) 
352
{
353
    if (!ereg("^[1-9][0-9]*$", $_GET['wyc'])) 
354
    {
355
        error (ERROR);
356
    }
357
    $dwyc = $db -> Execute("SELECT * FROM pmarket WHERE id=".$_GET['wyc']);
358
    if ($dwyc -> fields['seller'] != $player -> id) 
359
    {
360
        error (NOT_YOUR);
361
    }
362
    require_once('includes/marketdel.php');
363
    deletemin($_GET['wyc'], $dwyc -> fields['nazwa'], $dwyc -> fields['ilosc'], $player -> id, $arrMinerals);
364
    $smarty -> assign("Message", YOU_DELETE." (<a href=\"pmarket.php\">".A_BACK."</a>)");
365
}
366
367
/**
368
* List of all oferts on market
369
*/
370
if (isset($_GET['view']) && $_GET['view'] == 'all') 
371
{
372
    $oferts = $db -> Execute("SELECT nazwa FROM pmarket GROUP BY nazwa");
373
    $arrname = array();
374
    $arramount = array();
375
    $i = 0;
376
    while (!$oferts -> EOF) 
377
    {
378
        $arrname[$i] = $oferts -> fields['nazwa'];
379
        $arramount[$i] = 0;
380
        $query = $db -> Execute("SELECT id FROM pmarket WHERE nazwa='".$arrname[$i]."'");
381
        while (!$query -> EOF) 
382
        {
383
            $arramount[$i] = $arramount[$i] + 1;
384
            $query -> MoveNext();
385
        }
386
        $query -> Close();
387
        $oferts -> MoveNext();
388
        $i = $i + 1;
389
    }
390
    $oferts -> Close();
391
    $smarty -> assign(array("Name" => $arrname, 
392
        "Amount" => $arramount, 
393
        "Message" => "<br />(<a href=\"pmarket.php\">".A_BACK."</a>)",
394
        "Listinfo" => LIST_INFO,
395
        "Mname" => M_NAME,
396
        "Mamount" => M_AMOUNT,
397
        "Maction" => M_ACTION,
398
        "Ashow" => A_SHOW));
399
}
400
401
/**
402
* Initialization of variables
403
*/
404
if (!isset($_GET['view'])) 
405
{
406
    $_GET['view'] = '';
407
}
408
if (!isset($_GET['wyc'])) 
409
{
410
    $_GET['wyc'] = '';
411
}
412
if (!isset($_GET['buy'])) 
413
{
414
    $_GET['buy'] = '';
415
}
416
417
/**
418
* Assign variables to template and display page
419
*/
420
$smarty -> assign(array("View" => $_GET['view'], 
421
    "Delete" => $_GET['wyc'], 
422
    "Buy" => $_GET['buy'],
423
    "Aback" => A_BACK));
424
$smarty -> display('pmarket.tpl');
425
426
require_once("includes/foot.php");
427
?>