class Item
{
constructor(playerId, itemId, room)
{
this.playerId = playerId;
this.itemId = itemId;
this.updateKey();
this.details = {};
this.room = room;
this.handlers = [];
}
updateKey()
{
this.key = this.playerId + "_" + this.itemId;
}
getJSON()
{
var ret = {...this.details, player_id:this.playerId, item_id:this.itemId, key:this.key};
delete ret.__change_ownership_player__;
return ret;
}
detailsUpdated()
{
for (var h of this.handlers)
{
if (typeof h.itemUpdated === "function") h.itemUpdated();
}
}
getHandler(handlerType, createIfNotFound = false)
{
for (var h of this.handlers)
{
if (h instanceof handlerType)
{
return h;
}
}
if (!createIfNotFound) return null;
let handler = new handlerType(this.room, this);
if (typeof handler.start === "function") handler.start();
this.handlers.push(handler);
return handler;
}
start()
{
}
stop()
{
for (var h of this.handlers)
{
if (typeof h.stop === "function") h.stop();
}
this.handlers.length = 0;
}
removeHandler(h)
{
const index = this.handlers.indexOf(h);
if (index != -1)
{
this.handlers.splice(index, 1);
if (typeof h.stop === "function") h.stop();
}
}
}
module.exports = Item;