2017-02-09 00:00:35 +00:00
|
|
|
module TS.SpaceTac {
|
2017-02-19 21:33:07 +00:00
|
|
|
/**
|
|
|
|
* Function called to inform subscribers of new events.
|
|
|
|
*/
|
2017-05-29 23:15:32 +00:00
|
|
|
export type LogSubscriber = (event: BaseBattleEvent) => any;
|
2017-02-19 21:33:07 +00:00
|
|
|
|
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
|
2017-02-07 18:54:53 +00:00
|
|
|
export class BattleLog {
|
2014-12-31 00:00:00 +00:00
|
|
|
// Full list of battle events
|
2017-07-10 21:40:52 +00:00
|
|
|
events: BaseBattleEvent[]
|
2014-12-31 00:00:00 +00:00
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// List of subscribers
|
2017-07-10 21:40:52 +00:00
|
|
|
private subscribers: LogSubscriber[]
|
2014-12-31 00:00:00 +00:00
|
|
|
|
2015-01-22 00:00:00 +00:00
|
|
|
// List of event codes to ignore
|
2017-07-10 21:40:52 +00:00
|
|
|
private filters: string[]
|
|
|
|
|
|
|
|
// Indicator that the battle has ended
|
|
|
|
private ended = false
|
2015-01-22 00:00:00 +00:00
|
|
|
|
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 = [];
|
|
|
|
}
|
|
|
|
|
2017-02-09 00:26:04 +00:00
|
|
|
postUnserialize(): void {
|
|
|
|
this.subscribers = [];
|
2015-03-04 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 00:00:00 +00:00
|
|
|
// Clear the stored events
|
|
|
|
clear(): void {
|
2017-07-10 21:40:52 +00:00
|
|
|
this.ended = false;
|
2015-01-22 00:00:00 +00:00
|
|
|
this.events = [];
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a battle event to the log
|
2017-05-29 23:15:32 +00:00
|
|
|
add(event: BaseBattleEvent): void {
|
2015-01-22 00:00:00 +00:00
|
|
|
// Apply filters
|
|
|
|
var filtered = false;
|
2017-07-10 21:40:52 +00:00
|
|
|
this.filters.forEach(code => {
|
2015-01-22 00:00:00 +00:00
|
|
|
if (event.code === code) {
|
|
|
|
filtered = true;
|
|
|
|
}
|
|
|
|
});
|
2017-07-10 21:40:52 +00:00
|
|
|
if (filtered || this.ended) {
|
2015-01-22 00:00:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
this.events.push(event);
|
2014-12-31 00:00:00 +00:00
|
|
|
|
2017-02-19 21:33:07 +00:00
|
|
|
this.subscribers.forEach(subscriber => {
|
2014-12-31 00:00:00 +00:00
|
|
|
subscriber(event);
|
|
|
|
});
|
2017-07-10 21:40:52 +00:00
|
|
|
|
|
|
|
if (event instanceof EndBattleEvent) {
|
|
|
|
this.ended = true;
|
|
|
|
}
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
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
|
2017-02-19 21:33:07 +00:00
|
|
|
subscribe(callback: LogSubscriber): LogSubscriber {
|
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
|
2017-02-19 21:33:07 +00:00
|
|
|
unsubscribe(callback: LogSubscriber): void {
|
2014-12-31 00:00:00 +00:00
|
|
|
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
|
|
|
}
|