Add: JsonCards , DecksCreator, Test:DecksCreator

This commit is contained in:
rubenpirreram
2026-03-20 18:28:53 +01:00
parent 7028724728
commit c356064093
7 changed files with 892 additions and 40 deletions
@@ -1,8 +1,15 @@
package it.polimi.ingsw.gc14.Model;
import com.google.gson.*;
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.EventType;
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.*;
@@ -14,17 +21,18 @@ public class DecksCreator {
Gson gson = new Gson();
Type listType = new com.google.gson.reflect.TypeToken<List<CardDefinition>>(){}.getType();
try (InputStream is = DecksCreator.class.getResourceAsStream(resourcePath);
Reader reader = new InputStreamReader(is)) {
InputStream is = DecksCreator.class.getResourceAsStream(resourcePath);
if (is == null) {
throw new RuntimeException("File non trovato: " + resourcePath);
}
try (Reader reader = new InputStreamReader(is)) {
List<CardDefinition> definitions = gson.fromJson(reader, listType);
List<TribeCard> cards = new ArrayList<>();
for (CardDefinition def : definitions) {
cards.add(createCard(def));
}
return cards;
} catch (IOException e) {
throw new RuntimeException("Errore caricamento mazzo: " + resourcePath, e);
}
@@ -32,8 +40,18 @@ public class DecksCreator {
private static TribeCard createCard(CardDefinition def) {
int era = def.era;
int[] p = def.params.stream().mapToInt(d -> (int) Math.round((Float) d)).toArray();
int[] p = def.params.stream().mapToInt(d -> ((Double) d).intValue()).toArray();
if(def.isEvent)
{
return switch (def.type) {
case "Sustenance" -> new Sustenance(era,p[0]);
case "Hunt" -> new Hunt(era,p[0]);
case "CavePaintings" -> new CavePaintings(era,p[0],p[1],p[2]);
case "ShamanicRitual" -> new ShamanicRitual(era,p[0],p[1]);
default -> throw new IllegalArgumentException("Tipo sconosciuto: " + def.type);
};
}
return switch (def.type) {
case "Hunter" -> p.length == 0
? new Hunter(era, def.armed)
@@ -71,6 +89,7 @@ public class DecksCreator {
String type;
int era;
boolean armed;
boolean isEvent;
List<Object> params; // Object per gestire boolean e int misti
}
}
@@ -1,11 +1,15 @@
package it.polimi.ingsw.gc14.Model;
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Characters.Inventor;
import java.util.*;
public class Main {
public static void main(String[] args) {
Inventor inventor= new Inventor(1,0);
System.out.println(inventor);
List<TribeCard>cards= DecksCreator.loadDeck("/Cards/cards_era1.json");
}
}