-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCheckQuestionAction.java
More file actions
63 lines (44 loc) · 1.77 KB
/
CheckQuestionAction.java
File metadata and controls
63 lines (44 loc) · 1.77 KB
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
import java.time.Instant;
public class CheckQuestionAction implements Action {
private Player player;
private Question question;
private String answer;
public CheckQuestionAction(Player player, Question question, String answer) {
this.player = player;
this.question = question;
this.answer = answer;
}
@Override
public void execute() {
if(question == null) {
System.out.println("No question selected.");
return;
}
boolean isCorrect = answer.equalsIgnoreCase(question.getRightAnswer());
if(isCorrect){
player.setScore(player.getScore() + question.getValue());
question.setAnswered(true);
question.setValue(0);
}
// Create log
GameEventLog log = new GameEventLog(Game.getInstance().getCaseId(),Instant.now().toString(),"checkAnswer");
log.addPlayerInfo(player.getPlayerId());
log.selectCategory(question.getCategory());
log.selectValue(question.getValue());
log.setContent(question.getContent());
log.addAnswer(answer);
log.setCorrect(isCorrect);
log.updateScore(player.getScore());
player.getPublisher().setEventLog(log);
if(isCorrect) {
System.out.println("Correct answer! " + player.getPlayerId() + "'s new score is: " + player.getScore());
} else {
System.out.println("Incorrect answer. The correct answer was: " + question.getRightAnswer() + ". " + player.getPlayerId() + "'s score remains: " + player.getScore());
}
System.out.println("Current Score: " + player.getScore());
}
@Override
public String toString() {
return "checkAnswer";
}
}