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

11. prednaska

parent 57aac5c2
package sk.tuke.gamestudio.game.mines.core;
import sk.tuke.gamestudio.game.mines.entity.CommandType;
import sk.tuke.gamestudio.game.mines.entity.GamePlay;
import sk.tuke.gamestudio.service.GamestudioException;
import java.io.*;
......@@ -22,6 +24,8 @@ public class Field implements Serializable {
private long startMillis;
private GamePlay gamePlay;
public Field(int rowCount, int columnCount, int mineCount) {
if (rowCount * columnCount <= mineCount)
throw new IllegalArgumentException("Illegal setting");
......@@ -30,7 +34,9 @@ public class Field implements Serializable {
this.columnCount = columnCount;
this.mineCount = mineCount;
tiles = new Tile[rowCount][columnCount];
gamePlay = new GamePlay(rowCount, columnCount);
generate();
System.out.println(gamePlay);
}
private void generate() {
......@@ -47,6 +53,7 @@ public class Field implements Serializable {
int column = random.nextInt(columnCount);
if (tiles[row][column] == null) {
tiles[row][column] = new Mine();
gamePlay.addMineCoordinate(row, column);
storedMines++;
}
}
......@@ -106,6 +113,10 @@ public class Field implements Serializable {
return mineCount;
}
public GamePlay getGamePlay() {
return gamePlay;
}
public Tile getTile(int row, int column) {
return tiles[row][column];
}
......@@ -115,6 +126,7 @@ public class Field implements Serializable {
Tile tile = tiles[row][column];
if (tile.getState() == TileState.CLOSED) {
tile.setState(TileState.OPEN);
gamePlay.addCommand(CommandType.OPEN, row, column);
numberOfOpenTiles++;
if (tile instanceof Mine)
......@@ -128,6 +140,7 @@ public class Field implements Serializable {
}
}
}
System.out.println(gamePlay);
}
private void openNeighbourhTiles(int row, int column) {
......@@ -162,8 +175,10 @@ public class Field implements Serializable {
Tile tile = tiles[row][column];
if (tile.getState() == TileState.CLOSED) {
tile.setState(TileState.MARKED);
gamePlay.addCommand(CommandType.MARK, row, column);
} else if (tile.getState() == TileState.MARKED) {
tile.setState(TileState.CLOSED);
gamePlay.addCommand(CommandType.MARK, row, column);
}
}
}
......
package sk.tuke.gamestudio.game.mines.entity;
import javax.persistence.*;
@Entity
public class Command {
@Id
@GeneratedValue
private int ident;
@Enumerated(EnumType.STRING)
private CommandType type;
private int row;
private int col;
public Command() {
}
public Command(CommandType type, int row, int col) {
this.type = type;
this.row = row;
this.col = col;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public CommandType getType() {
return type;
}
public void setType(CommandType type) {
this.type = type;
}
@Override
public String toString() {
return "Command{" +
"type=" + type +
", row=" + row +
", col=" + col +
'}';
}
}
package sk.tuke.gamestudio.game.mines.entity;
public enum CommandType {
OPEN, MARK
}
package sk.tuke.gamestudio.game.mines.entity;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
public class GamePlay {
@Id
@GeneratedValue
private int ident;
private int rowCount;
private int columnCount;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "ident_game_play")
private Set<MineCoordinate> mineCoordinates;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "ident_game_play")
@OrderColumn(name = "command_order")
private List<Command> commands;
public GamePlay() {
}
public GamePlay(int rowCount, int columnCount) {
this.rowCount = rowCount;
this.columnCount = columnCount;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getColumnCount() {
return columnCount;
}
public void setColumnCount(int columnCount) {
this.columnCount = columnCount;
}
public Set<MineCoordinate> getMineCoordinates() {
return mineCoordinates;
}
public void setMineCoordinates(Set<MineCoordinate> mineCoordinates) {
this.mineCoordinates = mineCoordinates;
}
public List<Command> getCommands() {
return commands;
}
public void setCommands(List<Command> commands) {
this.commands = commands;
}
public void addMineCoordinate(int row, int column) {
if (mineCoordinates == null)
mineCoordinates = new HashSet<>();
mineCoordinates.add(new MineCoordinate(row, column));
}
public void addCommand(CommandType type, int row, int column) {
if (commands == null)
commands = new ArrayList<>();
commands.add(new Command(type, row, column));
}
@Override
public String toString() {
return "GamePlay{" +
"rowCount=" + rowCount +
", columnCount=" + columnCount +
", mineCoordinates=" + mineCoordinates +
", commands=" + commands +
'}';
}
}
package sk.tuke.gamestudio.game.mines.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class MineCoordinate {
@Id
@GeneratedValue
private int ident;
private int row;
private int col;
public MineCoordinate() {
}
public MineCoordinate(int row, int col) {
this.row = row;
this.col = col;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
@Override
public String toString() {
return "MineCoordinate{" +
"row=" + row +
", col=" + col +
'}';
}
}
package sk.tuke.gamestudio.game.mines.service;
import sk.tuke.gamestudio.game.mines.entity.GamePlay;
public interface GamePlayService {
void save(GamePlay gamePlay);
GamePlay loadLast();
}
package sk.tuke.gamestudio.game.mines.service;
import sk.tuke.gamestudio.game.mines.entity.GamePlay;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
@Transactional
public class GamePlayServiceJPA implements GamePlayService {
@PersistenceContext
private EntityManager entityManager;
@Override
public void save(GamePlay gamePlay) {
entityManager.persist(gamePlay);
}
@Override
public GamePlay loadLast() {
try {
return (GamePlay) entityManager.createQuery(
"SELECT g FROM GamePlay g WHERE g.ident = (SELECT MAX(g2.ident) FROM GamePlay g2)").getSingleResult();
} catch (NoResultException e) {
return null;
}
}
}
......@@ -5,12 +5,14 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sk.tuke.gamestudio.game.mines.service.GamePlayService;
import sk.tuke.gamestudio.game.mines.service.GamePlayServiceJPA;
import sk.tuke.gamestudio.service.ScoreService;
import sk.tuke.gamestudio.service.ScoreServiceJPA;
@SpringBootApplication
@Configuration
@EntityScan("sk.tuke.gamestudio.entity")
@EntityScan({"sk.tuke.gamestudio.entity", "sk.tuke.gamestudio.game.mines.entity"})
public class GameStudioServer {
public static void main(String[] args) {
SpringApplication.run(GameStudioServer.class);
......@@ -20,4 +22,7 @@ public class GameStudioServer {
public ScoreService scoreService() {
return new ScoreServiceJPA();
}
@Bean
public GamePlayService gamePlayService() {return new GamePlayServiceJPA();}
}
......@@ -14,6 +14,7 @@ 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.game.mines.service.GamePlayService;
import sk.tuke.gamestudio.service.ScoreService;
import java.util.Date;
......@@ -28,6 +29,9 @@ public class MinesController {
@Autowired
private ScoreService scoreService;
@Autowired
private GamePlayService gamePlayService;
private Field field = new Field(9, 9, 10);
private boolean marking;
......@@ -48,10 +52,14 @@ public class MinesController {
field.openTile(Integer.parseInt(row), Integer.parseInt(column));
if (field.getState() == GameState.SOLVED && userController.isLogged())
scoreService.addScore(new Score("mines", userController.getLoggedUser().getLogin(), field.getScore(), new Date()));
if (field.getState() == GameState.SOLVED)
gamePlayService.save(field.getGamePlay());
}
}
} catch (Exception e) {
//Jaro: Tato vynimka znamena, ze parametre neprisli
e.printStackTrace();
}
}
......
......@@ -128,4 +128,8 @@ table {
td {
padding: 0;
}
#minefieldFromService a {
cursor: pointer;
}
\ No newline at end of file
Markdown is supported
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