diff --git a/api/actions/broadcast.php b/api/actions/broadcast.php new file mode 100644 index 0000000..68801ac --- /dev/null +++ b/api/actions/broadcast.php @@ -0,0 +1,15 @@ +update("players", [ + "latitude" => $VARS["latitude"], + "longitude" => $VARS["longitude"], + "lastping" => date("Y-m-d H:i:s") + ], ["accountid" => getRequestUser()->getUID()]); + +sendJsonResp(); \ No newline at end of file diff --git a/api/actions/createplayer.php b/api/actions/createplayer.php new file mode 100644 index 0000000..e531147 --- /dev/null +++ b/api/actions/createplayer.php @@ -0,0 +1,24 @@ +has("players", ["accountid" => getRequestUser()->getUID()])) { + sendJsonResp($Strings->get("Your account is already active.", false), "ERROR"); +} + +$database->insert("players", [ + "accountid" => getRequestUser()->getUID(), + "level" => 1.0, + "energy" => 100, + "maxenergy" => 100, + "credits" => 500, + "lastping" => date("Y-m-d H:i:s"), + "teamid" => $VARS['team'], + "nickname" => getRequestUser()->getName(), +]); + +sendJsonResp(); \ No newline at end of file diff --git a/api/actions/getprofile.php b/api/actions/getprofile.php new file mode 100644 index 0000000..1bf697c --- /dev/null +++ b/api/actions/getprofile.php @@ -0,0 +1,24 @@ +getUID(); +} else { + $accountid = $VARS["id"]; +} + +if ($database->has("players", ["accountid" => $accountid])) { + $profile = $database->get("players", ["accountid (id)", "nickname (name)", "level", "energy", "maxenergy", "teamid"], ["accountid" => $accountid]); +} else { + sendJsonResp($Strings->get("Player does not exist.", false), "ERROR"); +} + +exitWithJson([ + "status" => "OK", + "profile" => $profile +]); diff --git a/api/actions/items.php b/api/actions/items.php new file mode 100644 index 0000000..0bdab1e --- /dev/null +++ b/api/actions/items.php @@ -0,0 +1,27 @@ +select( + 'items', [ + '[>]inventory' => ['itemid' => 'itemid'], + '[>]itemclasses' => ['classid', 'classid'] + ], [ + 'inventory.itemuuid (uuid)', + 'inventory.itemid (id)', + 'inventory.itemjson (json)', + 'items.itemname (name)', + 'items.itemdesc (description)', + 'items.itemcode (code)', + 'itemclasses.classid (classid)', + 'itemclasses.classname (classname' + ], [ + "playeruuid" => getRequestUser()->getUID() + ] +); + +exitWithJson(["status" => "OK", "items" => $items]); \ No newline at end of file diff --git a/api/actions/nearby.php b/api/actions/nearby.php new file mode 100644 index 0000000..a4fa65a --- /dev/null +++ b/api/actions/nearby.php @@ -0,0 +1,97 @@ +boundingCoordinates($radius, "miles"); + +ob_flush(); +$people = $database->debug()->select("accounts", [ + "publicid", + "name", + "username", + "verified", + "latitude", + "longitude" + ], [ + "AND" => [ + 'latitude[<>]' => [$searchbounds[0]->getLatitudeInDegrees(), $searchbounds[1]->getLatitudeInDegrees()], + 'longitude[<>]' => [$searchbounds[0]->getLongitudeInDegrees(), $searchbounds[1]->getLongitudeInDegrees()], + "lastgpsfix[>]" => date("Y-m-d H:i:s", strtotime("-1 hour")), + "type" => 2 + ], + "LIMIT" => 100 + ] +); +$query = ob_get_contents(); +ob_clean(); +$people = $database->query($query)->fetchAll(); + +$nearby = []; + +if (!empty($VARS["format"]) && $VARS["format"] == "geojson") { + $geojson = [ + "name" => "Nearby People", + "type" => "FeatureCollection", + "features" => [] + ]; + + foreach ($people as $person) { + $geojson["features"][] = [ + "type" => "Feature", + "geometry" => [ + "type" => "Point", + "coordinates" => [ + $person["longitude"] * 1.0, + $person["latitude"] * 1.0 + ] + ], + "properties" => [ + "id" => $person["publicid"], + "name" => utf8_encode(empty($person["name"]) ? $person["username"] : $person["name"]), + "username" => $person["username"], + "verified" => $person["verified"] == 1 + ] + ]; + } + + exitWithJson($geojson); +} + +foreach ($people as $person) { + $nearby[] = [ + "name" => (empty($person["name"]) ? $person["username"] : $person["name"]), + "username" => $person["username"], + "verified" => $person["verified"] == 1, + "publicid" => $person["publicid"], + "latitude" => $person["latitude"] * 1.0, + "longitude" => $person["longitude"] * 1.0 + ]; +} + +exitWithJson([ + "status" => "OK", + "radius" => $radius, + "bounds" => [ + 0 => [ + "latitude" => $searchbounds[0]->getLatitudeInDegrees(), + "longitude" => $searchbounds[0]->getLongitudeInDegrees() + ], + 1 => [ + "latitude" => $searchbounds[1]->getLatitudeInDegrees(), + "longitude" => $searchbounds[1]->getLongitudeInDegrees() + ], + ], + "nearby" => $nearby +]); diff --git a/api/actions/playerexists.php b/api/actions/playerexists.php new file mode 100644 index 0000000..3dc1f38 --- /dev/null +++ b/api/actions/playerexists.php @@ -0,0 +1,18 @@ +getUID(); +} else { + $accountid = $VARS["id"]; +} + +exitWithJson([ + "status" => "OK", + "exists" => $database->has("players", ["accountid" => $accountid]) +]); diff --git a/api/actions/submitcode.php b/api/actions/submitcode.php new file mode 100644 index 0000000..33e5bcf --- /dev/null +++ b/api/actions/submitcode.php @@ -0,0 +1,81 @@ + "OK", + "item" => "", + "munzee" => "", + "messages" => [] +]; + +try { + if (strpos($VARS["code"], "munzee") > 1) { + if (!empty($VARS["latitude"]) && !empty($VARS["longitude"]) && !empty($VARS["accuracy"])) { + include 'capturemunzee.php'; + } + } +} catch (Exception $ex) { +//file_put_contents("munzee.log", "Error with Munzee code: $ex\n", FILE_APPEND); +} + +if ($database->has('claimedcodes', ["AND" => ['code' => $VARS["code"], 'accountid' => getRequestUser()->getUID()]])) { + $returndata['messages'][] = $Strings->get("You've already discovered that.", false); +} else { + $codearray = str_split($VARS["code"]); + + $codeint = 0; + foreach ($codearray as $chr) { + $codeint += ord($chr); + } + Random::seed($codeint); + + $items = $database->select("items", ["itemid", "weight"]); + $weighted = []; + + foreach ($items as $item) { + for ($i = 0; $i < $item["weight"]; $i++) { + $weighted[] = $item["itemid"]; + } + } + $itemid = $weighted[Random::num(0, count($weighted))]; + + $database->insert('inventory', ['accountid' => getRequestUser()->getUID(), 'itemid' => $itemid]); + $database->insert('claimedcodes', ['code' => $VARS["code"], 'accountid' => getRequestUser()->getUID()]); + $itemname = $database->get('items', 'itemname', ['itemid' => $itemid]); + + $returndata["item"] = $itemname; + + $returndata['messages'][] = $Strings->build("You found one {item}", ["item" => $itemname], false); +} + +exitWithJson($returndata); diff --git a/api/apisettings.php b/api/apisettings.php index a0f511b..95c8998 100644 --- a/api/apisettings.php +++ b/api/apisettings.php @@ -11,5 +11,35 @@ $APIS = [ "load" => "ping.php", "vars" => [ ] + ], + "items" => [ + "load" => "items.php" + ], + "createplayer" => [ + "load" => "createplayer.php", + "vars" => [ + "team" => "/[1-6]/" + ] + ], + "playerexists" => [ + "load" => "playerexists.php", + "vars" => [ + "id (optional)" => "/[0-9]+/" + ] + ], + "getprofile" => [ + "load" => "getprofile.php", + "vars" => [ + "id (optional)" => "/[0-9]+/" + ] + ], + "code" => [ + "load" => "submitcode.php", + "vars" => [ + "code" => "string", + "latitude (optional)" => "/[0-9]{0,3}\.[0-9]{2,10}/", + "longitude (optional)" => "/[0-9]{0,3}\.[0-9]{2,10}/", + "accuracy (optional)" => "number" + ] ] -]; +]; \ No newline at end of file diff --git a/database.mwb b/database.mwb index a905e35..4cf762e 100644 Binary files a/database.mwb and b/database.mwb differ diff --git a/database.mwb.bak b/database.mwb.bak new file mode 100644 index 0000000..3414328 Binary files /dev/null and b/database.mwb.bak differ diff --git a/database/migrate_v1.sql b/database/migrate_v1.sql new file mode 100644 index 0000000..993c226 --- /dev/null +++ b/database/migrate_v1.sql @@ -0,0 +1,136 @@ +/* + * 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/. + */ +/** + * Author: Skylar Ittner + * Created: Apr 26, 2019 + */ + +ALTER TABLE `terranquest`.`inventory` +DROP FOREIGN KEY `fk_inventory_players`; + +ALTER TABLE `terranquest`.`player_badges` +DROP FOREIGN KEY `fk_achievements_has_players_players1`; + +ALTER TABLE `terranquest`.`locations` +DROP FOREIGN KEY `fk_locations_players1`; + +ALTER TABLE `terranquest`.`claimedcodes` +DROP FOREIGN KEY `fk_claimedcodes_players1`; + +ALTER TABLE `terranquest`.`munzee` +DROP FOREIGN KEY `fk_munzee_players1`; + +ALTER TABLE `terranquest`.`messages` +DROP FOREIGN KEY `fk_messages_players1`; + +ALTER TABLE `terranquest`.`artifacts` +DROP FOREIGN KEY `fk_artifacts_players1`; + +ALTER TABLE `terranquest`.`private_messages` +DROP FOREIGN KEY `fk_private_messages_players1`, +DROP FOREIGN KEY `fk_private_messages_players2`; + +ALTER TABLE `terranquest`.`inventory` +CHANGE COLUMN `playeruuid` `accountid` VARCHAR(60) CHARACTER SET 'utf8' NOT NULL ; + +ALTER TABLE `terranquest`.`players` +CHANGE COLUMN `uuid` `accountid` VARCHAR(60) CHARACTER SET 'utf8' NOT NULL ; + +ALTER TABLE `terranquest`.`player_badges` +CHANGE COLUMN `playeruuid` `accountid` VARCHAR(60) CHARACTER SET 'utf8' NOT NULL ; + +ALTER TABLE `terranquest`.`locations` +CHANGE COLUMN `owneruuid` `ownerid` VARCHAR(60) CHARACTER SET 'utf8' NULL DEFAULT NULL ; + +ALTER TABLE `terranquest`.`claimedcodes` +CHANGE COLUMN `playeruuid` `accountid` VARCHAR(60) CHARACTER SET 'utf8' NOT NULL ; + +ALTER TABLE `terranquest`.`munzee` +CHANGE COLUMN `player_uuid` `accountid` VARCHAR(60) CHARACTER SET 'utf8' NOT NULL ; + +ALTER TABLE `terranquest`.`messages` +CHANGE COLUMN `message` `message` VARCHAR(500) COLLATE 'utf8mb4_bin' NOT NULL , +CHANGE COLUMN `uuid` `accountid` VARCHAR(60) CHARACTER SET 'utf8' NULL DEFAULT NULL ; + +ALTER TABLE `terranquest`.`artifacts` +CHANGE COLUMN `currentlife` `currentlife` DECIMAL(7,2) NOT NULL DEFAULT 100 , +CHANGE COLUMN `maxlife` `maxlife` DECIMAL(7,2) NOT NULL DEFAULT 100 , +CHANGE COLUMN `playeruuid` `accountid` VARCHAR(60) CHARACTER SET 'utf8' NULL DEFAULT NULL ; + +ALTER TABLE `terranquest`.`private_messages` +CHANGE COLUMN `from_uuid` `from_id` VARCHAR(60) CHARACTER SET 'utf8' NOT NULL DEFAULT 0 , +CHANGE COLUMN `to_uuid` `to_id` VARCHAR(60) CHARACTER SET 'utf8' NOT NULL ; + +ALTER TABLE `terranquest`.`inventory` +ADD CONSTRAINT `fk_inventory_players` + FOREIGN KEY (`accountid`) + REFERENCES `terranquest`.`players` (`accountid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION; + +ALTER TABLE `terranquest`.`player_badges` +ADD CONSTRAINT `fk_achievements_has_players_players1` + FOREIGN KEY (`accountid`) + REFERENCES `terranquest`.`players` (`accountid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION; + +ALTER TABLE `terranquest`.`locations` +ADD CONSTRAINT `fk_locations_players1` + FOREIGN KEY (`ownerid`) + REFERENCES `terranquest`.`players` (`accountid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION; + +ALTER TABLE `terranquest`.`claimedcodes` +ADD CONSTRAINT `fk_claimedcodes_players1` + FOREIGN KEY (`accountid`) + REFERENCES `terranquest`.`players` (`accountid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION; + +ALTER TABLE `terranquest`.`munzee` +ADD CONSTRAINT `fk_munzee_players1` + FOREIGN KEY (`accountid`) + REFERENCES `terranquest`.`players` (`accountid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION; + +ALTER TABLE `terranquest`.`messages` +ADD CONSTRAINT `fk_messages_players1` + FOREIGN KEY (`accountid`) + REFERENCES `terranquest`.`players` (`accountid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION; + +ALTER TABLE `terranquest`.`artifacts` +ADD CONSTRAINT `fk_artifacts_players1` + FOREIGN KEY (`accountid`) + REFERENCES `terranquest`.`players` (`accountid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION; + +ALTER TABLE `terranquest`.`private_messages` +ADD CONSTRAINT `fk_private_messages_players1` + FOREIGN KEY (`from_id`) + REFERENCES `terranquest`.`players` (`accountid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, +ADD CONSTRAINT `fk_private_messages_players2` + FOREIGN KEY (`to_id`) + REFERENCES `terranquest`.`players` (`accountid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION; + +ALTER TABLE `terranquest`.`items` +ADD COLUMN `weight` INT(3) NOT NULL DEFAULT 1 AFTER `itemcode`; + +ALTER TABLE `terranquest`.`messages` +CHANGE COLUMN `message` `message` VARCHAR(500) COLLATE 'utf8mb4_bin' NOT NULL ; + +ALTER TABLE `terranquest`.`artifacts` +CHANGE COLUMN `currentlife` `currentlife` DECIMAL(7,2) NOT NULL DEFAULT 100 , +CHANGE COLUMN `maxlife` `maxlife` DECIMAL(7,2) NOT NULL DEFAULT 100 ; \ No newline at end of file