Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

187 righe
6.8 KiB
PHP

<?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/.
*/
/**
* https://www.sitepoint.com/php-random-number-generator/
*/
class Random {
// random seed
private static $RSeed = 0;
// set seed
public static function seed($s = 0) {
self::$RSeed = abs(intval($s)) % 9999999 + 1;
self::num();
}
// generate random number
public static function num($min = 0, $max = 9999999) {
if (self::$RSeed == 0) {
self::seed(mt_rand());
}
self::$RSeed = (self::$RSeed * 125) % 2796203;
return self::$RSeed % ($max - $min + 1) + $min;
}
}
$returndata = [
"status" => "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);
// Get all items, except ones that have no rarity, which can't be found normally
$items = $database->select("items", ["itemid", "weight"], ["weight[>]" => 0]);
$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()]);
$player = new Player(getRequestUser());
$player->stats->updateStat(PlayerStats::SCANS, 1);
$player->save();
$itemname = $database->get('items', 'itemname', ['itemid' => $itemid]);
$returndata["item"] = $itemname;
$returndata['messages'][] = $Strings->build("You found one {item}", ["item" => $itemname], false);
}
if (strpos($VARS["code"], "munzee") !== false && $database->has('munzee', ['accountid' => getRequestUser()->getUID()])) {
if (!empty($VARS["latitude"]) && !empty($VARS["longitude"]) && !empty($VARS["accuracy"])) {
$latitude = $VARS["latitude"];
$longitude = $VARS["longitude"];
$accuracy = $VARS["accuracy"];
/* Check if we need to refresh the bearer token first */
if ($database->has('munzee', ["AND" => ['accountid' => getRequestUser()->getUID(), 'expires[<=]' => (time() + 30)]])) {
$url = 'https://api.munzee.com/oauth/login';
$fields = array(
'client_id' => urlencode($SETTINGS["munzee"]["id"]),
'client_secret' => urlencode($SETTINGS["munzee"]["secret"]),
'grant_type' => 'refresh_token',
'refresh_token' => $database->select('munzee', 'refreshtoken', ['accountid' => getRequestUser()->getUID()])[0]
);
foreach ($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $fields_string,
CURLOPT_RETURNTRANSFER => 1, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "TerranQuest Game Server", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
$data = json_decode($result, TRUE)['data'];
$status_code = json_decode($result, TRUE)['status_code'];
if ($status_code == 200) {
$database->update('munzee', ['bearertoken' => $data['token']['access_token'], 'refreshtoken' => $data['token']['refresh_token'], 'expires' => $data['token']['expires']], ['accountid' => getRequestUser()->getUID()]);
}
}
/* Check again now */
if ($database->has('munzee', ["AND" => ['accountid' => getRequestUser()->getUID(), 'expires[>]' => (time() + 30)]])) {
$url = 'https://api.munzee.com/capture/light/';
$header = array(
'Authorization: ' . $database->select('munzee', ['bearertoken'], ['accountid' => getRequestUser()->getUID()])[0]['bearertoken']
);
$time = time();
$fields = ['data' => json_encode([
"language" => "EN",
"latitude" => $latitude,
"longitude" => $longitude,
"accuracy" => $accuracy,
"time" => time(),
"code" => $VARS["code"]
])
];
//open connection
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $fields,
CURLOPT_HTTPHEADER => $header,
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "TerranQuest Game Server", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
//close connection
curl_close($ch);
$data = json_decode($result, TRUE);
if ($data['status_code'] == 200) {
// Add munzee capture info to response
$returndata["munzee"] = $data["data"]["result"];
}
}
}
}
exitWithJson($returndata);