-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
166 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package oopang.controller.database; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.function.Consumer; | ||
|
||
public class DatabaseManager { | ||
|
||
private Connection connection; | ||
|
||
public void createConnection() throws SQLException { | ||
final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; | ||
try { | ||
Class.forName(driver); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
final String url = "jdbc:sqlserver://den1.mssql6.gear.host;databaseName=pangleaderboard"; | ||
this.connection = DriverManager.getConnection(url, "pangleaderboard", "Vw2Zlr12o_!5"); | ||
} | ||
|
||
public void getRecordsFromQuery(final String query, final Consumer<ResultSet> action) throws SQLException { | ||
Statement state = this.connection.createStatement(); | ||
ResultSet res = null; | ||
try { | ||
res = state.executeQuery(query); | ||
while (res.next()) | ||
{ | ||
action.accept(res); | ||
} | ||
} catch (Exception e) { | ||
throw new SQLException(); | ||
} finally { | ||
if (res != null) { | ||
res.close(); | ||
} | ||
state.close(); | ||
} | ||
} | ||
|
||
public void closeConnection() { | ||
try { | ||
this.connection.close(); | ||
} catch (SQLException e) { | ||
} | ||
} | ||
|
||
public void insertRecords(final String query) throws SQLException { | ||
Statement state = this.connection.createStatement(); | ||
try { | ||
state.executeUpdate(query); | ||
} catch (Exception e) { | ||
throw new SQLException(); | ||
} finally { | ||
state.close(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/oopang/controller/leaderboard/OnlineLeaderboardManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package oopang.controller.leaderboard; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Optional; | ||
|
||
import oopang.controller.database.DatabaseManager; | ||
|
||
public class OnlineLeaderboardManager implements LeaderboardManager { | ||
|
||
private static final String STORY_MODE_TABLE_NAME = "StoryModeLeaderboard"; | ||
private static final String SURVIVAL_MODE_TABLE_NAME = "SurvivalModeLeaderBoard"; | ||
private final DatabaseManager manager; | ||
|
||
public OnlineLeaderboardManager() { | ||
this.manager = new DatabaseManager(); | ||
} | ||
|
||
private Optional<Leaderboard> load(final String tableName) { | ||
try { | ||
this.manager.createConnection(); | ||
final String query = "SELECT TOP(" + 10 + ") * " + | ||
"FROM " + tableName + " " + | ||
"ORDER BY Score DESC"; | ||
final Leaderboard leaderboard = new Leaderboard(); | ||
this.manager.getRecordsFromQuery(query, r -> { | ||
try { | ||
leaderboard.addRecord( | ||
new LeaderboardRecord( | ||
r.getString("Name"), | ||
r.getInt("Score"), | ||
r.getInt("Stage"))); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
return Optional.of(leaderboard); | ||
} catch (SQLException e) { | ||
return Optional.empty(); | ||
} finally { | ||
this.manager.closeConnection(); | ||
} | ||
} | ||
|
||
private boolean save(final LeaderboardRecord record, final String tableName) { | ||
try { | ||
this.manager.createConnection(); | ||
final String query = "INSERT INTO " + tableName + " (Name, Score, Stage) " + | ||
"VALUES ('" + record.getName() + "', " + record.getScore() + ", " + | ||
record.getStage() + ")"; | ||
this.manager.insertRecords(query); | ||
return true; | ||
} catch (SQLException e) { | ||
return false; | ||
} finally { | ||
this.manager.closeConnection(); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<Leaderboard> loadStoryModeLeaderboard() { | ||
return this.load(STORY_MODE_TABLE_NAME); | ||
} | ||
|
||
@Override | ||
public Optional<Leaderboard> loadSurvivalModeLeaderboard() { | ||
return this.load(SURVIVAL_MODE_TABLE_NAME); | ||
} | ||
|
||
@Override | ||
public boolean saveStoryModeLeaderboardRecord(LeaderboardRecord record) { | ||
return this.save(record, STORY_MODE_TABLE_NAME); | ||
} | ||
|
||
@Override | ||
public boolean saveSurvivalModeLeaderboardRecord(LeaderboardRecord record) { | ||
return this.save(record, SURVIVAL_MODE_TABLE_NAME); | ||
} | ||
|
||
} |