Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.02 KB | None | 0 0
  1. const db = require("sqlite-sync");
  2.  
  3. var dir = "./routes/db/userdata.db";
  4.  
  5. function verify(attemptPassword, actual){
  6. if(attemptPassword == actual){
  7. return true;
  8. }
  9. return false;
  10. }
  11.  
  12. function addUser(username, password){
  13. db.connect(dir);
  14.  
  15. let user = [username, password, 50];
  16.  
  17.  
  18. let sql = `SELECT * FROM users WHERE username = ?`
  19.  
  20. var output = true;
  21. db.run(sql, [username], (res) => {
  22. if (res.error) {
  23. throw res.error;
  24. }
  25.  
  26. if(res.length > 0){
  27. output = false;
  28. }
  29. });
  30.  
  31. if(output == false){
  32. return false;
  33. }
  34.  
  35.  
  36.  
  37. sql = `INSERT INTO users(username, pass, avg_score) VALUES(?, ?, ?)`;
  38.  
  39. console.log("running");
  40. db.run(sql, user, function(res) {
  41. if (res.error) {
  42. return console.error(res.error);
  43. }
  44. console.log(`A user has been created with id ${res}`);
  45. });
  46.  
  47. db.close();
  48.  
  49. return output;
  50. }
  51.  
  52. function userInDB(username, password){
  53. db.connect(dir);
  54.  
  55. let sql = 'SELECT DISTINCT pass pass FROM users WHERE username = ?';
  56.  
  57. var output = true;
  58.  
  59. db.run(sql, [username], (res) => {
  60. if (res.error) {
  61. throw res.error;
  62. }
  63.  
  64. if(res.length > 0){
  65. output = verify(password, res[0].pass);
  66. }else{
  67. output = false;
  68. }
  69. });
  70.  
  71.  
  72. db.close();
  73. return output;
  74. }
  75.  
  76. function getUserScore(username) {
  77. db.connect(dir);
  78.  
  79. let sql = `SELECT avg_score FROM users
  80. WHERE username = ?`;
  81.  
  82. var output;
  83.  
  84. db.run(sql, [username], (res) => {
  85. if(res.error) {
  86. throw res.error;
  87. }
  88. output = res;
  89. });
  90.  
  91. db.close();
  92. return output;
  93. }
  94.  
  95. function getUser(username){
  96. db.connect(dir);
  97.  
  98. let sql = 'SELECT * FROM users WHERE username=?';
  99.  
  100. var output;
  101.  
  102. db.run(sql, [username], (res) => {
  103. if (res.error) {
  104. throw res.error;
  105. }
  106. output = res;
  107. });
  108.  
  109. db.close();
  110. return output;
  111. }
  112.  
  113. function getAllUsers(){
  114. db.connect(dir);
  115.  
  116. let sql = 'SELECT username FROM users';
  117.  
  118. var output;
  119.  
  120. db.run(sql, [], (res) => {
  121. if (res.error) {
  122. throw res.error;
  123. }
  124. output = res;
  125. });
  126.  
  127. db.close();
  128. return output;
  129. }
  130.  
  131. function getTracks(){
  132. db.connect(dir);
  133.  
  134. let sql = 'SELECT * FROM tracks';
  135.  
  136. var output;
  137.  
  138. db.run(sql, [], (res) => {
  139. if (res.error) {
  140. throw res.error;
  141. }
  142. output = res;
  143. });
  144.  
  145.  
  146. db.close();
  147. return output;
  148. }
  149.  
  150. function getTrackNames() {
  151. db.connect(dir);
  152.  
  153. let sql = `SELECT track_id, name, desc FROM tracks
  154. ORDER BY name`;
  155.  
  156. var output;
  157.  
  158. db.run(sql, [], (res) => {
  159. if (res.error) {
  160. throw res.error;
  161. }
  162. output = res;
  163. });
  164.  
  165. db.close();
  166.  
  167. return output;
  168. }
  169.  
  170. function getGoal(goalID) {
  171. db.connect(dir);
  172.  
  173. let sql = `SELECT * FROM goals WHERE goal_id = ?`;
  174.  
  175. var output;
  176.  
  177. db.run(sql, [goalID], (res) => {
  178. if (res.error) {
  179. throw res.error;
  180. }
  181. output = res;
  182. });
  183.  
  184. db.close();
  185. return output;
  186. }
  187.  
  188. function getGoalsForTrack(trackID) {
  189. db.connect(dir);
  190.  
  191. let sql = `SELECT goal_id, name, desc, max_num_per_week FROM goals
  192. WHERE track_id = ?
  193. ORDER BY name`;
  194.  
  195. var output;
  196.  
  197. db.run(sql, [trackID], (res) => {
  198. if(res.error) {
  199. throw res.error;
  200. }
  201. output = res;
  202. });
  203.  
  204. db.close();
  205. return output;
  206. }
  207.  
  208. function getWeeklyScore(username, dateStart) {
  209. db.connect(dir);
  210.  
  211. dateStart = Math.floor(dateStart/604800000);
  212.  
  213. let sql = `SELECT * FROM weeklyScore
  214. WHERE username = ?
  215. AND date_start = ?`;
  216.  
  217. var output;
  218.  
  219. db.run(sql, [username, dateStart], (res) => {
  220. if(res.error) {
  221. throw res.error;
  222. }
  223. output = res;
  224. });
  225.  
  226. db.close();
  227. return output;
  228. }
  229.  
  230. function getTrackScore(username, dateStart, trackID) {
  231. db.connect(dir);
  232.  
  233. dateStart = Math.floor(dateStart/604800000);
  234.  
  235. let sql = `SELECT * FROM trackScores
  236. WHERE track_id = ?
  237. AND week_id = (SELECT week_id FROM weeklyScore
  238. WHERE username = ?
  239. AND date_start = ?)`;
  240.  
  241. var output;
  242.  
  243. db.run(sql, [trackID, username, dateStart], (res) => {
  244. if(res.error) {
  245. throw res.error;
  246. }
  247. output = res;
  248. });
  249.  
  250. db.close();
  251. return output;
  252. }
  253.  
  254. function getGoalScore(username, dateStart, trackID, goalID) {
  255. db.connect(dir);
  256.  
  257. dateStart = Math.floor(dateStart/604800000);
  258.  
  259. let sql = `SELECT * FROM goalScores
  260. WHERE goal_id = ?
  261. AND track_score_id = (SELECT track_score_id FROM trackScores
  262. WHERE track_id = ?
  263. AND week_id = (SELECT week_id FROM weeklyScore
  264. WHERE username = ?
  265. AND date_start = ?))`;
  266.  
  267. var output;
  268.  
  269. db.run(sql, [goalID, trackID, username, dateStart], (res) => {
  270. if(res.error) {
  271. throw res.error;
  272. }
  273. output = res;
  274. });
  275.  
  276. db.close();
  277. return output;
  278. }
  279.  
  280. function getNextID(table) {
  281. db.connect(dir);
  282.  
  283. let sql = `SELECT * FROM ?`;
  284.  
  285. var output;
  286.  
  287. db.run(sql, [table], (res) => {
  288. if(res.error) {
  289. //throw res.error;
  290. }
  291. output = res.length;
  292. });
  293.  
  294. db.close();
  295. return output;
  296. }
  297.  
  298. function getNumGoals(trackID) {
  299. db.connect(dir);
  300.  
  301. let sql = `SELECT goal_id FROM goals
  302. WHERE track_id = ?`;
  303.  
  304. var output;
  305.  
  306. db.run(sql, [trackID], (res) => {
  307. if(res.error) {
  308. throw res.error;
  309. }
  310. output = res.length;
  311. });
  312.  
  313. db.close();
  314. return output;
  315. }
  316.  
  317. function setGoalScores(trackID, trackScoreID, numGoals, nextGoalID) {
  318. for(var i=1; i<=numGoals; i++) {
  319. db.connect(dir);
  320.  
  321. let sql = `SELECT * FROM goalScores`;
  322.  
  323. var nextID;
  324.  
  325. db.run(sql, [], (res) => {
  326. if(res.error) {
  327. throw res.error;
  328. }
  329. nextID = res.length;
  330. });
  331.  
  332. sql = `INSERT INTO goalScores(goal_score_id, track_score_id, goal_id, goal_score, num_this_week)
  333. VALUES(?, ?, ?, ?, ?)`;
  334.  
  335. let newGoal = [nextID, trackScoreID, nextGoalID+i, 0, 0];
  336.  
  337. db.run(sql, newGoal, (res) => {
  338. if(res.error) {
  339. throw res.error;
  340. }
  341. });
  342.  
  343. db.close();
  344. }
  345. }
  346.  
  347. function setTrackScores(weekID) {
  348. var nextGoalID = 0;
  349. for (var i=1; i<=8; i++){
  350. db.connect(dir);
  351.  
  352. let sql = `SELECT * FROM trackScores`;
  353.  
  354. var nextID;
  355.  
  356. db.run(sql, [], (res) => {
  357. if(res.error) {
  358. throw res.error;
  359. }
  360. nextID = res.length;
  361. });
  362.  
  363. sql = `INSERT INTO trackScores(track_score_id, week_id, track_id, track_score)
  364. VALUES(?, ?, ?, ?)`;
  365.  
  366. let newTrack = [nextID, weekID, i, 0];
  367.  
  368. db.run(sql, newTrack, (res) => {
  369. if(res.error) {
  370. throw res.error;
  371. }
  372. });
  373.  
  374. db.close();
  375.  
  376. var numGoals = getNumGoals(i);
  377. setGoalScores(i, nextID, numGoals, nextGoalID);
  378. nextGoalID += numGoals;
  379. }
  380. }
  381.  
  382. function setWeeklyScore(username, date) {
  383. db.connect(dir);
  384.  
  385. date = Math.floor(date/604800000);
  386.  
  387. let sql = `SELECT * FROM weeklyScore`;
  388.  
  389. var nextID;
  390.  
  391. db.run(sql, [], (res) => {
  392. if(res.error) {
  393. throw res.error;
  394. }
  395. nextID = res.length;
  396. });
  397.  
  398. sql = `INSERT INTO weeklyScore(week_id, username, date_start, total_score)
  399. VALUES(?, ?, ?, ?)`;
  400.  
  401. let newWeek = [nextID, username, date, 0];
  402.  
  403. db.run(sql, newWeek, (res) => {
  404. if(res.error) {
  405. throw res.error;
  406. }
  407. });
  408.  
  409. db.close();
  410.  
  411. setTrackScores(nextID);
  412. console.log(`A new week has been started for ${username} starting at ${date}`);
  413. }
  414.  
  415. function updateWeeklyScore(weekID, increment) {
  416. db.connect(dir);
  417.  
  418. let sql = `SELECT total_score FROM weeklyScore
  419. WHERE week_id = ?`;
  420.  
  421. var newScore;
  422.  
  423. db.run(sql, [weekID], (res) => {
  424. if (res.error) {
  425. throw res.error;
  426. }
  427. newScore = res[0].total_score + increment;
  428. })
  429.  
  430. sql = `UPDATE weeklyScore
  431. SET total_score = ?
  432. WHERE week_id = ?`;
  433.  
  434. db.run(sql, [newScore, weekID], (res) => {
  435. if (res.error) {
  436. throw res.error;
  437. }
  438. });
  439.  
  440. db.close();
  441. }
  442.  
  443. function updateTrackScore(trackScoreID, increment) {
  444. db.connect(dir);
  445.  
  446. let sql = `SELECT tracks.threshold, tracks.weight, trackScores.track_score, trackScores.week_id
  447. FROM tracks, trackScores
  448. WHERE trackScores.track_score_id = ?
  449. AND tracks.track_id = (SELECT trackScores.track_id FROM trackScores
  450. WHERE trackScores.track_score_id = ?)`;
  451.  
  452. var threshold;
  453. var weight;
  454. var newScore;
  455. var weekID;
  456.  
  457. db.run(sql, [trackScoreID, trackScoreID], (res) => {
  458. if(res.error) {
  459. throw res.error;
  460. }
  461. threshold = res[0].threshold;
  462. weight = res[0].weight;
  463. newScore = res[0].track_score + increment;
  464. weekID = res[0].week_id;
  465. if (newScore > threshold) {
  466. newScore = threshold;
  467. }
  468. });
  469.  
  470. sql = `UPDATE trackScores
  471. SET track_score = ?
  472. WHERE track_score_id = ?`;
  473.  
  474. db.run(sql, [newScore, trackScoreID], (res) => {
  475. if(res.error) {
  476. throw res.error;
  477. }
  478. });
  479.  
  480. db.close();
  481.  
  482. var scoreForward = ((newScore/threshold)*weight);
  483.  
  484. updateWeeklyScore(weekID, scoreForward);
  485. }
  486.  
  487. function updateGoalScore(username, date, trackID, goalID) {
  488. db.connect(dir);
  489.  
  490. date = Math.floor(date/604800000);
  491.  
  492. let sql = `SELECT goal_score_id FROM goalScores
  493. WHERE goal_id = ?
  494. AND track_score_id = (SELECT track_score_id FROM trackScores
  495. WHERE track_id = ?
  496. AND week_id = (SELECT week_id FROM weeklyScore
  497. WHERE username = ?
  498. AND date_start = ?))`;
  499.  
  500. var goalScoreID;
  501.  
  502. db.run(sql, [goalID, trackID, username, date], (res) => {
  503. if(res.error) {
  504. throw res.error;
  505. }
  506. console.log(res);
  507. goalScoreID = res[0].goal_score_id;
  508. });
  509.  
  510.  
  511.  
  512. sql = `SELECT goals.max_num_per_week, goalScores.num_this_week
  513. FROM goals, goalScores
  514. WHERE goalScores.goal_score_id = ?
  515. AND goals.goal_id = (SELECT goalScores.goal_id FROM goalScores
  516. WHERE goalScores.goal_score_id = ?)`;
  517.  
  518. var output = true;
  519. var currentNum;
  520.  
  521. db.run(sql, [goalScoreID, goalScoreID], (res) => {
  522. if(res.error) {
  523. throw res.error;
  524. }
  525.  
  526. currentNum = res[0].num_this_week;
  527. if (res[0].num_this_week >= res[0].max_num_per_week) {
  528. output = false;
  529. }
  530. });
  531.  
  532. if (output) {
  533. sql = `SELECT weight FROM goals
  534. WHERE goal_id = (SELECT goal_id FROM goalScores
  535. WHERE goal_score_id = ?)`;
  536.  
  537. var weight;
  538.  
  539. db.run(sql, [goalScoreID], (res) => {
  540. if(res.error) {
  541. throw res.error;
  542. }
  543. weight = res[0].weight;
  544. });
  545.  
  546. sql = `SELECT goal_score FROM goalScores
  547. WHERE goal_score_id = ?`;
  548.  
  549. var currentScore;
  550.  
  551. db.run(sql, [goalScoreID], (res) => {
  552. if(res.error) {
  553. throw res.error;
  554. }
  555. currentScore = res[0].goal_score;
  556. });
  557.  
  558. sql = `UPDATE goalScores
  559. SET goal_score = ?,
  560. num_this_week = ?
  561. WHERE
  562. goal_score_id = ?`;
  563.  
  564. db.run(sql, [currentScore+weight, currentNum+1, goalScoreID], (res) => {
  565. if(res.error) {
  566. throw res.error;
  567. }
  568. });
  569.  
  570. sql = `SELECT track_score_id FROM goalScores
  571. WHERE goal_score_id = ?`;
  572.  
  573. var trackScoreID;
  574.  
  575. db.run(sql, [goalScoreID], (res) => {
  576. if(res.error) {
  577. throw res.error;
  578. }
  579. trackScoreID = res[0].track_score_id;
  580. });
  581.  
  582. db.close();
  583.  
  584. updateTrackScore(trackScoreID, weight);
  585. } else {
  586. db.close();
  587. }
  588.  
  589. }
  590.  
  591. function updateUserScore(username, date) {
  592. db.connect(dir);
  593.  
  594. date = Math.floor(date/604800000);
  595.  
  596. let sql = `SELECT total_score FROM weeklyScore
  597. WHERE username = ?
  598. AND date_start = ?`;
  599.  
  600. var weekScore;
  601.  
  602. db.run(sql, [username, date], (res) => {
  603. if(res.error) {
  604. throw res.error;
  605. }
  606. console.log(res);
  607. console.log(username);
  608. weekScore = res[0].total_score;
  609. });
  610.  
  611. sql = `UPDATE users
  612. SET avg_score = ?
  613. WHERE username = ?`;
  614.  
  615. db.run(sql, [weekScore, username], (res) => {
  616. if(res.error) {
  617. throw res.error;
  618. }
  619. });
  620.  
  621. db.close();
  622. }
  623.  
  624. function getGoalHist(username, goalId) {
  625. db.connect(dir);
  626.  
  627. let sql = `SELECT date_start FROM weeklyScore
  628. WHERE username = ?
  629. ORDER BY date_start`;
  630.  
  631. var dates;
  632.  
  633. db.run(sql, [username], (res) => {
  634. if(res.error) {
  635. throw res.error;
  636. }
  637. dates = res;
  638. });
  639.  
  640. sql = `SELECT num_this_week FROM goalScores
  641. WHERE goal_id = ?
  642. AND track_score_id = (SELECT track_score_id FROM trackScores
  643. WHERE track_id = (SELECT track_id FROM goals
  644. WHERE goal_id = ?)
  645. AND week_id = (SELECT week_id FROM weeklyScore
  646. WHERE username = ?
  647. AND date_start = ?))`;
  648.  
  649.  
  650. var output;
  651. output = JSON.parse('{}');
  652. output.x = [];
  653. output.y = [];
  654.  
  655.  
  656. for (var i = 0; i < dates.length; i++) {
  657. db.run(sql, [goalId, goalId, username, dates[i].date_start], (res) => {
  658. if(res.error) {
  659. throw res.error;
  660. }
  661.  
  662. output.x.push(dates[i].date_start);
  663. output.y.push(res[0].num_this_week);
  664. });
  665.  
  666. }
  667.  
  668.  
  669.  
  670. db.close();
  671. return output;
  672. }
  673.  
  674. function getTrackFromGoal(goalId) {
  675. db.connect(dir);
  676.  
  677. let sql = `SELECT track_id FROM goals
  678. WHERE goal_id = ?`;
  679.  
  680. var output;
  681.  
  682. db.run(sql, [goalId], (res) => {
  683. if(res.error) {
  684. throw res.error;
  685. }
  686. output = res[0].track_id;
  687. });
  688.  
  689. db.close();
  690. return output;
  691. }
  692.  
  693. function newGoal(goalId, trackId, name, weight, desc, maxNum){
  694. console.log("lol nope");
  695. }
  696.  
  697. function avgTrackScore(username, trackID) {
  698. db.connect(dir);
  699.  
  700. let sql = `SELECT week_id FROM weeklyScore
  701. WHERE username = ?`;
  702.  
  703. var weeks;
  704. var numWeeks;
  705.  
  706. db.run(sql, [username], (res) => {
  707. if(run.error) {
  708. throw run.error;
  709. }
  710. weeks = res;
  711. numWeeks = res.length;
  712. });
  713.  
  714. sql = `SELECT track_score FROM trackScores
  715. WHERE week_id = ?
  716. AND track_id = ?`;
  717.  
  718. var totalScore = 0;
  719.  
  720. for (var i=0; i<numWeeks; i++) {
  721. db.run(sql, [weeks[i].week_id, trackID], (res) => {
  722. if(res.error) {
  723. throw res.error;
  724. }
  725. totalScore += res[0].track_score;
  726. });
  727. }
  728.  
  729. var output = Math.round(totalScore/numWeeks);
  730.  
  731. return output;
  732. }
  733.  
  734.  
  735.  
  736. module.exports.addUser = addUser;
  737. module.exports.userInDB = userInDB;
  738. module.exports.getAllUsers = getAllUsers;
  739. module.exports.getTracks = getTracks;
  740. module.exports.getTrackNames = getTrackNames;
  741. module.exports.getGoal = getGoal;
  742. module.exports.getGoalsForTrack = getGoalsForTrack;
  743. module.exports.getWeeklyScore = getWeeklyScore;
  744. module.exports.getTrackScore = getTrackScore;
  745. module.exports.getGoalScore = getGoalScore;
  746. module.exports.setWeeklyScore = setWeeklyScore;
  747. module.exports.updateGoalScore = updateGoalScore;
  748. module.exports.updateUserScore = updateUserScore;
  749. module.exports.getUser = getUser;
  750. module.exports.getTrackFromGoal = getTrackFromGoal;
  751. module.exports.getGoalHist = getGoalHist;
  752. module.exports.avgTrackScore = avgTrackScore;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement