Added SQL and Network

This commit is contained in:
2015-06-12 12:40:23 +02:00
parent ce35aaf667
commit 3dba4b6b92
8 changed files with 229 additions and 16 deletions
+5
View File
@@ -138,6 +138,11 @@ public class Song implements Comparable<Song>{
public File getFile() {
return file;
}
public String getFolder()
{
return file.getParentFile().getName();
}
public void setFile(File file) {
this.file = file;
+183
View File
@@ -0,0 +1,183 @@
package data.io;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import model.objects.Highscore;
import audio.Song;
import audio.SongInstance;
public class SQLConnector
{
private Connection myConn = null;
public SQLConnector()
{
this("178.62.254.153", "3306","colorstrike","csadmin","aardbei123");
}
//Start connection with Databse
public SQLConnector(String host, String port, String dbName, String userName, String password)
{
try
{
String url = "jdbc:mysql://" + host + ":" + port + "/"+ dbName + "?user="
+ userName
+ "&password="
+ password;
Class.forName("com.mysql.jdbc.Driver").newInstance ();
myConn = DriverManager.getConnection(url);
}
catch( SQLException ex)
{
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
catch(Exception ex)
{
System.out.println("Error : " + ex.getMessage());
}
}
public List<Highscore> getHighscore(String song)
{
Statement st = executeResultQuery("SELECT * FROM highscore");
ResultSet result = null;
try {
result = st.getResultSet();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
while(result.next())
{
}
} catch (SQLException e) {
}
return null;
}
public void update(List<Song> songs)
{
for(Song s : songs)
{
executeInsertQuery("INSERT INTO song (folder, title, author) VALUES ('" + s.getFolder() + "', '" + s.getTitle() + "', '" + s.getAuthor() + "');");
Statement st = executeResultQuery("SELECT id FROM song WHERE folder='" + s.getFolder() + "'");
ResultSet rs = null;
int id = -1;
try {
rs = st.getResultSet();
rs.next();
id = rs.getInt(1);
System.out.println("Song " + id + " added to database");
rs.close();
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(id != -1)
{
for(SongInstance si : s.getSongs())
{
Statement st2 = executeResultQuery("SELECT id FROM songinstance WHERE song=" + id + " AND difficulty='" + si.getDifficulty() + "'");
ResultSet rs2 = null;
try {
rs2 = st2.getResultSet();
if(rs2.first())
{
System.err.println("SongInstance Already Exists ");
}
else
{
executeInsertQuery("INSERT INTO songinstance (song, enemies, difficulty) VALUES (" + id + ", " + si.getObjects().size() + ", '" + si.getDifficulty() + "');");
System.out.println("Songinstance Added");
}
rs.close();
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
else
System.err.println("Insert Failed");
}
}
private void executeInsertQuery(String query)
{
try{
Statement s = myConn.createStatement();
s.execute(query);
s.close();
}
catch( SQLException ex)
{
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
catch( Exception ex)
{
System.out.println("getMeasurement: " + ex.getMessage());
}
}
//Execute a custom query
private Statement executeResultQuery(String query)
{
Statement s = null;
try{
s = myConn.createStatement();
s.executeQuery(query);
}
catch( SQLException ex)
{
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
catch( Exception ex)
{
System.out.println("getMeasurement: " + ex.getMessage());
}
return s;
}
//Close connection with Database
@Override
public void finalize() throws Throwable
{
// Close database connection
if( myConn != null )
{
try
{
myConn.close();
System.out.println("Database connection terminated");
}
catch( Exception e ) {}
}
super.finalize();
}
}
+10 -6
View File
@@ -17,10 +17,10 @@ import model.SongHandler;
import view.GameView;
import control.GameControl;
import control.GameStateManager;
import control.LedHandler;
import control.NetworkHandler;
import control.button.ButtonHandler;
import control.joystick.JoystickHandler;
import data.io.SQLConnector;
public class Window extends JFrame {
private static final long serialVersionUID = -9222956702898533696L;
@@ -37,9 +37,7 @@ public class Window extends JFrame {
//Create Events
Window.ON_RASP = ON_RASP;
LedHandler led = null;
if(ON_RASP){ //Only on the raspberry pi
led = new LedHandler();
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
@@ -57,15 +55,16 @@ public class Window extends JFrame {
ButtonHandler bth = new ButtonHandler();
JoystickHandler jsh = new JoystickHandler();
SQLConnector sql = new SQLConnector();
NetworkHandler ntw = new NetworkHandler("192.168.1.6", 1113, bth, jsh);
bth.setNetwork(ntw);
//Create Instances
final SongHandler sh = new SongHandler();
final SongHandler sh = new SongHandler(sql);
GameStateManager gsm = new GameStateManager(sh);
GameView view = new GameView(led,gsm);
GameModel model = new GameModel(sh, gsm, led);
GameView view = new GameView(gsm);
GameModel model = new GameModel(sh, gsm, ntw);
GameControl control = new GameControl(model, view,gsm);
setContentPane(view);
@@ -74,6 +73,11 @@ public class Window extends JFrame {
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
sh.close();
try {
sql.finalize();
} catch (Throwable e1) {
e1.printStackTrace();
}
System.exit(0);
}
});
+6 -6
View File
@@ -4,19 +4,19 @@ import java.awt.Color;
import main.Window;
import control.GameStateManager;
import control.LedHandler;
import control.NetworkHandler;
import control.button.ButtonHandler;
public class GameModel {
public static Color[] colors = { Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA, Color.CYAN, Color.ORANGE };
private GameStateManager gsm;
private LedHandler led;
private NetworkHandler ntw;
private int count = 0;
public GameModel(SongHandler sh, GameStateManager gsm, LedHandler led) {
public GameModel(SongHandler sh, GameStateManager gsm, NetworkHandler ntw) {
this.gsm = gsm;
this.led = led;
this.ntw = ntw;
for (int i = 1; i < ButtonHandler.getButtons().size(); i++) {
ButtonHandler.getButtons().get(i).setColor(colors[i - 1]);
@@ -34,9 +34,9 @@ public class GameModel {
if(count>15)
{
for (int i = 7; i < 54; i++) {
led.setLed(i, c.getRed(), c.getGreen(), c.getBlue());
ntw.setLed(i, c.getRed(), c.getGreen(), c.getBlue());
}
led.show();
ntw.show();
count = 0;
}
}
+7 -1
View File
@@ -12,6 +12,7 @@ import audio.Song;
import audio.SongInstance;
import audio.io.DirScanner;
import audio.sorting.SortALPHA;
import data.io.SQLConnector;
public class SongHandler {
@@ -20,13 +21,16 @@ public class SongHandler {
private Song currentSong;
private SongInstance currentSongInstance;
private int currentIndex;
SQLConnector sql;
private File dir;
private AudioPlayer p;
public SongHandler()
public SongHandler(SQLConnector sql)
{
this.sql = sql;
songs = new ArrayList<Song>();
currentSong = null;
@@ -44,6 +48,8 @@ public class SongHandler {
Collections.sort(songs, new SortALPHA());
sql.update(songs);
updatePlayer();
}
+17
View File
@@ -0,0 +1,17 @@
package model.objects;
import audio.SongInstance;
public class Highscore {
public SongInstance si;
public String name;
public int score;
public long time;
public Highscore(SongInstance si, String name, int score, long time) {
this.si = si;
this.name = name;
this.score = score;
this.time = time;
}
}
Binary file not shown.
+1 -3
View File
@@ -20,13 +20,11 @@ public class GameView extends JPanel {
*/
private static final long serialVersionUID = 1939480784205689618L;
LedHandler led;
GameStateManager gsm;
// Font fpsfont = new Font("OCR A Extended", Font.BOLD, 60);
public GameView(LedHandler led, GameStateManager gsm) {
this.led = led;
public GameView(GameStateManager gsm) {
this.gsm = gsm;
this.setPreferredSize(new Dimension(1280, 1024));
}