Skip to content

Commit

Permalink
moving to promises. added energy points
Browse files Browse the repository at this point in the history
  • Loading branch information
kbingman committed Dec 20, 2014
1 parent 6d5e662 commit d64b3b4
Show file tree
Hide file tree
Showing 14 changed files with 463 additions and 365 deletions.
382 changes: 203 additions & 179 deletions public/js/bundle.js

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions public/js/bundle.min.js

Large diffs are not rendered by default.

32 changes: 18 additions & 14 deletions server.go
Expand Up @@ -22,17 +22,17 @@ var (
type Weapon struct {
Id int `json:"id"`
Name string `json:"name"`
Cost int `json:"cost"`
// CostFactor int
// EnergyRequired int
Cost float32 `json:"cost"`
EnergyPoints float32 `json:"energyPoints"`
// RequiredTechLevel int
}

type Battery struct {
Id int `json:"id"`
Name string `json:"name"`
Count int `json:"count"`
Cost int `json:"cost"`
Cost float32 `json:"cost"`
// EnergyPoints int `json:"energyPoints"`
}

type Configuration struct {
Expand All @@ -49,7 +49,9 @@ type Starship struct {
Configuration string `json:"configuration"`
Mass int64 `json:"mass"`
Thrust int64 `json:"thrust"`
Ep float32 `json:"ep"`
Reactor int64 `json:"reactor"`
Price float32 `json:"price"`
Ftl int64 `json:"ftl"`
PrimaryWeapon Weapon `json:"primaryWeapon"`
PointDefenseWeapons []Battery `json:"pointDefenseWeapons"`
Expand Down Expand Up @@ -200,22 +202,24 @@ func renderShip(w http.ResponseWriter, req *http.Request, params httprouter.Para
}

primaryWeapons := []*Weapon{
{1, "Railgun", 1},
{2, "Mass Driver", 1},
{3, "Ion Gun", 1},
{1, "Railgun", 1, 1},
{2, "Mass Driver", 1, 1},
{3, "Ion Gun", 1, 1},
}

batteryWeapons := []*Weapon{
{1, "Missle Tubes", 1},
{2, "Brilliant Pebble Launcher", 1},
{3, "Railgun", 1},
{4, "Gauss Gun", 1},
{1, "Missle Tubes", 20, 0},
{2, "Brilliant Pebble Launcher", 30, 0},
{3, "Railgun", 40, 40},
{4, "Coilgun", 50, 50},
}

pointDefenseWeapons := []*Weapon{
{2, "Projectile", 1},
{3, "Pulse Laser", 1},
{4, "Railgun", 1},
{1, "Missle", 0.75, 0},
{2, "Projectile", 0.5, 0},
{3, "Pulse Laser", 1.0, 1},
{4, "Railgun", 2.0, 2},
{5, "Coilgun", 4.0, 3},
}

context := map[string]interface{}{
Expand Down
44 changes: 20 additions & 24 deletions src/js/actions/ship_actions.js
@@ -1,7 +1,7 @@
var flight = require('../lib/flight');

// Flux
var ShipDispatcher = require('../dispatcher');
var Dispatcher = require('../dispatcher');
var ShipStore = require('../stores/ship_store');

// Mixins
Expand All @@ -11,55 +11,55 @@ var withShipXHR = require('../mixin/with_ship_xhr.js');
module.exports = flight.component(withAjax, withShipXHR, function() {

this.newShip = function(e, data) {
Dispatcher.find();
this.trigger(document, 'showNewShip')
}

this.removeShip = function(e, data) {
ShipDispatcher.delete(data);
Dispatcher.delete(data);
this.deleteShip(e, data);
this.trigger(document, 'showNewShip');
};

this.updateShip = function(e, data) {
ShipDispatcher.update(data);

this.save(ShipDispatcher.getStore('ship').currentShip);
Dispatcher.update(data);
this.save(Dispatcher.getStore('ship').currentShip);
};

this.save = flight.utils.throttle(function(ship){
if (ship.id) {
this.putShip(null, ship);
this.putShip(null, ship).then(function(r) {
Dispatcher.update(data.starship);
});
} else {
this.createShip(null, ship);
this.createShip(null, ship).then(function(data) {
Dispatcher.update(data.starship);
});
}
}, 2000);

this.editShip = function(e, data) {
ShipDispatcher.find(data);
Dispatcher.find(data);
this.trigger(document, 'displayShip');
};

this.displayShips = function(e, data) {
ShipDispatcher.reset(data);
};

this.addCreatedShip = function(e, data) {
ShipDispatcher.update(data.starship);
Dispatcher.reset(data);
};

this.increaseShipAttr = function(e, data) {
ShipDispatcher.increase(data.attr);
this.save(ShipDispatcher.getStore('ship').currentShip);
Dispatcher.increase(data.attr);
this.save(Dispatcher.getStore('ship').currentShip);
};

this.decreaseShipAttr = function(e, data) {
ShipDispatcher.decrease(data.attr);
this.save(ShipDispatcher.getStore('ship').currentShip);
Dispatcher.decrease(data.attr);
this.save(Dispatcher.getStore('ship').currentShip);
};

this.addWeapons = function(e, data) {
ShipDispatcher.addWeapons(data);
this.save(ShipDispatcher.getStore('ship').currentShip);
Dispatcher.addWeapons(data);
this.save(Dispatcher.getStore('ship').currentShip);
};

this.init = function() {
Expand All @@ -68,8 +68,8 @@ module.exports = flight.component(withAjax, withShipXHR, function() {

this.after('initialize', function() {
this.init();
this.on(document, 'displayShipInfo', this.displayShips);

this.on(document, 'displayShipInfo', this.displayShips);
this.on(document, 'getShips', this.getAllShips);
this.on(document, 'updateShip', this.updateShip);
this.on(document, 'deleteShip', this.removeShip);
Expand All @@ -79,10 +79,6 @@ module.exports = flight.component(withAjax, withShipXHR, function() {
this.on(document, 'decreaseShipAttr', this.decreaseShipAttr);
this.on(document, 'addWeapons', this.addWeapons);

// temp
this.on(document, 'addNewShipData', this.addCreatedShip);
// this.on(document, 'removeShipData', this.log);
// this.on(document, 'updateShipData', this.log);
});

});
71 changes: 40 additions & 31 deletions src/js/mixin/with_ajax.js
@@ -1,4 +1,4 @@
var promise = require('es6-promise');
require('es6-promise').polyfill();

module.exports = function withAjax() {

Expand All @@ -7,37 +7,46 @@ module.exports = function withAjax() {
});

this.request = function(options) {
var url = options.xhr.path || '';
var url = this.attr.baseUrl + options.xhr.path || '';
var method = options.xhr.method || 'GET';
var xhr = new XMLHttpRequest();
var data = options.xhr.data ? JSON.stringify(options.xhr.data) : null;

xhr.responseType = 'json';
xhr.addEventListener('readystatechange', readystatechangeHandler.bind(this), false);
xhr.upload.addEventListener('progress', progressHandler.bind(this), false);

xhr.open(method, this.attr.baseUrl + url, true);
xhr.send(data);

function readystatechangeHandler(response) {
if (xhr.readyState == 4 && xhr.status == 200) {
triggerEvent.call(this, 'done');
}
else if (xhr.readyState == 4 && xhr.status != 200) {
triggerEvent.call(this, 'fail');
}
};

function progressHandler(response) {
triggerEvent.call(this, 'progress');
};

function triggerEvent(e) {
var event = options.events[e];
if (event) {
this.trigger(event, xhr.response);
}
};
var data = options.xhr.data ? JSON.stringify(options.xhr.data) : undefined;

function request(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';

xhr.addEventListener('readystatechange', readystatechangeHandler.bind(this), false);
xhr.upload.addEventListener('progress', progressHandler.bind(this), false);

xhr.open(method, url, true);
xhr.send(data);

function readystatechangeHandler(response) {
if (xhr.readyState == 4 && xhr.status == 200) {
triggerEvent.call(this, 'done');
resolve(xhr.response);
}
else if (xhr.readyState == 4 && xhr.status != 200) {
triggerEvent.call(this, 'fail');
reject(xhr.response);
}
};

function progressHandler(response) {
triggerEvent.call(this, 'progress');
};

function triggerEvent(e) {
var event = options.events[e];
if (event) {
this.trigger(event, xhr.response);
}
};

}

return new Promise(request.bind(this));

};

}
37 changes: 37 additions & 0 deletions src/js/mixin/with_hogan.js
@@ -0,0 +1,37 @@
var flight = require('../lib/flight');

module.exports = function withFormUtils() {

this.render = function(template, data, partials) {

var renderer = function(context) {
return function(text) {
return template.c.compile(text, template.options).render(context, partials);
};
};

var utils = {
format: function() {
return function(text){
var render = renderer(this);
var num = (Math.round(parseFloat(render(text)) * 100) / 100).toFixed(2);
var digits = num.split('.')[0];
var fraction = num.split('.')[1];

return digits.replace(/\B(?=(\d{3})+(?!\d))/g, ',') + '.' + fraction;
};
},
round: function() {
return function(text){
var render = renderer(this);
var num = (Math.round(parseFloat(render(text)) * 100) / 100).toFixed(0);

return num.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};
}
};

return template.render(flight.utils.merge(utils, data), partials);
}

}
11 changes: 4 additions & 7 deletions src/js/mixin/with_ship_xhr.js
Expand Up @@ -8,7 +8,7 @@ module.exports = function() {
});

this.getAllShips = function() {
this.request({
return this.request({
'xhr': {
'path': 'ships'
},
Expand All @@ -20,7 +20,7 @@ module.exports = function() {
};

this.createShip = function(e, data) {
this.request({
return this.request({
'xhr': {
'path': 'ships',
'method': 'post',
Expand All @@ -29,14 +29,13 @@ module.exports = function() {
}
},
'events': {
'done': 'addNewShipData',
'fail': 'ajaxError'
}
});
};

this.putShip = function(e, data) {
this.request({
return this.request({
'xhr': {
'path': 'ships/' + data.id,
'method': 'put',
Expand All @@ -45,20 +44,18 @@ module.exports = function() {
}
},
'events': {
'done': 'updateShipData',
'fail': 'ajaxError'
}
});
}

this.deleteShip = function(e, data) {
this.request({
return this.request({
'xhr': {
'path': 'ships/' + data.id,
'method': 'delete'
},
'events': {
'done': 'removeShipData',
'fail': 'ajaxError'
}
});
Expand Down

0 comments on commit d64b3b4

Please sign in to comment.