2014-12-31 00:00:00 +00:00
|
|
|
module SpaceTac.Game {
|
2015-01-07 00:00:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Log of a battle
|
|
|
|
// This keeps track of all events in a battle
|
|
|
|
// It also allows to register a callback to receive these events
|
|
|
|
export class BattleLog {
|
|
|
|
// Full list of battle events
|
2014-12-31 00:00:00 +00:00
|
|
|
events: BaseLogEvent[];
|
2014-12-31 00:00:00 +00:00
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// List of subscribers
|
|
|
|
private subscribers: Function[];
|
|
|
|
|
2015-01-22 00:00:00 +00:00
|
|
|
// List of event codes to ignore
|
|
|
|
private filters: string[];
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Create an initially empty log
|
|
|
|
constructor() {
|
|
|
|
this.events = [];
|
2014-12-31 00:00:00 +00:00
|
|
|
this.subscribers = [];
|
2015-01-22 00:00:00 +00:00
|
|
|
this.filters = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the stored events
|
|
|
|
clear(): void {
|
|
|
|
this.events = [];
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a battle event to the log
|
2015-01-22 00:00:00 +00:00
|
|
|
add(event: BaseLogEvent): void {
|
|
|
|
// Apply filters
|
|
|
|
var filtered = false;
|
|
|
|
this.filters.forEach((code: string) => {
|
|
|
|
if (event.code === code) {
|
|
|
|
filtered = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (filtered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
this.events.push(event);
|
2014-12-31 00:00:00 +00:00
|
|
|
|
2015-01-07 00:00:00 +00:00
|
|
|
this.subscribers.forEach((subscriber: Function) => {
|
2014-12-31 00:00:00 +00:00
|
|
|
subscriber(event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-22 00:00:00 +00:00
|
|
|
// Filter out a type of event
|
|
|
|
addFilter(event_code: string): void {
|
|
|
|
this.filters.push(event_code);
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Subscribe a callback to receive further events
|
2014-12-31 00:00:00 +00:00
|
|
|
subscribe(callback: (event: BaseLogEvent) => void): Function {
|
2014-12-31 00:00:00 +00:00
|
|
|
this.subscribers.push(callback);
|
|
|
|
return callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsubscribe a callback
|
|
|
|
// Pass the value returned by 'subscribe' as argument
|
|
|
|
unsubscribe(callback: Function): void {
|
|
|
|
var index = this.subscribers.indexOf(callback);
|
|
|
|
if (index >= 0) {
|
|
|
|
this.subscribers.splice(index, 1);
|
|
|
|
}
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-07 00:00:00 +00:00
|
|
|
}
|