Working on 0.8.x

- Title's Advancements are now reflected on actor
- Migration update
This commit is contained in:
Vlyan
2021-05-22 17:20:17 +02:00
parent 4f9b72c63f
commit 53f04e6cef
33 changed files with 423 additions and 281 deletions

View File

@@ -58,9 +58,19 @@ export class BaseSheetL5r5e extends ActorSheet {
// Add tech the character knows
sheetData.items.forEach((item) => {
if (item.type === "technique") {
out[item.data.technique_type].push(item);
}
switch (item.type) {
case "technique":
out[item.data.technique_type].push(item);
break;
case "title":
Array.from(item.data.items).forEach(([id, embedItem]) => {
if (embedItem.data.type === "technique") {
out[embedItem.data.data.technique_type].push(embedItem.data);
}
});
break;
} //swi
});
// Remove unused techs
@@ -170,35 +180,44 @@ export class BaseSheetL5r5e extends ActorSheet {
) {
return;
}
item = item.toJSON();
// Dropped a item with same "id" as one owned, add qte instead
if (item.data.quantity && this.actor.data.items) {
const tmpItem = this.actor.data.items.find((e) => e.name === item.name && e.type === item.type);
if (item.data.data.quantity && this.actor.data.items) {
const tmpItem = this.actor.data.items.find((e) => e.name === item.data.name && e.type === item.data.type);
if (tmpItem && this._modifyQuantity(tmpItem.id, 1)) {
return;
}
}
// Item subtype specific
switch (item.type) {
switch (item.data.type) {
case "bond": // no break
case "advancement": // no break
case "peculiarity": // no break
case "item_pattern": // no break
case "signature_scroll":
// Modify the bought at rank to the current actor rank
if (this.actor.data.data.identity?.school_rank) {
item.data.bought_at_rank = this.actor.data.data.identity.school_rank;
item.data.data.bought_at_rank = this.actor.data.data.identity.school_rank;
}
break;
case "advancement":
// Modify the bought at rank to the current actor rank
if (this.actor.data.data.identity?.school_rank) {
item.data.data.bought_at_rank = this.actor.data.data.identity.school_rank;
}
// Specific advancements, remove 1 to selected ring/skill
await this.actor.addBonus(item);
break;
case "technique":
// School_ability and mastery_ability, allow only 1 per type
if (CONFIG.l5r5e.techniques.get(item.data.technique_type)?.type === "school") {
if (CONFIG.l5r5e.techniques.get(item.data.data.technique_type)?.type === "school") {
if (
Array.from(this.actor.items).some(
(e) => e.type === "technique" && e.data.data.technique_type === item.data.technique_type
(e) =>
e.type === "technique" && e.data.data.technique_type === item.data.data.technique_type
)
) {
ui.notifications.info(game.i18n.localize("l5r5e.techniques.only_one"));
@@ -206,24 +225,25 @@ export class BaseSheetL5r5e extends ActorSheet {
}
// No cost for schools
item.data.xp_cost = 0;
item.data.xp_used = 0;
item.data.in_curriculum = true;
item.data.data.xp_cost = 0;
item.data.data.xp_used = 0;
item.data.data.in_curriculum = true;
} else {
// Check if technique is allowed for this character
if (!game.user.isGM && !this.actor.data.data.techniques[item.data.technique_type]) {
if (!game.user.isGM && !this.actor.data.data.techniques[item.data.data.technique_type]) {
ui.notifications.info(game.i18n.localize("l5r5e.techniques.not_allowed"));
return;
}
// Verify cost
item.data.xp_cost = item.data.xp_cost > 0 ? item.data.xp_cost : CONFIG.l5r5e.xp.techniqueCost;
item.data.xp_used = item.data.xp_cost;
item.data.data.xp_cost =
item.data.data.xp_cost > 0 ? item.data.data.xp_cost : CONFIG.l5r5e.xp.techniqueCost;
item.data.data.xp_used = item.data.data.xp_cost;
}
// Modify the bought at rank to the current actor rank
if (this.actor.data.data.identity?.school_rank) {
item.data.bought_at_rank = this.actor.data.data.identity.school_rank;
item.data.data.bought_at_rank = this.actor.data.data.identity.school_rank;
}
break;
}
@@ -234,7 +254,7 @@ export class BaseSheetL5r5e extends ActorSheet {
return;
}
return this._onDropItemCreate(item);
return this._onDropItemCreate(item.data.toObject(false));
}
/**
@@ -395,12 +415,25 @@ export class BaseSheetL5r5e extends ActorSheet {
event.preventDefault();
event.stopPropagation();
let item;
const itemId = $(event.currentTarget).data("item-id");
if (!itemId) {
return;
}
const item = this.actor.items.get(itemId);
const itemParentId = $(event.currentTarget).data("item-parent-id");
if (itemParentId) {
// Embed Item
const parentItem = this.actor.items.get(itemParentId);
if (!parentItem) {
return;
}
item = parentItem.items.get(itemId);
} else {
// Regular item
item = this.actor.items.get(itemId);
}
if (!item) {
return;
}
@@ -421,35 +454,20 @@ export class BaseSheetL5r5e extends ActorSheet {
return;
}
// Remove 1 qty if possible
const tmpItem = this.actor.items.get(itemId);
if (tmpItem && tmpItem.data.data.quantity > 1 && this._modifyQuantity(tmpItem.id, -1)) {
if (!tmpItem) {
return;
}
// Remove 1 qty if possible
if (tmpItem.data.data.quantity > 1 && this._modifyQuantity(tmpItem.id, -1)) {
return;
}
const callback = async () => {
// Specific advancements, remove 1 to selected ring/skill
if (tmpItem.type === "advancement") {
const actor = duplicate(this.actor.data.data);
const itmData = tmpItem.data.data;
if (itmData.advancement_type === "ring") {
// Ring
actor.rings[itmData.ring] = Math.max(1, actor.rings[itmData.ring] - 1);
} else {
// Skill
const skillCatId = CONFIG.l5r5e.skills.get(itmData.skill);
if (skillCatId) {
actor.skills[skillCatId][itmData.skill] = Math.max(
0,
actor.skills[skillCatId][itmData.skill] - 1
);
}
}
// Update Actor
this.actor.update({
data: diffObject(this.actor.data.data, actor),
});
await this.actor.removeBonus(tmpItem);
}
return this.actor.deleteEmbeddedDocuments("Item", [itemId]);
};

View File

@@ -49,13 +49,10 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e {
// Split Money
sheetData.data.data.money = this._zeniToMoney(this.actor.data.data.zeni);
// Split school advancements by rank, and calculate xp spent
// Split school advancements by rank, and calculate xp spent and add it to total
this._prepareSchoolAdvancement(sheetData);
// Titles
this._prepareTitles(sheetData);
// Others
// Split Others advancements, and calculate xp spent and add it to total
this._prepareOthersAdvancement(sheetData);
// Total
@@ -105,13 +102,6 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e {
.activate("advancement_rank_" + (this.actor.data.data.identity.school_rank || 0));
}
/**
* Prepare Titles, and get xp spend
*/
_prepareTitles(sheetData) {
// TODO
}
/**
* Split the school advancement, calculate the total xp spent and the current total xp spent by rank
*/
@@ -148,12 +138,23 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e {
* Prepare Bonds, Item Pattern, Signature Scroll and get xp spend
*/
_prepareOthersAdvancement(sheetData) {
// Split OthersAdvancement from items
sheetData.data.advancementsOthers = sheetData.items.filter((item) =>
["bond", "item_pattern", "title", "signature_scroll"].includes(item.type)
);
// Sort by rank desc
// sheetData.data.bondsList.sort((a, b) => (b.data.rank || 0) - (a.data.rank || 0));
sheetData.data.advancementsOthers.sort((a, b) => (b.data.rank || 0) - (a.data.rank || 0));
// Total xp spent
sheetData.data.advancementsOthersTotalXp = sheetData.data.advancementsOthers.reduce(
(acc, item) => acc + (item.data.xp_used || 0),
0
);
// Update the total spent
sheetData.data.data.xp_spent =
parseInt(sheetData.data.data.xp_spent) + sheetData.data.advancementsOthersTotalXp;
}
/**
@@ -177,6 +178,12 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e {
return super._updateObject(event, formData);
}
/**
* Convert a sum in Zeni to Zeni, Bu and Koku
* @param {number} zeni
* @return {{bu: number, koku: number, zeni: number}}
* @private
*/
_zeniToMoney(zeni) {
const money = {
koku: 0,
@@ -196,6 +203,14 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e {
return money;
}
/**
* Convert a sum in Zeni, Bu and Koku to Zeni
* @param {number} koku
* @param {number} bu
* @param {number} zeni
* @return {number}
* @private
*/
_moneyToZeni(koku, bu, zeni) {
return Math.floor(koku * CONFIG.l5r5e.money[0]) + Math.floor(bu * CONFIG.l5r5e.money[1]) + Math.floor(zeni);
}

View File

@@ -287,11 +287,14 @@ export class TwentyQuestions {
// Clear and add items to actor
const deleteIds = actor.data.items.map((e) => e.id);
await actor.deleteEmbeddedDocuments("Item", deleteIds);
if (deleteIds.length > 0) {
await actor.deleteEmbeddedDocuments("Item", deleteIds);
}
// Add items in 20Q to actor
for (const types of Object.values(itemsCache)) {
for (const item of types) {
const newItemsData = [];
Object.values(itemsCache).forEach((types) => {
types.forEach((item) => {
const itemData = foundry.utils.duplicate(item.data);
if (itemData.data?.bought_at_rank) {
itemData.data.bought_at_rank = 0;
@@ -299,8 +302,11 @@ export class TwentyQuestions {
if (itemData.data?.xp_spent) {
itemData.data.xp_spent = 0;
}
await actor.createEmbeddedDocuments("Item", [itemData]);
}
newItemsData.push(itemData);
});
});
if (newItemsData.length > 0) {
await actor.createEmbeddedDocuments("Item", newItemsData);
}
// Update actor