package oop13.space.controller;
import oop13.space.exceptions.EmptyPlayerNameException;
import oop13.space.model.HighScore;
import oop13.space.model.IModel;
import oop13.space.views.GameOverInterface;
import oop13.space.views.SubmitScore.SubmitScoreObserver;
import oop13.space.views.SubmitScoreInterface;
/**
* Controller handling the interaction with the
* {@link oop13.space.views.SubmitScore} view.
*
* @author Manuel Bottazzi
*/
public class SubmitScoreController implements SubmitScoreObserver {
private static final int MAX_NAME_LENGTH = 20;
private SubmitScoreInterface subScore;
private GameOverInterface gameOver;
private IModel model;
/**
* Creates a new SubmitScoreController with the parameters
* provided in input.
*
* @param gameOver - The current {@link oop13.space.views.GameOverInterface}
* @param model - The data used by the controller.
*/
public SubmitScoreController(GameOverInterface gameOver, IModel model) {
this.gameOver = gameOver;
this.model = model;
}
/**
* Sets the {@link oop13.space.views.SubmitScoreInterface} view
* the controller has to observe.
*
* @param submitScoreInterface - The {@link oop13.space.views.SubmitScoreInterface}
* to observe.
*/
public void setView(SubmitScoreInterface submitScoreInterface) {
this.subScore = submitScoreInterface;
this.subScore.attachObserver(this);
}
@Override
public void submitScore() throws EmptyPlayerNameException {
Integer score = this.model.getStatistics().getScore();
String name = this.subScore.getTextField().getText();
if (name.trim().isEmpty()) {
throw new EmptyPlayerNameException();
}
if (name.length() >= MAX_NAME_LENGTH) {
name = name.substring(0, MAX_NAME_LENGTH);
}
HighScore.getHighScore().addScore(name, score);
this.gameOver.getSubmitButton().setEnabled(false);
this.subScore.getFrame().dispose();
}
@Override
public void comeBack() {
this.subScore.getFrame().dispose();
}
}