PHP game server for TerranQuest. https://terranquest.net

buyitem.php 890B

123456789101112131415161718192021222324252627
  1. <?php
  2. require 'required.php';
  3. require 'onlyloggedin.php';
  4. if (!$database->has('shopitems', ['merchid' => $VARS['merchid']])) {
  5. sendError(ITEM_UNAVAILABLE, true);
  6. }
  7. $shopitem = $database->select('shopitems', ['merchid', 'itemid', 'quantity', 'cost'], ['merchid' => $VARS['merchid']])[0];
  8. if (!is_empty($VARS['cost']) && !($shopitem['cost'] == $VARS['cost'])) {
  9. sendError(ITEM_INCORRECT_PRICE, true);
  10. }
  11. $credits = $database->select('players', ['credits'], ['uuid' => $_SESSION['uuid']])[0]['credits'];
  12. if ($credits < $shopitem['cost']) {
  13. sendError(PLAYER_NOT_ENOUGH_CREDITS, true);
  14. }
  15. for ($i = 0; $i < $shopitem['quantity']; $i++) {
  16. $database->insert('inventory', ['playeruuid' => $_SESSION['uuid'], 'itemid' => $shopitem['itemid']]);
  17. }
  18. $database->update('players', ['credits' => ($credits - $shopitem['cost'])], ['uuid' => $_SESSION['uuid']]);
  19. sendOK(ITEM_PURCHASED);