소스 검색

No longer duplicate item data in the player object.

Sam Jaffe 8 년 전
부모
커밋
94703ea03f

+ 0 - 20
resources/Potato.json

@@ -118,17 +118,7 @@
         "name": "Quarterstaff",
         "count": 1,
         "countEquipped": 1,
-        "value": {"pp": 0, "gp": 0, "sp": 0, "cp": 0},
-        "page": "PH116",
-        "slot": "TWO_HANDS",
-        "weight": 4,
         "weapon": {
-          "damage": "1d6",
-          "secondaryDamage": "1d6",
-          "criticalThreat": 20,
-          "criticalDamage": 2,
-          "range": "Melee",
-          "type": "Bludgeoning",
           "masterwork": true
         }
       },
@@ -136,17 +126,7 @@
         "name": "Full Plate Armor",
         "count": 1,
         "countEquipped": 1,
-        "value": {"pp": 0, "gp": 1500, "sp": 0, "cp": 0},
-        "page": "PH123",
-        "slot": "BODY",
-        "weight": 50,
         "armor": {
-          "acBonus": 8,
-          "type": "Heavy",
-          "maxDex": 1,
-          "checkPenalty": -6,
-          "spellFailure": 35,
-          "speed": 15,
           "masterwork": true,
           "bonus": "+1"
         }

+ 1 - 0
resources/items/item.json

@@ -0,0 +1 @@
+[]

+ 16 - 10
src/main/lombok/org/leumasjaffe/charsheet/model/inventory/DDArmor.java

@@ -2,27 +2,33 @@ package org.leumasjaffe.charsheet.model.inventory;
 
 import lombok.AccessLevel;
 import lombok.Data;
-import lombok.EqualsAndHashCode;
+import lombok.experimental.Delegate;
 import lombok.experimental.FieldDefaults;
 
-@Data @EqualsAndHashCode(callSuper=true)
+@Data
 @FieldDefaults(level=AccessLevel.PRIVATE)
-public class DDArmor extends DDEnchantableItem {
+public class DDArmor  {
 	public static enum Type { Light, Medium, Heavy }
-	int acBonus, maxDex, speed, spellFailure, checkPenalty;
-	Type type;
+	@Data @FieldDefaults(level=AccessLevel.PRIVATE)
+	public static class Prototype {
+		int acBonus, maxDex, speed, spellFailure, checkPenalty;
+		Type type;
+	}
+	
+	@Delegate Prototype name = new Prototype();
+	@Delegate DDEnchantedItem enchant = new DDEnchantedItem();
 	
 	public int getActualAcBonus() {
-		return acBonus + bonus.value;
+		return getAcBonus() + getBonus().value;
 	}
 	
 	public int getActualCheckPenalty() {
-		return isMasterwork() ? checkPenalty + 1 : checkPenalty;
+		return isMasterwork() ? getCheckPenalty() + 1 : getCheckPenalty();
 	}
 	
 	public Money getActualValue() {
-		int gp = isMasterwork ? 150 : 0;
-		gp += 1000 * (bonus.value + getEnchantBonus());
-		return Money.fromCopperToGold(100 * gp + getAdHocPrice());
+		int gp = isMasterwork() ? 150 : 0;
+		gp += 1000 * (getBonus().value + enchant.getEnchantBonus());
+		return Money.fromCopperToGold(100 * gp + enchant.getAdHocPrice());
 	}
 }

+ 1 - 1
src/main/lombok/org/leumasjaffe/charsheet/model/inventory/DDEnchantableItem.java

@@ -6,7 +6,7 @@ import java.util.List;
 import lombok.Data;
 
 @Data
-class DDEnchantableItem {
+class DDEnchantedItem {
 	boolean isMasterwork = false;
 	Enhancement bonus = Enhancement.NONE;
 	List<Enchantment> enchantments = new ArrayList<>();

+ 43 - 17
src/main/lombok/org/leumasjaffe/charsheet/model/inventory/DDItem.java

@@ -5,34 +5,53 @@ import java.util.Optional;
 import org.leumasjaffe.charsheet.model.observable.IntValue;
 import org.leumasjaffe.charsheet.model.observable.StringValue;
 
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
 import lombok.AccessLevel;
-import lombok.AllArgsConstructor;
 import lombok.Data;
-import lombok.NoArgsConstructor;
+import lombok.Getter;
+import lombok.experimental.Delegate;
 import lombok.experimental.FieldDefaults;
 
-@AllArgsConstructor @NoArgsConstructor
-@Data
 @FieldDefaults(level=AccessLevel.PRIVATE)
 public class DDItem {
-	String name = "";
-	IntValue count = new IntValue(1);
-	IntValue countEquipped = new IntValue(0);
-	float weight = 0.f;
-	Money value = new Money(0, 0, 0, 0);
-	StringValue page = new StringValue();
-	EquipmentSlot slot = EquipmentSlot.NONE;
-	Optional<DDWeapon> weapon;
-	Optional<DDArmor> armor;
+	@Data @FieldDefaults(level=AccessLevel.PRIVATE)
+	public static class Prototype {
+		String name = "";
+		float weight = 0.f;
+		Money value = new Money(0, 0, 0, 0);
+		StringValue page = new StringValue();
+		EquipmentSlot slot = EquipmentSlot.NONE;
+		Optional<DDWeapon> weapon = Optional.empty();
+		Optional<DDArmor> armor = Optional.empty();
+	}
 	
+	@Delegate Prototype name;
+	@Getter IntValue count = new IntValue(1);
+	@Getter IntValue countEquipped = new IntValue(0);
+
+	@JsonCreator
+	public DDItem(@JsonProperty("name") String name, 
+			@JsonProperty("count") int count, 
+			@JsonProperty("countEquipped") int countEquipped, 
+			@JsonProperty("weapon") Optional<DDEnchantedItem> weapon, 
+			@JsonProperty("armor") Optional<DDEnchantedItem> armor) {
+		this.name = DDItemFactory.loadItem(name);
+		this.count.value(count);
+		this.countEquipped.value(countEquipped);
+		weapon.ifPresent(e -> getWeapon().get().setEnchant(e));
+		armor.ifPresent(e -> getArmor().get().setEnchant(e));
+	}
+		
 	public String getFullName() {
-		return weapon.map(DDWeapon::getNameModifier).orElse(
-				armor.map(DDArmor::getNameModifier).orElse("")) + getName();
+		return getWeapon().map(DDWeapon::getNameModifier).orElse(
+				getArmor().map(DDArmor::getNameModifier).orElse("")) + getName();
 	}
 	
 	public Money getActualValue() {
-		return getValue().sum(weapon.map(DDWeapon::getActualValue).orElse(
-				armor.map(DDArmor::getActualValue).orElse(Money.fromCopper(0))));
+		return getValue().sum(getWeapon().map(DDWeapon::getActualValue).orElse(
+				getArmor().map(DDArmor::getActualValue).orElse(Money.fromCopper(0))));
 	}
 	
 	public void adjustCount(int amt) {
@@ -46,4 +65,11 @@ public class DDItem {
 	public int getUnequippedCount() {
 		return count.value() - countEquipped.value();
 	}
+
+	@JsonProperty("name")
+	private String json_getName() { return getName(); }
+	@JsonProperty("armor")
+	private Optional<DDEnchantedItem> json_getArmor() { return getWeapon().map(DDWeapon::getEnchant); }
+	@JsonProperty("weapon")
+	private Optional<DDEnchantedItem> json_getWeapon() { return getArmor().map(DDArmor::getEnchant); }
 }

+ 51 - 0
src/main/lombok/org/leumasjaffe/charsheet/model/inventory/DDItemFactory.java

@@ -0,0 +1,51 @@
+package org.leumasjaffe.charsheet.model.inventory;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+
+import lombok.AccessLevel;
+import lombok.SneakyThrows;
+import lombok.Synchronized;
+import lombok.experimental.FieldDefaults;
+import lombok.experimental.UtilityClass;
+
+@UtilityClass
+@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
+final class DDItemFactory {
+	Set<String> resourcesLoaded = new HashSet<>();
+	Map<String, DDItem.Prototype> itemStore = new HashMap<>();
+	
+	ObjectMapper mapper = new ObjectMapper();
+
+	
+	static {
+		mapper.registerModule(new Jdk8Module());
+		loadIfAbsent("resources/items/item.json");
+		loadIfAbsent("resources/items/armor.json");
+		loadIfAbsent("resources/items/weapon.json");
+	}
+	
+	public DDItem.Prototype loadItem(final String name) {
+		return itemStore.get(name);
+	}
+	
+	@Synchronized
+	@SneakyThrows
+	private void loadIfAbsent(final String rname) {
+		final List<DDItem.Prototype> temp = mapper.readValue(
+				new File(rname),
+				new TypeReference<ArrayList<DDItem.Prototype>>() {
+				});
+		resourcesLoaded.add(rname);
+		temp.forEach(s -> itemStore.put(s.getName(), s));
+	}
+}

+ 23 - 19
src/main/lombok/org/leumasjaffe/charsheet/model/inventory/DDWeapon.java

@@ -2,44 +2,48 @@ package org.leumasjaffe.charsheet.model.inventory;
 
 import lombok.AccessLevel;
 import lombok.Data;
-import lombok.EqualsAndHashCode;
+import lombok.experimental.Delegate;
 import lombok.experimental.FieldDefaults;
 
-@Data @EqualsAndHashCode(callSuper=true)
-@FieldDefaults(level=AccessLevel.PRIVATE)
-public class DDWeapon extends DDEnchantableItem {
+@Data @FieldDefaults(level=AccessLevel.PRIVATE)
+public class DDWeapon {
 	public static enum Type { Piercing, Bludgeoning, Slashing }
-
-	String damage;
-	String secondaryDamage;
-	int criticalThreat;
-	int criticalDamage;
-	int secondaryCriticalDamage;
+	@Data @FieldDefaults(level=AccessLevel.PRIVATE)
+	public static class Prototype {
+		String damage;
+		String secondaryDamage;
+		int criticalThreat;
+		int criticalDamage;
+		int secondaryCriticalDamage;
+		
+		Range range;
+		Type type;
+	}
 	
-	Range range;
-	Type type;
+	@Delegate Prototype name = new Prototype();
+	@Delegate DDEnchantedItem enchant = new DDEnchantedItem();
 	
 	public Money getActualValue() {
-		int gp = isMasterwork ? 300 : 0;
+		int gp = isMasterwork() ? 300 : 0;
 		if (hasSecondaryAttack()) gp *= 2;
-		gp += 2000 * (bonus.value + getEnchantBonus());
-		return Money.fromCopperToGold(100 * gp + getAdHocPrice());
+		gp += 2000 * (getBonus().value + enchant.getEnchantBonus());
+		return Money.fromCopperToGold(100 * gp + enchant.getAdHocPrice());
 	}
 
 	public boolean hasCriticalThreat() {
-		return criticalThreat != 20 && criticalThreat != 0;
+		return getCriticalThreat() != 20 && getCriticalThreat() != 0;
 	}
 
 	public boolean hasSecondaryAttack() {
-		return secondaryDamage != null && !secondaryDamage.isEmpty();
+		return getSecondaryDamage() != null && !getSecondaryDamage().isEmpty();
 	}
 	
 	public int getDamageBonus() {
-		return bonus.getValue();
+		return getBonus().getValue();
 	}
 
 	public int getAttackBonus() {
-		return bonus.getValue() == 0 && isMasterwork ? 1 : bonus.getValue();
+		return getBonus().getValue() == 0 && isMasterwork() ? 1 : getBonus().getValue();
 	}
 
 }