fvtt-pegasus-rpg/modules/pegasus-create-char.js

428 lines
15 KiB
JavaScript
Raw Normal View History

2022-01-06 18:22:05 +01:00
import { PegasusUtility } from "./pegasus-utility.js";
import { PegasusActor } from "./pegasus-actor.js";
import { PegasusActorSheet } from "./pegasus-actor-sheet.js";
2022-01-13 21:05:55 +01:00
import { PegasusItemSheet } from "./pegasus-item-sheet.js";
2022-01-06 18:22:05 +01:00
export class PegasusActorCreate {
/* -------------------------------------------- */
async start() {
this.actor = await Actor.create({
name: "New Actor",
type: "character"
});
2022-02-16 12:14:34 +01:00
this.actor.sheet.render(true)
this.actor.computeValue = true // To force value computation
const racesPack = await PegasusUtility.loadCompendium("fvtt-pegasus-rpg.race")
this.races = racesPack.map(i => i.toObject())
const rolesPack = await PegasusUtility.loadCompendium("fvtt-pegasus-rpg.role")
this.roles = rolesPack.map(i => i.toObject())
2022-11-26 09:49:59 +01:00
const perksPack = await PegasusUtility.loadCompendium("fvtt-pegasus-rpg.perks")
2022-02-16 12:14:34 +01:00
this.perks = perksPack.map(i => i.toObject())
2022-09-26 20:57:55 +02:00
const specPack = await PegasusUtility.loadCompendium("fvtt-pegasus-rpg.specialisations")
this.specs = specPack.map(i => i.toObject())
2022-01-06 18:22:05 +01:00
this.showRaces()
}
/* -------------------------------------------- */
createFormData(step) {
let formData = {
name: this.actor.name,
img: this.actor.img,
step: step,
races: this.races,
2022-01-07 20:40:40 +01:00
roles: this.roles,
nboptionnal: this.raceOptionnalAbilities?.nboptionnal?? 0,
optionnalabilities: this.raceOptionnalAbilities?.optionnalabilities?? [],
}
if ( this.raceSelectableStats ) {
formData.numberstats = this.raceSelectableStats.numberstats;
formData.statsonlyonce = this.raceSelectableStats.statsonlyonce;
formData.stats = this.raceSelectableStats.stats;
2022-01-06 18:22:05 +01:00
}
return formData;
}
2022-09-26 20:57:55 +02:00
/* -------------------------------------------- */
getSpecFromRoleStat( role) {
let specList = []
for(let stat of role.system.statincreasechoice) {
if (stat.flag) {
specList = specList.concat( this.specs.filter( spec => spec.system.statistic.toLowerCase() == stat.name.toLowerCase() ))
}
}
return specList
}
/* -------------------------------------------- */
getPerksFromRole( role ) {
let perks = this.perks.filter( perk => perk.system.category.toLowerCase() == role.system.perksrole.toLowerCase())
return perks
}
2022-01-06 18:22:05 +01:00
/* -------------------------------------------- */
processChatEvent( event ) {
const step = $(event.currentTarget).data("step-name");
const itemId = $(event.currentTarget).data("item-id");
2022-01-08 18:28:01 +01:00
2022-01-06 18:22:05 +01:00
if ( step == "select-race") {
let race = this.races.find( item => item._id == itemId);
2022-01-08 18:28:01 +01:00
this.currentRace = race;
2022-01-07 20:40:40 +01:00
this.actor.applyRace( race);
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
2022-09-26 20:57:55 +02:00
if ( race.system.nboptionnal > 0 && race.system.optionnalabilities.length > 0) {
2022-01-07 20:40:40 +01:00
this.manageOptionnalAbilities(race);
} else {
2022-09-26 20:57:55 +02:00
if ( race.system.selectablestats ) {
2022-01-07 20:40:40 +01:00
this.manageSelectableStats(race);
2022-09-26 20:57:55 +02:00
} else if ( race.system.perksgained) {
2022-01-12 16:25:55 +01:00
this.manageRacePerks(race);
2022-01-07 20:40:40 +01:00
} else {
this.showRoles()
}
}
}
if ( step == 'select-race-optionnal') {
let ability = this.raceOptionnalAbilities.optionnalabilities.find( item => item._id == itemId);
2022-01-11 23:35:23 +01:00
let update = []
this.actor.applyAbility( ability, update );
this.actor.update( update )
2022-01-07 20:40:40 +01:00
this.actor.createEmbeddedDocuments( 'Item', [ability]);
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
this.raceOptionnalAbilities.optionnalabilities = this.raceOptionnalAbilities.optionnalabilities.filter( item => item._id != itemId);
this.raceOptionnalAbilities.nboptionnal -= 1;
this.processOptionnalAbilitiesStep();
}
if (step == 'select-race-stats') {
let statKey = $(event.currentTarget).data("stat-key");
this.actor.modStat( statKey, 1);
this.raceSelectableStats.stats[statKey].used = true;
this.raceSelectableStats.numberstats -=1;
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
this.processSelectableStats();
}
2022-01-12 16:25:55 +01:00
if (step == 'select-race-perks') {
2022-01-08 18:28:01 +01:00
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
let perk = this.racePerks.find( item => item._id == itemId);
this.actor.createEmbeddedDocuments( 'Item', [perk]);
this.racePerks = this.racePerks.filter( item => item._id != itemId);
2022-01-12 16:25:55 +01:00
this.nbRacePerks -= 1;
if ( this.nbRacePerks == 0 || this.racePerks.length == 0) {
2022-01-08 18:28:01 +01:00
this.showRoles()
2022-01-12 16:25:55 +01:00
} else {
this.manageRacePerks()
2022-01-08 18:28:01 +01:00
}
}
2022-01-07 20:40:40 +01:00
if ( step == 'select-role') {
let role = this.roles.find( item => item._id == itemId);
2022-01-08 10:49:08 +01:00
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
2022-01-07 20:40:40 +01:00
this.actor.applyRole( role );
2022-01-08 10:49:08 +01:00
this.currentRole = role;
this.nbRoleStat = 2;
2022-09-26 20:57:55 +02:00
this.roleStats = duplicate(role.system.statincreasechoice)
2022-01-10 08:00:27 +01:00
this.showRoleStartSpec( );
}
if ( step == 'select-role-start-spec') {
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
let spec = this.roleSpecStart.find( item => item._id == itemId);
this.actor.addIncSpec(spec, 1);
this.nbRoleSpecStart--;
this.roleSpecStart = this.roleSpecStart.filter( item => item._id != itemId);//Remove selected spec
if( this.nbRoleSpecStart == 0) {
this.showRoleStat( );
} else {
this.showRoleStartSpec( );
}
2022-01-08 10:49:08 +01:00
}
if ( step == 'select-role-stat') {
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
let statKey = $(event.currentTarget).data("stat-key");
this.actor.valueStat( statKey, 1);
for (let stat of this.roleStats ) {
if ( stat.name.toLowerCase() == statKey.toLowerCase()) {
stat.flag = false
}
}
this.nbRoleStat--;
if ( this.nbRoleStat == 0 || this.roleStats.length == 0) {
2022-09-26 20:57:55 +02:00
this.roleSpec = this.getSpecFromRoleStat( this.currentRole )
2022-01-08 10:49:08 +01:00
this.nbDT2 = 1;
this.nbDT1 = 2;
this.showRoleSpecialisations()
} else{
this.showRoleStat( );
}
}
if (step == 'select-role-spec') {
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
let spec = this.roleSpec.find( item => item._id == itemId);
if (this.nbDT2 > 0) {
this.actor.addIncSpec(spec, 2)
this.nbDT2--;
} else {
this.actor.addIncSpec(spec, 1)
this.nbDT1--;
}
this.roleSpec = this.roleSpec.filter( item => item._id != itemId);//Remove selected spec
if ( this.nbDT1 == 0 || this.roleSpec.length == 0) {
2022-09-26 20:57:55 +02:00
this.rolePerks = this.getPerksFromRole( this.currentRole ) // duplicate(this.currentRole.data.perks)
2022-01-08 10:49:08 +01:00
this.nbPerks = 2;
this.showRolePerks()
} else {
this.showRoleSpecialisations()
}
}
if (step == 'select-role-perk') {
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
let perk = this.rolePerks.find( item => item._id == itemId);
this.actor.addItemWithoutDuplicate(perk)
this.nbPerks--;
this.rolePerks = this.rolePerks.filter( item => item._id != itemId);//Remove selected perk
if (this.nbPerks == 0 || this.rolePerks.length == 0) {
2022-11-26 11:05:41 +01:00
this.nbGlobalSpec = 5
this.showGlobalSpec()
2022-01-08 10:49:08 +01:00
} else {
this.showRolePerks()
}
2022-01-06 18:22:05 +01:00
}
2022-11-26 11:05:41 +01:00
if (step == 'select-global-spec') {
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
let spec = this.specs.find( item => item._id == itemId);
this.actor.addIncSpec(spec, 1)
this.nbGlobalSpec--;
if (this.nbGlobalSpec == 0 ) {
this.showCharacterEnd()
} else {
this.showGlobalSpec()
}
}
2022-01-06 18:22:05 +01:00
}
2022-01-13 21:05:55 +01:00
/* -------------------------------------------- */
async openItemView( event) {
let step = $(event.currentTarget).data("step");
let itemId = $(event.currentTarget).data("item-id");
let itemData
if ( step == 'select-race') {
itemData = this.races.find( item => item._id == itemId );
}
if ( step == 'select-race-optionnal') {
itemData = this.raceOptionnalAbilities.optionnalabilities.find( item => item._id == itemId );
}
if ( step == 'select-race-perks') {
itemData = this.perks.find( item => item._id == itemId );
}
if ( step == 'select-race-perks'){
itemData = this.racePerks.find( item => item._id == itemId );
}
if ( step == 'select-role') {
itemData = this.roles.find( item => item._id == itemId );
}
if ( step == 'select-role-start-spec') {
itemData = this.roleSpecStart.find( item => item._id == itemId );
}
if (step == 'select-role-spec') {
itemData = this.roleSpec.find( item => item._id == itemId );
}
if (step == 'select-role-perk') {
itemData = this.rolePerks.find( item => item._id == itemId );
}
if (itemData) {
let item = await Item.create(itemData, {temporary: true});
new PegasusItemSheet(item).render(true);
}
}
2022-01-07 20:40:40 +01:00
/* -------------------------------------------- */
manageSelectableStats( race ) {
this.raceSelectableStats = {
"race": race,
"statsonlyonce": race.data.statsonlyonce,
"numberstats": race.data.numberstats,
"stats": duplicate(this.actor.data.data.statistics)
}
this.processSelectableStats()
}
2022-01-08 18:28:01 +01:00
/* -------------------------------------------- */
async renderChatMessage( formData) {
let chatData = {
user: game.user.id,
alias : this.actor.name,
rollMode: game.settings.get("core", "rollMode"),
whisper: [game.user.id].concat( ChatMessage.getWhisperRecipients('GM') ),
content: await renderTemplate('systems/fvtt-pegasus-rpg/templates/chat-create-actor.html', formData)
};
//console.log("Apply damage chat", chatData );
await ChatMessage.create( chatData );
}
/* --------------- -------------------- --------- */
manageRacePerks(race) {
2022-01-12 16:25:55 +01:00
if ( !this.currentRace.data.perksgained ) {
this.showRoles()
return;
2022-01-08 18:28:01 +01:00
}
2022-01-12 16:25:55 +01:00
if ( !this.racePerks) { // First init
if ( this.currentRace.data.perksall) {
this.racePerks = duplicate(this.perks)
} else {
this.racePerks = duplicate(this.currentRace.data.perks)
}
this.nbRacePerks = this.currentRace.data.perksnumber;
2022-01-08 18:28:01 +01:00
}
2022-01-12 16:25:55 +01:00
let formData = this.createFormData("select-race-perks")
formData.raceperks = this.racePerks;
formData.nbraceperks = this.nbRacePerks;
2022-01-08 18:28:01 +01:00
this.renderChatMessage(formData)
}
/* --------------- -------------------- --------- */
2022-01-07 20:40:40 +01:00
async processSelectableStats() {
// End of race options choice
if ( this.raceSelectableStats.numberstats == 0) {
2022-01-12 16:25:55 +01:00
this.manageRacePerks();
2022-01-07 20:40:40 +01:00
return;
}
let formData = this.createFormData("select-race-stats")
let chatData = {
user: game.user.id,
alias : this.actor.name,
rollMode: game.settings.get("core", "rollMode"),
whisper: [game.user.id].concat( ChatMessage.getWhisperRecipients('GM') ),
content: await renderTemplate('systems/fvtt-pegasus-rpg/templates/chat-create-actor.html', formData)
};
//console.log("Apply damage chat", chatData );
await ChatMessage.create( chatData );
}
/* --------------- ----------------------------- */
manageOptionnalAbilities( race) {
this.raceOptionnalAbilities = {
"nboptionnal": race.data.nboptionnal,
"optionnalabilities": duplicate(race.data.optionnalabilities),
}
this.processOptionnalAbilitiesStep()
}
/* -------------------------------------------- */
async processOptionnalAbilitiesStep() {
// End of race options choice
if ( this.raceOptionnalAbilities.nboptionnal == 0) {
if ( this.raceSelectableStats ) {
2022-01-08 18:28:01 +01:00
this.manageSelectableStats(this.currentrace);
2022-09-26 20:57:55 +02:00
} else if ( this.currentRace.system.perksgained) {
2022-01-08 18:28:01 +01:00
this.manageRacePerks(this.currentRace);
2022-01-07 20:40:40 +01:00
} else {
this.showRoles()
}
2022-01-08 18:28:01 +01:00
} else {
let formData = this.createFormData("select-race-optionnal")
this.renderChatMessage( formData)
2022-01-07 20:40:40 +01:00
}
}
2022-01-06 18:22:05 +01:00
/* -------------------------------------------- */
async showRaces() {
let formData = this.createFormData("select-race")
2022-01-08 18:28:01 +01:00
this.renderChatMessage( formData)
2022-01-06 18:22:05 +01:00
}
/* -------------------------------------------- */
async showRoles() {
2022-01-07 20:40:40 +01:00
let formData = this.createFormData("select-role")
2022-01-08 18:28:01 +01:00
this.renderChatMessage( formData)
2022-01-06 18:22:05 +01:00
}
2022-01-08 10:49:08 +01:00
2022-01-10 08:00:27 +01:00
/* ------------------------------- ------------- */
async showRoleStartSpec() {
if ( !this.roleSpecStart) {
2022-09-26 20:57:55 +02:00
this.roleSpecStart = this.specs.filter(spec => spec.system.statistic.toUpperCase() == this.currentRole.system.statincrease1.toUpperCase() || spec.system.statistic.toUpperCase() == this.currentRole.system.statincrease2.toUpperCase() )
console.log("SPEC FOUND", this.roleSpecStart)
//this.roleSpecStart = duplicate(this.currentRole.data.specialisationsplus1)
2022-01-10 08:00:27 +01:00
this.nbRoleSpecStart = 2;
}
let formData = this.createFormData("select-role-start-spec")
formData.rolestartspec = this.roleSpecStart
formData.nbrolespecstart = this.nbRoleSpecStart;
this.renderChatMessage( formData)
}
/* ------------------------------- ------------- */
2022-01-08 10:49:08 +01:00
async showRoleStat( ) {
let formData = this.createFormData("select-role-stat")
formData.rolestats = []
for(let stat of this.roleStats) {
if (stat.flag) {
2022-09-26 20:57:55 +02:00
formData.rolestats.push( duplicate(this.actor.system.statistics[stat.name.toLowerCase()]) )
2022-01-08 10:49:08 +01:00
}
}
2022-02-16 12:14:34 +01:00
//console.log("STAT", this.roleStats, formData)
2022-01-08 18:28:01 +01:00
this.renderChatMessage( formData)
2022-01-08 10:49:08 +01:00
}
/* -------------------------------------------- */
async showRoleSpecialisations() {
let formData = this.createFormData("select-role-spec")
formData.rolespec = duplicate(this.roleSpec)
formData.dt = 1
if (this.nbDT2 > 0 ) {
formData.dt = 2
}
2022-01-08 18:28:01 +01:00
this.renderChatMessage( formData)
2022-01-08 10:49:08 +01:00
}
2022-11-26 11:05:41 +01:00
2022-01-08 10:49:08 +01:00
/* -------------------------------------------- */
async showRolePerks() {
let formData = this.createFormData("select-role-perk")
formData.roleperks = duplicate(this.rolePerks)
formData.nbperks = this.nbPerks
2022-01-08 18:28:01 +01:00
this.renderChatMessage( formData)
2022-01-08 10:49:08 +01:00
}
2022-11-26 11:05:41 +01:00
/* -------------------------------------------- */
async showGlobalSpec() {
let formData = this.createFormData("select-global-spec")
let excludedSpecs = this.actor.items.filter(it => it.type =="specialisation" && it.system.level >= 4)
formData.specs = []
for( let spec of this.specs) {
let isOK = true
for (let excluded of excludedSpecs) {
if (excluded.name == spec.name) {
isOK = false
break
}
}
if (isOK) {
formData.specs.push(spec)
}
}
formData.specs.sort( function compare(a, b) { if (a.name < b.name) {return -1} else {return 1} } )
this.renderChatMessage( formData )
}
2022-01-08 10:49:08 +01:00
/* -------------------------------------------- */
async showCharacterEnd() {
2022-02-16 12:14:34 +01:00
await this.actor.computeNRGHealth()
this.actor.computeValue = false // To force value computation
2022-01-08 10:49:08 +01:00
let formData = this.createFormData("character-end")
2022-01-08 18:28:01 +01:00
this.renderChatMessage( formData)
2022-01-08 10:49:08 +01:00
}
2022-01-06 18:22:05 +01:00
}