-
Notifications
You must be signed in to change notification settings - Fork 1
/
weRuleIO.js
138 lines (126 loc) · 5.32 KB
/
weRuleIO.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
/****************************************************************************/
//this script is specifically designed to read from the message display div
//in the DOM, called "userConsole".
//it provides a backup system for this sloppy method of reading data sent
//accross the p2p network, by re-reading the entire contents of the userConsole
//and checking for {json} content that has not been evaluated.
/****************************************************************************/
var funkyUserConsoleCache = (function(){
//this cache is only for strings with objects that have the property "legislation"
var _contents = [];
function _get(text){
return (_contents.indexOf(text) > -1)? true : false;
};
function _set(text){
_contents.push(text);
};
return {
cachable: function(text){
return (text.indexOf("legislation") > -1)? true : false;
},
get: function(text){
if(this.cachable(text)){
console.log("just checked a get in funky")
return _get(text);
}else{
return false;
}
},
set: function(text){
if(this.cachable(text)){
if(this.get(text) === false){
_set(text);
return true;
}else{
return false;
}
}else{
return false;
}
},
checkDomForStrays: function(){
console.log("checkDomForStrays()")
return new Promise(function(resolve,reject){
var i = 0;
var j = [];
var msg;
while(userConsole.children[i] !== undefined){
msg = userConsole.children[i].innerHTML.substring(userConsole.children[i].innerHTML.indexOf(": ") + 2);
console.log("mesage from children",msg);
if(funkyUserConsoleCache.cachable(msg)){
if(funkyUserConsoleCache.get(msg) === false){
funkyUserConsoleCache.set(msg);
j[i] = processStrayLine(msg);
}
}
i++;
}
resolve(j);
});
}
}
}());
var processStrayLine = function(line){
var obj;
return new Promise(function(resolve,reject){
if(obj = IsValidLegislationJSONString(line)){
// funkyUserConsoleCache.set(line);///this is only done when action is taken
// console.log("object is valid",obj)
//now have access to last peer message and can process as JSON object.
//this should always spring an error when this client sees his own message.
// so, first discard the action if the uid is the same as this user.
if(obj.uid != undefined && obj.hash != undefined && obj.legislation == "1"){
if(obj.uid === window.clientId){
console.log("user sees his own action and does nothing")
///this action was already registered prior to being seen by this function
resolve(false);
return;
}
///new legislation
if(!Legislate.getItemByHash(obj.hash) && obj.vote == undefined){
Legislate.newLegislation(obj.proposer,obj.hash,obj.law);
funkyUserConsoleCache.set(line);
console.log("New legislation received")
resolve(false);
return;
}
//vote from voter
if(obj.vote !== undefined && Legislate.getItemByHash(obj.hash)){
Legislate.receiveVote(obj);
funkyUserConsoleCache.set(line);
console.log("vote received from peer")
resolve(false);
return;
}
if(obj.receipt && Legislate.getItemByHash(obj.hash)){
console.log("receipt received for registered legislation")
resolve(false);
return;
}
console.log("message object was related to legislation, but not handled",obj)
resolve(false);
return;
//new legislation, or,
//vote
}else{
console.log("message received as legislation, but without correct object structure",obj)
//no uid, so exit function
resolve(false);
return;
}
}
console.log("string passed as legislation was not a valid string");
resolve(false);
})
}
var getLastPeerMessage = function(){
var m = userConsole.lastElementChild.innerHTML;
m = m.substring(m.indexOf(": ") + 2);
window.processStrayLine(m).then((x)=>{
return funkyUserConsoleCache.checkDomForStrays();
});
};
/***************************************************/
///heres whats starts all the peer receiving action:
userConsole.onscroll = getLastPeerMessage;
// userConsole.onscroll = funkyUserConsoleCache.checkDomForStrays();