From 713c717b4749f1370616e51dc2123a007c3e973f Mon Sep 17 00:00:00 2001 From: TheCodeBoat Date: Fri, 28 Jun 2013 14:33:13 +0200 Subject: [PATCH] add date to db --- .../wyrez/shootingstars/gui/HighscoreGUI.java | 17 +++++++++++++++-- .../gui/manager/HighscoreManager.java | 14 ++++++++------ .../wyrez/shootingstars/gui/model/Score.java | 13 ++++++++++++- .../wyrez/shootingstars/io/SQLiteConnector.java | 2 +- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/ShootingStars/src/org/wyrez/shootingstars/gui/HighscoreGUI.java b/ShootingStars/src/org/wyrez/shootingstars/gui/HighscoreGUI.java index 44fc0f5..26f796d 100644 --- a/ShootingStars/src/org/wyrez/shootingstars/gui/HighscoreGUI.java +++ b/ShootingStars/src/org/wyrez/shootingstars/gui/HighscoreGUI.java @@ -18,6 +18,7 @@ package org.wyrez.shootingstars.gui; import com.jme3.input.event.MouseButtonEvent; import com.jme3.math.Vector2f; +import java.text.DateFormat; import java.util.List; import org.wyrez.shootingstars.gui.controls.ButtonBase; import org.wyrez.shootingstars.gui.listener.HighscoreListener; @@ -67,6 +68,7 @@ public class HighscoreGUI extends Panel implements Gui { float marginScoreNameLeft = screenHelper.calcX(14f); float marginScorePointsLeft = screenHelper.calcX(300f); + float marginScoreDateLeft = screenHelper.calcX(420f); float marginScoreSongNameLeft = screenHelper.calcX(600f); float labelFontSize = screenHelper.calcX(20f); @@ -81,9 +83,13 @@ public class HighscoreGUI extends Panel implements Gui { lblScoreNameHead.setText("Name"); lblScoreNameHead.setFontSize(labelFontSize); - Label lblScorePointsHead = new Label(screen, new Vector2f(startPointx + marginScorePointsLeft, startPointy), new Vector2f(screenHelper.calcX(250f), screenHelper.calcY(40f))); + Label lblScorePointsHead = new Label(screen, new Vector2f(startPointx + marginScorePointsLeft, startPointy), new Vector2f(screenHelper.calcX(120f), screenHelper.calcY(40f))); lblScorePointsHead.setText("Points"); lblScorePointsHead.setFontSize(labelFontSize); + + Label lblScoreDateHead = new Label(screen, new Vector2f(startPointx + marginScoreDateLeft, startPointy), new Vector2f(screenHelper.calcX(180f), screenHelper.calcY(40f))); + lblScoreDateHead.setText("Date"); + lblScoreDateHead.setFontSize(labelFontSize); Label lblScoreSongNameHead = new Label(screen, new Vector2f(startPointx + marginScoreSongNameLeft, startPointy), new Vector2f(screenHelper.calcX(600f), screenHelper.calcY(40f))); lblScoreSongNameHead.setText("Song Name"); @@ -114,6 +120,7 @@ public class HighscoreGUI extends Panel implements Gui { this.addChild(lblScoreNameHead); this.addChild(lblScorePointsHead); + this.addChild(lblScoreDateHead); this.addChild(lblScoreSongNameHead); this.addChild(plnScores); this.addChild(txtFilter); @@ -134,6 +141,7 @@ public class HighscoreGUI extends Panel implements Gui { float marginScoreNameLeft = screenHelper.calcX(14f); float marginScorePointsLeft = screenHelper.calcX(300f); + float marginScoreDateLeft = screenHelper.calcX(420f); float marginScoreSongNameLeft = screenHelper.calcX(600f); float marginScoreTop = screenHelper.calcY(50f); @@ -153,9 +161,13 @@ public class HighscoreGUI extends Panel implements Gui { lblScoreName.setText(score.getPlayerName()); lblScoreName.setFontSize(labelFontSize); - Label lblScorePoints = new Label(screen, new Vector2f(startPointx + marginScorePointsLeft, startPointy + marginScoreTop), new Vector2f(screenHelper.calcX(250f), screenHelper.calcY(40f))); + Label lblScorePoints = new Label(screen, new Vector2f(startPointx + marginScorePointsLeft, startPointy + marginScoreTop), new Vector2f(screenHelper.calcX(120f), screenHelper.calcY(40f))); lblScorePoints.setText(String.valueOf(score.getScore())); lblScorePoints.setFontSize(labelFontSize); + + Label lblScoreDate = new Label(screen, new Vector2f(startPointx + marginScoreDateLeft, startPointy + marginScoreTop), new Vector2f(screenHelper.calcX(180f), screenHelper.calcY(40f))); + lblScoreDate.setText(DateFormat.getDateTimeInstance().format(score.getDate())); + lblScoreDate.setFontSize(labelFontSize); Label lblScoreSongName = new Label(screen, new Vector2f(startPointx + marginScoreSongNameLeft, startPointy + marginScoreTop), new Vector2f(screenHelper.calcX(600f), screenHelper.calcY(40f))); lblScoreSongName.setText(score.getSongName()); @@ -163,6 +175,7 @@ public class HighscoreGUI extends Panel implements Gui { plnScores.addChild(lblScoreName); plnScores.addChild(lblScorePoints); + plnScores.addChild(lblScoreDate); plnScores.addChild(lblScoreSongName); marginScoreTop += screenHelper.calcY(50f); } diff --git a/ShootingStars/src/org/wyrez/shootingstars/gui/manager/HighscoreManager.java b/ShootingStars/src/org/wyrez/shootingstars/gui/manager/HighscoreManager.java index e93201f..6f2ad4e 100644 --- a/ShootingStars/src/org/wyrez/shootingstars/gui/manager/HighscoreManager.java +++ b/ShootingStars/src/org/wyrez/shootingstars/gui/manager/HighscoreManager.java @@ -22,6 +22,7 @@ package org.wyrez.shootingstars.gui.manager; */ import java.sql.Connection; import java.sql.ResultSet; +import java.text.DateFormat; import java.util.*; import org.wyrez.shootingstars.game.GameSettings; import org.wyrez.shootingstars.gui.model.Score; @@ -54,8 +55,9 @@ public class HighscoreManager { public void addScore(GameSettings gameSettings, OptionSettings optionSettings, int score) { String songName = gameSettings.getTrackArtist() + " ~ " + gameSettings.getTrackTitle(); - scores.add(new Score(songName, optionSettings.getUsername(), score)); - updateScoreFile(optionSettings.getUsername(), score, songName); + Date date = new Date(System.currentTimeMillis()); + scores.add(new Score(songName, optionSettings.getUsername(), score, date)); + updateScoreFile(optionSettings.getUsername(), score, songName, date); } public List getScoresWithSongName(String songName) { @@ -64,7 +66,7 @@ public class HighscoreManager { try { ResultSet rs = connector.excecuteQuery(connection, "SELECT * FROM Scores WHERE LOWER(SongName) LIKE LOWER('%" + songName + "%');"); while (rs.next()) { - tempScores.add(new Score(rs.getString("SongName"), rs.getString("Name"), rs.getInt("Points"))); + tempScores.add(new Score(rs.getString("SongName"), rs.getString("Name"), rs.getInt("Points"), rs.getDate("Date"))); } rs.close(); } catch (Exception ex) { @@ -78,7 +80,7 @@ public class HighscoreManager { try { ResultSet rs = connector.excecuteQuery(connection, "SELECT * FROM Scores;"); while (rs.next()) { - scores.add(new Score(rs.getString("SongName"), rs.getString("Name"), rs.getInt("Points"))); + scores.add(new Score(rs.getString("SongName"), rs.getString("Name"), rs.getInt("Points"), rs.getDate("Date"))); } rs.close(); } catch (Exception ex) { @@ -86,9 +88,9 @@ public class HighscoreManager { } } - private void updateScoreFile(String name, int score, String songName) { + private void updateScoreFile(String name, int score, String songName, Date date) { try { - connector.excecuteUpdate(connection, "INSERT INTO Scores(Name,Points,SongName) values('" + name + "'," + score + ", '" + songName + "')"); + connector.excecuteUpdate(connection, "INSERT INTO Scores(Name,Points,SongName,Date) values('" + name + "'," + score + ", '" + songName + "', '" + DateFormat.getDateTimeInstance().format(date) +"' )"); } catch (Exception ex) { ex.printStackTrace(); } diff --git a/ShootingStars/src/org/wyrez/shootingstars/gui/model/Score.java b/ShootingStars/src/org/wyrez/shootingstars/gui/model/Score.java index 93518a8..fefd2f4 100644 --- a/ShootingStars/src/org/wyrez/shootingstars/gui/model/Score.java +++ b/ShootingStars/src/org/wyrez/shootingstars/gui/model/Score.java @@ -21,11 +21,13 @@ package org.wyrez.shootingstars.gui.model; * @author Rappold */ import java.io.Serializable; +import java.util.Date; public class Score implements Serializable { private int score; private String playerName; private String songName; + private Date date; public void setSongName(String songName) { this.songName = songName; @@ -39,6 +41,10 @@ public class Score implements Serializable { this.score = score; } + public void setDate(Date date) { + this.date = date; + } + public String getPlayerName() { return playerName; } @@ -51,9 +57,14 @@ public class Score implements Serializable { return score; } - public Score(String songName, String playerName, int score) { + public Date getDate() { + return date; + } + + public Score(String songName, String playerName, int score, Date date) { this.songName = songName; this.score = score; this.playerName = playerName; + this.date = date; } } \ No newline at end of file diff --git a/ShootingStars/src/org/wyrez/shootingstars/io/SQLiteConnector.java b/ShootingStars/src/org/wyrez/shootingstars/io/SQLiteConnector.java index acfb9c3..71581bd 100644 --- a/ShootingStars/src/org/wyrez/shootingstars/io/SQLiteConnector.java +++ b/ShootingStars/src/org/wyrez/shootingstars/io/SQLiteConnector.java @@ -39,7 +39,7 @@ public class SQLiteConnector { try { Class.forName("org.sqlite.JDBC"); - String sqliteTable = "CREATE TABLE Scores (Name VARCHAR(250), Points INT(20), SongName VARCHAR(250))"; + String sqliteTable = "CREATE TABLE Scores (Name VARCHAR(250), Points INT(20), SongName VARCHAR(250), Date datetime)"; File sQLiteDb = new File(DB_PATH); if (!sQLiteDb.exists()) { sQLiteDb.createNewFile();