Commit e0d11829 authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

4. prednaska

parent 7278f315
......@@ -20,4 +20,21 @@
<maven.compiler.source>1.13</maven.compiler.source>
<maven.compiler.target>1.13</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package sk.tuke.gamestudio.entity;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
public class Score implements Comparable<Score>, Serializable {
private String player;
private int points;
private String game;
private Date playedOn;
public Score(String player, int points, String game, Date playedOn) {
this.player = player;
this.points = points;
this.game = game;
this.playedOn = playedOn;
}
public String getPlayer() {
return player;
}
public void setPlayer(String player) {
this.player = player;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
public String getGame() {
return game;
}
public void setGame(String game) {
this.game = game;
}
public Date getPlayedOn() {
return playedOn;
}
public void setPlayedOn(Date playedOn) {
this.playedOn = playedOn;
}
@Override
public int compareTo(Score o) {
return o.points - this.points;
}
//Nasledujuce metody boli vygenerovane pomocou IntelliJ - Code / Generate ... / equals(), hashCode(), toString()
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Score score = (Score) o;
return points == score.points &&
Objects.equals(player, score.player) &&
Objects.equals(game, score.game) &&
Objects.equals(playedOn, score.playedOn);
}
@Override
public int hashCode() {
return Objects.hash(player, points, game, playedOn);
}
@Override
public String toString() {
return "Score{" +
"player='" + player + '\'' +
", points=" + points +
", game='" + game + '\'' +
", playedOn=" + playedOn +
'}';
}
}
package sk.tuke.gamestudio.game.mines.consoleui;
import sk.tuke.gamestudio.entity.Score;
import sk.tuke.gamestudio.game.mines.core.Clue;
import sk.tuke.gamestudio.game.mines.core.Field;
import sk.tuke.gamestudio.game.mines.core.GameState;
import sk.tuke.gamestudio.game.mines.core.Tile;
import sk.tuke.gamestudio.service.ScoreService;
import sk.tuke.gamestudio.service.ScoreServiceJDBC;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConsoleUI {
private static final String GAME_NAME = "mines";
private static final Pattern INPUT_PATTERN = Pattern.compile("([OM])([A-I])([1-9])");
private final Field field;
//private ScoreService scoreService = new ScoreServiceFile();
private ScoreService scoreService = new ScoreServiceJDBC();
public ConsoleUI(Field field) {
this.field = field;
}
public void play() {
printScores();
do {
printField();
processInput();
......@@ -26,6 +39,8 @@ public class ConsoleUI {
printField();
if (field.getState() == GameState.SOLVED) {
System.out.println("You won!!!");
scoreService.addScore(
new Score(System.getProperty("user.name"), field.getScore(), GAME_NAME, new Date()));
} else
System.out.println("You failed!!!");
}
......@@ -103,4 +118,15 @@ public class ConsoleUI {
}
}
}
private void printScores() {
List<Score> scores = scoreService.getTopScores(GAME_NAME);
Collections.sort(scores);
System.out.println("Top scores:");
for (Score s : scores) {
System.out.println(s);
}
}
}
......@@ -15,6 +15,8 @@ public class Field {
private int numberOfOpenTiles;
private long startMillis;
public Field(int rowCount, int columnCount, int mineCount) {
this.rowCount = rowCount;
this.columnCount = columnCount;
......@@ -31,6 +33,7 @@ public class Field {
private void generate() {
generateMines();
fillWithClues();
startMillis = System.currentTimeMillis();
}
private void generateMines() {
......@@ -155,4 +158,12 @@ public class Field {
// }
// }
}
private int getPlayingTime() {
return ((int) (System.currentTimeMillis() - startMillis)) / 1000;
}
public int getScore() {
return rowCount * columnCount * 3 - getPlayingTime();
}
}
package sk.tuke.gamestudio.service;
import sk.tuke.gamestudio.entity.Score;
import java.util.List;
public interface ScoreService {
void addScore(Score score);
List<Score> getTopScores(String gameName);
}
package sk.tuke.gamestudio.service;
import sk.tuke.gamestudio.entity.Score;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ScoreServiceFile implements ScoreService {
private static final String FILE_NAME = "score.db";
@Override
public void addScore(Score score) {
List<Score> scores = loadScore();
scores.add(score);
Collections.sort(scores);
saveScore(scores);
}
@Override
public List<Score> getTopScores(String gameName) {
List<Score> scores = loadScore();
//TODO: vyber podla gameName
return scores;
}
private void saveScore(List<Score> scores) {
try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
os.writeObject(scores);
} catch (IOException e) {
e.printStackTrace();
}
}
private List<Score> loadScore() {
try (ObjectInputStream is = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
return (List<Score>) is.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return new ArrayList<>();
}
}
package sk.tuke.gamestudio.service;
import sk.tuke.gamestudio.entity.Score;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ScoreServiceJDBC implements ScoreService {
private static final String URL = "jdbc:postgresql://localhost/gamestudio";
private static final String LOGIN = "postgres";
private static final String PASSWORD = "postgres";
private static final String CREATE_COMMAND = "CREATE TABLE score (player VARCHAR(64) NOT NULL, points INTEGER NOT NULL, game VARCHAR(64) NOT NULL, played_on TIMESTAMP NOT NULL)";
private static final String INSERT_COMMAND = "INSERT INTO score (player, points, game, played_on) VALUES (?, ?, ?, ?)";
private static final String SELECT_COMMAND = "SELECT player, points, game, played_on FROM score WHERE game = ? ORDER BY points DESC";
@Override
public void addScore(Score score) {
try (Connection connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
PreparedStatement stmt = connection.prepareStatement(INSERT_COMMAND);
) {
stmt.setString(1, score.getPlayer());
stmt.setInt(2, score.getPoints());
stmt.setString(3, score.getGame());
stmt.setTimestamp(4, new Timestamp(score.getPlayedOn().getTime()));
stmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public List<Score> getTopScores(String gameName) {
List<Score> results = new ArrayList<>();
try (Connection connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
PreparedStatement stmt = connection.prepareStatement(SELECT_COMMAND);
) {
stmt.setString(1, gameName);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
Score score = new Score(
rs.getString(1), rs.getInt(2),
rs.getString(3), rs.getTimestamp(4));
results.add(score);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return results;
}
}
package sk.tuke.gamestudio.service;
import org.junit.Test;
import sk.tuke.gamestudio.entity.Score;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class ScoreServiceTest {
private ScoreService service = new ScoreServiceFile();
//private ScoreService service = new ScoreServiceJDBC();
@Test
public void addScore() {
Score score = new Score("Zuzka", 200, "mines", new Date());
service.addScore(score);
List<Score> scores = service.getTopScores("mines");
assertEquals(1, scores.size());
assertEquals("Zuzka", scores.get(0).getPlayer());
assertEquals(200, scores.get(0).getPoints());
assertEquals("mines", scores.get(0).getGame());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment