Example #1
0
vertx.eventBus.registerHandler('/city/bank', function (message) {

    var action = message.action;
    var farmOrStoreId = message.from;
    var factoryId = message.charge;
    var quantity = message.quantity;
    var cost = message.cost;

    if (!(factoryId && farmOrStoreId && quantity && cost && action)) {
        console.warn('Invalid message received @/city/bank ' + message);
        return;
    }

    var factory = services.factory[factoryId];
    if (!factory) {
        console.warn('Unknown factory received @/city/bank ' + factoryId);
        return;
    }

    var farmOrStore;
    if ('purchase' == action && (farmOrStore = services.farm[farmOrStoreId])) {
        factory.stocks += quantity;
        factory.purchases += cost;
        factory.maxStocks = Math.max(factory.maxStocks, factory.stocks);
        farmOrStore.quantity -= quantity;
        farmOrStore.sales += cost;
    } else if ('sale' == action && (farmOrStore = services.store[farmOrStoreId])) {
        factory.stocks -= quantity;
        factory.sales += cost;
        factory.minStocks = Math.min(factory.minStocks, factory.stocks);
        farmOrStore.quantity += quantity;
        farmOrStore.purchases += cost;
    } else {
        console.warn('Unknown action, farm or store received @/city/bank ' + message);
        return;
    }

    // send purchase or sale infos to factory
    vertx.eventBus.send('/city/factory/' + factoryId, {
        action: action,
        from: conf.id,
        quantity: quantity,
        cost: cost
    });
});
Example #2
0
vertx.eventBus.registerHandler('/city', function (message) {

    var action = message.action;
    var team = message.team;
    var type = message.type;
    var id = message.from;

    container.logger.info(JSON.stringify(message));

    if ('hello' == action && team && type && id) {

        if (!services[type]) {
            // initialize object for type of service
            services[type] = {};
        }

        if (!services[type][id]) {
            // initialize object for service
            services[type][id] = {
                id: id,
                type: type,
                alive: true,
                team: team,
                purchases: 0,
                sales: 0,
                costs: 0,
                stocks: 0,
                maxStocks: 0,
                minStocks: 0
            };
        }

        // update lastAlive time flag
        services[type][id].lastAlive = new Date().getTime();

    } else if ('inventory' == action && id) {

        // on demand send inventory to a specific monitor
        vertx.eventBus.send('/city/monitor/' + id, {
            action: action,
            from: conf.id,
            services: services
        });

    } else {
        console.warn('Unknown message received @/city ' + message);
    }
});