forked from plainblack/Lacuna-Web-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildingShipyard.js
340 lines (300 loc) · 15.3 KB
/
buildingShipyard.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
331
332
333
334
335
336
337
338
339
340
YAHOO.namespace("lacuna.buildings");
if (typeof YAHOO.lacuna.buildings.Shipyard == "undefined" || !YAHOO.lacuna.buildings.Shipyard) {
(function(){
var Lang = YAHOO.lang,
Util = YAHOO.util,
Dom = Util.Dom,
Event = Util.Event,
Sel = Util.Selector,
Lacuna = YAHOO.lacuna,
Game = Lacuna.Game,
Lib = Lacuna.Library;
var Shipyard = function(result){
Shipyard.superclass.constructor.call(this, result);
this.service = Game.Services.Buildings.Shipyard;
};
Lang.extend(Shipyard, Lacuna.buildings.Building, {
getChildTabs : function() {
return [this._getQueueTab(), this._getBuildTab()];
},
_getQueueTab : function() {
var div = document.createElement("div");
div.innerHTML = ['<div>You may subsidize the build queue for 1 <img src="',Lib.AssetUrl,'ui/s/essentia.png" class="smallEssentia" /> per ship. <button type="button" class="shipQueueSubsidize">Subsidize</button> </div>',
'<ul class="shipQueue shipQueueHeader clearafter"><li class="shipQueueType">Type</li><li class="shipQueueEach">Time To Complete</li></ul>',
'<div id="qHt" style="overflow:auto;"><div id="shipsBuilding"></div></div>'].join('');
Event.on(Sel.query(".shipQueueSubsidize",div,true), "click", this.SubsidizeBuildQueue, this, true);
var queueTab = new YAHOO.widget.Tab({ label: "Build Queue", contentEl:div });
queueTab.subscribe("activeChange", function(e) {
if(e.newValue) {
this.getQueue();
var Ht = Game.GetSize().h - 210;
if(Ht > 300) { Ht = 300; }
Dom.setStyle(Dom.get('qHt'),'height',Ht + 'px');
}
}, this, true);
this.queueTab = queueTab;
return queueTab;
},
_getBuildTab : function() {
var buildTab = new YAHOO.widget.Tab({ label: "Build Ships", content: [
'<div>',
' <div class="clearafter" style="font-weight:bold;">',
' <span id="shipDocksAvailable" style="float:left;"></span>',
' <span style="float:right;"><select id="shipBuildView"><option value="All">All</option><option value="Now" selected="selected">Now</option><option value="Later">Later</option></select></span>',
' </div>',
' <div id="shipBuildMessage" class="error"></div>',
' <div id="bHt" style="overflow:auto;margin-top:2px;border-top:1px solid #52acff;">',
' <ul id="shipDetails">',
' </ul>',
' </div>',
'</div>'
].join('')});
buildTab.subscribe("activeChange", function(e) {
if(e.newValue) {
this.getBuild();
var Ht = Game.GetSize().h - 190;
if(Ht > 300) { Ht = 300; }
Dom.setStyle(Dom.get('bHt'),'height',Ht + 'px');
}
}, this, true);
Event.on("shipBuildView", "change", this.ShipPopulate, this, true);
this.buildTab = buildTab;
return buildTab;
},
getBuild : function() {
if(!this.ships) {
Lacuna.Pulser.Show();
this.service.get_buildable({session_id:Game.GetSession(),building_id:this.building.id}, {
success : function(o){
YAHOO.log(o, "info", "Shipyard.getBuild.get_buildable.success");
Lacuna.Pulser.Hide();
this.rpcSuccess(o);
this.ships = {
buildable: o.result.buildable,
docks_available: o.result.docks_available
};
this.SetDocksAvailableMessage();
this.ShipPopulate();
},
scope:this
});
}
else {
this.ShipPopulate();
}
},
getQueue : function() {
if(!this.ship_build_queue) {
Lacuna.Pulser.Show();
this.service.view_build_queue({session_id:Game.GetSession(),building_id:this.building.id,page_number:1}, {
success : function(o){
YAHOO.log(o, "info", "Shipyard.getQueue.view_build_queue.success");
Lacuna.Pulser.Hide();
this.rpcSuccess(o);
this.ship_build_queue = o.result;
this.ShipyardDisplay();
},
scope:this
});
}
else {
this.ShipyardDisplay();
}
},
ShipyardDisplay : function() {
var bq = this.ship_build_queue,
div = Dom.get("shipsBuilding");
if(div) {
var divParent = div.parentNode,
ul = document.createElement("ul"),
li = document.createElement("li"),
serverTime = Lib.getTime(Game.ServerData.time);
this.resetQueue();
div = divParent.removeChild(div);
div.innerHTML = "";
/*= {
number_of_ships_building: o.result.number_of_ships_building,
ships_building: o.result.ships_building
};*/
if(bq && bq.ships_building && bq.ships_building.length > 0) {
for(var i=0; i<bq.ships_building.length; i++) {
var bqo = bq.ships_building[i],
nUl = ul.cloneNode(false),
nLi = li.cloneNode(false),
ncs = (Lib.getTime(bqo.date_completed) - serverTime) / 1000;
nUl.Build = bqo;
Dom.addClass(nUl, "shipQueue");
Dom.addClass(nUl, "clearafter");
Dom.addClass(nLi,"shipQueueType");
nLi.innerHTML = bqo.type_human;
nUl.appendChild(nLi);
nLi = li.cloneNode(false);
Dom.addClass(nLi,"shipQueueEach");
nLi.innerHTML = Lib.formatTime(ncs);
nUl.appendChild(nLi);
div.appendChild(nUl);
this.addQueue(ncs, this.ShipyardQueue, nUl);
}
}
//add child back in
divParent.appendChild(div);
}
},
ShipyardQueue : function(remaining, elLine){
var compTime;
if(remaining <= 0) {
compTime = 'Overdue ' + Lib.formatTime(Math.round(-remaining));
}
else {
compTime = Lib.formatTime(Math.round(remaining));
}
Sel.query("li.shipQueueEach",elLine,true).innerHTML = compTime;
},
SubsidizeBuildQueue : function() {
Lacuna.Pulser.Show();
this.service.subsidize_build_queue({
session_id:Game.GetSession(),
building_id:this.building.id
}, {
success : function(o){
YAHOO.log(o, "info", "Shipyard.SubsidizeBuildQueue.success");
Lacuna.Pulser.Hide();
this.rpcSuccess(o);
this.ship_build_queue = undefined;
this.ShipyardDisplay();
},
scope:this
});
},
SetBuildMessage : function(message) {
var msg = Dom.get("shipBuildMessage");
if(msg) {
msg.innerHTML = message;
Lib.fadeOutElm("shipBuildMessage");
}
},
SetDocksAvailableMessage : function() {
var sda = Dom.get("shipDocksAvailable");
if(sda) {
if(this.ships.docks_available) {
sda.innerHTML = 'There are ' + this.ships.docks_available + ' docks available for new ships.';
}
else {
sda.innerHTML = 'You have no docks available. Do you still have a Space Port?';
}
}
},
ShipPopulate : function() {
var details = Dom.get("shipDetails");
if(details) {
var ships = this.ships.buildable,
li = document.createElement("li"),
shipNames = [],
filter = Lib.getSelectedOptionValue("shipBuildView"),
defReason = !this.ships.docks_available ? "No docks available at Space Port." : undefined;
Event.purgeElement(details);
details.innerHTML = "";
for(var shipType in ships) {
if(ships.hasOwnProperty(shipType)) {
if(filter == "All") {
shipNames.push(shipType);
}
else if(filter == "Now" && ships[shipType].can) {
shipNames.push(shipType);
}
else if(filter == "Later" && !ships[shipType].can) {
shipNames.push(shipType);
}
}
}
shipNames.sort();
for(var i=0; i<shipNames.length; i++) {
var shipName = shipNames[i],
ship = ships[shipName],
nLi = li.cloneNode(false),
reason="", attributes = [];
if(ship.reason) {
reason = '<div style="font-style:italic;">'+ship.reason[1]+'</div>';
//reason = '<div style="font-style:italic;">'+Lib.parseReason(ship.reason, defReason)+'</div>';
}
for(var a in ship.attributes) {
attributes[attributes.length] = '<span style="white-space:nowrap;margin-left:5px;"><label style="font-style:italic">';
attributes[attributes.length] = a.titleCaps('_',' ');
attributes[attributes.length] = ': </label>';
attributes[attributes.length] = ship.attributes[a];
attributes[attributes.length] = '</span> ';
}
nLi.innerHTML = ['<div class="yui-gb" style="margin-bottom:2px;">',
' <div class="yui-u first" style="width:15%;background:transparent url(',Lib.AssetUrl,'star_system/field.png) no-repeat center;text-align:center;">',
' <img src="',Lib.AssetUrl,'ships/',shipName,'.png" style="width:100px;height:100px;" class="shipImage" />',
' </div>',
' <div class="yui-u" style="width:63%">',
' <span class="shipName">',ship.type_human,'</span>: ',
' <div class="shipDesc" style="display:none;">',Game.GetShipDesc(shipName),'</div>',
' <div><label style="font-weight:bold;">Cost:</label>',
' <span style="white-space:nowrap;"><img src="',Lib.AssetUrl,'ui/s/food.png" title="Food" class="smallFood" />',ship.cost.food,'</span>',
' <span style="white-space:nowrap;"><img src="',Lib.AssetUrl,'ui/s/ore.png" title="Ore" class="smallOre" />',ship.cost.ore,'</span>',
' <span style="white-space:nowrap;"><img src="',Lib.AssetUrl,'ui/s/water.png" title="Water" class="smallWater" />',ship.cost.water,'</span>',
' <span style="white-space:nowrap;"><img src="',Lib.AssetUrl,'ui/s/energy.png" title="Energy" class="smallEnergy" />',ship.cost.energy,'</span>',
' <span style="white-space:nowrap;"><img src="',Lib.AssetUrl,'ui/s/waste.png" title="Waste" class="smallWaste" />',ship.cost.waste,'</span>',
' <span style="white-space:nowrap;"><img src="',Lib.AssetUrl,'ui/s/time.png" title="Time" class="smallTime" />',Lib.formatTime(ship.cost.seconds),'</span>',
' </div>',
' <div><label style="font-weight:bold;">Attributes:</label>',attributes.join(''),'</div>',
!ship.can ? reason : '',
' </div>',
' <div class="yui-u" style="width:18%">',
ship.can ? ' <input type="text" style="width:25px;" id="ship_'+shipName+'" value="1"> <button type="button">Build</button>' :
' </div>',
'</div>'].join('');
if(ship.can) {
Event.on(Sel.query("button", nLi, true), "click", this.ShipBuild, {Self:this,Type:shipName,Ship:ship}, true);
}
details.appendChild(nLi);
}
Event.delegate(details, "click", this.ShipExpandDesc, ".shipName");
Event.delegate(details, "click", this.ShipExpandDesc, ".shipImage");
}
},
ShipExpandDesc : function(e, matchedEl, container) {
var desc = Sel.query('div.shipDesc', matchedEl.parentNode.parentNode, true);
if(desc) {
var dis = Dom.getStyle(desc, "display");
Dom.setStyle(desc, "display", dis == "none" ? "" : "none");
}
},
ShipBuild : function(e) {
var btn = Event.getTarget(e);
btn.disabled = true;
Lacuna.Pulser.Show();
var qty = Dom.get("ship_"+this.Type);
this.Self.service.build_ship({
session_id:Game.GetSession(),
building_id:this.Self.building.id,
type:this.Type,
quantity:qty.value
}, {
success : function(o){
btn.disabled = false;
Lacuna.Pulser.Hide();
this.Self.rpcSuccess(o);
this.Self.ship_build_queue = o.result;
this.Self.ShipyardDisplay();
this.Self.ships.docks_available--;
if(this.Self.ships.docks_available < 0) {
this.Self.ships.docks_available = 0;
}
this.Self.SetDocksAvailableMessage();
this.Self.SetBuildMessage("Successfully started building " + this.Ship.type_human + ".");
},
failure : function(o){
btn.disabled = false;
},
scope:this
});
}
});
YAHOO.lacuna.buildings.Shipyard = Shipyard;
})();
YAHOO.register("shipyard", YAHOO.lacuna.buildings.Shipyard, {version: "1", build: "0"});
}
// vim: noet:ts=4:sw=4