summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/jeopardy/Controller.java21
-rw-r--r--src/jeopardy/DBModel.java78
-rw-r--r--src/jeopardy/Jeopardy.java15
-rw-r--r--src/jeopardy/Model.java6
-rw-r--r--src/jeopardy/View.java44
5 files changed, 153 insertions, 11 deletions
diff --git a/src/jeopardy/Controller.java b/src/jeopardy/Controller.java
index dc8687b..1cade9d 100644
--- a/src/jeopardy/Controller.java
+++ b/src/jeopardy/Controller.java
@@ -4,20 +4,25 @@ import java.util.ArrayList;
public class Controller {
- void beginGame(GameDescription description) {
+ private Model model;
+
+ public Controller(Model m) {
+ model = m;
+ }
+
+ public void beginGame(GameDescription description) {
}
- boolean isValidUser(String user) {
- return false;
+ public boolean isValidUser(String user) {
+ return model.userExists(user);
}
- void createUser(String user) {
-
+ public void createUser(String user) {
+ model.createUser(user);
}
- ArrayList<String> getCategories(ArrayList<String> players) {
- ArrayList<String> blubb = new ArrayList<String>();
- return blubb;
+ public ArrayList<String> getCategories(ArrayList<String> players) {
+ return model.validCategoriesForUsers(players);
}
}
diff --git a/src/jeopardy/DBModel.java b/src/jeopardy/DBModel.java
new file mode 100644
index 0000000..07abecc
--- /dev/null
+++ b/src/jeopardy/DBModel.java
@@ -0,0 +1,78 @@
+package jeopardy;
+import java.sql.*;
+import java.util.ArrayList;
+
+
+
+public class DBModel implements Model {
+ private Connection con;
+ private PreparedStatement createUserStmnt;
+ private PreparedStatement checkUserStmnt;
+ private PreparedStatement getValidCategoriesStmnt;
+
+ public DBModel(String url) {
+ try {
+ con = DriverManager.getConnection(url);
+ createUserStmnt = con.prepareStatement("INSERT INTO `users` (`name`) VALUES (?)");
+ checkUserStmnt = con.prepareStatement("SELECT COUNT(*) FROM `users` WHERE `name` = ?");
+ getValidCategoriesStmnt = con.prepareStatement( // TODO: this has to support more than 6 players
+ "SELECT categories.name FROM aqpairs, categories WHERE aqpairs.used = 0 AND aqpairs.category = categories.id AND aqpairs.id NOT IN "
+ + "(SELECT DISTINCT aqpairs.id FROM aqpairs, authors, users WHERE aqpairs.used = 0 AND users.name IN (?, ?, ?, ?, ?, ?) AND users.id = authors.author AND authors.aqpair = aqpairs.id )"
+ + " GROUP BY category HAVING COUNT(aqpairs.id) >= 2");
+ }
+ catch (SQLException e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+
+
+
+ public void createUser(String name) {
+ try {
+ createUserStmnt.setString(1, name);
+ createUserStmnt.execute();
+ }
+ catch (SQLException e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+
+ public boolean userExists(String name) {
+ try {
+ checkUserStmnt.setString(1, name);
+ ResultSet r = checkUserStmnt.executeQuery();
+ r.next();
+ return (r.getInt(1) == 1);
+ }
+ catch (SQLException e) {
+ e.printStackTrace();
+ System.exit(1);
+ return false;
+ }
+ }
+
+ public ArrayList<String> validCategoriesForUsers(ArrayList<String> users)
+ {
+ try {
+ for(int i = 0; i < 6; i++) {
+ getValidCategoriesStmnt.setString(i + 1, users.size() <= i ? "" : users.get(i));
+ }
+ ResultSet r = getValidCategoriesStmnt.executeQuery();
+ ArrayList<String> result = new ArrayList<String>();
+ while(r.next()) {
+ result.add(r.getString(1));
+ }
+ return result;
+ }
+ catch (SQLException e) {
+ e.printStackTrace();
+ System.exit(1);
+ return null;
+ }
+ }
+
+
+
+}
diff --git a/src/jeopardy/Jeopardy.java b/src/jeopardy/Jeopardy.java
index e6c5e12..dfd649a 100644
--- a/src/jeopardy/Jeopardy.java
+++ b/src/jeopardy/Jeopardy.java
@@ -1,6 +1,16 @@
package jeopardy;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import jeopardy.Controller;
+import jeopardy.DBModel;
+import jeopardy.Model;
import javax.swing.SwingUtilities;
+import javax.swing.UIDefaults;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+import javax.swing.plaf.FontUIResource;
public class Jeopardy {
@@ -10,10 +20,11 @@ public class Jeopardy {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
- View view = new View(new Controller());
+ Model mod = new DBModel("jdbc:mysql://phoenix.local:3306/jeopardy?user=jeopardy&password=j3opardy");
+ Controller c = new Controller(mod);
+ View view = new View(c);
view.createGameDialog(new GameDescription());
}
});
}
-
}
diff --git a/src/jeopardy/Model.java b/src/jeopardy/Model.java
index 8c6fcbc..f290abf 100644
--- a/src/jeopardy/Model.java
+++ b/src/jeopardy/Model.java
@@ -1,5 +1,9 @@
package jeopardy;
+import java.util.ArrayList;
+
public interface Model {
-
+ public boolean userExists(String name);
+ public void createUser(String name);
+ public ArrayList<String> validCategoriesForUsers(ArrayList<String> users);
}
diff --git a/src/jeopardy/View.java b/src/jeopardy/View.java
index a2f50c2..ba652e7 100644
--- a/src/jeopardy/View.java
+++ b/src/jeopardy/View.java
@@ -3,6 +3,7 @@ package jeopardy;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
+
import java.util.ArrayList;
public class View {
@@ -190,6 +191,7 @@ public class View {
public View(Controller c) {
controller = c;
+ setupLookAndFeel();
show = new JFrame("Show");
show.setSize(800, 600);
@@ -202,4 +204,46 @@ public class View {
labelPanel = new JPanel();
}
+ private void setupLookAndFeel() {
+ try {
+ UIDefaults.ProxyLazyValue font = new UIDefaults.ProxyLazyValue("javax.swing.plaf.FontUIResource", null, new Object[] {"Dialog", 0, 20});
+
+ UIManager.put("Button.font", font);
+ UIManager.put("ToggleButton.font", font);
+ UIManager.put("RadioButton.font", font);
+ UIManager.put("CheckBox.font", font);
+ UIManager.put("ColorChooser.font", font);
+ UIManager.put("ComboBox.font", font);
+ UIManager.put("Label.font", font);
+ UIManager.put("List.font", font);
+ UIManager.put("MenuBar.font", font);
+ UIManager.put("MenuItem.font", font);
+ UIManager.put("RadioButtonMenuItem.font", font);
+ UIManager.put("CheckBoxMenuItem.font", font);
+ UIManager.put("Menu.font", font);
+ UIManager.put("PopupMenu.font", font);
+ UIManager.put("OptionPane.font", font);
+ UIManager.put("Panel.font", font);
+ UIManager.put("ProgressBar.font", font);
+ UIManager.put("ScrollPane.font", font);
+ UIManager.put("Viewport.font", font);
+ UIManager.put("TabbedPane.font", font);
+ UIManager.put("Table.font", font);
+ UIManager.put("TableHeader.font", font);
+ UIManager.put("TextField.font", font);
+ UIManager.put("PasswordField.font", font);
+ UIManager.put("TextArea.font", font);
+ UIManager.put("TextPane.font", font);
+ UIManager.put("EditorPane.font", font);
+ UIManager.put("TitledBorder.font", font);
+ UIManager.put("ToolBar.font", font);
+ UIManager.put("ToolTip.font", font);
+ UIManager.put("Tree.font", font);
+ UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
+ }
+ catch(Exception e)
+ {
+ }
+ }
+
}