demondownload

Import from Workshop

Oct 28th, 2024
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const parseFile = (html) => {
  2.   const file = html.find("#file")[0].files[0];
  3.   const type = html.find("#type")[0].value;
  4.  
  5.   var reader = new FileReader();
  6.   reader.onload = async (e) => {
  7.     const data = JSON.parse(e.target.result);
  8.     let actor = null;
  9.  
  10.     switch (type) {
  11.       case "pilot":
  12.         actor = await buildPilot(data);
  13.         break;
  14.       case "mech":
  15.         actor = await buildMech(data);
  16.         break;
  17.       default:
  18.         ui.notifications.error("Invalid type");
  19.         break;
  20.     }
  21.  
  22.     if(actor){
  23.       actor.sheet.render(true)
  24.     }
  25.   };
  26.   reader.readAsText(file);
  27. };
  28.  
  29. const buildPilot = async (data) => {
  30.   const pilot = await Actor.create({
  31.     name: data.callsign,
  32.     type: 'pilot',
  33.     system: {
  34.       appearance: data.appearance,
  35.       background: data.background,
  36.       class: data.coreClassId,
  37.       motto: data.motto,
  38.       keepsake: data.keepsake,
  39.       skillPoints: data.trainingPoints,
  40.     }
  41.   })
  42.  
  43.   const abilities = game.packs.get('salvage-union.abilities').index
  44.   const equipment = game.packs.get('salvage-union.equipment').index
  45.  
  46.   for(const abilityId of data.abilityIds){
  47.     let ability = abilities.getName(abilityId)
  48.     if(ability) {
  49.       ability = await fromUuid(ability.uuid)
  50.     } else {
  51.       ability = {
  52.         name: abilityId,
  53.         type: 'equipment'
  54.       }
  55.     }
  56.     await pilot.createEmbeddedDocuments('Item', [{...ability}])
  57.   }
  58.  
  59.   for(const ownedGear of data.ownedGear){
  60.     let gear = equipment.getName(ownedGear.name);
  61.     if(gear){
  62.       gear = await fromUuid(gear.uuid)
  63.     }else {
  64.       gear = {
  65.         name: ownedGear.name,
  66.         type: "equipment",
  67.         inventorySlots: ownedGear.inventorySlots,
  68.       };
  69.     }
  70.     await pilot.createEmbeddedDocuments("Item", [{ ...gear }]);
  71.   }
  72.   return pilot
  73. };
  74.  
  75. const buildMech = async (data) => {
  76.   const mech = await Actor.create({
  77.     name: data.name,
  78.     type: 'mech',
  79.     system: {}
  80.   })
  81.  
  82.   const chassisPack = game.packs.get('salvage-union.chassis').index
  83.   let chassis = chassisPack.getName(data.baseChassisId)
  84.   if(chassis) {
  85.     chassis = await fromUuid(chassis.uuid)
  86.   } else {
  87.     chassis = {
  88.       name: data.baseChassisId,
  89.       type: 'chassis'
  90.     }
  91.   }
  92.   await mech.createEmbeddedDocuments('Item', [{...chassis}])
  93.  
  94.   const systemsPack = game.packs.get('salvage-union.systems').index
  95.   const modulesPack = game.packs.get('salvage-union.modules').index
  96.   for(const installedSystem of data.installedSystems){
  97.     let pack = installedSystem.systemOrModule === 0 ? systemsPack : modulesPack
  98.     let item = pack.getName(installedSystem.name);
  99.     if (item) {
  100.       item = await fromUuid(item.uuid);
  101.     } else {
  102.       item = {
  103.         type: installedSystem.systemOrModule === 0 ? 'system' : 'module',
  104.         name: installedSystem.name,
  105.       };
  106.     }
  107.     await mech.createEmbeddedDocuments('Item', [{...item}])
  108.   }
  109.  
  110.   return mech
  111. };
  112.  
  113. await new Promise((resolve) => {
  114.   new Dialog({
  115.     title: "Import from S.U. Workshop",
  116.     content: `<form>
  117.         <div class="form-group">
  118.           <label style="max-width:100px">JSON</label>
  119.           <div class="form-fields">
  120.             <input id="file" type="file"/>
  121.           </div>
  122.         </div>
  123.         <div class="form-group">
  124.           <label style="max-width: 100px">Type</label>
  125.           <div class="form-fields">
  126.             <select id="type">
  127.               <option value="pilot">Pilot</option>
  128.               <option value="mech">Mech</option>
  129.             </select>
  130.           </div>
  131.         </div>
  132.       </form>`,
  133.     buttons: {
  134.       roll: {
  135.         icon: '<i class="fas fa-file-import"></i>',
  136.         label: "Import",
  137.         callback: (html) => resolve(parseFile(html)),
  138.       },
  139.     },
  140.     close: () => resolve(null),
  141.   }).render(true);
  142. });
Advertisement
Add Comment
Please, Sign In to add comment