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.
83 lines
3.2 KiB
83 lines
3.2 KiB
let db = require("../config/db.js");
|
|
let mathFunc = require("./mathFunc.js");
|
|
let util = require("util");
|
|
|
|
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 = []
|
|
rows[0].map((point) => {
|
|
let polygon = {};
|
|
polygon.coords = [];
|
|
polygon.polygonID = point.polygonID;
|
|
polygon.coords.push({lat: point.lat, lng: point.lng});
|
|
polygon.gameID = point.gameID;
|
|
polygon.userID = point.userID;
|
|
polygon.colour = point.colour;
|
|
polygons.push(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 < 3) {
|
|
//area is just 1 for now we fix later.
|
|
query = util.format("CALL insertPolygon(\"%s\", \"%s\", 1)", gameID, userID);
|
|
db.get().query(query, function(err, rows) {
|
|
if (err) {
|
|
console.log(err);
|
|
return res.send(400);
|
|
}
|
|
|
|
let polygonID = rows[0][0].polygonID;
|
|
query = "INSERT INTO polygonPoints (polygonID, lat, lng) VALUES ";
|
|
let delimiterNecessary = false;
|
|
|
|
points.map((point) => {
|
|
if (delimiterNecessary) {
|
|
query += ",";
|
|
}
|
|
query += util.format("(\"%s\",%s,%s)", polygonID, point.lat, point.lng);
|
|
delimiterNecessary = true;
|
|
});
|
|
query += ";";
|
|
|
|
db.get().query(query, function(err, pointRows) {
|
|
if (err) {
|
|
console.log(err);
|
|
return res.send(400);
|
|
}
|
|
res.send(rows[0][0]);
|
|
return;
|
|
});
|
|
});
|
|
} else {
|
|
res.send([]);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
} |