All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.rpgframework.genericrpg.Pool Maven / Gradle / Ivy

The newest version!
package de.rpgframework.genericrpg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import de.rpgframework.genericrpg.modification.CheckModification;

/**
 * @author prelle
 *
 */
public class Pool implements Cloneable {

	private Map>> poolsBySet;
	private List checkMods;

	//-------------------------------------------------------------------
	public Pool() {
		poolsBySet = new HashMap<>();
		checkMods = new ArrayList<>();
	}

	//-------------------------------------------------------------------
	public Pool(T val) {
		poolsBySet = new HashMap<>();
		addStep(ValueType.NATURAL, new PoolCalculation(val, null));
		checkMods = new ArrayList<>();
	}

	//-------------------------------------------------------------------
	/**
	 * @see java.lang.Object#clone()
	 */
	public Object clone() {
		Pool cloned = new Pool();
		for (Entry>> entry : poolsBySet.entrySet()) {
			cloned.poolsBySet.put(entry.getKey(), new ArrayList>(entry.getValue()));
		}
		return cloned;
	}

	//-------------------------------------------------------------------
	public void addStep(ValueType set, PoolCalculation step) {
		List> list = poolsBySet.get(set);
		if (list==null) {
			list = new ArrayList<>();
			poolsBySet.put(set, list);
		}
		list.add(step);
	}

	//-------------------------------------------------------------------
	public boolean hasSet(ValueType set) {
		return poolsBySet.containsKey(set);
	}

	//-------------------------------------------------------------------
	/**
	 * @param set  NATURAL or ARTIFICIAL
	 * @return
	 */
	public List> getCalculation(ValueType set) {
		if (poolsBySet.containsKey(set))
			return poolsBySet.get(set);
		return new ArrayList<>();
	}

	//-------------------------------------------------------------------
	/**
	 * @param set  NATURAL or ARTIFICIAL
	 * @return
	 */
	public int getValue(ValueType set) {
		return (int)getCalculation(set).stream().collect(Collectors.summarizingInt(pc -> (int)pc.value)).getSum();
	}

	//-------------------------------------------------------------------
	public boolean isModified() {
		return poolsBySet.containsKey(ValueType.ARTIFICIAL)
				||
				poolsBySet.get(ValueType.NATURAL).size()>1;
	}

	//-------------------------------------------------------------------
	@SuppressWarnings("unchecked")
	public String toString() {
		StringBuffer buf = new StringBuffer();
		// Sum Non-Augmented Natural
		int nat = 0;
		int aug = 0;
		if (poolsBySet.get(ValueType.NATURAL) != null) {
			for (PoolCalculation c : poolsBySet.get(ValueType.NATURAL)) {
				if (!c.augment) {
					nat += ((PoolCalculation) c).value;
				} else {
					aug += ((PoolCalculation) c).value;
				}
			}
			;
		}

		buf.append( String.valueOf( nat));
		if ( aug>0 ) {
			buf.append("(");
//			int sum = poolsBySet.get(ValueType.NATURAL).stream()
//				.mapToInt(pc -> ((PoolCalculation)pc).value)
//				.sum();
			buf.append(String.valueOf(nat+aug));
			buf.append(")");
		}

		if (poolsBySet.containsKey(ValueType.ARTIFICIAL)) {
			int sum = poolsBySet.get(ValueType.ARTIFICIAL).stream()
			.mapToInt(pc -> ((PoolCalculation)pc).value)
			.sum();
			buf.append("|"+sum);
		}
		return buf.toString();
	}

	//-------------------------------------------------------------------
	public String toExplainString() {
		if (poolsBySet.get(ValueType.NATURAL) == null) return "";
		List ret = poolsBySet.get(ValueType.NATURAL).stream()
				.map(pc -> pc.value+" "+pc.source)
				.collect(Collectors.toList());
		if (poolsBySet.containsKey(ValueType.ARTIFICIAL)) {
			ret.add(" ---");
			ret.addAll(poolsBySet.get(ValueType.ARTIFICIAL).stream()
					.map(pc -> pc.value+" "+pc.source)
					.collect(Collectors.toList()));
		}
		return String.join("\n", ret);
	}

	//-------------------------------------------------------------------
	@SuppressWarnings("unchecked")
	public int getNatural() {
		if (poolsBySet.get(ValueType.NATURAL) == null) return 0;
		return poolsBySet.get(ValueType.NATURAL).stream()
				.mapToInt(pc -> ((PoolCalculation)pc).value)
				.sum();
	}

	//-------------------------------------------------------------------
	public void addAll(Pool other) {
		for (ValueType type : other.poolsBySet.keySet()) {
			List> list = poolsBySet.get(type);
			List> otherList = other.poolsBySet.get(type);
			if (list==null) {
				this.poolsBySet.put(type, otherList);
			} else {
				list.addAll(otherList);
			}
		}
	}

	//-------------------------------------------------------------------
	public void addCheckModification(CheckModification mod) {
		checkMods.add(mod);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy