WIP
This commit is contained in:
parent
05ffe52ef7
commit
b1921b2dbb
@ -9,35 +9,19 @@
|
||||
|
||||
const BDC_KEY = "067c718d2bbd4a2ba8d0645e1633cac2";
|
||||
|
||||
class GameHelper {
|
||||
/**
|
||||
* Fetch a game seed
|
||||
* @param {string} id
|
||||
* @return {Promise} Seed Promise
|
||||
*/
|
||||
static async fetchSeed(id) {
|
||||
async function fetchSeed(id) {
|
||||
return axios
|
||||
.get(`https://www.geoguessr.com/api/v3/games/${id}`)
|
||||
.then((res) => res.data)
|
||||
.catch((error) => console.log(error));
|
||||
};
|
||||
|
||||
static async getCountryBDC(location) {
|
||||
async function getCountryBDC(location) {
|
||||
return axios
|
||||
.get(`https://api.bigdatacloud.net/data/reverse-geocode?latitude=${location.lat}&longitude=${location.lng}&key=${BDC_KEY}`)
|
||||
.then((res) => countryCodes[res.data.countryCode])
|
||||
.catch((error) => error);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an array of 9 coodinates as objects.
|
||||
* Each coordinate is 100 meters aways from the given
|
||||
* coordinate y angles from 0 to 315
|
||||
* The first coordinate is the original passed
|
||||
* @param {Object} location {lat, lng}
|
||||
* @return {Array} Coordinates [{lat, lng}, {lat, lng}] x 8
|
||||
*/
|
||||
static getSurroundings (location) {
|
||||
function getSurroundings (location) {
|
||||
const meters = 100;
|
||||
const R_EARTH = 6378.137;
|
||||
const M = 1 / (((2 * Math.PI) / 360) * R_EARTH) / 1000;
|
||||
@ -57,32 +41,16 @@ class GameHelper {
|
||||
return coordinates;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if the param is coordinates
|
||||
* @param {string} coordinates
|
||||
* @return {boolean}
|
||||
*/
|
||||
static isCoordinates (coordinates) {
|
||||
function isCoordinates (coordinates) {
|
||||
const regex = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/g;
|
||||
return regex.test(coordinates);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns map scale
|
||||
* @param {Object} bounds map bounds
|
||||
* @return {number} map scale
|
||||
*/
|
||||
static calculateScale(bounds){
|
||||
return GameHelper.haversineDistance({ lat: bounds.min.lat, lng: bounds.min.lng }, { lat: bounds.max.lat, lng: bounds.max.lng }) / 7.458421;
|
||||
function calculateScale(bounds){
|
||||
return haversineDistance({ lat: bounds.min.lat, lng: bounds.min.lng }, { lat: bounds.max.lat, lng: bounds.max.lng }) / 7.458421;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns distance in km between two coordinates
|
||||
* @param {Object} mk1 {lat, lng}
|
||||
* @param {Object} mk2 {lat, lng}
|
||||
* @return {number} km
|
||||
*/
|
||||
static haversineDistance(mk1, mk2){
|
||||
function haversineDistance(mk1, mk2){
|
||||
const R = 6371.071;
|
||||
const rlat1 = mk1.lat * (Math.PI / 180);
|
||||
const rlat2 = mk2.lat * (Math.PI / 180);
|
||||
@ -95,52 +63,22 @@ class GameHelper {
|
||||
return km;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns score based on distance and scale
|
||||
* @param {number} distance
|
||||
* @param {number} scale
|
||||
* @return {number} score
|
||||
*/
|
||||
static calculateScore(distance, scale){
|
||||
function calculateScore(distance, scale){
|
||||
return Math.round(5000 * Math.pow(0.99866017, (distance * 1000) / scale));
|
||||
}
|
||||
|
||||
/** Replace special chars
|
||||
* @param {String} val
|
||||
*/
|
||||
static normalize(val){
|
||||
function normalize(val){
|
||||
return val.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
||||
}
|
||||
|
||||
/** Matches words above 3 letters
|
||||
* @param {String} input
|
||||
* @param {String} key
|
||||
*/
|
||||
static isMatch(input, key){
|
||||
function isMatch(input, key){
|
||||
return input.length >= 3 && key.includes(input) && input.length <= key.length;
|
||||
}
|
||||
|
||||
/** Find country by code or name
|
||||
* @param {String} input
|
||||
* @return {Object} countryCodesNames
|
||||
*/
|
||||
static findCountry(input){
|
||||
function findCountry(input){
|
||||
const normalized = GameHelper.normalize(input);
|
||||
return countryCodesNames.find((country) => country.code === normalized || GameHelper.isMatch(normalized, country.names.toLowerCase()));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var isOpen = $.getSetIniDbBoolean('chatguessr', 'isOpen', false),
|
||||
seed = {},
|
||||
|
Loading…
x
Reference in New Issue
Block a user