Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mur 52 assignment list class #35

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package de.marvinbrieger.toothbrushgame.domain;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import de.marvinbrieger.toothbrushgame.exceptions.MurderAssignmentNotFoundException;
import de.marvinbrieger.toothbrushgame.webservice.mapping.FilteredMurderAssignmentsSerializer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.bytebuddy.implementation.bind.annotation.IgnoreForBinding;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Embeddable
public class AssignmentList {

@Transient
private Game game;

@OneToMany(
cascade = CascadeType.ALL,
mappedBy = "game"
)
@JsonSerialize(using = FilteredMurderAssignmentsSerializer.class)
private List<MurderAssignment> murderAssignments;

public MurderAssignment findAssignment(Long assignmentId) {
return this.murderAssignments.parallelStream()
.filter(assignment -> assignment.getId().equals(assignmentId))
.findAny()
.orElseThrow(() -> new MurderAssignmentNotFoundException(assignmentId));
}

private MurderAssignment findSuccessor(MurderAssignment source) {
return murderAssignments.parallelStream()
.filter(source::hasSuccessor)
.findAny()
.orElseThrow(IllegalArgumentException::new);
}

public void newAssignemntForSuccessfullKiller(MurderAssignment currentAssignment) {
Player killer = currentAssignment.getKiller();
MurderAssignment victimsAssignment = findSuccessor(currentAssignment);

MurderAssignment killersNewMission = new MurderAssignment(null, game, killer, victimsAssignment.getTarget(),
MurderAssignmentStatus.PENDING, null);

getMurderAssignments().add(killersNewMission);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package de.marvinbrieger.toothbrushgame.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import de.marvinbrieger.toothbrushgame.exceptions.GameInWrongStateException;
import de.marvinbrieger.toothbrushgame.exceptions.MurderAssignmentNotFoundException;
import de.marvinbrieger.toothbrushgame.exceptions.PlayerAlreadyExistsException;
import de.marvinbrieger.toothbrushgame.webservice.mapping.FilteredMurderAssignmentsSerializer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -43,12 +41,8 @@ public class Game {
@OneToMany(mappedBy = "game")
private List<Player> players;

@OneToMany(
cascade = CascadeType.ALL,
mappedBy = "game"
)
@JsonSerialize(using = FilteredMurderAssignmentsSerializer.class)
private List<MurderAssignment> murderAssignments;
@Embedded
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wooh, that’s cool! Would not have thought that this doable in so a simple way 😀

private AssignmentList murderAssignments;

/**
* Creates a new game with the given game code, the title and preferences as the given game, the status {@link GameStatus#PREPARATION},
Expand All @@ -66,73 +60,54 @@ public Game(Game game, String gameCode, ApplicationUser creator) {
this.gameCode = gameCode;
this.gameStatus = GameStatus.PREPARATION;
this.players = new ArrayList<>();
this.murderAssignments = new ArrayList<>();
this.owner = new Player(game.getOwner(), this, creator);
this.murderAssignments = game.getMurderAssignments();

// owner is also a player
this.players.add(this.owner);
}

public boolean inPreparation() {
return gameStatus == GameStatus.PREPARATION;
public boolean notInPreparation() {
return gameStatus != GameStatus.PREPARATION;
}

@JsonIgnore
public boolean isRunning() {
return gameStatus == GameStatus.RUNNING;
}

private MurderAssignment findSuccessor(MurderAssignment source) {
for (MurderAssignment potentialSuccessor : murderAssignments)
if (source.hasSuccessor(potentialSuccessor))
return potentialSuccessor;

throw new IllegalArgumentException();
}

private void addSucceedingAssignment(MurderAssignment currentAssignment) {
Player killer = currentAssignment.getKiller();
MurderAssignment targetsAssignment = findSuccessor(currentAssignment);

MurderAssignment killersNewMission = new MurderAssignment(null, this, killer, targetsAssignment.getTarget(),
MurderAssignmentStatus.PENDING, null);

getMurderAssignments().add(killersNewMission);
public boolean notRunning() {
return gameStatus != GameStatus.RUNNING;
}

/**
* Fulfills the assignment with given ID.
*
* @param assignmentId ID of the assignment that is fulfilled
* @throws GameInWrongStateException if the game is not running
* @throws MurderAssignmentNotFoundException if there is no assignment with that ID that is part of the game
* @see MurderAssignment#commitMurder()
*/
public void commitMurder(Long assignmentId) {
// ensure game is running
if (!this.isRunning())
if (this.notRunning())
throw new GameInWrongStateException(GameStatus.RUNNING, getGameStatus());

// get the assignment
MurderAssignment assignment = this.murderAssignments.parallelStream()
.filter(assig -> assig.getId().equals(assignmentId))
.findAny()
.orElseThrow(() -> new MurderAssignmentNotFoundException(assignmentId));
MurderAssignment currentAssignment = murderAssignments.findAssignment(assignmentId);

// commit murder and add new assignment for the killer
assignment.commitMurder();
assignment.getTarget().getCurrentAssignment().setAssignmentStatus(MurderAssignmentStatus.FAILED);
addSucceedingAssignment(assignment);
currentAssignment.commitMurder();
currentAssignment.getTarget().setFailed();
murderAssignments.newAssignemntForSuccessfullKiller(currentAssignment);
}

/**
* Lets the given player join the game.
*
* @param player the joining player
* @throws GameInWrongStateException if the game is not in preparation
* @throws PlayerAlreadyExistsException if there is already a player with the same name or user in the game
*/
public void addPlayer(Player player) {
// check game state
if (!inPreparation())
if (notInPreparation())
throw new GameInWrongStateException(GameStatus.PREPARATION, getGameStatus());

// check neither player with same name nor with same user has already joined
Expand All @@ -157,21 +132,22 @@ public void addPlayer(Player player) {
* @throws GameInWrongStateException if the game is not in preparation
*/
public void start(List<MurderAssignment> assignments) {
// check and update state
if (!inPreparation())
if (notInPreparation())
throw new GameInWrongStateException(GameStatus.PREPARATION, getGameStatus());
setGameStatus(GameStatus.RUNNING);

setMurderAssignments(assignments);
setGameStatus(GameStatus.RUNNING);
setMurderAssignments(new AssignmentList(this, assignments));
}

/**
* Ends the game.
*
* @throws GameInWrongStateException if the game is not running
*/
public void end() {
if (isRunning())
if (notRunning())
throw new GameInWrongStateException(GameStatus.RUNNING, getGameStatus());

setGameStatus(GameStatus.FINISHED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ public boolean isPending() {
return assignmentStatus == MurderAssignmentStatus.PENDING;
}


public boolean hasSuccessor(MurderAssignment assignment) {
return target.equals(assignment.killer) && assignment.isPending();
}

public void commitMurder() {
if (this.getAssignmentStatus() != MurderAssignmentStatus.PENDING)
if (!isPending())
throw new MurderAssignmentNotFoundException(this.getId(), MurderAssignmentStatus.PENDING);

Murder murder = new Murder(null, Instant.now(), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ public Player(Player player, Game game, ApplicationUser user) {
this(null, game, player.getPlayerName(), user, new LinkedList<>());
}

public void setFailed() {
getCurrentAssignment().setAssignmentStatus(MurderAssignmentStatus.FAILED);
}

@JsonIgnore
public MurderAssignment getCurrentAssignment() {
private MurderAssignment getCurrentAssignment() {
return assignments.parallelStream()
.filter(assignment -> assignment.getAssignmentStatus() == MurderAssignmentStatus.PENDING)
.findFirst()
Expand Down