1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
// Copyright 2019 Campbell Crowley. All rights reserved.
// Author: Campbell Crowley (dev@campbellcrowley.com)
const HungryGames = require('./HungryGames.js');
/**
* @description An Arena event storing Events.
* @memberof HungryGames
* @inner
* @augments HungryGames~Event
*/
class ArenaEvent extends HungryGames.Event {
/**
* @param {string} message The message at the start of the arena event.
* @param {HungryGames~NormalEvent[]} outcomes All possible events in this
* arena event.
* @param {?HungryGames~OutcomeProbabilities} outcomeProbs Overrides the
* global setting for arena event outcome probabilities for this event.
*/
constructor(message, outcomes, outcomeProbs) {
super(message);
/**
* All possible events in this arena event.
*
* @public
* @type {HungryGames~NormalEvent[]}
*/
this.outcomes = outcomes || [];
/**
* Outcome probabilities specific to this arena event. Overrides the global
* arena event outcome probability settings. Null to use global settings.
*
* @public
* @type {?HungryGames~OutcomeProbabilities}
*/
this.outcomeProbs = outcomeProbs || null;
}
/**
* @description Validate that the given data is properly typed and structured
* to be converted to a ArenaEvent. Also coerces values to correct types if
* possible.
* @public
* @static
* @param {HungryGames~ArenaEvent} evt The event data to verify.
* @returns {?string} Error string, or null if no error.
*/
static validate(evt) {
const err = HungryGames.Event.validate(evt);
if (err) return err;
if (evt.outcomes && !Array.isArray(evt.outcomes)) {
return 'BAD_OUTCOMES';
} else if (evt.outcomes) {
let outerr;
let index;
evt.outcomes.find((el, i) => {
outerr = HungryGames.NormalEvent.validate(el);
index = i;
return outerr;
});
if (outerr) {
return `BAD_OUTCOME_${index}_${outerr}`;
}
}
if (evt.outcomeProbs) {
const keys = ['kill', 'wound', 'thrive', 'revive', 'nothing'];
const fail = keys.find(
(key) => evt.outcomeProbs[key] != null &&
isNaN(evt.outcomeProbs[key] *= 1));
if (fail) return 'BAD_OUTCOME_PROBS';
}
return null;
}
/**
* @description Create a new ArenaEvent object from a ArenaEvent-like object.
* Similar to copy-constructor.
*
* @public
* @static
* @param {object} obj Event-like object to copy.
* @returns {HungryGames~ArenaEvent} Copy of event.
*/
static from(obj) {
const out =
new ArenaEvent(obj.message, undefined, obj.outcomeProbs || null);
out.fill(obj);
if (Array.isArray(obj.outcomes)) {
obj.outcomes.forEach(
(el) => out.outcomes.push(HungryGames.NormalEvent.from(el)));
}
return out;
}
}
module.exports = ArenaEvent;