Skip to content

Commit

Permalink
read,write json files initial
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasotocerny committed Apr 29, 2017
1 parent 5a111c0 commit 9b8a264
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 38 deletions.
9 changes: 9 additions & 0 deletions node_modules/fs/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions node_modules/fs/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"countdown": "^2.6.0",
"fs": "0.0.1-security",
"react": "^15.5.4",
"react-dom": "^15.5.4"
},
Expand Down
5 changes: 5 additions & 0 deletions src/data/answerSheet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"answers":{
"1": "0",
"2": "0",
"3": "0"
}}
5 changes: 5 additions & 0 deletions src/data/dataTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"scoreSheet":[{"color":"red","points":0,"questions":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}},
{"color":"white","points":0,"questions":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}},
{"color":"blue","points":0,"questions":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}},
{"color":"black","points":0,"questions":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}}],
"submissions":[{"color":"red","question":1,"answer":3.5,"correct":true,"time":"20:04:32","solvers":["lukas cerny"]}]}
71 changes: 33 additions & 38 deletions src/data/database.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
const scoreSheet = [
{color:"red",points:0,questions:{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}},
{color:"blue",points:0,questions:{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}},
{color:"white",points:0,questions:{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}},
{color:"black",points:0,questions:{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}}];
const fs = require('fs');

let submissionsSheet = [
{color:"red",question:"2",answer:"5.6",time:"20:32:34"},
{color:"blue",question:"1",answer:"7",time:"20:56:08"},
{color:"black",question:"1",answer:"8",time:"20:51:12"}
]
const teams = ["red", "blue", "black", "white"];

const getRandomArbitrary = (min, max) => Math.random() * (max - min) + min;

const answerSheet = {"1":"7","2":"5.6"};

const teams = ["red", "blue", "black", "white"];

let initializeScoreSheet = (n) => {
teams.map(team => {
const points = getRandomArbitrary(0,n);
const pointsTemp = points;
let sheet;
for (let i=0;i<n;i++) {
if (pointsTemp > 0) {
sheet[i] = "correct";
} else {
sheet[i] = 0;
}
let getScoreSheet = () => {
fs.readFile("scoreSheet.json", (err, data) => {
if (err) {
console.log(err);
} else {
return JSON.parse(data).scoreSheet;
}
})
}

let addSubmission = (t, q, a) => {
let date = new Date();
submissionsSheet.push({color:t,question:q,answer:a,time:date.getHours().toString().concat(date.getMinutes().toString())})
if (validateAnswer(q,a)) {
scoreSheet[t].questions[q] = "correct";
scoreSheet[t].points++;
} else {
scoreSheet[t].questions[q]++;
}
let addSubmission = (team, question, answer, solvers) => {
fs.readFile('submissionSheet.json', (err, data) => {
if (err){
console.log(err);
} else {
let submissionSheet = JSON.parse(data);
const date = new Date();
submissionSheet.submissions.push({"color":team,"question":question,"answer":answer,
"time":date.getHours().toString().concat(":",date.getMinutes().toString()),"correct":validateAnswer(question, answer)});
const json = JSON.stringify(submissionSheet);
fs.writeFile('submissionSheet.json', json, (err) => (err) ? console.log(err) : console.log("Submission recorded.")); // write it back
}
});
}

let validateAnswer = (q, a) => a==answerSheet[q];
let validateAnswer = (q, a) => {
fs.readFile('answerSheet.json', (err, data) => {
if (err) {
console.log(err);
return false;
} else {
let answerSheet = JSON.parse(data);
return answerSheet.answers[q]==a.toString();
}
})
}

module.exports = {
scoreSheet: scoreSheet
}
console.log(getScoreSheet());
4 changes: 4 additions & 0 deletions src/data/scoreSheet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"scoreSheet":{"red":{"color":"red","points":0,"questions":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}},
"white":{"color":"white","points":0,"questions":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}},
"blue":{"color":"blue","points":0,"questions":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}},
"black":{"color":"black","points":0,"questions":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0}}}}
1 change: 1 addition & 0 deletions src/data/submissionSheet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"submissions":[{"color":"red","question":1,"answer":3.5,"correct":true,"time":"20:04:32","solvers":["lukas cerny"]}]}

0 comments on commit 9b8a264

Please sign in to comment.