2017-04-22 22:49:49 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2018-03-19 04:54:40 +00:00
|
|
|
const random = require('../random')
|
2018-03-19 03:23:05 +00:00
|
|
|
const utils = require('../utils')
|
2018-08-25 05:37:02 +00:00
|
|
|
const logger = require('../logging')
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
var o = null // eslint-disable-line no-unused-vars
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
class Objects {
|
|
|
|
constructor () {
|
|
|
|
this.counter = 0
|
|
|
|
this.container = {}
|
2017-04-25 22:21:31 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
|
|
|
|
add (category, member) {
|
|
|
|
if (!member) {
|
|
|
|
member = 'o' + this.counter
|
|
|
|
}
|
|
|
|
if (!this.has(category)) {
|
|
|
|
this.container[category] = []
|
|
|
|
}
|
|
|
|
this.container[category].push({type: category, name: member})
|
|
|
|
++this.counter
|
|
|
|
return this.container[category].slice(-1)[0].name
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
get (category, last) {
|
|
|
|
if (!(category in this.container)) {
|
|
|
|
// return {type:null, name:null};
|
|
|
|
logger.traceback()
|
2018-05-21 15:50:44 +00:00
|
|
|
throw new Error(`${category} is not available.`)
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
if (last) {
|
|
|
|
return this.container[category].slice(-1)[0]
|
|
|
|
}
|
|
|
|
return random.pick(this.container[category])
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
|
|
|
|
pick (category, last) {
|
|
|
|
try {
|
|
|
|
return this.get(category, last).name
|
|
|
|
} catch (e) {
|
|
|
|
logger.traceback()
|
2018-05-21 15:50:44 +00:00
|
|
|
throw logger.JSError(`Error: pick('${category}') is undefined.`)
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
pop (objectName) {
|
|
|
|
let self = this
|
|
|
|
utils.common.getKeysFromHash(this.container).forEach(function (category) {
|
|
|
|
self.container[category].forEach(function (obj) {
|
|
|
|
if (obj.name === objectName) {
|
|
|
|
self.container[category].splice(self.container[category].indexOf(obj), 1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
contains (categoryNames) {
|
|
|
|
let categories = []
|
|
|
|
let self = this
|
|
|
|
categoryNames.forEach(function (name) {
|
|
|
|
if (self.has(name)) {
|
|
|
|
categories.push(name)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
})
|
2018-03-19 03:23:05 +00:00
|
|
|
return (categories.length === 0) ? null : categories
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
show (category) {
|
|
|
|
return (category in this.container) ? this.container[category] : this.container
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
count (category) {
|
|
|
|
return (category in this.container) ? this.container[category].length : 0
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
has (category) {
|
|
|
|
if (category in this.container) {
|
|
|
|
this.check(category)
|
|
|
|
return this.container[category].length > 0
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
valid () {
|
|
|
|
let items = []
|
|
|
|
let self = this
|
|
|
|
utils.common.getKeysFromHash(self.container).forEach(function (category) {
|
|
|
|
self.check(category)
|
|
|
|
})
|
|
|
|
utils.common.getKeysFromHash(self.container).forEach(function (category) {
|
|
|
|
for (let i = 0; i < self.container[category].length; i++) {
|
|
|
|
items.push(self.container[category][i].name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
|
|
|
check (category) {
|
|
|
|
let self = this
|
|
|
|
self.container[category].forEach(function (object) {
|
|
|
|
try {
|
|
|
|
let x = /* frame.contentWindow. */ eval(object.name) // eslint-disable-line no-eval
|
|
|
|
if (x === undefined || x === null) {
|
|
|
|
self.pop(object.name)
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2017-04-25 22:21:31 +00:00
|
|
|
self.pop(object.name)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
})
|
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
|
2018-03-19 05:34:18 +00:00
|
|
|
module.exports = {
|
|
|
|
o,
|
|
|
|
Objects
|
|
|
|
}
|