List, export, recommendations done
This commit is contained in:
89
routes/recommends.ts
Normal file
89
routes/recommends.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import * as express from "express";
|
||||
const router = express.Router();
|
||||
|
||||
router.use((req, res, next) => {
|
||||
if (!req.cookies || !req.cookies.uid) {
|
||||
res.status(404);
|
||||
res.send();
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/", (req, res, next) => {
|
||||
const user = (req.app as any).db.get("users").find({id: req.cookies.uid}).value();
|
||||
const data = [{
|
||||
backgroundColor: "rgba(200, 200, 200, 1)",
|
||||
data: [],
|
||||
label: "Noch nicht probiert",
|
||||
}, {
|
||||
backgroundColor: "#8b0000",
|
||||
data: [],
|
||||
label: "1",
|
||||
}, {
|
||||
backgroundColor: "#b24502",
|
||||
data: [],
|
||||
label: "2",
|
||||
}, {
|
||||
backgroundColor: "#d87501",
|
||||
data: [],
|
||||
label: "3",
|
||||
}, {
|
||||
backgroundColor: "#ffa500",
|
||||
data: [],
|
||||
label: "4",
|
||||
}, {
|
||||
backgroundColor: "#b59200",
|
||||
data: [],
|
||||
label: "5",
|
||||
}, {
|
||||
backgroundColor: "#6b7d00",
|
||||
data: [],
|
||||
label: "6",
|
||||
}, {
|
||||
backgroundColor: "#006400",
|
||||
data: [],
|
||||
label: "7",
|
||||
}];
|
||||
const recommends = [];
|
||||
(req.app as any).cocktails.forEach((value) => {
|
||||
let found = false;
|
||||
const triedCocktails = [];
|
||||
for (const cocktail of user.cocktails) {
|
||||
if (cocktail.cocktail === value[3]) {
|
||||
data[cocktail.level].data.push({x: value[0], y: value[1], r: 4, val: value});
|
||||
found = true;
|
||||
break;
|
||||
} else {
|
||||
triedCocktails.push(cocktail);
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
let dist = 0;
|
||||
let distsum = 0;
|
||||
for (const tried of triedCocktails) {
|
||||
const idx = (req.app as any).cocktailList.indexOf(tried.cocktail);
|
||||
if (idx > -1) {
|
||||
const c = (req.app as any).cocktails[idx];
|
||||
const d = Math.abs(c[0] - value[0]) + Math.abs(c[1] - value[1]);
|
||||
if (d > 0.001) {
|
||||
dist += (tried.level) / d;
|
||||
distsum += 1 / d;
|
||||
} else {
|
||||
dist = tried.level;
|
||||
distsum = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
recommends.push([dist / distsum, value]);
|
||||
data[0].data.push({x: value[0], y: value[1], r: 3, val: value});
|
||||
}
|
||||
});
|
||||
recommends.sort((a, b) => {
|
||||
return a[0] < b[0] ? 1 : (a[0] === b[0] ? 0 : -1);
|
||||
});
|
||||
res.render("recommends", {data, title: "Ähnliche Cocktails", user, cocktails: (req.app as any).cocktails, cocktailList: (req.app as any).cocktailList, recommends});
|
||||
});
|
||||
|
||||
export = router;
|
||||
Reference in New Issue
Block a user