-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 세션 검증V2 구현 * refactor: 코틀린 마이그레이션 * feat(snackgame): 테스트 작성 * refactor: 리뷰 반영 * feat(board): 테스트 추가 * fix: AttributeConverter 분리 * fix: 테스트 수정
- Loading branch information
Showing
46 changed files
with
865 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ain/java/com/snackgame/server/game/snackgame.infinite/service/SnackgameInfiniteService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
.../com/snackgame/server/game/snackgame.infinite/service/dto/SnackgameInfiniteEndResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/com/snackgame/server/game/snackgame/domain/Board.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.snackgame.server.game.snackgame.domain | ||
|
||
import com.snackgame.server.game.snackgame.exception.InvalidBoardSizeException | ||
import com.snackgame.server.game.snackgame.exception.InvalidCoordinateException | ||
import com.snackgame.server.game.snackgame.exception.SnackNotRemovableException | ||
import com.snackgame.server.game.snackgame.snack.EmptySnack | ||
import com.snackgame.server.game.snackgame.snack.Snack | ||
import java.util.stream.Collectors | ||
|
||
|
||
class Board() { | ||
private var snacks: MutableList<MutableList<Snack>> = arrayListOf() | ||
|
||
constructor(snacks: MutableList<MutableList<Snack>>) : this() { | ||
validateIsRectangle(snacks) | ||
this.snacks = snacks.map { ArrayList(it) }.toMutableList() | ||
} | ||
|
||
constructor(height: Int, width: Int) : this(createRandomized(height, width)) | ||
|
||
private fun validateIsRectangle(snacks: List<List<Snack>>) { | ||
if (snacks.isEmpty() || snacks[0].isEmpty()) { | ||
throw InvalidBoardSizeException() | ||
} | ||
} | ||
|
||
fun reset(): MutableList<MutableList<Snack>> { | ||
return createRandomized(getHeight(), getWidth()) | ||
} | ||
|
||
fun removeSnacksIn(streak: Streak): List<Snack> { | ||
validateIsIncluded(streak.toCoordinates()) | ||
validateSumOf(streak.toCoordinates()) | ||
|
||
return streak.toCoordinates().stream().map(this::removeSnacksAt).filter(Snack::exists) | ||
.collect(Collectors.toList()) | ||
} | ||
|
||
private fun validateIsIncluded(coordinates: List<Coordinate>) { | ||
coordinates.forEach { coordinate -> | ||
if (coordinate.y >= snacks.size || coordinate.x >= snacks[0].size) { | ||
throw InvalidCoordinateException() | ||
} | ||
} | ||
} | ||
|
||
private fun validateSumOf(coordinates: List<Coordinate>) { | ||
if (sumSnacksIn(coordinates) != REMOVABLE_SUM) { | ||
throw SnackNotRemovableException("스낵들의 합이 " + REMOVABLE_SUM + "이 아닙니다") | ||
} | ||
} | ||
|
||
private fun sumSnacksIn(coordinates: List<Coordinate>): Int { | ||
return getSnacksIn(coordinates).stream().map(Snack::getNumber).reduce(0, Integer::sum) | ||
|
||
} | ||
|
||
private fun getSnacksIn(coordinates: List<Coordinate>): List<Snack> { | ||
return coordinates.stream().map { coordinate -> snacks[coordinate.y][coordinate.x] } | ||
.collect(Collectors.toList()) | ||
} | ||
|
||
private fun removeSnacksAt(coordinate: Coordinate): Snack { | ||
val row = snacks[coordinate.y] | ||
return row.set(coordinate.x, EmptySnack.get()) | ||
} | ||
|
||
fun getSnacks(): List<List<Snack>> { | ||
return snacks.map { ArrayList(it) } | ||
} | ||
|
||
|
||
private fun getHeight(): Int { | ||
return this.snacks.size | ||
} | ||
|
||
private fun getWidth(): Int { | ||
return this.snacks[0].size | ||
} | ||
|
||
companion object { | ||
private const val REMOVABLE_SUM = 10; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/snackgame/server/game/snackgame/domain/BoardConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.snackgame.server.game.snackgame.domain | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect | ||
import com.fasterxml.jackson.annotation.PropertyAccessor | ||
import com.fasterxml.jackson.core.JsonProcessingException | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import javax.persistence.AttributeConverter | ||
|
||
|
||
class BoardConverter : AttributeConverter<Board, String> { | ||
|
||
override fun convertToDatabaseColumn(board: Board): String { | ||
return try { | ||
OBJECT_MAPPER.writeValueAsString(board) | ||
} catch (e: JsonProcessingException) { | ||
throw RuntimeException(e) | ||
} | ||
} | ||
|
||
override fun convertToEntityAttribute(dbJson: String): Board { | ||
return try { | ||
OBJECT_MAPPER.readValue(dbJson, Board::class.java) | ||
} catch (e: JsonProcessingException) { | ||
throw RuntimeException(e) | ||
} | ||
} | ||
|
||
companion object { | ||
private val OBJECT_MAPPER = ObjectMapper().apply { | ||
setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) | ||
setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE) | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/snackgame/server/game/snackgame/domain/Coordinate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.snackgame.server.game.snackgame.domain | ||
|
||
import com.snackgame.server.game.snackgame.exception.NegativeCoordinateException | ||
|
||
|
||
class Coordinate(val y: Int, val x: Int) { | ||
|
||
init { | ||
validateNonNegative(y) | ||
validateNonNegative(x) | ||
} | ||
|
||
|
||
private fun validateNonNegative(axisCoordinate: Int) { | ||
if (axisCoordinate < 0) { | ||
throw NegativeCoordinateException() | ||
} | ||
} | ||
|
||
override fun equals(o: Any?): Boolean { | ||
|
||
if (this === o) return true | ||
if (o == null || javaClass != o.javaClass) return false | ||
|
||
val that = o as Coordinate | ||
|
||
return if (y != that.y) false else x == that.x | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = y | ||
result = 31 * result + x | ||
return result | ||
} | ||
|
||
override fun toString(): String { | ||
return "Coordinate{" + | ||
"y=" + y + | ||
", x=" + x + | ||
'}' | ||
} | ||
} |
Oops, something went wrong.