-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcookie.js
134 lines (125 loc) · 3.71 KB
/
cookie.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
/**
* cookie.js
*
* functions related to cookie management (parsing and creating cookies)
*/
/**
* Gets the state of the objects (position,etc.) in a scene
* @param {Number} scene_number : id of a scene
* @param {String} scene_number : cookie containing the value of the different states (from every scenes)
* @returns the part of the cookie corresponding to the scene scene_number
*/
function getStateBySceneId(scene_number, cook){
let len = cook.length-1;
let len2 = cook.length-1;
let len3 = cook.length-1;
while(len >= 0){
if(cook[len] == ":"){
len2 = len;
}
if(cook[len] == "/"){
if(cook.substring(len+1, len2) == scene_number){
return(cook.substring(len2+1, len3));
}
len3 = len;
}
len = len-1;
}
if(cook.substring(len, len2) == scene_number){
return(cook.substring(len2+1, len3));
}
return "";
}
/**
* Gets the index of the states of the objects (position,etc.) in a scene
* @param {Number} scene_number : id of the scene
* @param {String} cook : cookie containing the value of the different states (from every scenes)
* @returns : the state index of the object (for example for the gif if a frame is accepted or stopped)
*/
function getIndexStateBySceneId(scene_number, cook){
let len = cook.length-1;
let len2 = cook.length-1;
let len3 = cook.length-1;
while(len >= 0){
if(cook[len] == ":"){
len2 = len;
}
if(cook[len] == "/"){
if(cook.substring(len+1, len2) == scene_number){
return[len+1, len3+1];
}
len3 = len;
}
len = len-1;
}
if(cook.substring(len, len2) == scene_number){
return[len+1, len3+1];
}
return [cook.length, cook.length];
}
/**
* adds the state of a gif (current image) to the cookie containing all the state of the objects (from all scenes)
* @param {String} state : current state of the gif
* @param {Number} scene_number : id of the scene
* @param {*} toAdd : value to add in the cookie
*/
function addGifStateCookie(state, sceneNumber, toAdd){
const indexes = getIndexStateBySceneId(sceneNumber, state);
document.cookie = "gif_state=" + state.substring(0, indexes[0]) + toAdd + state.substring(indexes[1]);
}
/**
* Adds the state of a puzzle (position) to the cookie containing all the state of the objects (from all scenes)
* @param {String} cook : cookie containing the value of the different states (from every scenes)
* @param {Number} topPos : position of the piece from the top of the page
* @param {Number} sceneNb : id of the scene
*/
function storePuzzleInCookie(cook, topPos, sceneNb){
let indexes = getIndexStateBySceneId(sceneNb, cook);
document.cookie = "puzzle_pos=" + cook.substring(0, indexes[0]) + sceneNb + ":" + topPos + cook.substring(indexes[1]) + "/";
}
/**
* Returns the index of cookie whose name is 'cname' in cookie 'cook'
* @param {String} cname
* @param {Cookie} cook
* @returns the index of the cookie if it exists, -1 otherwise
*/
function getIndexName(cname, cook) {
var toSearch = cname + "=";
var i = 0;
var begin_chaine = 0;
while (i < cook.length) {
if (i == begin_chaine && cook[i] == " ") {
begin_chaine += 1;
}
if (cook[i] == "=") {
var str = cook.substring(begin_chaine, i + 1);
if (toSearch == str) {
return i;
}
}
if (cook[i] == ";") {
begin_chaine = i + 1;
}
i += 1;
}
return -1;
}
/**
* Get the value of the cookie whose name is 'cname'
* @param {String} cname
* @returns value of the cookie (as a String)
*/
function getCookieValue(cname) {
const cook = document.cookie;
var ind = getIndexName(cname, cook);
var i = ind;
var j = 0;
while (j == 0 && i < cook.length) {
i = i + 1;
if (cook[i] == ";") {
j = i;
}
}
j = i
return cook.substring(ind + 1, j);
}