package net.coljac.pirates.data;
import net.coljac.util.FileTools;
import net.coljac.pirates.data.HibernateUtil;
import net.coljac.pirates.CardDatabase;
import net.coljac.pirates.Ship;
import net.coljac.pirates.Card;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
/**
* By Colin Jacobs, colin@q9software.com
* Date: Feb 28, 2006
*/
public class MakeDB {
public static CardDatabase db;
public static void main(String[] args) throws Exception {
db = CardDatabase.init("cards.db");
// makeDB();
importMyShips("./my_ships.txt");
}
public static void makeDB() {
String[] sets = new String[]{"PSM", "CC", "PR", "BC", "SCS"};
GetData getter = new GetData();
for (int i = 0; i < sets.length; i++) {
String set = sets[i];
try {
getter.getShips(set);
getter.getCrew(set);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
getter.getForts("PSM");
getter.getEvents("PSM");
} catch (Exception e) {
e.printStackTrace();
}
// importMyShips("./my_ships.txt");
// CardDatabase db = null;
// try {
// db = CardDatabase.init("cards.db");
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// List l = HibernateUtil.createQuery("from Ship as ship").list();
// System.out.println("Ships in DB: " + l.size());
// db.getShips().addAll(l);
//
// l = HibernateUtil.createQuery("from Crew as crew").list();
// System.out.println("Crew in DB: " + l.size());
// db.getCrew().addAll(l);
//
// l = HibernateUtil.createQuery("from Event as event").list();
// System.out.println("Events in DB: " + l.size());
// db.getEvents().addAll(l);
//
// l = HibernateUtil.createQuery("from Fort as fort").list();
// System.out.println("Forts in DB: " + l.size());
// db.getShips().addAll(l);
//
// l = HibernateUtil.createQuery("from Card as card").list();
// System.out.println("Cards in DB: " + l.size());
// db.getCards().addAll(l);
ImportTreasure.main(null);
db.save();
// try {
// HibernateUtil.currentSession().connection().createStatement().execute("SHUTDOWN");
// } catch (SQLException e) {
// e.printStackTrace();
// }
}
public static void importMyShips(String fileName) {
List<String> lines = FileTools.getFileContentsAsList(fileName);
for (String s : lines) {
String[] tokens = s.split("\t");
int wanted = 0;
int owned = 0;
try {
owned = Integer.parseInt(tokens[1]);
} catch (NumberFormatException e) {
}
try {
wanted = Integer.parseInt(tokens[0]);
} catch (NumberFormatException e) {
}
String name = tokens[3];
String number = tokens[2];
while(number.length()<3) {
number = "0" + number;
}
List<Card> ships = db.getCardsByName(name);
if (ships.size() == 1) {
Ship theShip = (Ship)ships.get(0);
theShip.setOwned(owned);
theShip.setWanted(wanted);
} else if (ships.size() == 0) {
System.out.println("Can't find ship " + name);
} else {
List numbered = new ArrayList<Ship>();
for(Card c: ships) {
if(c.getNumber().equals(number)) {
numbered.add((Ship)c);
}
}
if(numbered.size()==1) {
Ship theShip = (Ship)numbered.get(0);
theShip.setOwned(owned);
theShip.setWanted(wanted);
db.save();
} else {
System.out.println("Too many ships: " + name + " " + numbered.size());
}
}
db.save();
}
}
}