forked from WhatPumpkin/Sburb-Legacy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrigger.js
208 lines (179 loc) · 5.03 KB
/
Trigger.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
var Sburb = (function(Sburb){
/////////////////////////////////////////
//Trigger Class
/////////////////////////////////////////
//constructor
Sburb.Trigger = function(info,action,followUp,restart,detonate,operator){
//console.log("Trigger constructor with: "+info, info);
if(typeof info == "string"){
info = [info];
}
this.info = info;
this.followUp = followUp?followUp:null;
this.action = action?action:null;
this.restart = restart?restart:false;
this.detonate = detonate?detonate:false;
this.operator = operator?operator.toUpperCase():"AND";
this.events = [];
for(var i=0;i<info.length;i++){
var inf = this.info[i].trim();
var params = inf.split(",");
var type = params[0];
//console.log("parsed trigger args: "+type+"("+inf+")");
this.events[i] = new Sburb.events[type](inf);
}
this.reset();
}
//parse the trigger info into an actual event to watch
Sburb.Trigger.prototype.reset = function(){
for(var i=0; i<this.events.length; i++){
this.events[i].reset();
}
}
Sburb.Trigger.prototype.checkCompletion = function() {
return this["operator"+this.operator]();
}
//check if the trigger has been satisfied
Sburb.Trigger.prototype.tryToTrigger = function(){
if(this.checkCompletion()){
if(this.action){
Sburb.performAction(this.action);
}
if(this.followUp){
if(this.followUp.tryToTrigger()){
this.followUp = null;
}
}
if(this.restart){
reset();
}
return this.detonate;
}
}
//Serialize the Trigger to XML
Sburb.Trigger.prototype.serialize = function(output){
output = output.concat("\n<trigger"+
(this.restart?" restart='true'":"")+
(this.detonate?" detonate='true'":"")+
(this.operator?" operator='"+this.operator+"'":"")+
">");
for(var i=0;i<this.info.length;i++){
if(this.events[i].serialize) {
output = output.concat("<args>"+escape(this.events[i].serialize())+"</args>");
} else {
output = output.concat("<args>"+escape(this.info[i])+"</args>");
}
}
if(this.action){
output = this.action.serialize(output);
}
if(this.followUp){
output = this.followUp.serialize(output);
}
output = output.concat("\n</trigger>");
return output;
}
Sburb.Trigger.prototype.operatorAND = function(){
var result = true;
for(var i=0;i<this.events.length;i++){
result = result && this.events[i].checkCompletion();
}
return result;
}
Sburb.Trigger.prototype.operatorOR = function(){
var result = false;
for(var i=0;i<this.events.length;i++){
result = result || this.events[i].checkCompletion();
}
return result;
}
Sburb.Trigger.prototype.operatorXOR = function(){
var result = false;
for(var i=0;i<this.events.length;i++){
if(this.events[i].checkCompletion()){
if(result){
return false; //*EXCLUSIVE* OR!
}else{
result = true;
}
}
}
return result;
}
Sburb.Trigger.prototype.operatorNAND = Sburb.Trigger.prototype.operatorNOT = function(){
return !this.operatorAND();
}
Sburb.Trigger.prototype.operatorNOR = function(){
return !this.operatorOR();
}
////////////////////////////////////////on
//Related Utility Functions
////////////////////////////////////////
//Parse a Trigger from XML
Sburb.parseTrigger = function(triggerNode){
var firstTrigger = null;
var oldTrigger = null;
do{
var attributes = triggerNode.attributes;
var info = getNodeText(triggerNode);
for(var i=0;i<info.length;i++){
info[i] = unescape(info[i]);
}
var actions = triggerNode.getElementsByTagName("action");
var action = null;
var restart = false;
var detonate = false;
var operator = null;
if(actions.length>0 && actions[0].parentNode==triggerNode){
action = Sburb.parseAction(actions[0]);
}
restart = attributes.getNamedItem("restart")?attributes.getNamedItem("restart").value=="true":restart;
detonate = attributes.getNamedItem("detonate")?attributes.getNamedItem("detonate").value=="true":detonate;
operator = attributes.getNamedItem("operator")?attributes.getNamedItem("operator").value:operator;
var trigger = new Sburb.Trigger(info,action,null,restart,detonate,operator);
if(!firstTrigger){
firstTrigger = trigger;
}
if(oldTrigger){
oldTrigger.followUp = trigger;
}
oldTrigger = trigger;
var triggerNodes = triggerNode.getElementsByTagName("trigger");
if(triggerNodes){
triggerNode = triggerNodes[0];
}else{
break;
}
}while(triggerNode)
return firstTrigger;
}
function getNodeText(xmlNode){
if(!xmlNode) return [];
var outputs = [];
for(var i=0;i<xmlNode.childNodes.length;i++){
var child = xmlNode.childNodes[i];
if(child.tagName=="args"){
for(var k=0;k<child.childNodes.length;k++){
if(child.childNodes[k].firstChild){
serializer = new XMLSerializer();
var output = "";
for(var j=0; j<child.childNodes.length; j++){
output += serializer.serializeToString(child.childNodes[j]);
}
outputs.push(output);
}
}
if(typeof(child.textContent) != "undefined"){
outputs.push(child.textContent);
}else{
outputs.push(child.firstChild.nodeValue);
}
}
}
if(outputs.length==0){
outputs.push(xmlNode.firstChild.nodeValue);
}
return outputs;
}
return Sburb;
})(Sburb || {});