405 lines
16 KiB
HTML
405 lines
16 KiB
HTML
|
|
|
|
|
|
<!DOCTYPE html>
|
|
<html id="htmlId">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
|
|
<title>Coverage Report > DecksCreator</title>
|
|
<style type="text/css">
|
|
@import "../../css/coverage.css";
|
|
@import "../../css/idea.min.css";
|
|
</style>
|
|
<script type="text/javascript" src="../../js/highlight.min.js"></script>
|
|
<script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="content">
|
|
<div class="breadCrumbs">
|
|
Current scope: <a href="../../index.html">all classes</a>
|
|
<span class="separator">|</span>
|
|
<a href="../index.html">it.polimi.ingsw.gc14.Model</a>
|
|
</div>
|
|
|
|
<h1>Coverage Summary for Class: DecksCreator (it.polimi.ingsw.gc14.Model)</h1>
|
|
|
|
<table class="coverageStats">
|
|
|
|
<tr>
|
|
<th class="name">Class</th>
|
|
<th class="coverageStat
|
|
">
|
|
Method, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Branch, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Line, %
|
|
</th>
|
|
</tr>
|
|
<tr>
|
|
<td class="name">DecksCreator</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
87.5%
|
|
</span>
|
|
<span class="absValue">
|
|
(7/8)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
100%
|
|
</span>
|
|
<span class="absValue">
|
|
(57/57)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
87.5%
|
|
</span>
|
|
<span class="absValue">
|
|
(70/80)
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td class="name">DecksCreator$1</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
100%
|
|
</span>
|
|
<span class="absValue">
|
|
(1/1)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
100%
|
|
</span>
|
|
<span class="absValue">
|
|
(1/1)
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td class="name">DecksCreator$2</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
100%
|
|
</span>
|
|
<span class="absValue">
|
|
(1/1)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
100%
|
|
</span>
|
|
<span class="absValue">
|
|
(1/1)
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td class="name">DecksCreator$BuildingCardDefinition</td>
|
|
</tr>
|
|
<tr>
|
|
<td class="name">DecksCreator$TribeCardDefinition</td>
|
|
</tr>
|
|
<tr>
|
|
<td class="name"><strong>Total</strong></td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
90%
|
|
</span>
|
|
<span class="absValue">
|
|
(9/10)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
100%
|
|
</span>
|
|
<span class="absValue">
|
|
(57/57)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
87.8%
|
|
</span>
|
|
<span class="absValue">
|
|
(72/82)
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<br/>
|
|
<br/>
|
|
|
|
|
|
<pre>
|
|
<code class="sourceCode" id="sourceCode"> package it.polimi.ingsw.gc14.Model;
|
|
|
|
import com.google.gson.*;
|
|
import it.polimi.ingsw.gc14.Model.Cards.Building.Effects.*;
|
|
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Characters.*;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.CavePaintings;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.Hunt;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.ShamanicRitual;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.Sustenance;
|
|
|
|
import java.io.*;
|
|
import java.lang.reflect.Type;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* Utility class responsible for loading and creating decks of cards and slots for the game.
|
|
* Cards are loaded from JSON resource files and instantiated according to their type and parameters.
|
|
*/
|
|
<b class="nc"> public class DecksCreator {</b>
|
|
|
|
|
|
/**
|
|
* Loads the tribe card deck for the specified era from the corresponding JSON resource file.
|
|
*
|
|
* @param era the era number (1, 2, or 3).
|
|
* @return a list of {@link TribeCard} objects for the specified era.
|
|
* @throws IllegalArgumentException if {@code era} is not 1, 2, or 3.
|
|
*/
|
|
public static List<TribeCard> loadTribeDeckByEra(int era) throws IllegalArgumentException
|
|
{
|
|
<b class="fc"> return switch (era) {</b>
|
|
<b class="fc"> case 1 -> loadTribeDeck("/Cards/tribe_era1.json");</b>
|
|
<b class="fc"> case 2 -> loadTribeDeck("/Cards/tribe_era2.json");</b>
|
|
<b class="fc"> case 3 -> loadTribeDeck("/Cards/tribe_era3.json");</b>
|
|
<b class="fc"> default -> throw new IllegalArgumentException();</b>
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Loads a tribe card deck from the specified JSON resource file path.
|
|
*
|
|
* @param resourcePath the path to the JSON resource file.
|
|
* @return a list of {@link TribeCard} objects defined in the resource file.
|
|
* @throws RuntimeException if the resource file is not found or an error occurs while reading it.
|
|
*/
|
|
public static List<TribeCard> loadTribeDeck(String resourcePath) {
|
|
<b class="fc"> Gson gson = new Gson();</b>
|
|
<b class="fc"> Type listType = new com.google.gson.reflect.TypeToken<List<TribeCardDefinition>>(){}.getType();</b>
|
|
|
|
<b class="fc"> InputStream is = DecksCreator.class.getResourceAsStream(resourcePath);</b>
|
|
<b class="fc"> if (is == null) {</b>
|
|
<b class="fc"> throw new RuntimeException("Resource file not found: " + resourcePath);</b>
|
|
}
|
|
|
|
<b class="fc"> try (Reader reader = new InputStreamReader(is)) {</b>
|
|
<b class="fc"> List<TribeCardDefinition> definitions = gson.fromJson(reader, listType);</b>
|
|
<b class="fc"> List<TribeCard> cards = new ArrayList<>();</b>
|
|
<b class="fc"> for (TribeCardDefinition def : definitions) {</b>
|
|
<b class="fc"> cards.add(createCard(def));</b>
|
|
}
|
|
<b class="fc"> return cards;</b>
|
|
} catch (IOException e) {
|
|
<b class="nc"> throw new RuntimeException("Error loading deck: " + resourcePath, e);</b>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Loads the building card deck for the specified era, filtering cards from the global building deck.
|
|
*
|
|
* @param era the era number (1, 2, or 3).
|
|
* @return a list of {@link BuildingCard} objects belonging to the specified era.
|
|
* @throws IllegalArgumentException if {@code era} is not 1, 2, or 3.
|
|
*/
|
|
public static List<BuildingCard> loadBuildingDeckByEra(int era) throws IllegalArgumentException
|
|
{
|
|
<b class="fc"> if(era<=0 || era>3) throw new IllegalArgumentException();</b>
|
|
<b class="fc"> return loadBuildingDeck("/Cards/buildingCards.json").stream().filter(x->x.getEra()==era).toList();</b>
|
|
}
|
|
|
|
/**
|
|
* Loads the full building card deck from the specified JSON resource file path.
|
|
*
|
|
* @param resourcePath the path to the JSON resource file.
|
|
* @return a list of {@link BuildingCard} objects defined in the resource file.
|
|
* @throws RuntimeException if the resource file is not found or an error occurs while reading it.
|
|
*/
|
|
public static List<BuildingCard> loadBuildingDeck(String resourcePath) {
|
|
<b class="fc"> Gson gson = new Gson();</b>
|
|
<b class="fc"> Type listType = new com.google.gson.reflect.TypeToken<List<BuildingCardDefinition>>(){}.getType();</b>
|
|
|
|
<b class="fc"> InputStream is = DecksCreator.class.getResourceAsStream(resourcePath);</b>
|
|
<b class="fc"> if (is == null) {</b>
|
|
<b class="fc"> throw new RuntimeException("Resource file not found: " + resourcePath);</b>
|
|
}
|
|
|
|
<b class="fc"> try (Reader reader = new InputStreamReader(is)) {</b>
|
|
<b class="fc"> List<BuildingCardDefinition> definitions = gson.fromJson(reader, listType);</b>
|
|
<b class="fc"> List<BuildingCard> cards = new ArrayList<>();</b>
|
|
<b class="fc"> for (BuildingCardDefinition def : definitions) {</b>
|
|
<b class="fc"> cards.add(createCard(def));</b>
|
|
}
|
|
<b class="fc"> return cards;</b>
|
|
} catch (IOException e) {
|
|
<b class="nc"> throw new RuntimeException("Error loading deck: " + resourcePath, e);</b>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates and returns the list of slots used as the game board.
|
|
* Slots are labeled with the letters A through G.
|
|
*
|
|
* @return a list of {@link Slot} objects representing the game board.
|
|
*/
|
|
public static List<Slot> loadSlotDeck()
|
|
{
|
|
<b class="fc"> List<Slot> slots = new ArrayList<>();</b>
|
|
<b class="fc"> char[]chars={'A','B','C','D','E','F','G'};</b>
|
|
<b class="fc"> for(char c : chars)</b>
|
|
<b class="fc"> slots.add(new Slot(c));</b>
|
|
<b class="fc"> return slots;</b>
|
|
}
|
|
|
|
/**
|
|
* Instantiates a {@link TribeCard} from the given definition.
|
|
* Depending on whether the card is an event or a character, the appropriate subclass is created
|
|
* using the type and parameters specified in the definition.
|
|
*
|
|
* @param def the {@link TribeCardDefinition} containing the card's type, era, and parameters.
|
|
* @return the instantiated {@link TribeCard}.
|
|
* @throws IllegalArgumentException if the card type is unknown or the parameters are invalid.
|
|
*/
|
|
private static TribeCard createCard(TribeCardDefinition def) {
|
|
<b class="fc"> int era = def.era;</b>
|
|
<b class="fc"> int[] p = def.params.stream().mapToInt(d -> ((Double) d).intValue()).toArray();</b>
|
|
<b class="fc"> if(def.isEvent)</b>
|
|
{
|
|
<b class="pc"> return switch (def.type) {</b>
|
|
<b class="fc"> case "Sustenance" -> new Sustenance(def.id,era,p[0]);</b>
|
|
<b class="fc"> case "Hunt" -> new Hunt(def.id,era,p[0]);</b>
|
|
<b class="fc"> case "CavePaintings" -> new CavePaintings(def.id,era,p[0],p[1],p[2]);</b>
|
|
<b class="fc"> case "ShamanicRitual" -> new ShamanicRitual(def.id,era,p[0],p[1]);</b>
|
|
<b class="nc"> default -> throw new IllegalArgumentException("Unknown event type: " + def.type);</b>
|
|
};
|
|
}
|
|
<b class="pc"> return switch (def.type) {</b>
|
|
<b class="fc"> case "Hunter" -> p.length == 0</b>
|
|
<b class="fc"> ? new Hunter(def.id,era, def.armed)</b>
|
|
<b class="fc"> : new Hunter(def.id,era, def.armed, p[0]);</b>
|
|
<b class="pc"> case "Builder" -> switch (p.length) {</b>
|
|
<b class="fc"> case 2 -> new Builder(def.id,era, p[0], p[1]);</b>
|
|
<b class="fc"> case 3 -> new Builder(def.id,era, p[0], p[1], p[2]);</b>
|
|
<b class="nc"> default -> throw new IllegalArgumentException("Builder: invalid parameters");</b>
|
|
};
|
|
<b class="pc"> case "Gatherer" -> switch (p.length) {</b>
|
|
<b class="fc"> case 0 -> new Gatherer(def.id,era);</b>
|
|
<b class="fc"> case 1 -> new Gatherer(def.id,era, p[0]);</b>
|
|
<b class="nc"> default -> throw new IllegalArgumentException("Gatherer: invalid parameters");</b>
|
|
};
|
|
<b class="pc"> case "Artist" -> switch (p.length) {</b>
|
|
<b class="fc"> case 0 -> new Artist(def.id,era);</b>
|
|
<b class="fc"> case 1 -> new Artist(def.id,era, p[0]);</b>
|
|
<b class="nc"> default -> throw new IllegalArgumentException("Artist: invalid parameters");</b>
|
|
};
|
|
<b class="pc"> case "Inventor" -> switch (p.length) {</b>
|
|
<b class="fc"> case 1 -> new Inventor(def.id,era, p[0]);</b>
|
|
<b class="fc"> case 2 -> new Inventor(def.id,era, p[0], p[1]);</b>
|
|
<b class="nc"> default -> throw new IllegalArgumentException("Inventor: invalid parameters");</b>
|
|
};
|
|
<b class="pc"> case "Shaman" -> switch (p.length) {</b>
|
|
<b class="fc"> case 1 -> new Shaman(def.id,era, p[0]);</b>
|
|
<b class="fc"> case 2 -> new Shaman(def.id,era, p[0], p[1]);</b>
|
|
<b class="nc"> default -> throw new IllegalArgumentException("Shaman: invalid parameters");</b>
|
|
};
|
|
<b class="nc"> default -> throw new IllegalArgumentException("Unknown card type: " + def.type);</b>
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Instantiates a {@link BuildingCard} from the given definition.
|
|
* The appropriate subclass is selected based on the effect ID specified in the definition.
|
|
* If no specific subclass matches the effect ID, a base {@link BuildingCard} is created.
|
|
*
|
|
* @param def the {@link BuildingCardDefinition} containing the card's effect ID, era, price, prestige value, and parameters.
|
|
* @return the instantiated {@link BuildingCard}.
|
|
*/
|
|
private static BuildingCard createCard(BuildingCardDefinition def) {
|
|
<b class="pc"> return switch (def.effectId) {</b>
|
|
<b class="fc"> case 0 -> new Building0(def.id,def.era, def.price, def.prestigeValue);</b>
|
|
<b class="fc"> case 1 -> new Building1(def.id,def.era, def.price, def.prestigeValue, CharacterType.valueOf(((String) def.params.get(0)).toUpperCase()));</b>
|
|
<b class="fc"> case 4 -> new Building4(def.id,def.era, def.price, def.prestigeValue);</b>
|
|
<b class="fc"> case 8 -> new Building8(def.id,def.era, def.price, def.prestigeValue);</b>
|
|
<b class="fc"> case 10 -> new Building10(def.id,def.era, def.price, def.prestigeValue);</b>
|
|
<b class="fc"> case 11 -> new Building11(def.id,def.era, def.price, def.prestigeValue, CharacterType.valueOf(((String) def.params.get(0)).toUpperCase()), ((Double) def.params.get(1)).intValue());</b>
|
|
<b class="fc"> case 13 -> new Building13(def.id,def.era, def.price, def.prestigeValue);</b>
|
|
<b class="fc"> default -> new BuildingCard(def.id,def.effectId, def.era, def.price, def.prestigeValue);</b>
|
|
};
|
|
|
|
}
|
|
|
|
/**
|
|
* Internal data class representing the raw definition of a tribe card as loaded from a JSON file.
|
|
* Contains the card identifier, type, era, whether it is armed, whether it is an event card,
|
|
* and a list of additional parameters.
|
|
*/
|
|
private static class TribeCardDefinition {
|
|
String id;
|
|
String type;
|
|
int era;
|
|
boolean armed;
|
|
boolean isEvent;
|
|
List<Object> params;
|
|
}
|
|
|
|
/**
|
|
* Internal data class representing the raw definition of a building card as loaded from a JSON file.
|
|
* Contains the card identifier, effect ID, era, price, prestige value, and a list of additional parameters.
|
|
*/
|
|
private static class BuildingCardDefinition {
|
|
String id;
|
|
int effectId;
|
|
int era;
|
|
int price;
|
|
int prestigeValue;
|
|
List<Object> params;
|
|
}
|
|
}
|
|
</code>
|
|
</pre>
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
(function() {
|
|
var msie = false, msie9 = false;
|
|
/*@cc_on
|
|
msie = true;
|
|
@if (@_jscript_version >= 9)
|
|
msie9 = true;
|
|
@end
|
|
@*/
|
|
|
|
if (!msie || msie && msie9) {
|
|
hljs.highlightAll()
|
|
hljs.initLineNumbersOnLoad();
|
|
}
|
|
})();
|
|
</script>
|
|
|
|
<div class="footer">
|
|
|
|
<div style="float:right;">generated on 2026-06-19 22:53</div>
|
|
</div>
|
|
</body>
|
|
</html>
|