You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.2 KiB

const config = require("./config.json");
import CapturedArea from "./components/captured-area";
export const sendArea = (gameID, username, polygon) => {
// console.log(polygon)
points = []
polygon.map((point) => {
points.push({lat: point.coordinate.latitude, lng: point.coordinate.longitude})
})
let formBody = [];
formBody.push("gameID=" + gameID);
formBody.push("userID=" + username);
formBody.push("points=" + JSON.stringify(points));
formBody = formBody.join("&");
console.log(formBody)
return fetch("http://"+config.host + ":" + config.port + "/v1/polygon", {
method: "post",
headers: {'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'},
body: formBody
})
.then(res => {return res.json()});
}
export const updateAreas = (gameID, update) => {
getPolygons(gameID).then((responseJson) => {
let polygons = []
responseJson.map((polygon) => {
let userID = polygon.userID
let strokeColor = "#"+polygon.colour
let fillColor = "#"+polygon.colour
let coordinates = []
polygon.coords.map((point) => {
coordinates.push({
"latitude" : point.lat,
"longitude" : point.lng
})
});
polygons.push(new CapturedArea(userID, coordinates, strokeColor, hexToRgbA(fillColor,0.2), 1));
});
update(polygons);
})
}
function getPolygons(gameID) {
console.log("http://"+config.host + ":" + config.port + "/v1/polygons?gameID=" + gameID)
return fetch("http://"+config.host + ":" + config.port + "/v1/polygons?gameID=" + gameID, {method: 'GET',
headers: {'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'}})
.then(response => response.json())
.catch(error => {
console.error(error);
});
}
function hexToRgbA(hex, a){
var c;
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
c= hex.substring(1).split('');
if(c.length== 3){
c= [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c= '0x'+c.join('');
return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',' + a + ')';
}
throw new Error('Bad Hex');
}