Skip to content

Commit

Permalink
Some CC and add Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
GMartigny committed May 23, 2017
1 parent 4d556d2 commit 98b2867
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/css/events.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import (reference) "base";

#eventList {
.eventList {
position: absolute;
bottom: 0;
padding: 5px;
Expand Down
2 changes: 1 addition & 1 deletion src/css/logs.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import (reference) "base";

#logs {
.logs {
position: absolute;
top: @resourceListHeight;
left: 50%;
Expand Down
4 changes: 2 additions & 2 deletions src/css/people.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import (reference) "base";

#peopleList {
.peopleList {
position: absolute;
left: 0;
top: @resourceListHeight;
Expand All @@ -9,7 +9,7 @@

width: 50%;
height: @firstPartHeight;
overflow: visible auto;
overflow: auto;

.People {
border: 1px solid #666;
Expand Down
2 changes: 1 addition & 1 deletion src/css/resources.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import (reference) "base";

#resourceList {
.resourceList {
padding: 0 5px;
background-color: #b57052;
height: @resourceListHeight;
Expand Down
2 changes: 1 addition & 1 deletion src/css/visualPane.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import (reference) "base";

#visualPane {
.visualPane {
position: absolute;
display: flex;
align-items: center;
Expand Down
28 changes: 28 additions & 0 deletions src/js/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
if (!IS_DEV) {
(function (i, s, o, g, r, a, m) {
i.GoogleAnalyticsObject = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments);
};
i[r].l = +new Date();
a = s.createElement(o);
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m);
})(window, document, "script", "https://www.google-analytics.com/analytics.js", "ga");

ga("create", "UA-99773946-1", "auto");
ga("send", "pageview");
}
/**
* Send an event hit to Analytics
* @param {String} category - The type of event
* @param {String} label - Some infos about the event
* @param {Number} [value] - Any value
*/
function sendEvent (category, label, value) {
if (ga) {
ga("send", "event", "Game", category, label, value);
}
}
16 changes: 8 additions & 8 deletions src/js/managers/data_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ var DataManager = (function () {
];
},
asset: "forum+1",
log: ""
log: "It'll be nice to have someone else helping."
},
furnace: {
name: "furnace",
Expand Down Expand Up @@ -491,11 +491,11 @@ var DataManager = (function () {
];
},
asset: "forum+2",
log: ""
log: "Another room for someone to join. So far, "
},
plot1: {
name: "field",
desc: "",
desc: "A larger crop field to produce more food.",
time: 10,
consume: function () {
return [
Expand Down Expand Up @@ -549,7 +549,7 @@ var DataManager = (function () {
];
},
asset: "forum+3",
log: ""
log: "All the forum space is now used for sleeping place."
},
workshop: {
name: "workshop",
Expand Down Expand Up @@ -874,10 +874,9 @@ var DataManager = (function () {
},
giveSpan: [7, 10],
give: function (action, option, effect) {
var location = option;
// remember it for log
effect.location = location;
var give = randomizeMultiple(location.give(), action.data.giveSpan);
effect.location = option;
var give = randomizeMultiple(option.give(), action.data.giveSpan);
var quartz = data.resources.gatherables.special.quartz;
if (random() < quartz.dropRate) {
give.push([1, quartz]);
Expand Down Expand Up @@ -1088,13 +1087,14 @@ var DataManager = (function () {
time: 12,
energy: 30,
isOut: 1,
unique: true,
consume: function () {
return [
[10, data.resources.gatherables.uncommon.oil]
];
},
give: function () {
MessageBus.notify(MessageBus.MSG_TYPES.WIN);
MessageBus.notify(MessageBus.MSG_TYPES.WIN, this.getSettledTime());
return [];
},
log: function () {
Expand Down
9 changes: 8 additions & 1 deletion src/js/managers/log_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ var LogManager = (function () {
})

.observe(MessageBus.MSG_TYPES.LOOSE, function (survivalDuration) {
var message = "We held up for " + survivalDuration + ", but all is lost now.";
sendEvent("death", "survival duration", survivalDuration);
var message = "We held up for " + formatTime(survivalDuration) + ", but all is lost now.";
self.log(message, self.LOG_TYPES.FLAVOR);
})

Expand All @@ -50,6 +51,12 @@ var LogManager = (function () {
.observe(MessageBus.MSG_TYPES.GAIN_PERK, function (people) {
var message = people.name + " is now known as the \"" + capitalize(people.perk.name) + "\".";
self.log(message, self.LOG_TYPES.EVENT);
})

.observe(MessageBus.MSG_TYPES.WIN, function (survivalDuration) {
sendEvent("win", "survival duration", survivalDuration);
var message = "It took " + formatTime(survivalDuration) + " to escape.";
self.log(message, self.LOG_TYPES.FLAVOR);
});
},
/**
Expand Down
176 changes: 108 additions & 68 deletions src/js/models/action.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
"use strict";

/**
* @typedef {Object} ActionEffect
* @param {String} name - Action's name
* @param {People} people - Action's owner
* @param {String} [give] - Resources given by the action
* @param {String} [build] - Name of the build building (prefix with "a" or "an")
* @param [*] - Can carry any other data put by action's function
*/

/**
* Class for actions
* @extends Model
* @param {People} owner - THe action owner
* @param {ActionData} data - The action data
* @param {People} owner - Action's owner
* @param {ActionData} data - Action's data
* @param {Action} [parentAction] - If this action has a parent
* @constructor
*/
Expand Down Expand Up @@ -197,86 +207,34 @@ Action.extends(Model, "Action", /** @lends Action.prototype */ {
people: this.owner
};

// Build
if (isFunction(this.data.build)) {
var build = this.data.build(this, option, effect);
effect.build = an(build.name);
}
var result = this.resolveAction(effect, option);

// Give
var give = [];
if (isFunction(this.data.give)) {
give = this.data.give(this, option, effect);
}
// Add from constructed building
if (build && isFunction(build.give)) {
give = give.concat(build.give(this, option, effect));
}
give = compactResources(give);
if (give.length) {
MessageBus.notify(MessageBus.MSG_TYPES.GIVE, give);
effect.give = formatArray(give);
if (result.give.length) {
MessageBus.notify(MessageBus.MSG_TYPES.GIVE, result.give);
}

// Unlock
var unlockForOne = [];
var unlockForAll = [];
if (isFunction(this.data.unlock)) {
var unlock = this.data.unlock(this, option, effect).filter(function (action) {
return !action.condition || (action.condition && action.condition(this));
}.bind(this));

// Unique actions have to unlock for everyone
if (this.data.unique) {
unlockForAll = unlock;
}
else {
unlockForOne = unlock;
}
}
// Add from constructed building
if (build && isFunction(build.unlock)) {
unlockForAll = unlockForAll.concat(build.unlock(this, option, effect));
}
if (unlockForAll.length) {
if (result.unlock.forAll.length) {
// add to all
MessageBus.notify(MessageBus.MSG_TYPES.UNLOCK, unlockForAll);
MessageBus.notify(MessageBus.MSG_TYPES.UNLOCK, result.unlock.forAll);
}
if (unlockForOne.length) {
if (result.unlock.forOne.length) {
// add to owner
this.owner.addAction(unlockForOne);
this.owner.addAction(result.unlock.forOne);
}

// Lock
var lockForOne = [];
var lockForAll = [];
if (isFunction(this.data.lock)) {
var lock = this.data.lock(this, option, effect);

// Unique actions have to lock for everyone
if (this.data.unique) {
lockForAll = lock;
}
else {
lockForOne = lock;
}
}
if (this.data.unique) {
lockForAll.push(this.data.id);
}
// Add from constructed building
if (build && isFunction(build.lock)) {
lockForAll = lockForAll.concat(build.lock(this, option, effect));
}
if (lockForAll.length) {
MessageBus.notify(MessageBus.MSG_TYPES.LOCK, lockForAll);
if (result.lock.forAll.length) {
MessageBus.notify(MessageBus.MSG_TYPES.LOCK, result.lock.forAll);
}
if (lockForOne.length) {
this.owner.lockAction(lockForOne);
if (result.lock.forOne.length) {
this.owner.lockAction(result.lock.forOne);
}

if (build) {
MessageBus.notify(MessageBus.MSG_TYPES.BUILD, build);
// Build
if (result.build) {
MessageBus.notify(MessageBus.MSG_TYPES.BUILD, result.build);
}

// Log
Expand All @@ -290,8 +248,90 @@ Action.extends(Model, "Action", /** @lends Action.prototype */ {
}
var log = LogManager.personify(rawLog, effect);
MessageBus.notify(effect.logType || MessageBus.MSG_TYPES.LOGS.INFO, capitalize(log));

this.owner.finishAction();
},
/**
* Resolve all function of this action
* @param {ActionEffect} effect - An editable object carrying effect for log
* @param {CraftableData|BuildingData} [option] - The chosen option
* @return {{give: Array, unlock: {forAll: Array, forOne: Array}, lock: {forAll: Array, forOne: Array}}}
*/
resolveAction: function (effect, option) {
var result = {
give: [],
unlock: {
forAll: [],
forOne: []
},
lock: {
forAll: [],
forOne: []
}
};
var data = this.data;

// Give
if (isFunction(data.give)) {
result.give = data.give(this, option, effect) || [];
}

// Unlock
if (isFunction(data.unlock)) {
var unlock = data.unlock(this, option, effect).filter(function (action) {
return !action.condition || (action.condition && action.condition(this));
}.bind(this));

// Unique actions have to unlock for everyone
if (data.unique) {
result.unlock.forAll = unlock;
}
else {
result.unlock.forOne = unlock;
}
}

// Lock
if (isFunction(data.lock)) {
var lock = data.lock(this, option, effect);

// Unique actions have to lock for everyone
if (data.unique) {
result.lock.forAll = lock;
}
else {
result.lock.forOne = lock;
}
}
// Unique action lock itself
if (data.unique) {
result.lock.forAll.push(data.id);
}

// Build
if (isFunction(data.build)) {
result.build = data.build(this, option, effect);
if (result.build) {
effect.build = an(result.build.name);

// Add from building
if (isFunction(result.build.give)) {
result.give = result.give.concat(result.build.give(this, option, effect));
}
if (isFunction(result.build.unlock)) {
result.unlock.forAll = result.unlock.forAll.concat(result.build.unlock(this, option, effect));
}
if (isFunction(result.build.lock)) {
result.lock.forAll = result.lock.forAll.concat(result.build.lock(this, option, effect));
}
}
}

result.give = compactResources(result.give);
effect.give = formatArray(result.give);

return result;
},
/**
* Change the action according to an effect
* @param {Function} effect -
Expand Down
Loading

0 comments on commit 98b2867

Please sign in to comment.