-
Notifications
You must be signed in to change notification settings - Fork 2
/
storageProviderRedis.js
149 lines (124 loc) · 3.83 KB
/
storageProviderRedis.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
145
146
147
148
149
var redis = require('redis');
const __overviewKeyPrefix = "overview_";
const __indexKey = "firewireIndexKey";
// init RedisClient
var client = redis.createClient(19477, "pub-redis-19477.us-east-1.1.azure.garantiadata.com");
client.auth("firewire69", redis.print);
// WrapperHelper for DB
var getRedisValue = function(key, successCallback) {
client.get(key, function(err, res) {
if (err) {
console.log(err);
} else {
successCallback(res);
}
});
}
var readFileToIndex = function() {
};
var appendStepToGame = function(game, step) {
game.push(step);
client.set(step.id, JSON.stringify(game, null, 4), redis.print);
}
var saveStep = function(step, completedCallback) {
getRedisValue(step.id, function(gameAsJSONString) {
if (gameAsJSONString != null) {
var game = JSON.parse(gameAsJSONString);
appendStepToGame(game, step);
} else {
appendStepToGame(new Array(), 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 writeFinalStepToIndex = function(step, completedCallback) {
// load index aggregate, insert and sort
getAllSortedGames(function(sortedGameOverviews) {
sortedGameOverviews.push(step);
sortedGameOverviews = sortGames(sortedGameOverviews);
var indexAsStringArray = sortedGameOverviews.map(function(item) {
return __overviewKeyPrefix + item.id;
});
client.set(__indexKey, indexAsStringArray.join(","), redis.print);
client.set(__overviewKeyPrefix + step.id, JSON.stringify(step, null, 4), redis.print);
var index = sortedGameOverviews.indexOf(step);
step.rank = index + 1;
completedCallback(step);
});
}
var sortGames = function(games) {
var gamesSorted = games;
gamesSorted.sort(function(gameA, gameB) {
var typeCompare = gameB.type.localeCompare(gameA.type);
if (typeCompare != 0) {
return typeCompare;
} else {
return gameB.duration < gameA.duration;
}
});
return gamesSorted;
}
var getAllSortedGames = function(callback) {
getRedisValue(__indexKey, function(indexArrayAsString) {
// Check if return Value empty
if (!indexArrayAsString) {
callback(new Array());
return;
}
var indexAsStringArray = indexArrayAsString.split(",");
client.mget(indexAsStringArray, function(err, sortedGameOverviews) {
if (err) {
console.log(err);
return;
}
var gamesRanked = sortedGameOverviews.map(function(gameOverviewAsJSONString, index) {
var gameOverview = JSON.parse(gameOverviewAsJSONString);
gameOverview.rank = index + 1;
return gameOverview;
});
callback(gamesRanked);
});
});
}
var getAllGamesteps = function(gameId, callBack) {
getRedisValue(gameId, function(gameAsJSONString) {
var game = JSON.parse(gameAsJSONString);
callBack(game);
});
}
var deleteGame = function(gameId, callback) {
client.del(gameId);
client.del(__overviewKeyPrefix + gameId);
getRedisValue(__indexKey, function(indexArrayAsString) {
var indexAsStringArray = indexArrayAsString.split(",");
var index = indexAsStringArray.indexOf(__overviewKeyPrefix + gameId);
indexAsStringArray.splice(index, 1);
client.set(__indexKey, indexAsStringArray.join(","), callback);
});
};
var getSortedGameById = function(gameId, callback) {
getRedisValue(__indexKey, function(indexArrayAsString) {
var indexAsStringArray = indexArrayAsString.split(",");
var rank = indexAsStringArray.indexOf(__overviewKeyPrefix + gameId) + 1;
getRedisValue(__overviewKeyPrefix + gameId, function(gameOverviewAsJSONString) {
var gameOverview = JSON.parse(gameOverviewAsJSONString);
gameOverview.rank = rank;
callback(gameOverview);
});
});
}
module.exports = {
readFileToIndex : readFileToIndex,
deleteGame : deleteGame,
saveStep : saveStep,
getAllSortedGames : getAllSortedGames,
getAllGamesteps : getAllGamesteps,
};