2022-02-09 02:52:17 +01:00
|
|
|
(function() {
|
2022-02-09 02:51:18 +01:00
|
|
|
|
2022-02-09 03:12:49 +01:00
|
|
|
function fetchSeed(id) {
|
2022-02-09 03:12:09 +01:00
|
|
|
var HttpRequest = Packages.com.gmt2001.HttpRequest,
|
2022-02-09 03:16:02 +01:00
|
|
|
HashMap = Packages.java.util.HashMap,
|
2022-02-09 03:17:32 +01:00
|
|
|
JSONObject = Packages.org.json.JSONObject,
|
2022-02-09 03:16:02 +01:00
|
|
|
header = new HashMap(0),
|
|
|
|
json = new JSONObject('{}');
|
2022-02-09 03:19:16 +01:00
|
|
|
var request = HttpRequest.getData(HttpRequest.RequestType.GET,'https://www.geoguessr.com/api/v3/games/'+id,json.toString(),header);
|
2022-02-09 03:10:51 +01:00
|
|
|
if (request.success) {
|
|
|
|
var data = new JSONObject(request.content)
|
2022-02-09 21:54:11 +01:00
|
|
|
return JSON.parse(data);
|
2022-02-09 03:10:51 +01:00
|
|
|
}
|
2022-02-09 02:51:18 +01:00
|
|
|
};
|
2022-02-09 03:12:49 +01:00
|
|
|
function getCountryBDC(location) {
|
2022-02-09 03:12:09 +01:00
|
|
|
var HttpRequest = Packages.com.gmt2001.HttpRequest,
|
2022-02-09 03:16:02 +01:00
|
|
|
HashMap = Packages.java.util.HashMap,
|
2022-02-09 03:17:32 +01:00
|
|
|
JSONObject = Packages.org.json.JSONObject,
|
2022-02-09 03:16:02 +01:00
|
|
|
header = new HashMap(0),
|
|
|
|
json = new JSONObject('{}');
|
2022-02-09 03:12:09 +01:00
|
|
|
const BDC_KEY = "067c718d2bbd4a2ba8d0645e1633cac2";
|
2022-02-09 03:19:16 +01:00
|
|
|
var request = HttpRequest.getData(HttpRequest.RequestType.GET,'https://api.bigdatacloud.net/data/reverse-geocode?latitude='+location.lat+'&longitude='+location.lng+'&key='+BDC_KEY,json.toString(),header);
|
2022-02-09 03:10:51 +01:00
|
|
|
if (request.success) {
|
|
|
|
var data = new JSONObject(request.content)
|
|
|
|
return data.countryCode;
|
|
|
|
}
|
2022-02-09 02:51:18 +01:00
|
|
|
};
|
2022-02-09 03:21:37 +01:00
|
|
|
function moveFrom(coords, angle, distance) {
|
2022-02-09 03:23:25 +01:00
|
|
|
var radianAngle = (angle * Math.PI) / 180;
|
|
|
|
var x = 0 + distance * Math.cos(radianAngle);
|
|
|
|
var y = 0 + distance * Math.sin(radianAngle);
|
|
|
|
var newLat = coords.lat + y * M;
|
|
|
|
var newLng = coords.lng + (x * M) / Math.cos(coords.lat * (Math.PI / 180));
|
2022-02-09 03:21:37 +01:00
|
|
|
return { lat: newLat, lng: newLng };
|
|
|
|
}
|
2022-02-09 21:35:12 +01:00
|
|
|
|
|
|
|
function toPos(coords) {
|
2022-02-09 21:56:25 +01:00
|
|
|
return { lat: parseFloat(coords.split(",")[0]), lng: parseFloat(coords.split(",")[1]) };
|
2022-02-09 21:35:12 +01:00
|
|
|
}
|
2022-02-09 02:55:43 +01:00
|
|
|
function getSurroundings (location) {
|
2022-02-09 02:51:18 +01:00
|
|
|
const meters = 100;
|
|
|
|
const R_EARTH = 6378.137;
|
|
|
|
const M = 1 / (((2 * Math.PI) / 360) * R_EARTH) / 1000;
|
2022-02-09 03:23:25 +01:00
|
|
|
var coordinates = [location];
|
|
|
|
for (var angle = 0; angle < 360; angle += 45) {
|
2022-02-09 02:51:18 +01:00
|
|
|
coordinates.push(moveFrom({ lat: location.lat, lng: location.lng }, angle, meters));
|
|
|
|
}
|
|
|
|
return coordinates;
|
|
|
|
};
|
|
|
|
|
2022-02-09 02:55:43 +01:00
|
|
|
function isCoordinates (coordinates) {
|
2022-02-09 02:51:18 +01:00
|
|
|
const regex = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/g;
|
|
|
|
return regex.test(coordinates);
|
|
|
|
};
|
|
|
|
|
2022-02-09 02:55:43 +01:00
|
|
|
function calculateScale(bounds){
|
|
|
|
return haversineDistance({ lat: bounds.min.lat, lng: bounds.min.lng }, { lat: bounds.max.lat, lng: bounds.max.lng }) / 7.458421;
|
2022-02-09 02:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-09 02:55:43 +01:00
|
|
|
function haversineDistance(mk1, mk2){
|
2022-02-09 02:51:18 +01:00
|
|
|
const R = 6371.071;
|
|
|
|
const rlat1 = mk1.lat * (Math.PI / 180);
|
|
|
|
const rlat2 = mk2.lat * (Math.PI / 180);
|
|
|
|
const difflat = rlat2 - rlat1;
|
|
|
|
const difflon = (mk2.lng - mk1.lng) * (Math.PI / 180);
|
2022-02-09 21:35:12 +01:00
|
|
|
const km = 2 * R *
|
2022-02-09 02:51:18 +01:00
|
|
|
Math.asin(Math.sqrt(Math.sin(difflat / 2) * Math.sin(difflat / 2) + Math.cos(rlat1) * Math.cos(rlat2) * Math.sin(difflon / 2) * Math.sin(difflon / 2)));
|
|
|
|
return km;
|
|
|
|
};
|
|
|
|
|
2022-02-09 02:55:43 +01:00
|
|
|
function calculateScore(distance, scale){
|
2022-02-09 02:51:18 +01:00
|
|
|
return Math.round(5000 * Math.pow(0.99866017, (distance * 1000) / scale));
|
|
|
|
}
|
|
|
|
|
2022-02-09 02:55:43 +01:00
|
|
|
function normalize(val){
|
2022-02-09 02:51:18 +01:00
|
|
|
return val.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
|
|
}
|
|
|
|
|
2022-02-09 02:55:43 +01:00
|
|
|
function isMatch(input, key){
|
2022-02-09 02:51:18 +01:00
|
|
|
return input.length >= 3 && key.includes(input) && input.length <= key.length;
|
|
|
|
}
|
|
|
|
|
2022-02-09 02:55:43 +01:00
|
|
|
function findCountry(input){
|
2022-02-09 02:51:18 +01:00
|
|
|
const normalized = GameHelper.normalize(input);
|
|
|
|
return countryCodesNames.find((country) => country.code === normalized || GameHelper.isMatch(normalized, country.names.toLowerCase()));
|
|
|
|
};
|
2022-02-09 02:52:17 +01:00
|
|
|
|
2022-02-10 10:35:37 +01:00
|
|
|
$.getSetIniDbBoolean('chatguessr', 'isOpen', false);
|
|
|
|
var seed = {},
|
2022-02-09 21:35:12 +01:00
|
|
|
scale,
|
2022-02-07 17:19:38 +01:00
|
|
|
guesses = {};
|
|
|
|
|
2022-02-09 21:35:12 +01:00
|
|
|
function addGuess(user,position) {
|
|
|
|
if(seed.round == undefined) return;
|
2022-02-09 21:58:48 +01:00
|
|
|
var current_round = ""+(seed.round-1);
|
2022-02-09 21:35:12 +01:00
|
|
|
if(guesses[current_round]==undefined) guesses[current_round] = []
|
|
|
|
if(guesses[current_round].some((v)=>v.user==user)) return;
|
|
|
|
if(!isCoordinates(position)) return;
|
2022-02-09 21:37:26 +01:00
|
|
|
var pos = toPos(position)
|
2022-02-09 21:58:48 +01:00
|
|
|
var distance = haversineDistance(pos, {lat: seed.rounds[current_round].lat, lng: seed.rounds[current_round].lng})
|
2022-02-09 21:35:12 +01:00
|
|
|
|
2022-02-09 21:37:26 +01:00
|
|
|
var score = calculateScore(distance, scale)
|
2022-02-09 21:35:12 +01:00
|
|
|
guesses[current_round].push({
|
|
|
|
user:user,
|
2022-02-09 22:00:16 +01:00
|
|
|
location:position,
|
2022-02-09 21:35:12 +01:00
|
|
|
distance:distance,
|
|
|
|
score:score,
|
|
|
|
streak: 0,
|
|
|
|
});
|
2022-02-10 10:57:32 +01:00
|
|
|
guesses.round = seed.round - 1;
|
2022-02-14 15:16:07 +01:00
|
|
|
sendGuessData();
|
2022-02-07 17:19:38 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 15:16:07 +01:00
|
|
|
function sendGuessData() {
|
|
|
|
guesses.rounds = seed.rounds;
|
2022-02-07 17:19:38 +01:00
|
|
|
$.panelsocketserver.sendJSONToAll(JSON.stringify({
|
|
|
|
'eventFamily': 'chatguessr',
|
2022-02-14 15:28:18 +01:00
|
|
|
'eventType': 'guesses',
|
2022-02-14 15:16:07 +01:00
|
|
|
'data': JSON.stringify(guesses)
|
2022-02-07 17:19:38 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:56:00 +01:00
|
|
|
function accGuesses(){
|
|
|
|
var res = {};
|
|
|
|
for(var rr = 0; rr < guesses.round; ++rr){
|
|
|
|
for(var s = 0; s < guesses[rr].length; ++s){
|
|
|
|
if(!res[guesses[rr][s].user]){
|
|
|
|
res[guesses[rr][s].user] = {score:0,distance:0}
|
|
|
|
}
|
2022-02-14 18:07:41 +01:00
|
|
|
res[guesses[rr][s].user].score += guesses[rr][s].score
|
|
|
|
res[guesses[rr][s].user].distance += guesses[rr][s].distance
|
2022-02-14 17:56:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Object.keys(res).map(k => {
|
2022-02-14 17:57:46 +01:00
|
|
|
var v = res[k]
|
2022-02-14 17:56:00 +01:00
|
|
|
v.user = k;
|
|
|
|
return v;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-07 17:19:38 +01:00
|
|
|
$.bind('command', function(event) {
|
2022-02-09 04:48:58 +01:00
|
|
|
const sender = event.getSender().toLowerCase(),
|
2022-02-07 17:19:38 +01:00
|
|
|
command = event.getCommand(),
|
|
|
|
args = event.getArgs(),
|
|
|
|
action = args[0];
|
|
|
|
|
|
|
|
if (command.equalsIgnoreCase('g')) {
|
2022-02-10 10:35:37 +01:00
|
|
|
if($.getIniDbBoolean('chatguessr', 'isOpen')) addGuess(sender,args);
|
2022-02-07 17:19:38 +01:00
|
|
|
}else if (command.equalsIgnoreCase('cg')) {
|
2022-02-09 02:28:05 +01:00
|
|
|
//send url
|
|
|
|
//$.say($.whisperPrefix(sender) + $.lang.get('chatguessr.help', ' Use "!cg [open | close | ...]" to open/close the feature.'));
|
|
|
|
}else if(command.equalsIgnoreCase('cga')){
|
|
|
|
if(action.equalsIgnoreCase('open')){
|
2022-02-09 16:59:48 +01:00
|
|
|
$.setIniDbBoolean('cgstatus', 'isOpen', true);
|
2022-02-09 02:28:05 +01:00
|
|
|
}else if(action.equalsIgnoreCase('close')){
|
2022-02-09 16:59:48 +01:00
|
|
|
$.setIniDbBoolean('cgstatus', 'isOpen', false)
|
2022-02-09 02:28:05 +01:00
|
|
|
}else if(action.equalsIgnoreCase('start')){
|
2022-02-09 17:09:00 +01:00
|
|
|
seed = fetchSeed(args[1]);
|
2022-02-10 20:31:34 +01:00
|
|
|
scale = calculateScale(seed.bounds);
|
|
|
|
guesses.round = seed.round - 1;
|
2022-02-14 15:16:07 +01:00
|
|
|
sendGuessData();
|
2022-02-09 02:28:05 +01:00
|
|
|
}else if(action.equalsIgnoreCase('end')){
|
2022-02-09 16:59:48 +01:00
|
|
|
$.setIniDbBoolean('cgstatus', 'isOpen', false)
|
2022-02-09 21:35:12 +01:00
|
|
|
guesses = {};
|
|
|
|
seed = {};
|
2022-02-14 15:16:07 +01:00
|
|
|
sendGuessData();
|
2022-02-09 02:28:05 +01:00
|
|
|
}else if(action.equalsIgnoreCase('refresh')){
|
2022-02-10 10:44:01 +01:00
|
|
|
if(seed.token == undefined) return;
|
|
|
|
var newseed = fetchSeed(seed.token);
|
2022-02-14 15:30:43 +01:00
|
|
|
if(newseed.round > seed.round){
|
2022-02-10 10:57:32 +01:00
|
|
|
$.setIniDbBoolean('chatguessr', 'isOpen',false) //NEW ROUND /Score
|
2022-02-10 10:35:37 +01:00
|
|
|
var gp = newseed.player.guesses[seed.round - 1].lat+","+newseed.player.guesses[seed.round - 1].lng
|
|
|
|
addGuess($.channelName,gp);
|
2022-02-14 16:53:30 +01:00
|
|
|
}else if(newseed.state=="finished" && newseed.player.guesses.length > seed.player.guesses.length){
|
|
|
|
$.setIniDbBoolean('chatguessr', 'isOpen',false)
|
|
|
|
var gp = newseed.player.guesses[newseed.round - 1].lat+","+newseed.player.guesses[newseed.round - 1].lng
|
|
|
|
addGuess($.channelName,gp);
|
|
|
|
}else if(newseed.state=="finished"){
|
|
|
|
$.setIniDbBoolean('chatguessr', 'isOpen',false) //FINISHED
|
|
|
|
seed = newseed;
|
2022-02-14 17:56:00 +01:00
|
|
|
guesses.final = accGuesses()
|
|
|
|
//Compute total scores + give reward
|
2022-02-14 16:53:30 +01:00
|
|
|
sendGuessData();
|
2022-02-10 10:35:37 +01:00
|
|
|
}else{
|
2022-02-14 16:53:30 +01:00
|
|
|
$.setIniDbBoolean('chatguessr', 'isOpen',true) //NEW ROUD /Guess
|
|
|
|
guesses.round = seed.round - 1;
|
|
|
|
sendGuessData();
|
2022-02-09 21:35:12 +01:00
|
|
|
}
|
2022-02-10 10:35:37 +01:00
|
|
|
seed = newseed;
|
2022-02-09 02:28:05 +01:00
|
|
|
}else if(action.equalsIgnoreCase('gg')){
|
2022-02-14 15:16:07 +01:00
|
|
|
sendGuessData();
|
2022-02-09 21:35:12 +01:00
|
|
|
}else if(action.equalsIgnoreCase('fg')){
|
2022-02-09 21:47:38 +01:00
|
|
|
addGuess("Alpha","0,0");
|
|
|
|
addGuess("Beta","40,0");
|
|
|
|
addGuess("Charlie","0,60");
|
|
|
|
addGuess("Delta","80,80");
|
2022-02-07 17:19:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.bind('initReady', function() {
|
2022-02-09 02:28:05 +01:00
|
|
|
$.registerChatCommand('./custom/custom/chatguessr.js', 'cg',7);
|
2022-02-07 17:19:38 +01:00
|
|
|
$.registerChatCommand('./custom/custom/chatguessr.js', 'g',7);
|
2022-02-09 03:45:14 +01:00
|
|
|
$.registerChatCommand('./custom/custom/chatguessr.js', 'cga',7);
|
2022-02-09 04:48:58 +01:00
|
|
|
$.registerChatSubcommand('cga', 'open', 2);
|
|
|
|
$.registerChatSubcommand('cga', 'close', 2);
|
|
|
|
$.registerChatSubcommand('cga', 'start', 2);
|
|
|
|
$.registerChatSubcommand('cga', 'end', 2);
|
|
|
|
$.registerChatSubcommand('cga', 'refresh', 2);
|
|
|
|
$.registerChatSubcommand('cga', 'gg', 2);
|
2022-02-09 21:35:12 +01:00
|
|
|
$.registerChatSubcommand('cga', 'fg', 2);
|
2022-02-07 17:19:38 +01:00
|
|
|
});
|
|
|
|
|
2022-02-09 02:28:05 +01:00
|
|
|
})();
|