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

모든 플레이어 투표 완료시 상태 변경 #55

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion src/main/java/mafia/mafiatogether/domain/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ public String getJobsTarget(final String name) {
return jobTarget.getTarget(jobType).getName();
}

public void votePlayer(final String name, final String targetName) {
public void votePlayer(final String name, final String targetName, final Long now) {
final Player player = getPlayer(name);
final Player target = targetName.isBlank() ? Player.NONE : getPlayer(targetName);
vote.choose(player, target);
if (vote.isAllParticipatedVote(roomInfo.getTotal())) {
this.status = status.getNextStatus(this, now);
}
}

public String getVoteResult() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/mafia/mafiatogether/domain/Vote.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ public void clear() {
votedPlayer = Player.NONE;
playerVote.clear();
}

public boolean isAllParticipatedVote(final int total) {
return this.playerVote.keySet().size() == total;
}
}
3 changes: 2 additions & 1 deletion src/main/java/mafia/mafiatogether/service/VoteService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mafia.mafiatogether.service;

import java.time.Clock;
import lombok.RequiredArgsConstructor;
import mafia.mafiatogether.domain.Room;
import mafia.mafiatogether.domain.RoomManager;
Expand All @@ -14,7 +15,7 @@ public class VoteService {

public void votePlayer(final String code, final String name, final String targetName) {
final Room room = roomManager.findByCode(code);
room.votePlayer(name, targetName);
room.votePlayer(name, targetName, Clock.systemDefaultZone().millis());
}

public VoteResultResponse getResult(final String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import java.time.Clock;
import java.util.Base64;
import java.util.Map;
import mafia.mafiatogether.domain.Player;
Expand Down Expand Up @@ -94,9 +95,9 @@ void setRoom() {
void 투표_결과를_조회한다() {
// given
final String basic = Base64.getEncoder().encodeToString((code + ":" + player1.getName()).getBytes());
room.votePlayer(player1.getName(), player2.getName());
room.votePlayer(player2.getName(), player2.getName());
room.votePlayer(player3.getName(), player1.getName());
room.votePlayer(player1.getName(), player2.getName(), Clock.systemDefaultZone().millis());
room.votePlayer(player2.getName(), player2.getName(), Clock.systemDefaultZone().millis());
room.votePlayer(player3.getName(), player1.getName(), Clock.systemDefaultZone().millis());
room.executeVote();

// when & then
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/mafia/mafiatogether/domain/RoomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ class RoomTest {
room.modifyStatus(StatusType.DAY, Clock.systemDefaultZone().millis());

// when
room.votePlayer(a.getName(), b.getName());
room.votePlayer(a.getName(), b.getName());
room.votePlayer(b.getName(), d.getName());
room.votePlayer(c.getName(), d.getName());
room.votePlayer(a.getName(), b.getName(), Clock.systemDefaultZone().millis());
room.votePlayer(a.getName(), b.getName(), Clock.systemDefaultZone().millis());
room.votePlayer(b.getName(), d.getName(), Clock.systemDefaultZone().millis());
room.votePlayer(c.getName(), d.getName(), Clock.systemDefaultZone().millis());
room.executeVote();

// then
Expand All @@ -105,10 +105,10 @@ class RoomTest {
room.modifyStatus(StatusType.DAY, Clock.systemDefaultZone().millis());

// when
room.votePlayer(a.getName(), c.getName());
room.votePlayer(b.getName(), c.getName());
room.votePlayer(c.getName(), e.getName());
room.votePlayer(d.getName(), e.getName());
room.votePlayer(a.getName(), c.getName(), Clock.systemDefaultZone().millis());
room.votePlayer(b.getName(), c.getName(), Clock.systemDefaultZone().millis());
room.votePlayer(c.getName(), e.getName(), Clock.systemDefaultZone().millis());
room.votePlayer(d.getName(), e.getName(), Clock.systemDefaultZone().millis());

// then
assertNull(room.getVoteResult());
Expand Down
22 changes: 18 additions & 4 deletions src/test/java/mafia/mafiatogether/domain/status/StatusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ void setRoom() {
room.getStatusType(noticeTime);
room.getStatusType(dayTime);
room.getStatusType(voteTime);
room.votePlayer(PLAYER1, PLAYER3);
room.votePlayer(PLAYER2, PLAYER3);
room.votePlayer(PLAYER1, PLAYER3, voteTime);
room.votePlayer(PLAYER2, PLAYER3, voteTime);
room.getStatusType(voteResultTime);

// then
Expand All @@ -162,12 +162,26 @@ void setRoom() {
room.getStatusType(noticeTime);
room.getStatusType(dayTime);
room.getStatusType(voteTime);
room.votePlayer(PLAYER1, PLAYER3);
room.votePlayer(PLAYER2, PLAYER3);
room.votePlayer(PLAYER1, PLAYER3, voteTime);
room.votePlayer(PLAYER2, PLAYER3, voteTime);
room.getStatusType(voteResultTime);
room.getStatusType(nightIntroTime);

// when & then
Assertions.assertThat(room.getVoteResult()).isNull();
}

@Test
void 모든_사람이_투표시_상태가_변경된다() {
// given
room.modifyStatus(StatusType.DAY, dayIntroTime);
room.getStatusType(noticeTime);
room.getStatusType(dayTime);
room.votePlayer(PLAYER1, PLAYER3, dayTime);
room.votePlayer(PLAYER2, PLAYER3, dayTime);
room.votePlayer(PLAYER3, PLAYER3, dayTime);

// then
Assertions.assertThat(room.getStatusType(dayTime)).isEqualTo(StatusType.VOTE);
}
}
Loading