forked from marcbaechinger/firewire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorageProvider.js
144 lines (135 loc) · 3.29 KB
/
storageProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var fs = require('fs');
// path to public storage folder
const __path = __dirname + "/public/storage";
const __indexPath = __path + "/index.json";
// array containing steps for a single given game
var steps = new Array();
// array containing a summary of final steps
var index = new Array();
var getGameFilePath = function(gameId) {
return __path + "/games/steps_" + gameId + ".json";
};
var saveIndexFile = function() {
fs.writeFile(__indexPath, JSON.stringify(index, null, 4), function(err) {
if (err) {
console.log(err);
}
});
};
// read existing file and append current step
var updateStepToFile = function(filePath, step) {
fs.readFile(filePath, function(err, data) {
if (err) {
console.log(err);
} else {
steps = JSON.parse(data);
writeStepToFile(filePath, step);
}
});
}
// add current step to steps array and write array into file
var writeStepToFile = function(filePath, step) {
steps.push(step);
fs.writeFile(filePath, JSON.stringify(steps, null, 4), function(err) {
if (err) {
console.log(err);
}
});
}
var saveStep = function(step, completedCallback) {
var filePath = getGameFilePath(step.id);
fs.exists(filePath, function(exists) {
if (exists) {
updateStepToFile(filePath, step);
} else {
steps = [];
writeStepToFile(filePath, step);
}
});
// add step to index if type is game-complete or failure
switch (step.type) {
case "game-complete":
case "failure":
writeFinalStepToIndex(step, completedCallback);
break;
default:
break;
}
}
var deleteGame = function(gameId, callback) {
var filePath = getGameFilePath(gameId);
fs.exists(filePath, function(exists) {
if (exists) {
fs.unlink(filePath, function(err) {
if (err) {
callback(err);
} else {
index.forEach(function(game, idx) {
if (game.id === gameId) {
index.splice(idx, 1);
}
});
saveIndexFile();
callback();
}
});
}
});
};
var writeFinalStepToIndex = function(step, completedCallback) {
index.push(step);
saveIndexFile();
getSortedGameById(step.id, function(game) {
completedCallback(game);
});
}
var readFileToIndex = function() {
fs.exists(__indexPath, function(exists) {
if (exists) {
fs.readFile(__indexPath, function(err, data) {
if (err) {
console.log(err);
} else {
index = JSON.parse(data);
}
});
}
});
}
var getAllSortedGames = function(callback) {
var gamesSorted = index;
gamesSorted.sort(function(gameA, gameB) {
var typeCompare = gameB.type.localeCompare(gameA.type);
if (typeCompare != 0) {
return typeCompare;
} else {
return gameB.duration < gameA.duration;
}
});
gamesRanked = gamesSorted.map(function(game, index) {
game.rank = index + 1;
return game;
});
callback(gamesRanked);
}
var getAllGamesteps = function(gameId, callback) {
var filePath = __path + "/games/steps_" + gameId + ".json";
var data = fs.readFileSync(filePath);
var gamesteps = JSON.parse(data);
callback(gamesteps);
}
var getSortedGameById = function(gameId, callback) {
getAllSortedGames(function(games) {
var game = games.filter(function (item) {
return item.id === gameId;
})[0];
callback(game);
});
}
module.exports = {
saveStep : saveStep,
deleteGame : deleteGame,
readFileToIndex : readFileToIndex,
getAllSortedGames : getAllSortedGames,
getAllGamesteps : getAllGamesteps,
};