Parcourir la source

API: add useitem

master
Skylar Ittner il y a 7 mois
Parent
révision
bfc85c006f
5 fichiers modifiés avec 89 ajouts et 1 suppressions
  1. 2
    1
      .gitignore
  2. 77
    0
      api/actions/useitem.php
  3. 7
    0
      api/apisettings.php
  4. BIN
      database.mwb.bak
  5. 3
    0
      langs/en/items.json

+ 2
- 1
.gitignore Voir le fichier

@@ -1,4 +1,5 @@
vendor
settings.php
nbproject/private
*.sync-conflict*
*.sync-conflict*
database.mwb.bak

+ 77
- 0
api/actions/useitem.php Voir le fichier

@@ -0,0 +1,77 @@
<?php

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

$user = getRequestUser();

if (!$database->has("inventory", ["AND" => ["itemuuid" => $VARS["uuid"], "accountid" => $user->getUID()]])) {
sendJsonResp("That item doesn't exist.", "ERROR");
}

$item = $database->get(
"items",
[
"[>]inventory" => ["itemid" => "itemid"],
"[>]itemclasses" => ["classid" => "classid"]
],
[
"inventory.itemjson",
"items.classid",
"itemclasses.classname",
"items.itemcode"
],
[
"AND" => [
"inventory.itemuuid" => $VARS["uuid"],
"inventory.accountid" => $user->getUID()
]
]
);
$itemuuid = $VARS["uuid"];
$item['itemcode'] = json_decode($item['itemcode'], true);
if (empty($item['itemjson']) || $item['itemjson'] == "[]") {
$itemjson = json_encode(['uses' => $item['itemcode']['uses']]);
} else {
$itemjson = json_decode($item["itemjson"], true);
}

$player = $database->get("players", ["energy", "maxenergy", "teamid"], ["accountid" => $user->getUID()]);

switch ($item["classname"]) {
case "healmagic":
if ($player["energy"] < $player["maxenergy"]) {
$newhp = $player["energy"] + $item["itemcode"]["amount"];
if ($newhp > $player["maxenergy"]) {
$newhp = $player["maxenergy"];
}
$diff = $newhp - $player["energy"];
$database->update("players", ["energy" => $newhp], ["accountid" => $user->getUID()]);
if ($itemjson["uses"] <= 1) {
$database->delete("inventory", ["AND" => ["itemuuid" => $itemuuid, "accountid" => $user->getUID()]]);
} else if ($itemjson["uses"] > 1) {
$itemjson["uses"] -= 1;
$database->update("inventory", ["itemjson" => json_encode($itemjson)], ["itemuuid" => $itemuuid]);
}
sendJsonResp($Strings->build("Restored {x} energy points.", ["x" => $diff], false));
} else {
sendJsonResp($Strings->get("That would have no effect.", false));
}
break;
case "artifact":
if (empty($VARS["placeid"]) || !$database->has("locations", ["AND" => ["osmid" => $VARS["placeid"], "teamid" => $player["teamid"]]])) {
sendJsonResp($Strings->get("You can't use that right now.", false));
}
$place = $database->get("locations", ["locationid", "teamid", "ownerid", "currentlife", "maxlife"], ["locationid" => $VARS["placeid"]]);
$placelife = $place["currentlife"] + $item["itemcode"]["amount"];
$placemax = $place["maxlife"] + $item["itemcode"]["amount"];
$database->update("locations", ["currentlife" => $placelife, "maxlife" => $placemax], ["osmid" => $VARS["placeid"]]);
// TODO: give user some exp
sendJsonResp($Strings->get("Artifact activated."));
break;
default:
sendJsonResp($Strings->get("You can't use that right now.", false));
}

+ 7
- 0
api/apisettings.php Voir le fichier

@@ -74,4 +74,11 @@ $APIS = [
"message" => "string"
]
],
"useitem" => [
"load" => "useitem.php",
"vars" => [
"uuid" => "numeric",
"placeid (optional)" => "numeric"
]
]
];

BIN
database.mwb.bak Voir le fichier


+ 3
- 0
langs/en/items.json Voir le fichier

@@ -0,0 +1,3 @@
{
"Restored {x} energy points.": "Restored {x} energy points."
}

Chargement…
Annuler
Enregistrer