Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
Issue #48 Add basic support of Template Generation
Browse files Browse the repository at this point in the history
Ads basic support for the Imperial Asssault Army overlay
generation.  Pages have been split out to the common types
vs the game specific types.
  • Loading branch information
kingargyle committed Nov 1, 2017
1 parent 4c0fc56 commit 977673e
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<#function findDeployment deployment>
<#list allDeployments as rdeployment>
<#if rdeployment.iaspecName = deployment.iaSpecName && rdeployment.faction == deployment.faction>
<#return rdeployment>
</#if>
</#list>
<#return null>
</#function>
Original file line number Diff line number Diff line change
@@ -1,61 +1,50 @@
<div id="army">
<hr/>
<#list iaspec.army.deployments as deployments>
<#assign dbDeployment>${fun.deploymentLookup(deployments.deployment)}</#assign>
<#if dbDeployment??>
<div class="deployment">
<div class="name">
<span class="mercenary"></span><span class="deploymentName">${deployments.deployment.name}</span>
<span>(${deployments.deployment.deploymentCost})</span>
<span class="${dbDeployment.faction.toString()}"></span><span class="deploymentName">${dbDeployment.name}</span>
<span>(${dbDeployment.deploymentCost})</span>
</div>
<#if dbDeployment.attack?? || dbDeployment.defense??>
<div id="attack_defense">
<#if deployments.deployment.attack.attackType == "range">
<#if dbDeployment.attack.attackType.toString() == "range">
<div class="left-text range"></div>
<#else>
<#elseif dbDeployment.attack.attackType == "melee">
<div class="left-text melee"></div>
</#if>
<div class="box red">&nbsp;</div>
<div class="spacer">&nbsp;</div>
<div class="box blue">&nbsp;</div>
<#if dbDeployment.attack.getDicePool()?? && dbDeployment.deploymentType.toString() == "deployment">
<#list dbDeployment.attack.getDicePool() as attackDice>
<#switch attackDice.toString()>
<#case "red"><div class="box red">&nbsp;</div><#break>
<#case "blue"><div class="box blue">&nbsp;</div><#break>
<#case "green"><div class="box green">&nbsp;</div><#break>
<#case "yellow"><div class="box yellow">&nbsp;</div><#break>
<#case "any"><div class="box any">&nbsp;</div><#break>
<#default><#break>
</#switch>
<div class="spacer">&nbsp;</div>
</#list>
</#if>
<#if dbDeployment.getDefenseDicePool()?? && dbDeployment.deploymentType.toString() == "deployment">
<div class="left-text defense">&nbsp;&nbsp;E</div>
<div class="box black">&nbsp;</div>
<#list dbDeployment.getDefenseDicePool() as defense>
<#switch defense.toString()>
<#case "white"><div class="box white">&nbsp;</div><#break>
<#case "black"><div class="box black">&nbsp;</div><#break>
<#default><#break>
</#switch>
</#list>
<div class="spacer">&nbsp;</div>
<div class="box white">&nbsp;</div>
</#if>
<br />
</div>

<#if pilot.pilotSkill??>
<span>${fun.pilotAliveDead(pilot)}</span>
</#if>
<span> ${pilot.name} </span>
<#if pilot.hasCriticalDamage()>
<span> <i class="xwing-miniatures-font xwing-miniatures-font-token-crit red"></i></span>
</#if>
</#if>
<#if dbDeployment.deploymentType.toString() == "deployment">
<div class="vitals">${dbDeployment.getDeploymentCost()}&#10084; ${dbDeployment.getSpeed()}S ${dbDeployment.getUnitsInGroup()}U</div>
</#if>
</div>
<div>
<span><span class="attack-symbol">%</span>&nbsp;<b class="attack">${fun.findShipAttack(pilot.ship)}&nbsp;</b></span>
<span><span class="agility-symbol">^</span>&nbsp;<b class="agility">${fun.findShipAgility(pilot.ship)}&nbsp;</b> </span>
<span><span class="hull-symbol">&</span>&nbsp;<b class="hull">${(pilot.hull)!"0"}&nbsp;</b> </span>
<span><span class="shields-symbol">*</span>&nbsp;<b class="shields">${(pilot.shields)!"0"}</b> &nbsp;&nbsp;</span>
<#if pilot.pilotId!?length gt 0>
<span class="idtag"><b>${pilot.pilotId}</b></span>
</#if>
<span> [${pilot.points}]</span>
</div>

<#if pilot.upgrades??>
<#if pilot.upgrades.additionalProperties??>
<div class="upgrades">
<#list pilot.upgrades.additionalProperties as key, value>
${fun.upgradeIconMarkup(key)}
<#list value as upgradeType>
<#if upgradeType?has_next>
<span>${fun.findUpgrade(upgradeType)}, </span>
<#else>
<span>${fun.findUpgrade(upgradeType)}</span><br/>
</#if>
</#list>
</#list>
</div>
</#if>
</#if>
</#if>
</#list>
</div>
10 changes: 10 additions & 0 deletions bundles/us.nineworlds.xstreamer.ia/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,15 @@
class="us.nineworlds.xstreamer.ia.Startup">
</startup>
</extension>

<extension
point="org.eclipse.ui.preferencePages">
<page
class="us.nineworlds.xstreamer.ia.preferences.ArmyPrefencesPage"
id="us.nineworlds.xstreamer.preferences.IAPreferencePage"
name="Imperial Assault">
</page>
</extension>


</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package us.nineworlds.xstreamer.ia.events;

public class GenerateArmyEvent {

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@
import us.nineworlds.iadata.deployment.Deployments;
import us.nineworlds.iadata.enums.Factions;
import us.nineworlds.iadata.util.IASpecLoader;
import us.nineworlds.xstreamer.eventbus.EventBus;
import us.nineworlds.xstreamer.eventbus.EventHandler;
import us.nineworlds.xstreamer.eventbus.GenerateSquadJobEvent;
import us.nineworlds.xstreamer.ia.core.Activator;
import us.nineworlds.xstreamer.ia.events.GenerateArmyEvent;
import us.nineworlds.xstreamer.ia.jobs.GenerateArmyJob;
import us.nineworlds.xstreamer.ia.listeners.ArmySelectionChangeListener;
import us.nineworlds.xstreamer.ia.lookup.DeploymentsLookup;
import us.nineworlds.xstreamer.ia.model.ArmyContentProvider;
import us.nineworlds.xstreamer.ia.model.ArmyLabelProvider;
import us.nineworlds.xstreamer.jobs.GenerateSquadJob;


public abstract class AbstractPlayerFormPage extends ViewPart {
Expand All @@ -43,6 +49,14 @@ public abstract class AbstractPlayerFormPage extends ViewPart {
public TreeViewer treeViewer;

public Text importArmyText;

private EventBus eventBus;

public AbstractPlayerFormPage() {
super();
eventBus = EventBus.getInstance();
eventBus.register(this);
}

@Override
public void createPartControl(Composite parent) {
Expand Down Expand Up @@ -170,9 +184,7 @@ public void widgetSelected(SelectionEvent e) {

resetPlayerModel(iaspec);
refreshTree();
// GenerateSquadJob job = new GenerateSquadJob("importxws", getPlayerModel(), playerFileName(),
// squadTemplate());
// job.schedule();
eventBus.post(new GenerateArmyEvent());
} catch (Exception e1) {
e1.printStackTrace();
}
Expand Down Expand Up @@ -221,5 +233,10 @@ public void widgetDefaultSelected(SelectionEvent e) {
public void refreshTree() {
treeViewer.setInput(getPlayerModel());
}


@EventHandler
public void updateJob(GenerateArmyEvent event) {
GenerateArmyJob job = new GenerateArmyJob("generateArmy", getPlayerModel(), playerFileName(), armyTemplate());
job.schedule();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,12 @@ public IASpec getPlayerModel() {

@Override
public String playerFileName() {
IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();

return preferenceStore.getString(PreferenceConstants.FIRST_PLAYER_SQUAD_FILENAME);
return "ia_player1_army.html";
}

@Override
public String armyTemplate() {
// IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
// return preferenceStore.getString(PreferenceConstants.TEMPLATE_FIRST_PLAYER_FILE);
return null;
return "army_left_aligned.ftl";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package us.nineworlds.xstreamer.ia.forms;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Composite;

import us.nineworlds.iadata.IASpec;
import us.nineworlds.xstreamer.ia.Activator;
import us.nineworlds.xstreamer.ia.preferences.PreferenceConstants;

public class SecondPlayerPage extends AbstractPlayerFormPage {

Expand Down Expand Up @@ -39,16 +36,12 @@ public IASpec getPlayerModel() {

@Override
public String playerFileName() {
IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();

return preferenceStore.getString(PreferenceConstants.SECOND_PLAYER_SQUAD_FILENAME);
return "ia_player2_army.html";
}

@Override
public String armyTemplate() {
// IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
// return preferenceStore.getString(PreferenceConstants.TEMPLATE_FIRST_PLAYER_FILE);
return null;
return "army_left_aligned.ftl";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.nio.file.Path;
import java.net.URL;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.preference.IPreferenceStore;
import org.osgi.framework.Bundle;

import com.github.xws.XwsSpec;

import freemarker.template.Configuration;
import freemarker.template.Template;
import us.nineworlds.iadata.IASpec;
import us.nineworlds.xstreamer.Activator;
import us.nineworlds.xstreamer.model.lookup.PilotLookup;
import us.nineworlds.xstreamer.model.lookup.ShipsLookup;
import us.nineworlds.xstreamer.model.lookup.UpgradeLookup;
import us.nineworlds.xstreamer.preferences.PreferenceConstants;

public class GenerateArmyJob extends Job {

Expand All @@ -41,10 +41,23 @@ public GenerateArmyJob(String name, IASpec model, String preferncePlayerFileName

@Override
protected IStatus run(IProgressMonitor monitor) {
IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
String templateType = findTemplateType(templateFilename);
String templateOutputDirectory = preferenceStore.getString(PreferenceConstants.TEMPLATE_XSTREAMER_OUTPUT_DIRECTORY);
String templateInputDirectory = preferenceStore.getString(PreferenceConstants.TEMPLATE_INPUT_DIRECTORY) + File.separator + "squads" + File.separator + templateType;
IPreferenceStore iaPreferenceStore = us.nineworlds.xstreamer.ia.Activator.getDefault().getPreferenceStore();

Bundle templateBundle = Platform.getBundle("us.nineworlds.xstreamer.ia.data");
Path path = new Path("templates");
URL url = FileLocator.find(templateBundle, path, null);
URL realUrl = null;
String templateInputDirectory = null;
try {
realUrl = FileLocator.toFileURL(url);
File templateDirectory = FileUtils.toFile(realUrl);
templateInputDirectory = templateDirectory.getCanonicalPath().toString() + File.separator + "squads" + File.separator + "html";
} catch (Exception ex) {

}


String templateOutputDirectory = iaPreferenceStore.getString(us.nineworlds.xstreamer.ia.preferences.PreferenceConstants.TEMPLATE_XSTREAMER_IA_OUTPUT_DIRECTORY);
if (StringUtils.isEmpty(templateOutputDirectory) || StringUtils.isEmpty(playerFilename) ||
StringUtils.isEmpty(templateFilename) || StringUtils.isEmpty(templateInputDirectory)) {
return Status.CANCEL_STATUS;
Expand All @@ -55,8 +68,8 @@ protected IStatus run(IProgressMonitor monitor) {
Configuration config = us.nineworlds.xstreamer.core.Activator.getDefault().getFreemarkerConfig();
config.setDirectoryForTemplateLoading(new File(templateInputDirectory));

Path path = Paths.get(templateFilename);
Template armyTemplate = config.getTemplate(path.getFileName().toString());
java.nio.file.Path jpath = java.nio.file.Paths.get(templateFilename);
Template armyTemplate = config.getTemplate(jpath.getFileName().toString());
playerArmyFile = new FileWriter(new File(templateOutputDirectory + File.separator + playerFilename));

Map<String, Object> input = new HashMap<>();
Expand All @@ -75,10 +88,4 @@ protected IStatus run(IProgressMonitor monitor) {
return Status.OK_STATUS;
}

private String findTemplateType(String filenamePath) {
if (filenamePath.contains("/html/")) {
return "html";
}
return "text";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package us.nineworlds.xstreamer.ia.preferences;

import org.eclipse.jface.preference.*;
import org.eclipse.ui.IWorkbenchPreferencePage;

import us.nineworlds.xstreamer.Activator;
import us.nineworlds.xstreamer.preferences.widgets.LabelFieldEditor;
import us.nineworlds.xstreamer.preferences.widgets.SpacerFieldEditor;

import org.eclipse.ui.IWorkbench;

public class ArmyPrefencesPage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

public ArmyPrefencesPage() {
super(GRID);
setPreferenceStore(Activator.getDefault().getPreferenceStore());
setDescription("Imperial Assault Generation Preferences");
}

public void createFieldEditors() {
addField(new DirectoryFieldEditor(PreferenceConstants.TEMPLATE_XSTREAMER_IA_OUTPUT_DIRECTORY,
"XStreamer Output Directory:", getFieldEditorParent()));
}

public void init(IWorkbench workbench) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
*/
public class PreferenceConstants {

public static final String TEMPLATE_XSTREAMER_IA_OUTPUT_DIRECTORY = "xstreamer.template.ia.output.dir";

public static final String FIRST_PLAYER_ARMY_IASPEC_FILE = "xstreamer.army.player1.iaspec";
public static final String SECOND_PLAYER_ARMY_IASPEC_FILE = "xstreamer.army.player2.iaspec";
public static final String FIRST_PLAYER_ARMY_GENERATED_FILE = "xstreamer.army.player1.file";
public static final String SECOND_PLAYER_ARMY_GENERATED_FILE = "xstreamer.army.player2.file";


public static final String FIRST_PLAYER_SQUAD_FILENAME = "xstreamer.player.first.army.filename";
public static final String SECOND_PLAYER_SQUAD_FILENAME = "xstreamer.player.second.army.filename";
}
Loading

0 comments on commit 977673e

Please sign in to comment.