forked from plainblack/Lacuna-Web-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildingBlackHoleGenerator.js
330 lines (290 loc) · 11.6 KB
/
buildingBlackHoleGenerator.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
YAHOO.namespace("lacuna.buildings");
if (typeof YAHOO.lacuna.buildings.BlackHoleGenerator == "undefined" ||
!YAHOO.lacuna.buildings.BlackHoleGenerator) {
(function(){
var Lang = YAHOO.lang,
Util = YAHOO.util,
Dom = Util.Dom,
Event = Util.Event,
Sel = Util.Selector,
Pager = YAHOO.widget.Paginator,
Lacuna = YAHOO.lacuna,
Game = Lacuna.Game,
Lib = Lacuna.Library;
var BlackHoleGenerator = function(result){
BlackHoleGenerator.superclass.constructor.call(this, result);
this.service = Game.Services.Buildings.BlackHoleGenerator;
};
Lang.extend(BlackHoleGenerator, Lacuna.buildings.Building, {
getChildTabs : function() {
return [this._getBHGTab()];
},
_getBHGTab : function() {
this.tab = new YAHOO.widget.Tab({ label: "Singularity", content: [
'<div id="bhg">',
' Target <select id="bhgTargetType">',
' <option value="body_name">Body Name</option>',
' <option value="body_id">Body Id</option>',
' <option value="xy">X,Y</option>',
' </select>',
' <span id="bhgTargetSelectText"><input type="text" id="bhgTargetText" /></span>',
' <span id="bhgTargetSelectXY" style="display:none;">',
' X:<input size="5" type="text" id="bhgTargetX" />',
' Y:<input size="5" type="text" id="bhgTargetY" />',
' </span>',
' <button type="button" id="bhgGetActions">Get Actions</button>',
' <div id="bhgTaskInfo"></div>',
' <div id="bhgActions" style="display:none;border-top:1px solid #52ACFF;margin-top:5px;padding-top:5px">',
' Singularity Target: <span id="bhgTargetNote"></span><span id="bhgTargetDist"></span>',
' <div style="border-top:1px solid #52ACFF;margin-top:5px;">',
' <ul id="bhgActionsAvail"></ul>',
' </div>',
' <div style="border-top:1px solid #52ACFF;margin-top:5px;">',
' <ul id="bhgResult"></ul>',
'</div>'
].join('')});
Event.on("bhgTargetType", "change", function(){
if(Lib.getSelectedOptionValue(this) == "xy") {
Dom.setStyle("bhgTargetSelectText", "display", "none");
Dom.setStyle("bhgTargetSelectXY", "display", "");
}
else {
Dom.setStyle("bhgTargetSelectText", "display", "");
Dom.setStyle("bhgTargetSelectXY", "display", "none");
}
});
Event.on("bhgGetActions", "click", this.bhgGetActions, this, true);
return this.tab;
},
bhgGetActions : function() {
Lacuna.Pulser.Show();
Dom.setStyle("bhgActions", "display", "none");
var type = Lib.getSelectedOptionValue("bhgTargetType"),
target = {};
if(type == "xy") {
target.x = Dom.get("bhgTargetX").value;
target.y = Dom.get("bhgTargetY").value;
Dom.get("bhgTargetNote").innerHTML = ['X: ', target.x, ', Y: ', target.y].join('');
}
else {
target[type] = Dom.get("bhgTargetText").value;
Dom.get("bhgTargetNote").innerHTML = target[type];
}
this.service.get_actions_for({
session_id:Game.GetSession(),
building_id:this.building.id,
target:target
}, {
success : function(o){
Lacuna.Pulser.Hide();
this.rpcSuccess(o);
this.PopulateBHGTab(target, o.result.tasks);
},
scope:this
});
},
PopulateBHGTab : function(target, actions) {
var details = Dom.get("bhgActionsAvail"),
detailsParent = details.parentNode,
li = document.createElement("li");
Event.purgeElement(details, true); //clear any events before we remove
details = detailsParent.removeChild(details); //remove from DOM to make this faster
details.innerHTML = "";
Dom.setStyle("bhgActions", "display", "");
Dom.setStyle(detailsParent, "display", "");
Dom.setStyle( Dom.get("bhgResult").parentNode, "display", "none");
if(actions.length === 0) {
details.innerHTML = "No available actions for singularity.";
}
else {
Dom.get("bhgTargetDist").innerHTML = [
' ; Dist: ', actions[0].dist, ' Range: ', actions[0].range].join('');
for(var i=0; i<actions.length; i++) {
var task = actions[i],
nLi = li.cloneNode(false);
var waste_out;
if (task.waste_cost < 1000000000) {
waste_out = [ Lib.formatNumber(task.waste_cost/1000000), 'M' ].join('');
}
else {
waste_out = [ Lib.formatNumber(task.waste_cost/1000000000), 'B' ].join('');
}
var canGenerate = 1;
if ( Game.GetCurrentPlanet().waste_stored < task.waste_cost ) {
canGenerate = 0;
}
else if ( task.success == 0 ) {
continue;
}
var typeSelector = "";
if ( task.name === "Change Type" ) {
typeSelector = '<select id="bhgChangeTypeSelect"><option value="">New Planet Type</option>';
for (var j=1; j<=20; j++) {
typeSelector = typeSelector + [
'<option value="', j, '">', j, '</option>'
].join('');
}
typeSelector = typeSelector + '</select>';
}
nLi.Task = task;
nLi.innerHTML = [
'<div class="yui-gd" style="margin-bottom:2px; border: 1px white solid; padding: 2px">',
' <div class="yui-u first" style="width:70%">',
' <label style="font-weight:bold;">',task.name,'</label>',
' <div>',
' Base Chance: ',100-task.base_fail,'%,',
' Success Chance: ',task.success,'%,<br/>',
' Waste Needed: ',waste_out,
' Recovery Time: ',Lib.formatTime(task.recovery),
' </div>',
' </div>',
' <div class="yui-u" style="width:25%; text-align:right;">',
canGenerate == 1
? typeSelector + '<button type="button">Generate</button>'
: '<b>Insufficient Waste</b>',
' </div>',
'</div>'].join('');
details.appendChild(nLi);
if ( task.success > 1 ) {
Event.on(Sel.query("button", nLi, true),
"click",
this.bhgGenerate,
{Self:this, Target:target, Task:task, building_id: this.building_id},
true);
}
}
}
detailsParent.appendChild(details); //add back as child
//wait for tab to display first
setTimeout(function() {
var Ht = Game.GetSize().h - 250;
if(Ht > 250) { Ht = 250; }
Dom.setStyle(detailsParent,"height",Ht + "px");
Dom.setStyle(detailsParent,"overflow-y","auto");
},10);
return this.tab;
},
bhgGenerate : function(e) {
var oSelf = this.Self,
target = this.Target,
task = this.Task;
if (target) {
var serviceParams = {
session_id:Game.GetSession(),
building_id:oSelf.building.id,
target:target,
task_name:task.name
};
if (task.name === "Change Type") {
var selectValue = Lib.getSelectedOptionValue("bhgChangeTypeSelect");
if ( selectValue == "" ) {
alert("Please select New Planet Type");
return;
}
serviceParams.planet_type = {
newtype: selectValue
};
}
this.Self.service.generate_singularity(
serviceParams,
{success : function(o){
Lacuna.Pulser.Hide();
this.Self.rpcSuccess(o);
this.Self.PopulateBHGResult(target, o.result.effect);
},
scope:this
});
}
},
PopulateBHGResult : function(target, effect) {
var details = Dom.get("bhgResult"),
detailsParent = details.parentNode,
li = document.createElement("li");
Event.purgeElement(details, true); //clear any events before we remove
details = detailsParent.removeChild(details); //remove from DOM to make this faster
details.innerHTML = "";
Dom.setStyle( Dom.get("bhgActionsAvail").parentNode, "display", "none");
Dom.setStyle(detailsParent, "display", "");
detailsParent.appendChild(details); //add back as child
if (effect.fail) {
var nLi = li.cloneNode(false);
nLi.innerHTML = [ '<div class="yui-gd" style="margin-bottom:2px;">',
' <div style="border:1px white solid;" class="yui-u" style="width:100%">',
' <label style="font-weight:bold;">Failure</label>',
' <div>',effect.fail.message,' at ',effect.fail.name,'</div>',
' </div></div>',
].join('');
details.appendChild(nLi);
}
else {
// success
if (effect.target) {
var nLi = li.cloneNode(false);
nLi.innerHTML = this.bhgParseResult(effect.target, 'Success');
details.appendChild(nLi);
}
if (effect.side) {
var nLi = li.cloneNode(false);
nLi.innerHTML = this.bhgParseResult(effect.side, 'Side-Effect');
details.appendChild(nLi);
}
}
//wait for tab to display first
setTimeout(function() {
var Ht = Game.GetSize().h - 250;
if(Ht > 250) { Ht = 250; }
Dom.setStyle(detailsParent,"height",Ht + "px");
Dom.setStyle(detailsParent,"overflow-y","auto");
},10);
},
bhgParseResult : function(result, type) {
var out = [ '<div class="yui-gd" style="margin-bottom:2px;">',
' <div style="border:1px white solid;" class="yui-u" style="width:100%">',
' <label style="font-weight:bold;">',type,'</label>',
' <div>'
].join('');
if ( result.message === "Swapped Places" ) {
out = out + [
result.message, ' with ', result.swapname,
' at orbit ', result.orbit
].join('');
}
else if ( result.message === "Changed Size" ) {
out = out + [
result.name, ' changed size from ', result.old_size, ' to ', result.size
].join('');
}
else if ( result.message === "Changed Type" ) {
var newtype = result['class'].replace( new RegExp(".*::", "g"), "" );
out = out + [
result.name, ' changed to type ', newtype, ' planet'
].join('');
}
else if ( result.message === "Made Asteroid" ) {
var newtype = result['class'].replace( new RegExp(".*::", "g"), "" );
out = out + [
result.name, ' is now a type ', newtype, ' asteroid of size ',
result.size
].join('');
}
else if ( result.message === "Made Planet" ) {
var newtype = result['class'].replace( new RegExp(".*::", "g"), "" );
out = out + [
result.name, ' is now a type ', newtype, ' planet of size ',
result.size
].join('');
}
else {
out = out + [
result.message, ' at ', result.name
].join('');
}
out = out + ' </div></div></div>';
return out;
}
});
YAHOO.lacuna.buildings.BlackHoleGenerator = BlackHoleGenerator;
})();
YAHOO.register("blackholegenerator", YAHOO.lacuna.buildings.BlackHoleGenerator, {version: "1", build: "0"});
}
// vim: noet:ts=4:sw=4