Open nicksellen opened 9 years ago
/******************************************************************************
DEPENDENCIES
- Copy & Paste from: https://wzrd.in/standalone/getval
******************************************************************************/
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();
else if("function"==typeof define&&define.amd)define([],e);else{var f;
"undefined"!=typeof window?f=window:"undefined"!=typeof global?
f=global:"undefined"!=typeof self&&(f=self),f.getval=e()}}(function(){var define,module,exports;
return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;
if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");
throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){
var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;
for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
module.exports = function resolve (root, path, delimiter) {
var tmp = root, keys = (''+path).split(delimiter||'.');
try { for (idx in keys) { tmp = tmp[keys[idx]]; }
} catch (e) { return; }
return tmp;
};},{}]},{},[1])(1)});
/******************************************************************************
INITIALIZE ECONOMIC HISTORY
******************************************************************************/
var history = [];
var Tnow = 0;
var Tnext = 1;
history[Tnow] = { economy: {}, journal: [] };
history[Tnext] = { economy: {}, journal: [] };
var BALANCE = {
ASSETS : {
PROPERTY : {
TANGIBLES : { FIXED: {}, CURRENT: {} },
INTANGIBLES : { FIXED: {}, CURRENT: {} },
},
STOCKS : {},
MONEY : { CLAIMS: {}, CASH: {} }
},
OWNERSHIP : {
EQUITY : {},
LIABILITIES : {}
}
};
function actor (name) {
if (history[Tnext].economy[name]) return;
history[Tnext].economy[name] = JSON.parse(JSON.stringify(BALANCE));
}
function execute (transactions) {
function batch (trans) {
var target = getval(currentEconomy, trans[0]);
var current = target[trans[1]];
target[trans[1]] = current ? current+=trans[2] : trans[2];
console.log(trans[0]+'.'+trans[1]+ ':' + trans[2]);
}
transactions.forEach(batch);
}
var currentEconomy, currentJournal, transaction;
/******************************************************************************
INITIALIZE & EXECUTE NEXT ECONOMIC PERIOD
******************************************************************************/
// Initialize
currentEconomy = history[Tnext].economy;
currentJournal = history[Tnow].journal;
// New Persons
actor('BANK');
actor('alex');
// All Transactions
transactions = [
// alex drills a well in the desert
['alex.ASSETS.PROPERTY.TANGIBLES.FIXED','well', +1000],
['alex.OWNERSHIP.EQUITY', 'value',+1000],
// alex takes a loan, because he needs money to buy food
['alex.OWNERSHIP.LIABILITIES', 'BANK', +1000], // interest maybe 5% each year
['alex.ASSETS.MONEY.CASH', 'value',+1000],
['BANK.ASSETS.MONEY.CLAIMS', 'alex', +1000], // takes alex "WELL" as security
['BANK.OWNERSHIP.LIABILITIES','alexBankAccount',+1000]
];
execute(transactions);
// Finish Current Period
Tnow++; Tnext++;
history[Tnext] = {
economy: JSON.parse(JSON.stringify(history[Tnow].economy)),
journal: []
};
/******************************************************************************
INITIALIZE & EXECUTE NEXT ECONOMIC PERIOD
******************************************************************************/
// Initialize
currentEconomy = history[Tnext].economy;
currentJournal = history[Tnow].journal;
// New Persons
actor('BANK');
actor('alex');
actor('nick');
// All Transactions
transactions = [
// nick goes to the forrest, collects wood and builds a table
['nick.ASSETS.PROPERTY.TANGIBLES.CURRENT','table', +500],
['nick.OWNERSHIP.EQUITY', 'value', +500],
// nick sells table to alex
['nick.ASSETS.MONEY.CASH', 'value', +700],
['nick.ASSETS.PROPERTY.TANGIBLES.CURRENT','table', -500],
['nick.OWNERSHIP.EQUITY', 'value', +200],
['BANK.OWNERSHIP.LIABILITIES', 'alexBankAccount',-700],
['BANK.OWNERSHIP.LIABILITIES', 'nickBankAccount',+700],
['alex.ASSETS.MONEY.CASH', 'value', -700],
['alex.ASSETS.PROPERTY.TANGIBLES.CURRENT','table', +700]
];
execute(transactions);
// Finish Current Period
Tnow++; Tnext++;
history[Tnext] = {
economy: JSON.parse(JSON.stringify(history[Tnow].economy)),
journal: []
};
Maybe you can play arround with that and simplify the code and maybe even create modules for it for certain parts for it.
What we need to do next is:
var BALANCE
structuresIf the code above leaves a lot of questions, then lets maybe start with those first :-)
Maybe could use https://github.com/nicksellen/worldview for representing state (would allow adding a watcher for my bank balance for example) - I should probably add explicit batched transactions to it first though (it currently does batching for performance - but the underlying mechanism is the same, it's just implicit vs explicit).
It of course shows me that I do need to do a bit more "homework" before I can meaningfully contribute to the business logic. I can play code though.
Thanks for this though, it's a great start :)
It would be nice to target browser and nodejs environments too. I'm imaging a pretty web visualization one day (with some d3 in there of course).
https://github.com/serapath/economy Maybe fork and create a pull request if you think worldview makes sense, add it :-) It works in the browser and in node for now. If you add "dom stuff", we have to see :-)
Don't worry, DOM stuff can go in a separate module :) - this is just the engine we're talking about...
Currently it lacks "rules/constraints" for whether transactions are legal or not. And it lacks functionality for setting up contracts which are then automatically executed in every period.
And why?