Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const parseFile = (html) => {
- const file = html.find("#file")[0].files[0];
- const type = html.find("#type")[0].value;
- var reader = new FileReader();
- reader.onload = async (e) => {
- const data = JSON.parse(e.target.result);
- let actor = null;
- switch (type) {
- case "pilot":
- actor = await buildPilot(data);
- break;
- case "mech":
- actor = await buildMech(data);
- break;
- default:
- ui.notifications.error("Invalid type");
- break;
- }
- if(actor){
- actor.sheet.render(true)
- }
- };
- reader.readAsText(file);
- };
- const buildPilot = async (data) => {
- const pilot = await Actor.create({
- name: data.callsign,
- type: 'pilot',
- system: {
- appearance: data.appearance,
- background: data.background,
- class: data.coreClassId,
- motto: data.motto,
- keepsake: data.keepsake,
- skillPoints: data.trainingPoints,
- }
- })
- const abilities = game.packs.get('salvage-union.abilities').index
- const equipment = game.packs.get('salvage-union.equipment').index
- for(const abilityId of data.abilityIds){
- let ability = abilities.getName(abilityId)
- if(ability) {
- ability = await fromUuid(ability.uuid)
- } else {
- ability = {
- name: abilityId,
- type: 'equipment'
- }
- }
- await pilot.createEmbeddedDocuments('Item', [{...ability}])
- }
- for(const ownedGear of data.ownedGear){
- let gear = equipment.getName(ownedGear.name);
- if(gear){
- gear = await fromUuid(gear.uuid)
- }else {
- gear = {
- name: ownedGear.name,
- type: "equipment",
- inventorySlots: ownedGear.inventorySlots,
- };
- }
- await pilot.createEmbeddedDocuments("Item", [{ ...gear }]);
- }
- return pilot
- };
- const buildMech = async (data) => {
- const mech = await Actor.create({
- name: data.name,
- type: 'mech',
- system: {}
- })
- const chassisPack = game.packs.get('salvage-union.chassis').index
- let chassis = chassisPack.getName(data.baseChassisId)
- if(chassis) {
- chassis = await fromUuid(chassis.uuid)
- } else {
- chassis = {
- name: data.baseChassisId,
- type: 'chassis'
- }
- }
- await mech.createEmbeddedDocuments('Item', [{...chassis}])
- const systemsPack = game.packs.get('salvage-union.systems').index
- const modulesPack = game.packs.get('salvage-union.modules').index
- for(const installedSystem of data.installedSystems){
- let pack = installedSystem.systemOrModule === 0 ? systemsPack : modulesPack
- let item = pack.getName(installedSystem.name);
- if (item) {
- item = await fromUuid(item.uuid);
- } else {
- item = {
- type: installedSystem.systemOrModule === 0 ? 'system' : 'module',
- name: installedSystem.name,
- };
- }
- await mech.createEmbeddedDocuments('Item', [{...item}])
- }
- return mech
- };
- await new Promise((resolve) => {
- new Dialog({
- title: "Import from S.U. Workshop",
- content: `<form>
- <div class="form-group">
- <label style="max-width:100px">JSON</label>
- <div class="form-fields">
- <input id="file" type="file"/>
- </div>
- </div>
- <div class="form-group">
- <label style="max-width: 100px">Type</label>
- <div class="form-fields">
- <select id="type">
- <option value="pilot">Pilot</option>
- <option value="mech">Mech</option>
- </select>
- </div>
- </div>
- </form>`,
- buttons: {
- roll: {
- icon: '<i class="fas fa-file-import"></i>',
- label: "Import",
- callback: (html) => resolve(parseFile(html)),
- },
- },
- close: () => resolve(null),
- }).render(true);
- });
Advertisement
Add Comment
Please, Sign In to add comment