From b1e757d02ceb0da6beb2244eb53cf52f8f9396e0 Mon Sep 17 00:00:00 2001 From: choelzl Date: Wed, 9 Feb 2022 21:35:12 +0100 Subject: [PATCH] WIP --- twitch/chatguessr.js | 82 ++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/twitch/chatguessr.js b/twitch/chatguessr.js index 05d5bba..4de7c7d 100644 --- a/twitch/chatguessr.js +++ b/twitch/chatguessr.js @@ -33,6 +33,10 @@ var newLng = coords.lng + (x * M) / Math.cos(coords.lat * (Math.PI / 180)); return { lat: newLat, lng: newLng }; } + + function toPos(coords) { + return { lat: parseFloat(msg.split(",")[0]), lng: parseFloat(msg.split(",")[1]) }; + } function getSurroundings (location) { const meters = 100; const R_EARTH = 6378.137; @@ -59,9 +63,7 @@ const rlat2 = mk2.lat * (Math.PI / 180); const difflat = rlat2 - rlat1; const difflon = (mk2.lng - mk1.lng) * (Math.PI / 180); - const km = - 2 * - R * + const km = 2 * R * 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; }; @@ -85,41 +87,28 @@ var isOpen = $.getSetIniDbBoolean('chatguessr', 'isOpen', false), seed = {}, - guesses = { - '1':[ - {user:'Alpha',location:'0,0',distance:5,score:2,streak:0}, - {user:'Beta',location:'5,0',distance:100,score:3,streak:0}, - {user:'Charlie',location:'6,6',distance:1000,score:0,streak:0}, - {user:'Delta',location:'42,6',distance:1,score:5,streak:1} - ], - '2':[ - - ], - }; - - function dbSetSeed(id) { - $.inidb.SetBoolean("chatguessr", "isOpen", "", open); - isOpen = open; - } - - function dbSetOpen(open) { - $.inidb.SetBoolean("chatguessr", "isOpen", "", open); - isOpen = open; - sendData('status',open); - } - function dbClearGuesses() { + scale, guesses = {}; - $.inidb.SetString("chatguessr", "guesses", JSON.stringify(guesses)); - sendData('guesses',guesses); - } - function dbAddGuess(user,position) { - if(guesses[user] && guesses[user].length > 0){ - }else if(isCoordinates(position)){ - guesses[user] = position; - $.inidb.SetString("chatguessr", "guesses", JSON.stringify(guesses)); - sendData('guesses',JSON.stringify(guesses)); - } + function addGuess(user,position) { + if(seed.round == undefined) return; + let current_round = ""+seed.round; + if(guesses[current_round]==undefined) guesses[current_round] = [] + if(guesses[current_round].some((v)=>v.user==user)) return; + if(!isCoordinates(position)) return; + let pos = toPos(position) + let distance = haversineDistance(pos, {lat:seed.rounds[current_round].lat,lon:seed.rounds[current_round].lon}) + + let score = calculateScore(distance, scale) + guesses[current_round].push({ + user:user, + location:location, + distance:distance, + score:score, + streak: 0, + }); + sendData('guesses',JSON.stringify(guesses)); + } function sendData(tpe, data) { @@ -130,11 +119,6 @@ })); } - 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); - }; - $.bind('command', function(event) { const sender = event.getSender().toLowerCase(), command = event.getCommand(), @@ -154,13 +138,28 @@ $.setIniDbBoolean('cgstatus', 'isOpen', false) }else if(action.equalsIgnoreCase('start')){ seed = fetchSeed(args[1]); + scale = calculateScale(seed.bouds) }else if(action.equalsIgnoreCase('end')){ isOpen = false; $.setIniDbBoolean('cgstatus', 'isOpen', false) + guesses = {}; + seed = {}; + sendData('guesses',JSON.stringify(guesses)); }else if(action.equalsIgnoreCase('refresh')){ var newseed = fetchSeed(args[1]); + if(newseed.state=="finished"){ + //FINISHED + }else if(newseed.player.guesses.length() > seed.player.guesses.length()){ + //NEW ROUND ! + } }else if(action.equalsIgnoreCase('gg')){ sendData('guesses',JSON.stringify(guesses)); + }else if(action.equalsIgnoreCase('fg')){ + DBAddG("Alpha","0,0"); + DBAddG("Beta","40,0"); + DBAddG("Charlie","0,60"); + DBAddG("Delta","80,80"); + sendData('guesses',JSON.stringify(guesses)); } } }); @@ -175,6 +174,7 @@ $.registerChatSubcommand('cga', 'end', 2); $.registerChatSubcommand('cga', 'refresh', 2); $.registerChatSubcommand('cga', 'gg', 2); + $.registerChatSubcommand('cga', 'fg', 2); }); })();