1
<?php
2
/**
3
 *   File functions:
4
 *   Herbs market - add, buy herbs from other players
5
 *
6
 *   @name                 : hmarket.php
7
 *   @copyright            : (C) 2004,2005,2006 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                : 27.11.2006
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: hmarket.php 856 2006-11-29 17:30:36Z thindil $
32
33
$title = "Rynek ziół";
34
require_once("includes/head.php");
35
36
/**
37
* Get the localization for game
38
*/
39
require_once("languages/".$player -> lang."/hmarket.php");
40
41
if ($player -> location != 'Altara' && $player -> location != 'Ardulith')
42
{
43
    error (ERROR);
44
}
45
$gr = $db -> Execute("SELECT `id`, `illani`, `illanias`, `nutari`, `dynallca`, `illani_seeds`, `illanias_seeds`, `nutari_seeds`, `dynallca_seeds` FROM `herbs` WHERE `gracz`=".$player -> id) or die($db -> ErrorMsg());
46
$arrName = array(N_HERB1, N_HERB2, N_HERB3, N_HERB4, N_HERB5, N_HERB6, N_HERB7, N_HERB8);
47
48
/**
49
* Assign variables to template
50
*/
51
$smarty -> assign(array("Message" => '',
52
                        "Previous" => '',
53
                        "Next" => ''));
54
55
/**
56
* Main menu
57
*/
58
if (!isset($_GET['view']) && !isset($_GET['buy']) && !isset($_GET['wyc']))
59
{
60
    $smarty -> assign(array("Minfo" => M_INFO,
61
                            "Aview" => A_VIEW,
62
                            "Asearch" => A_SEARCH,
63
                            "Aadd" => A_ADD,
64
                            "Adelete" => A_DELETE,
65
                            "Alist" => A_LIST,
66
                            "Aback2" => A_BACK2));
67
}
68
69
/**
70
* Search herbs on market
71
*/
72
if (isset ($_GET['view']) && $_GET['view'] == 'szukaj')
73
{
74
    $smarty -> assign(array("Sinfo" => S_INFO,
75
                            "Sinfo2" => S_INFO2,
76
                            "Herb" => HERB,
77
                            "Asearch" => A_SEARCH));
78
}
79
80
/**
81
* View oferts on market
82
*/
83
if (isset ($_GET['view']) && $_GET['view'] == 'market')
84
{
85
    if (empty($_POST['szukany']))
86
    {
87
        $msel = $db -> Execute("SELECT id FROM hmarket");
88
        $strSearch = '';
89
    }
90
        else
91
    {
92
        $_POST['szukany'] = strip_tags($_POST['szukany']);
93
        $_POST['szukany'] = str_replace("*","%", $_POST['szukany']);
94
        $strSearch = $db -> qstr($_POST['szukany'], get_magic_quotes_gpc());
95
        $msel = $db -> Execute("SELECT id FROM hmarket WHERE nazwa LIKE ".$strSearch);
96
    }
97
    $oferty = $msel -> RecordCount();
98
    $msel -> Close();
99
    if (!isset($_GET['limit']))
100
    {
101
        $_GET['limit'] = 0;
102
    }
103
    if (!isset($_GET['lista']))
104
    {
105
        $_GET['lista'] = 0;
106
    }
107
    if ($oferty == 0)
108
    {
109
        error(NO_OFERTS);
110
    }
111
    if ($_GET['limit'] < $oferty)
112
    {
113
        if (empty($_POST['szukany']))
114
        {
115
            $pm = $db -> SelectLimit("SELECT * FROM hmarket ORDER BY ".$_GET['lista']." DESC", 30, $_GET['limit']);
116
        }
117
            else
118
        {
119
            $pm = $db -> SelectLimit("SELECT * FROM hmarket WHERE nazwa LIKE ".$strSearch." ORDER BY ".$_GET['lista']." DESC", 30, $_GET['limit']);
120
        }
121
        $arrname = array();
122
        $arramount = array();
123
        $arrcost = array();
124
        $arrseller = array();
125
        $arraction = array();
126
        $arruser = array();
127
        $i = 0;
128
        while (!$pm -> EOF)
129
        {
130
            $arrname[$i] = $pm -> fields['nazwa'];
131
            $arramount[$i] = $pm -> fields['ilosc'];
132
            $arrcost[$i] = $pm -> fields['cost'];
133
            $arrseller[$i] = $pm -> fields['seller'];
134
            $seller = $db -> Execute("SELECT user FROM players WHERE id=".$pm -> fields['seller']);
135
            $arruser[$i] = $seller -> fields['user'];
136
            $seller -> Close();
137
            if ($player -> id == $pm -> fields['seller'])
138
            {
139
                $arraction[$i] = "<td>- <a href=hmarket.php?wyc=".$pm -> fields['id'].">".A_DELETE."</a></td></tr>";
140
            }
141
                else
142
            {
143
                $arraction[$i] = "<td>- <a href=hmarket.php?buy=".$pm -> fields['id'].">".A_BUY."</a></td></tr>";
144
            }
145
            $pm -> MoveNext();
146
            $i = $i + 1;
147
        }
148
        $pm -> Close();
149
        $smarty -> assign(array("Name" => $arrname,
150
                                "Amount" => $arramount,
151
                                "Cost" => $arrcost,
152
                                "Seller" => $arrseller,
153
                                "Action" => $arraction,
154
                                "User" => $arruser,
155
                                "Therb" => HERB,
156
                                "Tamount" => T_AMOUNT,
157
                                "Tcost" => T_COST,
158
                                "Tseller" => T_SELLER,
159
                                "Toptions" => T_OPTIONS,
160
                                "Viewinfo" => VIEW_INFO));
161
        if (!isset($_POST['szukany']))
162
        {
163
            $_POST['szukany'] = '';
164
        }
165
        if ($_GET['limit'] >= 30)
166
        {
167
            $lim = $_GET['limit'] - 30;
168
            $smarty -> assign ("Previous", "<form method=\"post\" action=\"hmarket.php?view=market&limit=".$lim."&lista=".$_GET['lista']."\"><input type=\"hidden\" name=\"szukany\" value=\"".$_POST['szukany']."\"><input type=\"submit\" value=\"".A_PREVIOUS."\"></form> ");
169
        }
170
        $_GET['limit'] = $_GET['limit'] + 30;
171
        if ($oferty > 30 && $_GET['limit'] < $oferty)
172
        {
173
            $smarty -> assign ("Next", " <form method=\"post\" action=\"hmarket.php?view=market&limit=".$_GET['limit']."&lista=".$_GET['lista']."\"><input type=\"hidden\" name=\"szukany\" value=\"".$_POST['szukany']."\"><input type=\"submit\" value=\"".A_NEXT."\"></form>");
174
        }
175
    }
176
}
177
178
/**
179
* Add ofert on market
180
*/
181
if (isset ($_GET['view']) && $_GET['view'] == 'add')
182
{
183
    $arrSqlname = array('illani', 'illanias', 'nutari', 'dynallca', 'illani_seeds', 'illanias_seeds', 'nutari_seeds', 'dynallca_seeds');
184
    $arrAmount = array($gr -> fields['illani'], $gr -> fields['illanias'], $gr -> fields['nutari'], $gr -> fields['dynallca'], $gr -> fields['illani_seeds'], $gr -> fields['illanias_seeds'], $gr -> fields['nutari_seeds'], $gr -> fields['dynallca_seeds']);
185
    $smarty -> assign(array("Addinfo" => ADD_INFO,
186
                            "Herb" => HERB,
187
                            "Herbname" => $arrName,
188
                            "Sqlname" => $arrSqlname,
189
                            "Hamount" => H_AMOUNT,
190
                            "Hcost" => H_COST,
191
                            "Aadd" => A_ADD,
192
                            "Herbamount" => $arrAmount,
193
                            "Tamount" => T_AMOUNT,
194
                            "Addofert" => 0));
195
    if (isset ($_GET['step']) && $_GET['step'] == 'add')
196
    {
197
        if (!isset($_POST['ilosc']) || !isset($_POST['cost']) || !ereg("^[1-9][0-9]*$", $_POST['ilosc']) || !ereg("^[1-9][0-9]*$", $_POST['cost']))
198
        {
199
            error (ERROR);
200
        }
201
        if (!in_array($_POST['mineral'], $arrSqlname))
202
        {
203
            error(ERROR);
204
        }
205
        $intKey = array_search($_POST['mineral'], $arrSqlname);
206
        if ($_POST['ilosc'] > $gr -> fields[$arrSqlname[$intKey]])
207
        {
208
            error(NO_AMOUNT.$arrName[$intKey]);
209
        }
210
        $objTest = $db -> Execute("SELECT `id` FROM `hmarket` WHERE `seller`=".$player -> id." AND `nazwa`='".$arrName[$intKey]."'");
211
        if (!$objTest -> fields['id'])
212
        {
213
            $db -> Execute("INSERT INTO `hmarket` (`seller`, `ilosc`, `cost`, `nazwa`, `lang`) VALUES(".$player -> id.",".$_POST['ilosc'].",".$_POST['cost'].",'".$arrName[$intKey]."', '".$player -> lang."')") or die($db -> ErrorMsg());
214
            $db -> Execute("UPDATE `herbs` SET `".$_POST['mineral']."`=`".$_POST['mineral']."`-".$_POST['ilosc']." WHERE `gracz`=".$player -> id);
215
            $smarty -> assign("Message", YOU_ADD.$_POST['ilosc']."</b> ".$arrName[$intKey].ON_MARKET.$_POST['cost'].FOR_GOLDS." <a href=\"hmarket.php?view=add\">".A_REFRESH."</a>");
216
        }
217
            else
218
        {
219
            $smarty -> assign(array("Addofert" => $objTest -> fields['id'],
220
                                    "Youwant" => YOU_WANT,
221
                                    "Ayes" => YES,
222
                                    "Herbname" => $_POST['mineral'],
223
                                    "Herbamount" => $_POST['ilosc'],
224
                                    "Herbcost" => $_POST['cost']));
225
            if (isset($_POST['ofert']))
226
            {
227
                if (!ereg("^[1-9][0-9]*$", $_POST['ofert']))
228
                {
229
                    error(ERROR);
230
                }
231
                require_once('includes/marketaddto.php');
232
                addtoherb($_POST['ofert'], $_POST['mineral'], $player -> id, $_POST['ilosc']);
233
                $smarty -> assign("Message", YOU_ADD.$_POST['ilosc']."</b> ".$arrName[$intKey].ON_MARKET2." <a href=\"hmarket.php?view=add\">".A_REFRESH."</a>");
234
            }
235
        }
236
        $objTest -> Close();
237
    }
238
}
239
240
/**
241
* Delete all oferts one player from market
242
*/
243
if (isset ($_GET['view']) && $_GET['view'] == 'del')
244
{
245
    require_once('includes/marketdelall.php');
246
    deleteallherb($player -> id, $arrName);
247
    $smarty -> assign("Message", YOU_DELETE." (<A href=hmarket.php>".A_BACK."</a>)");
248
}
249
250
/**
251
* Buy herbs from market
252
*/
253
if (isset($_GET['buy']))
254
{
255
    if (!ereg("^[1-9][0-9]*$", $_GET['buy']))
256
    {
257
        error (ERROR);
258
    }
259
    $buy = $db -> Execute("SELECT * FROM hmarket WHERE id=".$_GET['buy']) ;
260
    if (!$buy -> fields['id'])
261
    {
262
        error (NO_OFERTS);
263
    }
264
    if ($buy -> fields['seller'] == $player -> id)
265
    {
266
        error (IS_YOUR);
267
    }
268
    $seller = $db -> Execute("SELECT user FROM players WHERE id=".$buy -> fields['seller']);
269
    $smarty -> assign(array("Name" => $buy -> fields['nazwa'],
270
                            "Amount1" => $buy -> fields['ilosc'],
271
                            "Itemid" => $buy -> fields['id'],
272
                            "Cost" => $buy -> fields['cost'],
273
                            "Seller" => $seller -> fields['user'],
274
                            "Sid" => $buy -> fields['seller'],
275
                            "Buyinfo" => BUY_INFO,
276
                            "Bherb" => HERB,
277
                            "Oamount" => O_AMOUNT,
278
                            "Hcost" => H_COST,
279
                            "Hseller" => SELLER,
280
                            "Bamount" => B_AMOUNT,
281
                            "Abuy" => A_BUY));
282
    $buy -> Close();
283
    if (isset($_GET['step']) && $_GET['step'] == 'buy')
284
    {
285
        if (!ereg("^[1-9][0-9]*$", $_POST['amount']))
286
        {
287
            error (ERROR);
288
        }
289
        $buy = $db -> Execute("SELECT * FROM hmarket WHERE id=".$_GET['buy']);
290
        $price = (int)$_POST['amount'] * $buy -> fields['cost'];
291
        if ($price > $player -> credits)
292
        {
293
            error (NO_MONEY);
294
        }
295
        if ($_POST['amount'] > $buy -> fields['ilosc'])
296
        {
297
            error(NO_AMOUNT.$buy -> fields['nazwa'].ON_MARKET);
298
        }
299
        $db -> Execute("UPDATE players SET bank=bank+".$price." WHERE id=".$buy -> fields['seller']);
300
        $db -> Execute("UPDATE players SET credits=credits-".$price." WHERE id=".$player -> id);
301
        $arrSqlname = array('illani', 'illanias', 'nutari', 'dynallca', 'illani_seeds', 'illanias_seeds', 'nutari_seeds', 'dynallca_seeds');
302
        $intKey = array_search($buy -> fields['nazwa'], $arrName);
303
        if (!$gr -> fields['id'])
304
        {
305
            $db -> Execute("INSERT INTO herbs (gracz, ".$arrSqlname[$intKey].") VALUES(".$player -> id.",".$_POST['amount'].")");
306
        }
307
            else
308
        {
309
            $db -> Execute("UPDATE herbs SET ".$arrSqlname[$intKey]."=".$arrSqlname[$intKey]."+".$_POST['amount']." WHERE gracz=".$player -> id);
310
        }
311
        if ($_POST['amount'] == $buy -> fields['ilosc'])
312
        {
313
            $db -> Execute("DELETE FROM hmarket WHERE id=".$buy -> fields['id']);
314
        }
315
            else
316
        {
317
            $db -> Execute("UPDATE hmarket SET ilosc=ilosc-".$_POST['amount']." WHERE id=".$buy -> fields['id']);
318
        }
319
        $strDate = $db -> DBDate($newdate);
320
        $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.")");
321
        $smarty -> assign("Message", YOU_BUY.$_POST['amount'].I_AMOUNT.$buy -> fields['nazwa'].FOR_A.$price.GOLD_COINS);
322
    }
323
}
324
325
if (isset($_GET['wyc']))
326
{
327
    if (!ereg("^[1-9][0-9]*$", $_GET['wyc']))
328
    {
329
        error (ERROR);
330
    }
331
    $dwyc = $db -> Execute("SELECT * FROM hmarket WHERE id=".$_GET['wyc']);
332
    if ($dwyc -> fields['seller'] != $player -> id)
333
    {
334
        error (NOT_YOUR);
335
    }
336
    require_once('includes/marketdel.php');
337
    deleteherb($_GET['wyc'], $dwyc -> fields['nazwa'], $dwyc -> fields['ilosc'], $player -> id, $arrName);
338
    $smarty -> assign("Message", YOU_DELETE." (<A href=hmarket.php>".A_BACK."</a>)");
339
}
340
341
/**
342
* List of all ofers on market
343
*/
344
if (isset($_GET['view']) && $_GET['view'] == 'all')
345
{
346
    $oferts = $db -> Execute("SELECT nazwa FROM hmarket GROUP BY nazwa");
347
    $arrname = array();
348
    $arramount = array();
349
    $i = 0;
350
    while (!$oferts -> EOF)
351
    {
352
        $arrname[$i] = $oferts -> fields['nazwa'];
353
        $arramount[$i] = 0;
354
        $query = $db -> Execute("SELECT id FROM hmarket WHERE nazwa='".$arrname[$i]."'");
355
        while (!$query -> EOF)
356
        {
357
            $arramount[$i] = $arramount[$i] + 1;
358
            $query -> MoveNext();
359
        }
360
        $query -> Close();
361
        $oferts -> MoveNext();
362
        $i = $i + 1;
363
    }
364
    $oferts -> Close();
365
    $smarty -> assign(array("Name" => $arrname,
366
        "Amount" => $arramount,
367
        "Message" => "<br />(<a href=\"hmarket.php\">".A_BACK."</a>)",
368
        "Listinfo" => LIST_INFO,
369
        "Hname" => H_NAME,
370
        "Hamount" => H_AMOUNT,
371
        "Haction" => H_ACTION,
372
        "Ashow" => A_SHOW));
373
}
374
375
/**
376
* Initialization of variables
377
*/
378
if (!isset($_GET['view']))
379
{
380
    $_GET['view'] = '';
381
}
382
if (!isset($_GET['wyc']))
383
{
384
    $_GET['wyc'] = '';
385
}
386
if (!isset($_GET['buy']))
387
{
388
    $_GET['buy'] = '';
389
}
390
391
/**
392
* Assign variables to template and display page
393
*/
394
$smarty -> assign(array("View" => $_GET['view'],
395
    "Remowe" => $_GET['wyc'],
396
    "Buy" => $_GET['buy'],
397
    "Aback" => A_BACK));
398
$smarty -> display ('hmarket.tpl');
399
400
require_once("includes/foot.php");
401
?>