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.

64 lines
2.6 KiB

let db = require("../config/db.js");
let mathFunc = require("./mathFunc.js");
let util = require("util");
let areaFunctions = require("./areaFunctions.js");
module.exports = {
getPolygons : function (req, res) {
if (req.query.gameID != null) {
let query = "CALL getPolygonPoints(\"" + req.query.gameID + "\");";
db.get().query(query, function(err, rows){
if (err) {
console.log(err);
return res.send(400);
}
// console.log(rows);
let polygons = []
let polygonDict = {};
rows[0].map((point) => {
if (!(point.polygonID in polygonDict)) {
polygonDict[point.polygonID] = {};
polygonDict[point.polygonID].coords = [];
polygonDict[point.polygonID].gameID = point.gameID;
polygonDict[point.polygonID].userID = point.userID;
polygonDict[point.polygonID].colour = point.colour;
polygonDict[point.polygonID].polygonID = point.polygonID;
}
polygonDict[point.polygonID].coords.push({lat: point.lat, lng: point.lng});
});
for (let [polygon, polygonData] of Object.entries(polygonDict)){
polygons.push(polygonDict[polygon]);
}
res.send(polygons);
return;
});
} else {
res.send(500);
}
},
isPolygon: function (req, res) {
if (req.body.gameID != null && req.body.userID != null && req.body.points != null) {
let gameID = req.body.gameID;
let userID = req.body.userID;
let points = JSON.parse(req.body.points);
if (points.length < 2) {
res.send([]);
} else {
let firstPoint = points[0];
let lastPoint = points[points.length - 1];
let distance = mathFunc.gpsToMeters(firstPoint.lat, firstPoint.lng, lastPoint.lat, lastPoint.lng);
console.log(distance);
if (distance < 10) {
if (lastPoint.lat != firstPoint.lat && lastPoint.lng != firstPoint.lng) {
// points.pop();
points.push({lat: firstPoint.lat, lng: lastPoint.lng});
}
areaFunctions.collatePolygons(res, gameID, userID, points);
} else {
res.send([]);
}
}
}
}
}