|
|
|
@@ -62,14 +62,14 @@ export class RdDBaseActor extends Actor {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static remoteActorCall(callData) {
|
|
|
|
|
static remoteActorCall(callData, userId = undefined) {
|
|
|
|
|
if (game.user.isGM) {
|
|
|
|
|
RdDBaseActor.onRemoteActorCall(callData, game.user.id)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const gmUserId = Misc.firstConnectedGMId()
|
|
|
|
|
if (gmUserId){
|
|
|
|
|
if (gmUserId) {
|
|
|
|
|
game.socket.emit(SYSTEM_SOCKET_ID, {
|
|
|
|
|
msg: "msg_remote_actor_call",
|
|
|
|
|
data: callData,
|
|
|
|
@@ -417,6 +417,14 @@ export class RdDBaseActor extends Actor {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async depenserSols(sols) {
|
|
|
|
|
if (!this.isOwner) {
|
|
|
|
|
return RdDBaseActor.remoteActorCall({
|
|
|
|
|
userId: Misc.connectedGMOrUser(),
|
|
|
|
|
tokenId: this.token?.id,
|
|
|
|
|
actorId: this.id,
|
|
|
|
|
method: 'depenserSols', args: [sols]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
let reste = this.getFortune() - Number(sols);
|
|
|
|
|
if (reste >= 0) {
|
|
|
|
|
await Monnaie.optimiserFortune(this, reste);
|
|
|
|
@@ -425,32 +433,31 @@ export class RdDBaseActor extends Actor {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async ajouterSols(sols, fromActorId = undefined) {
|
|
|
|
|
sols = Number(sols);
|
|
|
|
|
sols = Number(sols)
|
|
|
|
|
if (sols == 0) {
|
|
|
|
|
return;
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (sols < 0) {
|
|
|
|
|
ui.notifications.error(`Impossible d'ajouter un gain de ${sols} <0`);
|
|
|
|
|
return;
|
|
|
|
|
ui.notifications.error(`Impossible d'ajouter un gain de ${sols} <0`)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (fromActorId && !this.isOwner) {
|
|
|
|
|
RdDBaseActor.remoteActorCall({
|
|
|
|
|
return RdDBaseActor.remoteActorCall({
|
|
|
|
|
userId: Misc.connectedGMOrUser(),
|
|
|
|
|
tokenId: this.token?.id,
|
|
|
|
|
actorId: this.id,
|
|
|
|
|
method: 'ajouterSols', args: [sols, fromActorId]
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const fromActor = game.actors.get(fromActorId)
|
|
|
|
|
await Monnaie.optimiserFortune(this, sols + this.getFortune());
|
|
|
|
|
|
|
|
|
|
RdDAudio.PlayContextAudio("argent"); // Petit son
|
|
|
|
|
ChatMessage.create({
|
|
|
|
|
whisper: ChatUtility.getOwners(this),
|
|
|
|
|
content: `Vous avez reçu <strong>${sols} Sols</strong> ${fromActor ? " de " + fromActor.name : ''}, qui ont été ajoutés à votre argent.`
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const fromActor = game.actors.get(fromActorId)
|
|
|
|
|
await Monnaie.optimiserFortune(this, sols + this.getFortune());
|
|
|
|
|
|
|
|
|
|
RdDAudio.PlayContextAudio("argent"); // Petit son
|
|
|
|
|
ChatMessage.create({
|
|
|
|
|
whisper: ChatUtility.getOwners(this),
|
|
|
|
|
content: `Vous avez reçu <strong>${sols} Sols</strong> ${fromActor ? " de " + fromActor.name : ''}, qui ont été ajoutés à votre argent.`
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
@@ -462,24 +469,28 @@ export class RdDBaseActor extends Actor {
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
async achatVente(achat) {
|
|
|
|
|
if (achat.vendeurId == achat.acheteurId) {
|
|
|
|
|
ui.notifications.info("Inutile de se vendre à soi-même");
|
|
|
|
|
return;
|
|
|
|
|
ui.notifications.info("Inutile de se vendre à soi-même")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (!game.user.isGM) {
|
|
|
|
|
const cout = Number(achat.prixTotal ?? 0)
|
|
|
|
|
const acheteur = achat.acheteurId ? game.actors.get(achat.acheteurId) : undefined
|
|
|
|
|
|
|
|
|
|
if (acheteur && !acheteur.isOwner) {
|
|
|
|
|
ui.notifications.warn(`${game.user.id} n'est pas propriétaire de ${this.name} et effectue un achat pour lui!`)
|
|
|
|
|
RdDBaseActor.remoteActorCall({
|
|
|
|
|
actorId: achat.vendeurId ?? achat.acheteurId,
|
|
|
|
|
actorId: achat.acheteurId,
|
|
|
|
|
method: 'achatVente', args: [achat]
|
|
|
|
|
});
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const cout = Number(achat.prixTotal ?? 0);
|
|
|
|
|
const vendeur = achat.vendeurId ? game.actors.get(achat.vendeurId) : undefined;
|
|
|
|
|
const acheteur = achat.acheteurId ? game.actors.get(achat.acheteurId) : undefined;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const vendeur = achat.vendeurId ? game.actors.get(achat.vendeurId) : undefined
|
|
|
|
|
const quantite = (achat.choix.nombreLots ?? 1) * (achat.vente.tailleLot);
|
|
|
|
|
const itemVendu = vendeur?.getItem(achat.vente.item._id) ?? game.items.get(achat.vente.item._id);
|
|
|
|
|
if (!itemVendu) {
|
|
|
|
|
ChatUtility.notifyUser(achat.userId, 'warn', vendeur ? `Le vendeur n'a pas plus de ${achat.vente.item.name} !` : `Impossible de retrouver: ${achat.vente.item.name} !`);
|
|
|
|
|
return;
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (vendeur && !vendeur.verifierQuantite(itemVendu, quantite)) {
|
|
|
|
|
ChatUtility.notifyUser(achat.userId, 'warn', `Le vendeur n'a pas assez de ${itemVendu.name} !`);
|
|
|
|
@@ -487,10 +498,10 @@ export class RdDBaseActor extends Actor {
|
|
|
|
|
}
|
|
|
|
|
if (acheteur && !acheteur.verifierFortune(cout)) {
|
|
|
|
|
ChatUtility.notifyUser(achat.userId, 'warn', `Vous n'avez pas assez d'argent pour payer ${Math.ceil(cout / 100)} sols !`);
|
|
|
|
|
return;
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
await vendeur?.vendre(itemVendu, quantite, cout);
|
|
|
|
|
await acheteur?.acheter(itemVendu, quantite, cout, achat)
|
|
|
|
|
await vendeur?.vendre(itemVendu, quantite, cout, acheteur);
|
|
|
|
|
await acheteur?.acheter(itemVendu, quantite, cout, achat, vendeur)
|
|
|
|
|
|
|
|
|
|
if (cout > 0) {
|
|
|
|
|
RdDAudio.PlayContextAudio("argent");
|
|
|
|
@@ -505,22 +516,32 @@ export class RdDBaseActor extends Actor {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!achat.vente.quantiteIllimite) {
|
|
|
|
|
if (achat.vente.nbLots <= achat.choix.nombreLots) {
|
|
|
|
|
ChatUtility.removeChatMessageId(achat.chatMessageIdVente);
|
|
|
|
|
}
|
|
|
|
|
else if (achat.chatMessageIdVente) {
|
|
|
|
|
await ChatVente.diminuerQuantiteAchatVente(achat.chatMessageIdVente, achat.choix.nombreLots)
|
|
|
|
|
}
|
|
|
|
|
await this.updateMessageVente(achat.chatMessageIdVente, achat.choix.nombreLots);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async vendre(item, quantite, cout) {
|
|
|
|
|
await this.ajouterSols(cout);
|
|
|
|
|
await this.decrementerQuantiteItem(item, quantite);
|
|
|
|
|
async updateMessageVente(chatMessageIdVente, nombreLots) {
|
|
|
|
|
const chatMessage = game.messages.get(chatMessageIdVente)
|
|
|
|
|
if (!chatMessage.isOwner) {
|
|
|
|
|
return RdDBaseActor.remoteActorCall({
|
|
|
|
|
userId: Misc.firstConnectedGMId(),
|
|
|
|
|
actorId: this.id,
|
|
|
|
|
method: 'updateMessageVente', args: [chatMessageIdVente, nombreLots]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
await ChatVente.diminuerQuantiteAchatVente(chatMessageIdVente, nombreLots);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async vendre(item, quantite, cout, acheteur) {
|
|
|
|
|
await this.ajouterSols(cout, acheteur?.id)
|
|
|
|
|
await this.decrementerQuantiteItem(item.id, quantite)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async acheter(item, quantite, cout, achat) {
|
|
|
|
|
await this.depenserSols(cout)
|
|
|
|
|
if (!this.isOwner) {
|
|
|
|
|
ui.notifications.warn(`${game.user.id} n'est pas propriétaire de ${this.name} et effectue un achat!`)
|
|
|
|
|
}
|
|
|
|
|
const createdItemId = await this.creerQuantiteItem(item, quantite)
|
|
|
|
|
if (item.type == ITEM_TYPES.nourritureboisson && achat.choix.consommer && createdItemId != undefined) {
|
|
|
|
|
achat.choix.doses = achat.choix.nombreLots;
|
|
|
|
@@ -542,328 +563,331 @@ export class RdDBaseActor extends Actor {
|
|
|
|
|
|
|
|
|
|
async consommerNourritureboisson(itemId, choix, userId) { }
|
|
|
|
|
|
|
|
|
|
async decrementerQuantiteItem(item, quantite, options = { supprimerSiZero: true }) {
|
|
|
|
|
async decrementerQuantiteItem(itemId, quantite, options = { supprimerSiZero: true }) {
|
|
|
|
|
const item = this.items.get(itemId)
|
|
|
|
|
if (item.isService()) {
|
|
|
|
|
return;
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const itemId = item.id;
|
|
|
|
|
let resteQuantite = (item.system.quantite ?? 1) - quantite;
|
|
|
|
|
if (resteQuantite <= 0) {
|
|
|
|
|
if (options.supprimerSiZero) {
|
|
|
|
|
await this.deleteEmbeddedDocuments("Item", [item.id]);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
await this.updateEmbeddedDocuments("Item", [{ _id: itemId, 'system.quantite': 0 }]);
|
|
|
|
|
}
|
|
|
|
|
if (resteQuantite < 0) {
|
|
|
|
|
ui.notifications.warn(`La quantité de ${item.name} était insuffisante, l'objet a donc été supprimé`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.isOwner) {
|
|
|
|
|
return RdDBaseActor.remoteActorCall({
|
|
|
|
|
userId: Misc.connectedGMOrUser(),
|
|
|
|
|
tokenId: this.token?.id,
|
|
|
|
|
actorId: this.id,
|
|
|
|
|
method: 'decrementerQuantiteItem', args: [itemId, quantite, options]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
else if (resteQuantite > 0) {
|
|
|
|
|
const realItem = this.getItem(item.id)
|
|
|
|
|
realItem.update({ 'system.quantite': resteQuantite });
|
|
|
|
|
await this.updateEmbeddedDocuments("Item", [{ _id: item.id, 'system.quantite': resteQuantite }]);
|
|
|
|
|
|
|
|
|
|
const resteQuantite = Math.max((item.system.quantite ?? 1) - quantite, 0)
|
|
|
|
|
if (resteQuantite <= 0 && options.supprimerSiZero) {
|
|
|
|
|
await this.deleteEmbeddedDocuments("Item", [itemId]);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
await this.updateEmbeddedDocuments("Item", [{ _id: itemId, 'system.quantite': resteQuantite }])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async creerQuantiteItem(item, quantite) {
|
|
|
|
|
if (this.canReceive(item)) {
|
|
|
|
|
const isItemEmpilable = "quantite" in item.system;
|
|
|
|
|
const baseItem = {
|
|
|
|
|
type: item.type,
|
|
|
|
|
img: item.img,
|
|
|
|
|
name: item.name,
|
|
|
|
|
system: foundry.utils.mergeObject(item.system, { quantite: isItemEmpilable ? quantite : undefined }, { inplace: false })
|
|
|
|
|
};
|
|
|
|
|
const newItems = isItemEmpilable ? [baseItem] : Array.from({ length: quantite }, (_, i) => baseItem);
|
|
|
|
|
const items = await this.createEmbeddedDocuments("Item", newItems);
|
|
|
|
|
return items.length > 0 ? items[0].id : undefined;
|
|
|
|
|
if (!this.canReceive(item)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isItemEmpilable = "quantite" in item.system;
|
|
|
|
|
const baseItem = {
|
|
|
|
|
type: item.type,
|
|
|
|
|
img: item.img,
|
|
|
|
|
name: item.name,
|
|
|
|
|
system: foundry.utils.mergeObject(item.system, { quantite: isItemEmpilable ? quantite : undefined }, { inplace: false })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newItems = isItemEmpilable ? [baseItem] : Array.from({ length: quantite }, (_, i) => baseItem);
|
|
|
|
|
const items = await this.createEmbeddedDocuments("Item", newItems);
|
|
|
|
|
return newItems.length > 0 ? items[0].id : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
async computeEncTotal() {
|
|
|
|
|
if (this.pack) {
|
|
|
|
|
this.encTotal = 0
|
|
|
|
|
if (this.pack) {
|
|
|
|
|
this.encTotal = 0
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const wasSurenc = this.isSurenc()
|
|
|
|
|
this.encTotal = this.items.filter(it => RdDItem.getItemTypesInventaire().includes(it.type))
|
|
|
|
|
.map(it => it.getEncTotal()).reduce(Misc.sum(), 0)
|
|
|
|
|
const isSurenc = this.isSurenc()
|
|
|
|
|
if (isSurenc != wasSurenc) {
|
|
|
|
|
await this.setEffect(STATUSES.StatusSurEnc, isSurenc)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const wasSurenc = this.isSurenc()
|
|
|
|
|
this.encTotal = this.items.filter(it => RdDItem.getItemTypesInventaire().includes(it.type))
|
|
|
|
|
.map(it => it.getEncTotal()).reduce(Misc.sum(), 0)
|
|
|
|
|
const isSurenc = this.isSurenc()
|
|
|
|
|
if (isSurenc != wasSurenc) {
|
|
|
|
|
await this.setEffect(STATUSES.StatusSurEnc, isSurenc)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getEncTotal() {
|
|
|
|
|
return Number(this.encTotal?.toFixed(2) ?? 0.00)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createItem(type, name = undefined) {
|
|
|
|
|
if (!name) {
|
|
|
|
|
name = 'Nouveau ' + Misc.typeName('Item', type);
|
|
|
|
|
}
|
|
|
|
|
await this.createEmbeddedDocuments('Item', [{ name: name, type: type }], { renderSheet: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canReceive(item) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async processDropItem(params) {
|
|
|
|
|
const targetActorId = this.id
|
|
|
|
|
const sourceActorId = params.sourceActorId
|
|
|
|
|
const sourceTokenId = params.sourceTokenId
|
|
|
|
|
const itemId = params.itemId
|
|
|
|
|
const destId = params.destId
|
|
|
|
|
const srcId = params.srcId
|
|
|
|
|
if (sourceActorId && sourceActorId != targetActorId) {
|
|
|
|
|
console.log("Moving objects", sourceActorId, sourceTokenId, targetActorId, itemId);
|
|
|
|
|
this.moveItemsBetweenActors(itemId, sourceActorId, sourceTokenId);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
let result = true;
|
|
|
|
|
const item = this.getItem(itemId);
|
|
|
|
|
if (item?.isInventaire('all') && sourceActorId == targetActorId) {
|
|
|
|
|
// rangement
|
|
|
|
|
if (srcId != destId && itemId != destId) { // déplacement de l'objet
|
|
|
|
|
const src = this.getItem(srcId);
|
|
|
|
|
const dest = this.getItem(destId);
|
|
|
|
|
const cible = this.getContenantOrParent(dest);
|
|
|
|
|
const [empilable, message] = item.isInventaireEmpilable(dest);
|
|
|
|
|
if (empilable) {
|
|
|
|
|
await dest.empiler(item)
|
|
|
|
|
result = false;
|
|
|
|
|
}
|
|
|
|
|
// changer de conteneur
|
|
|
|
|
else if (!cible || this.conteneurPeutContenir(cible, item)) {
|
|
|
|
|
await this.enleverDeConteneur(item, src, params.onEnleverConteneur);
|
|
|
|
|
await this.ajouterDansConteneur(item, cible, params.onAjouterDansConteneur);
|
|
|
|
|
if (message && !dest.isConteneur()) {
|
|
|
|
|
ui.notifications.info(cible
|
|
|
|
|
? `${message}<br>${item.name} a été déplacé dans: ${cible.name}`
|
|
|
|
|
: `${message}<br>${item.name} a été sorti du conteneur`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await this.computeEncTotal()
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getEncTotal() {
|
|
|
|
|
return Number(this.encTotal?.toFixed(2) ?? 0.00)
|
|
|
|
|
getContenantOrParent(dest) {
|
|
|
|
|
if (!dest || dest.isConteneur()) {
|
|
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
return this.getContenant(dest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createItem(type, name = undefined) {
|
|
|
|
|
if (!name) {
|
|
|
|
|
name = 'Nouveau ' + Misc.typeName('Item', type);
|
|
|
|
|
}
|
|
|
|
|
await this.createEmbeddedDocuments('Item', [{ name: name, type: type }], { renderSheet: true });
|
|
|
|
|
getContenant(item) {
|
|
|
|
|
return this.itemTypes['conteneur'].find(it => it.system.contenu.includes(item.id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
conteneurPeutContenir(dest, moved) {
|
|
|
|
|
if (!dest) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canReceive(item) {
|
|
|
|
|
if (!dest.isConteneur()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (moved.isConteneurContenu(dest)) {
|
|
|
|
|
ui.notifications.warn(`Impossible de déplacer un conteneur parent (${moved.name}) dans un de ses contenus ${dest.name} !`);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async processDropItem(params) {
|
|
|
|
|
const targetActorId = this.id
|
|
|
|
|
const sourceActorId = params.sourceActorId
|
|
|
|
|
const sourceTokenId = params.sourceTokenId
|
|
|
|
|
const itemId = params.itemId
|
|
|
|
|
const destId = params.destId
|
|
|
|
|
const srcId = params.srcId
|
|
|
|
|
if (sourceActorId && sourceActorId != targetActorId) {
|
|
|
|
|
console.log("Moving objects", sourceActorId, sourceTokenId, targetActorId, itemId);
|
|
|
|
|
this.moveItemsBetweenActors(itemId, sourceActorId, sourceTokenId);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
let result = true;
|
|
|
|
|
const item = this.getItem(itemId);
|
|
|
|
|
if (item?.isInventaire('all') && sourceActorId == targetActorId) {
|
|
|
|
|
// rangement
|
|
|
|
|
if (srcId != destId && itemId != destId) { // déplacement de l'objet
|
|
|
|
|
const src = this.getItem(srcId);
|
|
|
|
|
const dest = this.getItem(destId);
|
|
|
|
|
const cible = this.getContenantOrParent(dest);
|
|
|
|
|
const [empilable, message] = item.isInventaireEmpilable(dest);
|
|
|
|
|
if (empilable) {
|
|
|
|
|
await dest.empiler(item)
|
|
|
|
|
result = false;
|
|
|
|
|
}
|
|
|
|
|
// changer de conteneur
|
|
|
|
|
else if (!cible || this.conteneurPeutContenir(cible, item)) {
|
|
|
|
|
await this.enleverDeConteneur(item, src, params.onEnleverConteneur);
|
|
|
|
|
await this.ajouterDansConteneur(item, cible, params.onAjouterDansConteneur);
|
|
|
|
|
if (message && !dest.isConteneur()) {
|
|
|
|
|
ui.notifications.info(cible
|
|
|
|
|
? `${message}<br>${item.name} a été déplacé dans: ${cible.name}`
|
|
|
|
|
: `${message}<br>${item.name} a été sorti du conteneur`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await this.computeEncTotal()
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
// Calculer le total actuel des contenus
|
|
|
|
|
const encContenu = dest.getEncContenu();
|
|
|
|
|
const newEnc = moved.getEncTotal(); // Calculer le total actuel du nouvel objet
|
|
|
|
|
const placeDisponible = Misc.keepDecimals(dest.system.capacite - encContenu - newEnc, 4)
|
|
|
|
|
|
|
|
|
|
getContenantOrParent(dest) {
|
|
|
|
|
if (!dest || dest.isConteneur()) {
|
|
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
return this.getContenant(dest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getContenant(item) {
|
|
|
|
|
return this.itemTypes['conteneur'].find(it => it.system.contenu.includes(item.id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
conteneurPeutContenir(dest, moved) {
|
|
|
|
|
if (!dest) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (!dest.isConteneur()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (moved.isConteneurContenu(dest)) {
|
|
|
|
|
ui.notifications.warn(`Impossible de déplacer un conteneur parent (${moved.name}) dans un de ses contenus ${dest.name} !`);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculer le total actuel des contenus
|
|
|
|
|
const encContenu = dest.getEncContenu();
|
|
|
|
|
const newEnc = moved.getEncTotal(); // Calculer le total actuel du nouvel objet
|
|
|
|
|
const placeDisponible = Misc.keepDecimals(dest.system.capacite - encContenu - newEnc, 4)
|
|
|
|
|
|
|
|
|
|
// Teste si le conteneur de destination a suffisament de capacité pour recevoir le nouvel objet
|
|
|
|
|
if (placeDisponible < 0) {
|
|
|
|
|
ui.notifications.warn(
|
|
|
|
|
`Le conteneur ${dest.name} a une capacité de ${dest.system.capacite}, et contient déjà ${encContenu}.
|
|
|
|
|
// Teste si le conteneur de destination a suffisament de capacité pour recevoir le nouvel objet
|
|
|
|
|
if (placeDisponible < 0) {
|
|
|
|
|
ui.notifications.warn(
|
|
|
|
|
`Le conteneur ${dest.name} a une capacité de ${dest.system.capacite}, et contient déjà ${encContenu}.
|
|
|
|
|
Impossible d'y ranger: ${moved.name} d'encombrement ${newEnc}!`);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
/** Ajoute un item dans un conteneur, sur la base de leurs ID */
|
|
|
|
|
async ajouterDansConteneur(item, conteneur, onAjouterDansConteneur) {
|
|
|
|
|
if (conteneur?.isConteneur()) {
|
|
|
|
|
item.estContenu = true;
|
|
|
|
|
const nouveauContenu = [...conteneur.system.contenu, item.id];
|
|
|
|
|
await conteneur.update({ 'system.contenu': nouveauContenu });
|
|
|
|
|
onAjouterDansConteneur(item.id, conteneur.id)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
item.estContenu = false;
|
|
|
|
|
await conteneur?.update({ 'system.-=contenu': undefined })
|
|
|
|
|
}
|
|
|
|
|
if (conteneur?.isConteneur()) {
|
|
|
|
|
item.estContenu = true;
|
|
|
|
|
const nouveauContenu = [...conteneur.system.contenu, item.id];
|
|
|
|
|
await conteneur.update({ 'system.contenu': nouveauContenu });
|
|
|
|
|
onAjouterDansConteneur(item.id, conteneur.id)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
item.estContenu = false;
|
|
|
|
|
await conteneur?.update({ 'system.-=contenu': undefined })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
/** Fonction de remise à plat de l'équipement (ie vide les champs 'contenu') */
|
|
|
|
|
async nettoyerConteneurs() {
|
|
|
|
|
RdDConfirm.confirmer({
|
|
|
|
|
settingConfirmer: "confirmation-vider",
|
|
|
|
|
content: `<p>Etes vous certain de vouloir vider tous les conteneurs ?</p>`,
|
|
|
|
|
title: 'Vider les conteneurs',
|
|
|
|
|
buttonLabel: 'Vider',
|
|
|
|
|
onAction: async () => {
|
|
|
|
|
const corrections = [];
|
|
|
|
|
for (let item of this.items) {
|
|
|
|
|
if (item.estContenu) {
|
|
|
|
|
item.estContenu = undefined;
|
|
|
|
|
}
|
|
|
|
|
if (item.system.contenu != undefined) {
|
|
|
|
|
if (item.type == 'conteneur') {
|
|
|
|
|
corrections.push({ _id: item.id, 'system.contenu': [] });
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
corrections.push({ _id: item.id, 'system.-=contenu': undefined });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
RdDConfirm.confirmer({
|
|
|
|
|
settingConfirmer: "confirmation-vider",
|
|
|
|
|
content: `<p>Etes vous certain de vouloir vider tous les conteneurs ?</p>`,
|
|
|
|
|
title: 'Vider les conteneurs',
|
|
|
|
|
buttonLabel: 'Vider',
|
|
|
|
|
onAction: async () => {
|
|
|
|
|
const corrections = [];
|
|
|
|
|
for (let item of this.items) {
|
|
|
|
|
if (item.estContenu) {
|
|
|
|
|
item.estContenu = undefined;
|
|
|
|
|
}
|
|
|
|
|
if (corrections.length > 0) {
|
|
|
|
|
await this.updateEmbeddedDocuments('Item', corrections);
|
|
|
|
|
if (item.system.contenu != undefined) {
|
|
|
|
|
if (item.type == 'conteneur') {
|
|
|
|
|
corrections.push({ _id: item.id, 'system.contenu': [] });
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
corrections.push({ _id: item.id, 'system.-=contenu': undefined });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (corrections.length > 0) {
|
|
|
|
|
await this.updateEmbeddedDocuments('Item', corrections);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
buildSubConteneurObjetList(conteneurId, deleteList) {
|
|
|
|
|
let conteneur = this.getItem(conteneurId);
|
|
|
|
|
if (conteneur?.type == 'conteneur') { // Si c'est un conteneur
|
|
|
|
|
for (let subId of conteneur.system.contenu) {
|
|
|
|
|
let subObj = this.getItem(subId);
|
|
|
|
|
if (subObj) {
|
|
|
|
|
if (subObj.type == 'conteneur') {
|
|
|
|
|
this.buildSubConteneurObjetList(subId, deleteList);
|
|
|
|
|
}
|
|
|
|
|
deleteList.push({ id: subId, conteneurId: conteneurId });
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
buildSubConteneurObjetList(conteneurId, deleteList) {
|
|
|
|
|
let conteneur = this.getItem(conteneurId);
|
|
|
|
|
if (conteneur?.type == 'conteneur') { // Si c'est un conteneur
|
|
|
|
|
for (let subId of conteneur.system.contenu) {
|
|
|
|
|
let subObj = this.getItem(subId);
|
|
|
|
|
if (subObj) {
|
|
|
|
|
if (subObj.type == 'conteneur') {
|
|
|
|
|
this.buildSubConteneurObjetList(subId, deleteList);
|
|
|
|
|
}
|
|
|
|
|
deleteList.push({ id: subId, conteneurId: conteneurId });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
async deleteAllConteneur(itemId, options) {
|
|
|
|
|
let list = [];
|
|
|
|
|
list.push({ id: itemId, conteneurId: undefined }); // Init list
|
|
|
|
|
this.buildSubConteneurObjetList(itemId, list);
|
|
|
|
|
await this.deleteEmbeddedDocuments('Item', list.map(it => it.id), options);
|
|
|
|
|
}
|
|
|
|
|
let list = [];
|
|
|
|
|
list.push({ id: itemId, conteneurId: undefined }); // Init list
|
|
|
|
|
this.buildSubConteneurObjetList(itemId, list);
|
|
|
|
|
await this.deleteEmbeddedDocuments('Item', list.map(it => it.id), options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
/**
|
|
|
|
|
* Supprime un item d'un conteneur, sur la base de leurs ID
|
|
|
|
|
*/
|
|
|
|
|
async enleverDeConteneur(item, conteneur, onEnleverDeConteneur) {
|
|
|
|
|
if (conteneur) {
|
|
|
|
|
if (conteneur.isConteneur()) {
|
|
|
|
|
const contenu = conteneur.system.contenu.filter(id => id != item.id);
|
|
|
|
|
await conteneur.update({ 'system.contenu': contenu });
|
|
|
|
|
onEnleverDeConteneur();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
await conteneur.update({ 'system.-=contenu': undefined })
|
|
|
|
|
}
|
|
|
|
|
if (conteneur) {
|
|
|
|
|
if (conteneur.isConteneur()) {
|
|
|
|
|
const contenu = conteneur.system.contenu.filter(id => id != item.id);
|
|
|
|
|
await conteneur.update({ 'system.contenu': contenu });
|
|
|
|
|
onEnleverDeConteneur();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
await conteneur.update({ 'system.-=contenu': undefined })
|
|
|
|
|
}
|
|
|
|
|
item.estContenu = false;
|
|
|
|
|
}
|
|
|
|
|
item.estContenu = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
async moveItemsBetweenActors(itemId, sourceActorId, sourceTokenId) {
|
|
|
|
|
let sourceActor = RdDBaseActor.getRealActor(sourceActorId, sourceTokenId)
|
|
|
|
|
let itemsList = [{ id: itemId, conteneurId: undefined }]
|
|
|
|
|
sourceActor.buildSubConteneurObjetList(itemId, itemsList); // Get itemId list
|
|
|
|
|
let sourceActor = RdDBaseActor.getRealActor(sourceActorId, sourceTokenId)
|
|
|
|
|
let itemsList = [{ id: itemId, conteneurId: undefined }]
|
|
|
|
|
sourceActor.buildSubConteneurObjetList(itemId, itemsList); // Get itemId list
|
|
|
|
|
|
|
|
|
|
const itemsDataToCreate = itemsList.map(it => sourceActor.getItem(it.id))
|
|
|
|
|
.map(it => foundry.utils.duplicate(it))
|
|
|
|
|
.map(it => { it.system.contenu = []; return it; });
|
|
|
|
|
let newItems = await this.createEmbeddedDocuments('Item', itemsDataToCreate);
|
|
|
|
|
const itemsDataToCreate = itemsList.map(it => sourceActor.getItem(it.id))
|
|
|
|
|
.map(it => foundry.utils.duplicate(it))
|
|
|
|
|
.map(it => { it.system.contenu = []; return it; });
|
|
|
|
|
let newItems = await this.createEmbeddedDocuments('Item', itemsDataToCreate);
|
|
|
|
|
|
|
|
|
|
let itemMap = this._buildMapOldNewId(itemsList, newItems);
|
|
|
|
|
let itemMap = this._buildMapOldNewId(itemsList, newItems);
|
|
|
|
|
|
|
|
|
|
for (let item of itemsList) { // Second boucle pour traiter la remise en conteneurs
|
|
|
|
|
// gestion conteneur/contenu
|
|
|
|
|
if (item.conteneurId) { // l'Objet était dans un conteneur
|
|
|
|
|
const newConteneurId = itemMap[item.conteneurId];
|
|
|
|
|
const newConteneur = this.getItem(newConteneurId);
|
|
|
|
|
const newItemId = itemMap[item.id]; // Get newItem
|
|
|
|
|
for (let item of itemsList) { // Second boucle pour traiter la remise en conteneurs
|
|
|
|
|
// gestion conteneur/contenu
|
|
|
|
|
if (item.conteneurId) { // l'Objet était dans un conteneur
|
|
|
|
|
const newConteneurId = itemMap[item.conteneurId];
|
|
|
|
|
const newConteneur = this.getItem(newConteneurId);
|
|
|
|
|
const newItemId = itemMap[item.id]; // Get newItem
|
|
|
|
|
|
|
|
|
|
console.log('New conteneur filling!', newConteneur, newItemId, item);
|
|
|
|
|
const nouveauContenu = [...newConteneur.system.contenu, newItemId]
|
|
|
|
|
await newConteneur.update({ 'system.contenu': nouveauContenu })
|
|
|
|
|
}
|
|
|
|
|
console.log('New conteneur filling!', newConteneur, newItemId, item);
|
|
|
|
|
const nouveauContenu = [...newConteneur.system.contenu, newItemId]
|
|
|
|
|
await newConteneur.update({ 'system.contenu': nouveauContenu })
|
|
|
|
|
}
|
|
|
|
|
const deletedItemIds = itemsList.map(it => it.id)
|
|
|
|
|
await sourceActor.deleteEmbeddedDocuments('Item', deletedItemIds);
|
|
|
|
|
}
|
|
|
|
|
const deletedItemIds = itemsList.map(it => it.id)
|
|
|
|
|
await sourceActor.deleteEmbeddedDocuments('Item', deletedItemIds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_buildMapOldNewId(itemsList, newItems) {
|
|
|
|
|
let itemMap = {};
|
|
|
|
|
for (let i = 0; i < itemsList.length; i++) {
|
|
|
|
|
itemMap[itemsList[i].id] = newItems[i].id; // Pour garder le lien ancien / nouveau
|
|
|
|
|
}
|
|
|
|
|
return itemMap;
|
|
|
|
|
_buildMapOldNewId(itemsList, newItems) {
|
|
|
|
|
let itemMap = {};
|
|
|
|
|
for (let i = 0; i < itemsList.length; i++) {
|
|
|
|
|
itemMap[itemsList[i].id] = newItems[i].id; // Pour garder le lien ancien / nouveau
|
|
|
|
|
}
|
|
|
|
|
return itemMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
async postActorToChat(modeOverride) {
|
|
|
|
|
let chatData = {
|
|
|
|
|
doctype: 'Actor',
|
|
|
|
|
id: this.id,
|
|
|
|
|
type: this.type,
|
|
|
|
|
img: this.img,
|
|
|
|
|
pack: this.pack,
|
|
|
|
|
name: this.getAlias(),
|
|
|
|
|
system: { description: this.system.description }
|
|
|
|
|
}
|
|
|
|
|
renderTemplate('systems/foundryvtt-reve-de-dragon/templates/post-actor.hbs', chatData)
|
|
|
|
|
.then(html => ChatMessage.create(RdDUtility.chatDataSetup(html, modeOverride)));
|
|
|
|
|
let chatData = {
|
|
|
|
|
doctype: 'Actor',
|
|
|
|
|
id: this.id,
|
|
|
|
|
type: this.type,
|
|
|
|
|
img: this.img,
|
|
|
|
|
pack: this.pack,
|
|
|
|
|
name: this.getAlias(),
|
|
|
|
|
system: { description: this.system.description }
|
|
|
|
|
}
|
|
|
|
|
renderTemplate('systems/foundryvtt-reve-de-dragon/templates/post-actor.hbs', chatData)
|
|
|
|
|
.then(html => ChatMessage.create(RdDUtility.chatDataSetup(html, modeOverride)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actionImpossible(action) {
|
|
|
|
|
ui.notifications.info(`${this.getAlias()} ne peut pas faire cette action: ${action}`)
|
|
|
|
|
}
|
|
|
|
|
actionImpossible(action) {
|
|
|
|
|
ui.notifications.info(`${this.getAlias()} ne peut pas faire cette action: ${action}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isAlcoolise() { return false }
|
|
|
|
|
isAlcoolise() { return false }
|
|
|
|
|
async jetEthylisme() { this.actionImpossible("jet d'éthylisme") }
|
|
|
|
|
async rollAppelChance() { this.actionImpossible("appel à la chance") }
|
|
|
|
|
async jetDeMoral() { this.actionImpossible("jet de moral") }
|
|
|
|
|
|
|
|
|
|
async resetItemUse() { }
|
|
|
|
|
async incDecItemUse(itemId, shouldIncrease = true) { }
|
|
|
|
|
getItemUse(itemId) { return 0; }
|
|
|
|
|
getItemUse(itemId) { return 0; }
|
|
|
|
|
|
|
|
|
|
async finDeRound(options = { terminer: false }) { }
|
|
|
|
|
isActorCombat() { return false }
|
|
|
|
|
getCaracInit(competence) { return 0 }
|
|
|
|
|
isActorCombat() { return false }
|
|
|
|
|
getCaracInit(competence) { return 0 }
|
|
|
|
|
|
|
|
|
|
listAttaques() {
|
|
|
|
|
return this.listActions({ isAttaque: true, isEquipe: false })
|
|
|
|
|
}
|
|
|
|
|
listAttaques() {
|
|
|
|
|
return this.listActions({ isAttaque: true, isEquipe: false })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listActions({ isAttaque = false, isEquipe = false }) { return [] }
|
|
|
|
|
listActions({ isAttaque = false, isEquipe = false }) { return [] }
|
|
|
|
|
|
|
|
|
|
listActionsPossessions() {
|
|
|
|
|
return this.itemTypes[ITEM_TYPES.possession]
|
|
|
|
|
.filter(it => !it.system.possede)
|
|
|
|
|
.map(p => {
|
|
|
|
|
return {
|
|
|
|
|
label: p.name,
|
|
|
|
|
action: 'possession',
|
|
|
|
|
possessionid: p.system.possessionid,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
listActionsPossessions() {
|
|
|
|
|
return this.itemTypes[ITEM_TYPES.possession]
|
|
|
|
|
.filter(it => !it.system.possede)
|
|
|
|
|
.map(p => {
|
|
|
|
|
return {
|
|
|
|
|
label: p.name,
|
|
|
|
|
action: 'possession',
|
|
|
|
|
possessionid: p.system.possessionid,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|