This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
214 lines (160 loc) · 6.46 KB
/
data.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* This file is an API that will be used between Firebase and Front-end */
// private API
(function (){
var stored_data, firebase;
// PRIVATE
// FB
// insert a snapshot into a table
function fb_add_data(table, snapshot){
stored_data[table].push(snapshot.val());
}
// PRIVATE
// get the next identity for a table
function get_next_identity(table){
var i, list = stored_data[table];
var max_pk = 0;
for(i = 0; i < list.length; i++){
max_pk = Math.max(max_pk, list[i].pk);
}
return max_pk + 1;
}
// PRIVATE
// get the pk of the name
function get_table_fk(table, table_value){
var i;
for(i = 0; i < stored_data[table].length; i++){
if(stored_data[table][i].name.toLowerCase() == table_value.toLowerCase()){
return stored_data[table][i].pk;
}
}
return -1;
}
// PUBLIC
// initialise the connection
// db_name: the name of your DB
function data___init(db_name){
if(!db_name){
throw new Exception('You don\'t have a DB!');
return;
}
stored_data = {
student : [],
university : [],
category : [],
winner : [],
score : []
};
// our base firebase URL
var base_firebase = 'https://luminous-torch-1139.firebaseio.com/';
// our DB access string
firebase = base_firebase + db_name + '/';
new Firebase(firebase + 'student' ).on("child_added", function(snapshot){ fb_add_data('student', snapshot); });
new Firebase(firebase + 'university').on("child_added", function(snapshot){ fb_add_data('university', snapshot); });
new Firebase(firebase + 'category' ).on("child_added", function(snapshot){ fb_add_data('category', snapshot); });
new Firebase(firebase + 'winner' ).on("child_added", function(snapshot){ fb_add_data('winner', snapshot); });
new Firebase(firebase + 'score' ).on("child_added", function(snapshot){ fb_add_data('score', snapshot); });
}
// PRIVATE
function replace_name_for_fk(entry){
var stored_table;
// go through the list of table
for(stored_table in stored_data){
if(stored_data.hasOwnProperty(stored_table)){
// check if our entry has a property with is the name of another table
if(entry[stored_table + '_name'] !== undefined){
// get the pk linked to the name
var table_value = entry[stored_table + '_name'];
var table_fk = get_table_fk(stored_table, table_value);
if(table_fk == -1){
console.error("Could not save entry because missing table value.",{
table: stored_table,
incorrect_value: table_value,
entry: entry
});
continue;
}
delete entry[stored_table + '_name'];
entry[stored_table + '_fk'] = table_fk;
}
}
}
return entry;
}
// PUBLIC
// Saves a list of data in a certain format
// table: name of the table you want to insert into
// i.e: student, university, etc
// list: array of js object
// return bool if successful
function data___save(table, list){
if(!stored_data[table]){
return false;
}
var data_ref = new Firebase(firebase + table);
// go through the list
for(var i = 0; i < list.length; i++){
// get the next pk
list[i].pk = get_next_identity(table);
list[i] = replace_name_for_fk(list[i]);
// save our entry
data_ref.push(list[i]);
}
return true;
}
// PUBLIC
// create a callback when university get points
// it will call the function with the new total for the uni
// callback : function callback when points are added
// return an object
function data___init_university_total(callback){
new Firebase(firebase + 'score' ).on("child_added", function(snapshot){
var new_score = snapshot.val();
var score_list = stored_data.score;
var uni_details = {
university_fk : new_score.university_fk,
total : 0
}
uni_details.university_name = data___get_entry('university', uni_details.university_fk).name;
for(var i = 0; i < score_list.length; i++){
if(score_list[i].university_fk == new_score.university_fk){
uni_details.total += score_list[i].point;
}
}
callback.call(window, uni_details);
});
}
// PUBLIC
// return an entry with a certain pk
// table: name of table you want to get the entry fromCharCode
// pk : entry you want to get
// return an object
function data___get_entry(table, pk){
var list = stored_data[table];
for(var i = 0; i < list.length; i++){
if(list[i].pk == pk){
return list[i];
}
}
console.error("Could not retrieve " + table + " with pk " + pk);
return null;
}
// PUBLIC
// return the list of a certain table
// table: name of the table you wanna get
// return an array of object
function data___get_list(table){
return stored_data[table];
}
// PUBLIC
// get the whole DB
function data___get_everything(){
return stored_data;
}
window.data = {};
window.data.init = data___init;
window.data.init_university_total = data___init_university_total;
window.data.save = data___save;
window.data.get_list = data___get_list;
window.data.get_entry = data___get_entry;
window.data.get_everything = data___get_everything;
})();