Use SQLite database to hold cards, allowing user submitted cards.
This commit is contained in:
@ -71,6 +71,7 @@ public class Card{
|
|||||||
+ ", image=" + image + ", manaCost=" + manaCost + ", power=" + power + ", toughness=" + toughness + "]";
|
+ ", image=" + image + ", manaCost=" + manaCost + ", power=" + power + ", toughness=" + toughness + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Card() {}
|
||||||
public Card(String cardName, int manaCost, int power, int toughness, String description, String image,
|
public Card(String cardName, int manaCost, int power, int toughness, String description, String image,
|
||||||
String creatureType) {
|
String creatureType) {
|
||||||
this.cardName = cardName;
|
this.cardName = cardName;
|
||||||
|
|||||||
25
Deck.java
25
Deck.java
@ -5,6 +5,7 @@ public class Deck {
|
|||||||
protected ArrayList<Card> topdeck;
|
protected ArrayList<Card> topdeck;
|
||||||
protected ArrayList<Card> discard;
|
protected ArrayList<Card> discard;
|
||||||
protected ArrayList<Card> hand;
|
protected ArrayList<Card> hand;
|
||||||
|
|
||||||
public Card drawCard(){
|
public Card drawCard(){
|
||||||
if (topdeck.size()<=0)
|
if (topdeck.size()<=0)
|
||||||
return null;
|
return null;
|
||||||
@ -15,25 +16,15 @@ public class Deck {
|
|||||||
|
|
||||||
}
|
}
|
||||||
public Deck() {
|
public Deck() {
|
||||||
topdeck=new ArrayList<Card>();
|
SQLDriver d = new SQLDriver();
|
||||||
discard=new ArrayList<Card>();
|
d.setupTable();
|
||||||
hand=new ArrayList<Card>();
|
topdeck = new ArrayList<Card>();
|
||||||
for (int i = 0; i < 4; i++){
|
hand = new ArrayList<Card>();
|
||||||
topdeck.add(new Card("Devouring Dragon", 5, 5, 6, "He does to people what Greg does to booty.", "", "Dragon"));
|
discard = new ArrayList<Card>();
|
||||||
topdeck.add(new Card("Dragon Worshipper", 1, 1, 1, "He spends his days praying to dragons.", "", "Human"));
|
for (int i = 0; i < 60; i++){
|
||||||
topdeck.add(new Card("Dragon Whelp", 2, 3, 2, "He'll get there.", "", "Dragon"));
|
topdeck.add(d.getRandomCard());
|
||||||
topdeck.add(new Card("Dragon Egg", 1, 0, 2, "Close to hatching!", "", "Egg"));
|
|
||||||
topdeck.add(new Card("Lingering Flame", 3, 4, 3, "He lives among the dragons.", "", "Elemental"));
|
|
||||||
topdeck.add(new Card("Dragonguard Sentry", 1, 1, 2, "Devoted to the protection of the dragonflight.", "", "Human"));
|
|
||||||
topdeck.add(new Card("Withering Flamewitch", 2, 4, 2, "Her flame bites deep.", "", "Human"));
|
|
||||||
topdeck.add(new Card("Final Examination", 6, 9000, 0, "Oh dear, Greg.", "", "Parchment"));
|
|
||||||
topdeck.add(new Card("Drunken Dragon", 3, 4, 1, "He's trying.", "", "Dragon"));
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 24; i++){
|
|
||||||
topdeck.add(new Card("Isle of Power", -1, 0, 0, "Invoke this for mana.", "", "Land"));
|
|
||||||
}
|
}
|
||||||
Shuffle();
|
Shuffle();
|
||||||
|
|
||||||
for (int i = 0; i < 7; i++){
|
for (int i = 0; i < 7; i++){
|
||||||
drawCard();
|
drawCard();
|
||||||
}
|
}
|
||||||
|
|||||||
14
Makefile
14
Makefile
@ -1,8 +1,9 @@
|
|||||||
JCC = javac
|
JCC = javac
|
||||||
MAIN_CLASS = Driver
|
MAIN_CLASS = Driver
|
||||||
OUT_JAR = MTGClone.jar
|
OUT_JAR = MTGClone.jar
|
||||||
|
SQLITE_JDBC_JAR = sqlite-jdbc-3.32.3.2.jar
|
||||||
JFLAGS = -g
|
SQLITE_JDBC = https://github.com/xerial/sqlite-jdbc/releases/download/3.32.3.2/$(SQLITE_JDBC_JAR)
|
||||||
|
JFLAGS = -g -classpath ".:$(SQLITE_JDBC_JAR)"
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@echo \"make build\" to compile Java classes
|
@echo \"make build\" to compile Java classes
|
||||||
@ -10,7 +11,10 @@ default:
|
|||||||
@echo \"make jar\" to compile executable jar
|
@echo \"make jar\" to compile executable jar
|
||||||
@echo \"make clean\" to clean up artifacts
|
@echo \"make clean\" to clean up artifacts
|
||||||
|
|
||||||
build:
|
sqlite_jdbc:
|
||||||
|
wget -N $(SQLITE_JDBC)
|
||||||
|
|
||||||
|
build: sqlite_jdbc
|
||||||
$(JCC) $(JFLAGS) $(MAIN_CLASS).java
|
$(JCC) $(JFLAGS) $(MAIN_CLASS).java
|
||||||
|
|
||||||
run: jar
|
run: jar
|
||||||
@ -18,7 +22,7 @@ run: jar
|
|||||||
|
|
||||||
jar: build
|
jar: build
|
||||||
@echo "Manifest-Version: 1.0" > manifest.txt
|
@echo "Manifest-Version: 1.0" > manifest.txt
|
||||||
@echo "Class-Path: ." >> manifest.txt
|
@echo "Class-Path: ./sqlite-jdbc-3.32.3.2.jar" >> manifest.txt
|
||||||
@echo "Main-Class: $(MAIN_CLASS)" >> manifest.txt
|
@echo "Main-Class: $(MAIN_CLASS)" >> manifest.txt
|
||||||
@echo "" >> manifest.txt
|
@echo "" >> manifest.txt
|
||||||
jar -cmf manifest.txt $(OUT_JAR) *.class
|
jar -cmf manifest.txt $(OUT_JAR) *.class
|
||||||
@ -27,5 +31,7 @@ jar: build
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) *.class
|
$(RM) *.class
|
||||||
|
$(RM) cards.db
|
||||||
$(RM) manifest.txt
|
$(RM) manifest.txt
|
||||||
$(RM) $(OUT_JAR)
|
$(RM) $(OUT_JAR)
|
||||||
|
$(RM) $(SQLITE_JDBC_JAR)
|
||||||
|
|||||||
123
SQLDriver.java
Normal file
123
SQLDriver.java
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
import java.sql.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class SQLDriver {
|
||||||
|
protected Connection c = null;
|
||||||
|
|
||||||
|
public boolean tryConnect() {
|
||||||
|
try {
|
||||||
|
Class.forName("org.sqlite.JDBC");
|
||||||
|
c = DriverManager.getConnection("jdbc:sqlite:cards.db");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Card getRandomCard() {
|
||||||
|
try {
|
||||||
|
c = DriverManager.getConnection("jdbc:sqlite:cards.db");
|
||||||
|
c.setAutoCommit(false);
|
||||||
|
|
||||||
|
Statement stmt = c.createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery("SELECT * FROM CARDS ORDER BY RANDOM() LIMIT 1;");
|
||||||
|
String cardName = "";
|
||||||
|
int manaCost = 0;
|
||||||
|
int power = 0;
|
||||||
|
int toughness = 0;
|
||||||
|
String description = "";
|
||||||
|
String image = "";
|
||||||
|
String creatureType = "";
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
cardName = rs.getString("CARDNAME");
|
||||||
|
manaCost = rs.getInt("MANACOST");
|
||||||
|
power = rs.getInt("POWER");
|
||||||
|
toughness = rs.getInt("TOUGHNESS");
|
||||||
|
description = rs.getString("DESCRIPTION");
|
||||||
|
image = rs.getString("IMAGE");
|
||||||
|
creatureType = rs.getString("CREATURETYPE");
|
||||||
|
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
stmt.close();
|
||||||
|
c.close();
|
||||||
|
Card card = new Card(cardName, manaCost, power, toughness, description, image, creatureType);
|
||||||
|
return card;
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
System.err.println("Returning null.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean insertCard(Card card) {
|
||||||
|
Statement stmt = null;
|
||||||
|
try {
|
||||||
|
Class.forName("org.sqlite.JDBC");
|
||||||
|
c = DriverManager.getConnection("jdbc:sqlite:cards.db");
|
||||||
|
c.setAutoCommit(false);
|
||||||
|
String baseStmt = "INSERT INTO CARDS (CARDNAME,MANACOST,POWER,TOUGHNESS,DESCRIPTION,IMAGE,CREATURETYPE) VALUES ";
|
||||||
|
stmt = c.createStatement();
|
||||||
|
String sql = "(\'" + card.cardName + "\'," + card.manaCost + "," + card.power + "," + card.toughness + ", \'"
|
||||||
|
+ card.description + "\',\'" + card.image + "\',\'" + card.creatureType + "\');";
|
||||||
|
stmt.executeUpdate(baseStmt + sql);
|
||||||
|
stmt.close();
|
||||||
|
c.commit();
|
||||||
|
c.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean setupCards() {
|
||||||
|
ArrayList<Card> topdeck = new ArrayList<Card>();
|
||||||
|
topdeck.add(new Card("Devouring Dragon", 5, 5, 6, "He does to people what Greg does to booty.", "", "Dragon"));
|
||||||
|
topdeck.add(new Card("Dragon Worshipper", 1, 1, 1, "He spends his days praying to dragons.", "", "Human"));
|
||||||
|
topdeck.add(new Card("Dragon Whelp", 2, 3, 2, "He will get there.", "", "Dragon"));
|
||||||
|
topdeck.add(new Card("Dragon Egg", 1, 0, 2, "Close to hatching!", "", "Egg"));
|
||||||
|
topdeck.add(new Card("Lingering Flame", 3, 4, 3, "He lives among the dragons.", "", "Elemental"));
|
||||||
|
topdeck.add(
|
||||||
|
new Card("Dragonguard Sentry", 1, 1, 2, "Devoted to the protection of the dragonflight.", "", "Human"));
|
||||||
|
topdeck.add(new Card("Withering Flamewitch", 2, 4, 2, "Her flame bites deep.", "", "Human"));
|
||||||
|
topdeck.add(new Card("Final Examination", 6, 9000, 0, "Oh dear, Greg.", "", "Parchment"));
|
||||||
|
topdeck.add(new Card("Drunken Dragon", 3, 4, 1, "He is trying.", "", "Dragon"));
|
||||||
|
topdeck.add(new Card("Isle of Power", -1, 0, 0, "Invoke this for mana.", "", "Land"));
|
||||||
|
for (Card card : topdeck) {
|
||||||
|
if (!insertCard(card)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean setupTable() {
|
||||||
|
Statement stmt = null;
|
||||||
|
try {
|
||||||
|
Class.forName("org.sqlite.JDBC");
|
||||||
|
c = DriverManager.getConnection("jdbc:sqlite:cards.db");
|
||||||
|
stmt = c.createStatement();
|
||||||
|
String sql = "CREATE TABLE CARDS " + "(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
|
||||||
|
+ " CARDNAME TEXT NOT NULL, " + " MANACOST INT NOT NULL, "
|
||||||
|
+ " POWER INT NOT NULL, " + " TOUGHNESS INT NOT NULL, "
|
||||||
|
+ " DESCRIPTION TEXT NOT NULL, " + " IMAGE TEXT NOT NULL, "
|
||||||
|
+ " CREATURETYPE TEXT NOT NULL)";
|
||||||
|
stmt.executeUpdate(sql);
|
||||||
|
stmt.close();
|
||||||
|
c.close();
|
||||||
|
if (!setupCards()) {
|
||||||
|
System.exit(-1);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (!e.getMessage().contains("(table CARDS already exists)"))
|
||||||
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user