File size: 3,842 Bytes
2311079 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
package com.tacticmaster;
import com.tacticmaster.board.ChessboardView;
import com.tacticmaster.db.DatabaseAccessor;
import com.tacticmaster.puzzle.Puzzle;
import com.tacticmaster.rating.EloRatingCalculator;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class ChessboardController implements ChessboardView.PuzzleFinishedListener {
private final DatabaseAccessor databaseAccessor;
private final ChessboardView chessboardView;
private final PuzzleTextViews puzzleTextViews;
private int currentPuzzleIndex = 0;
private final Set<String> loadedPuzzleIds = new HashSet<>();
private final TreeSet<Puzzle> loadedPuzzles = new TreeSet<>();
private final List<Puzzle> playedPuzzles = new ArrayList<>();
private final Random randomNumberGenerator;
private int playerRating;
public ChessboardController(
DatabaseAccessor databaseAccessor,
ChessboardView chessboardView,
PuzzleTextViews puzzleTextViews, Random randomNumberGenerator) {
this.databaseAccessor = databaseAccessor;
this.chessboardView = chessboardView;
this.puzzleTextViews = puzzleTextViews;
this.randomNumberGenerator = randomNumberGenerator;
this.chessboardView.setPuzzleSolvedListener(this);
this.playerRating = databaseAccessor.getPlayerRating();
}
private void updatePlayerRating(int opponentRating, double result) {
playerRating = EloRatingCalculator.calculateNewRating(playerRating, opponentRating, result);
databaseAccessor.storePlayerRating(playerRating);
puzzleTextViews.setPlayerRating(playerRating);
}
private void loadNextPuzzles() {
var nextPuzzles = databaseAccessor
.getPuzzlesWithinRange(
this.playerRating - 50,
this.playerRating + 200, loadedPuzzleIds);
nextPuzzles.forEach(puzzle -> loadedPuzzleIds.add(puzzle.puzzleId()));
this.loadedPuzzles.addAll(nextPuzzles);
}
private void renderPuzzle() {
Puzzle puzzle = playedPuzzles.get(currentPuzzleIndex);
chessboardView.setPuzzle(puzzle);
puzzleTextViews.setPuzzleId(puzzle.puzzleId());
puzzleTextViews.setPuzzleRating(puzzle.rating());
puzzleTextViews.setPuzzlesSolved(databaseAccessor.getSolvedPuzzleCount(), databaseAccessor.getAllPuzzleCount());
puzzleTextViews.setPuzzleThemes(puzzle.themes());
puzzleTextViews.setPuzzleMoves(puzzle.moves());
puzzleTextViews.setPuzzlePopularity(puzzle.popularity());
puzzleTextViews.setPuzzleNbPlays(puzzle.nbPlays());
puzzleTextViews.setPlayerRating(playerRating);
}
public void loadPreviousPuzzle() {
currentPuzzleIndex -= 1;
if (currentPuzzleIndex < 0) {
currentPuzzleIndex = playedPuzzles.size() - 1;
}
renderPuzzle();
}
public void loadNextPuzzle() {
if (!this.playedPuzzles.isEmpty()) {
currentPuzzleIndex += 1;
}
if (currentPuzzleIndex >= playedPuzzles.size()) {
if (loadedPuzzles.isEmpty()) {
loadNextPuzzles();
}
var nextPuzzle = randomNumberGenerator.nextDouble() < 0.3 ? loadedPuzzles.pollFirst() : loadedPuzzles.pollLast();
this.playedPuzzles.add(nextPuzzle);
}
renderPuzzle();
}
@Override
public void onPuzzleSolved(Puzzle puzzle) {
databaseAccessor.setSolved(puzzle.puzzleId());
updatePlayerRating(puzzle.rating(), 1.0);
loadNextPuzzle();
}
@Override
public void onPuzzleNotSolved(Puzzle puzzle) {
updatePlayerRating(puzzle.rating(), 0.0);
loadNextPuzzle();
}
} |