Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo committed Aug 31, 2018
2 parents 1b21fbd + 4c79fd9 commit 3f0f37a
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 22 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<classpathentry kind="src" path="res"/>
<classpathentry kind="lib" path="lib/dyn4j-3.2.4.jar"/>
<classpathentry kind="lib" path="lib/w3c-dom.jar"/>
<classpathentry kind="lib" path="lib/sqljdbc42.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
6 changes: 0 additions & 6 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>edu.umd.cs.findbugs.plugin.eclipse.findbugsNature</nature>
<nature>net.sourceforge.pmd.eclipse.plugin.pmdNature</nature>
<nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
</natures>
</projectDescription>
Binary file added lib/sqljdbc42.jar
Binary file not shown.
17 changes: 11 additions & 6 deletions src/oopang/controller/ControllerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import oopang.controller.leaderboard.Leaderboard;
import oopang.controller.leaderboard.LeaderboardManager;
import oopang.controller.leaderboard.LeaderboardRecord;
import oopang.controller.leaderboard.OnlineLeaderboardManager;
import oopang.controller.loader.LevelData;
import oopang.controller.loader.LevelLoader;
import oopang.controller.loader.XMLLevelLoader;
Expand All @@ -39,7 +40,7 @@ public final class ControllerImpl implements Controller {
private final UserManager userManager;
private final LeaderboardManager leaderboardManager;
private Leaderboard leaderboard;
private Consumer<Leaderboard> saveAction;
private Consumer<LeaderboardRecord> saveAction;
private Consumer<Integer> saveMaxStage;
private Consumer<Integer> saveMaxScore;

Expand All @@ -54,7 +55,7 @@ public ControllerImpl(final Model model, final View view) {
this.model = model;
this.view = view;
this.userManager = new FileSystemUserManager();
this.leaderboardManager = new FileSystemLeaderboardManager();
this.leaderboardManager = new OnlineLeaderboardManager();
this.user = Optional.empty();
}

Expand All @@ -73,7 +74,7 @@ public void startStoryGameSession(final int levelIndex, final boolean isMultiPla
this.gameSession = new StoryModeGameSession(view, model, isMultiPlayer, this.getLevelLoader(), levelIndex);
this.gameSession.getShouldEndEvent().register(s -> this.handleSessionResult(s));
this.leaderboard = this.leaderboardManager.loadStoryModeLeaderboard().get();
this.saveAction = l -> this.leaderboardManager.saveStoryModeLeaderboard(l);
this.saveAction = l -> this.leaderboardManager.saveStoryModeLeaderboardRecord(l);
this.saveMaxStage = s -> this.user.ifPresent(u -> u.setArcadeMaxStage(s));
this.saveMaxScore = s -> this.user.ifPresent(u -> u.setArcadeMaxScore(s));
}
Expand All @@ -83,7 +84,7 @@ public void startInifiniteGameSession(final boolean isMultiPlayer) {
this.gameSession = new InfiniteGameSession(view, model, isMultiPlayer, this.getLevelLoader());
this.gameSession.getShouldEndEvent().register(s -> this.handleSessionResult(s));
this.leaderboard = this.leaderboardManager.loadSurvivalModeLeaderboard().get();
this.saveAction = l -> this.leaderboardManager.saveSurvivalModeLeaderboard(l);
this.saveAction = l -> this.leaderboardManager.saveSurvivalModeLeaderboardRecord(l);
this.saveMaxStage = s -> this.user.ifPresent(u -> u.setSurvivalMaxStage(s));
this.saveMaxScore = s -> this.user.ifPresent(u -> u.setSurvivalMaxScore(s));
}
Expand Down Expand Up @@ -120,10 +121,14 @@ public void sendCommand(final Command cmd, final PlayerTag player) {
private void handleSessionResult(final LevelResult result) {
if (result != LevelResult.FORCE_EXIT) {
this.user.ifPresent(u -> {
this.leaderboard.addRecord(new LeaderboardRecord(u.getName(), this.gameSession.getTotalScore(), this.gameSession.getStage()));
final LeaderboardRecord record = new LeaderboardRecord(
u.getName(),
this.gameSession.getTotalScore(),
this.gameSession.getStage());
this.leaderboard.addRecord(record);
u.addXpPoints(this.gameSession.getTotalScore());
this.saveUser();
this.saveAction.accept(this.leaderboard);
this.saveAction.accept(record);
this.saveMaxStage.accept(this.gameSession.getStage());
this.saveMaxScore.accept(this.gameSession.getTotalScore());
});
Expand Down
62 changes: 62 additions & 0 deletions src/oopang/controller/database/DatabaseManager.java
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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
*/
public final class FileSystemLeaderboardManager implements LeaderboardManager {

private boolean save(final Leaderboard leaderboard, final String path) {
private boolean save(final LeaderboardRecord record, final String path) {
final Leaderboard leaderboard = this.load(path).orElse(new Leaderboard());
try (
OutputStream file = new FileOutputStream(path);
OutputStream bstream = new BufferedOutputStream(file);
ObjectOutputStream ostream = new ObjectOutputStream(bstream)
) {
leaderboard.addRecord(record);
ostream.writeObject(leaderboard);
return true;
} catch (IOException e) {
Expand Down Expand Up @@ -59,12 +61,12 @@ public Optional<Leaderboard> loadSurvivalModeLeaderboard() {
}

@Override
public boolean saveStoryModeLeaderboard(final Leaderboard leaderboard) {
return this.save(leaderboard, InstallManager.STORY_FILE);
public boolean saveStoryModeLeaderboardRecord(final LeaderboardRecord record) {
return this.save(record, InstallManager.STORY_FILE);
}

@Override
public boolean saveSurvivalModeLeaderboard(final Leaderboard leaderboard) {
return this.save(leaderboard, InstallManager.SURVIVAL_FILE);
public boolean saveSurvivalModeLeaderboardRecord(final LeaderboardRecord record) {
return this.save(record, InstallManager.SURVIVAL_FILE);
}
}
10 changes: 5 additions & 5 deletions src/oopang/controller/leaderboard/LeaderboardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ public interface LeaderboardManager {
/**
* Save the story Leaderboard.
* @param leaderboard
* The Leaderboard.
* The Leaderboard record.
* @return
* true if the leaderboard is correctly saved
*/
boolean saveStoryModeLeaderboard(Leaderboard leaderboard);
boolean saveStoryModeLeaderboardRecord(LeaderboardRecord record);

/**
* Save the survival Leaderboard.
* @param leaderboard
* The Leaderboard.
* @param record
* The Leaderboard record.
* @return
* true if the leaderboard is correctly saved
*/
boolean saveSurvivalModeLeaderboard(Leaderboard leaderboard);
boolean saveSurvivalModeLeaderboardRecord(LeaderboardRecord record);

}
80 changes: 80 additions & 0 deletions src/oopang/controller/leaderboard/OnlineLeaderboardManager.java
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);
}

}

0 comments on commit 3f0f37a

Please sign in to comment.