Skip to content

Commit

Permalink
Merge pull request #2 from lkoiescg2031/feature/sudoku-back-init
Browse files Browse the repository at this point in the history
기본 설정 및 랜덤 api 추가
  • Loading branch information
lkoiescg2031 authored Mar 12, 2022
2 parents 4985eb2 + 0616fdc commit e4f8338
Show file tree
Hide file tree
Showing 23 changed files with 455 additions and 0 deletions.
10 changes: 10 additions & 0 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
9 changes: 9 additions & 0 deletions sudoku-back/bin/main/application-local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
logging:
config: classpath:logger/log4j2-local.xml

spring:
datasource:
driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url: jdbc:log4jdbc:postgresql://localhost:5432/sudoku
username: admin
password: admin
Empty file.
11 changes: 11 additions & 0 deletions sudoku-back/bin/main/io/sudoku/sudoku/SudokuApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.sudoku.sudoku

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class SudokuApplication

fun main(args: Array<String>) {
runApplication<SudokuApplication>(*args)
}
42 changes: 42 additions & 0 deletions sudoku-back/bin/main/io/sudoku/sudoku/config/SwaggerConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.sudoku.sudoku.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springfox.documentation.builders.ApiInfoBuilder
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.service.ApiInfo
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2

@EnableSwagger2
@Configuration
class SwaggerConfig {

companion object {
private val API_TITLE = "sudoku.io api"
private val API_VERSION = "1.0"
private val API_DESCRIPTION = "Sudoku io API Document"
private val API_LICENSE = "The MIT License"
}

@Bean
fun api() : Docket = Docket(DocumentationType.SWAGGER_2).run {
apiInfo(apiInfo())
select()
}.run {
apis(RequestHandlerSelectors.any())
paths(PathSelectors.any())
build()
}

fun apiInfo() : ApiInfo = ApiInfoBuilder().run {
title(API_TITLE)
version(API_VERSION)
description(API_DESCRIPTION)
license(API_LICENSE)
build()
}

}
22 changes: 22 additions & 0 deletions sudoku-back/bin/main/io/sudoku/sudoku/math/Matrix.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.sudoku.sudoku.math

operator fun List<Int>.times(other: List<Int>): List<Int> {
assert(this.size == 9)
assert(this.size == other.size)

return Array(9) { it }.let {
it[0] = this[0] * other[0] + this[1] * other[3] + this[2] * other[6]
it[1] = this[0] * other[1] + this[1] * other[4] + this[2] * other[7]
it[2] = this[0] * other[2] + this[1] * other[5] + this[2] * other[8]

it[3] = this[3] * other[0] + this[4] * other[3] + this[5] * other[6]
it[4] = this[3] * other[1] + this[4] * other[4] + this[5] * other[7]
it[5] = this[3] * other[2] + this[4] * other[5] + this[5] * other[8]

it[6] = this[6] * other[0] + this[7] * other[3] + this[8] * other[6]
it[7] = this[6] * other[1] + this[7] * other[4] + this[8] * other[7]
it[8] = this[6] * other[2] + this[7] * other[5] + this[8] * other[8]

it.toList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.sudoku.sudoku.sudoku.controller

import io.sudoku.sudoku.sudoku.model.Sudoku
import io.sudoku.sudoku.sudoku.model.SudokuRequest
import io.sudoku.sudoku.sudoku.service.SudokuService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v1/sudoku")
class SudokuController {

@Autowired
private lateinit var sudokuService: SudokuService;

@GetMapping("/random")
fun getRandomMap(sudokuRequest: SudokuRequest): Sudoku
= sudokuService.getRandomMap(sudokuRequest)
}
21 changes: 21 additions & 0 deletions sudoku-back/bin/main/io/sudoku/sudoku/sudoku/model/Sudoku.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.sudoku.sudoku.sudoku.model

typealias Board = List<List<Int>>

data class Sudoku(val map: Board)

/**
* ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
* | 1 2 3 | 1 2 3 | 1 2 3 |
* | 1 2 3 | 1 2 3 | 1 2 3 |
* | 1 2 3 | 1 2 3 | 1 2 3 |
* ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
* | 1 2 3 | 1 2 3 | 1 2 3 |
* | 1 2 3 | 1 2 3 | 1 2 3 |
* | 1 2 3 | 1 2 3 | 1 2 3 |
* ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
* | 1 2 3 | 1 2 3 | 1 2 3 |
* | 1 2 3 | 1 2 3 | 1 2 3 |
* | 1 2 3 | 1 2 3 | 1 2 3 |
* ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
* */
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.sudoku.sudoku.sudoku.model

import org.hibernate.validator.constraints.Range

data class SudokuRequest(

val seed: Long? = null,

@Range(min = 18, max = 81) val showCount: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.sudoku.sudoku.sudoku.service

import io.sudoku.sudoku.sudoku.model.Sudoku
import io.sudoku.sudoku.sudoku.model.SudokuRequest

interface SudokuService {
fun getRandomMap(sudokuRequest: SudokuRequest): Sudoku
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.sudoku.sudoku.sudoku.service

import io.sudoku.sudoku.math.times
import io.sudoku.sudoku.sudoku.model.Sudoku
import io.sudoku.sudoku.sudoku.model.SudokuRequest
import org.springframework.stereotype.Service
import java.util.*

@Service
class SudokuServiceImpl : SudokuService {

override fun getRandomMap(sudokuRequest: SudokuRequest): Sudoku {
val s0 = sudokuRequest.seed?.let {
getLine(it)
} ?: getLine()

val map = getMap(s0)
val ret = filterMap(map, sudokuRequest.showCount)

return Sudoku(ret)
}

private fun getLine(): List<Int> = (1..9).toList().shuffled()

private fun getLine(seed: Long): List<Int> = (1..9).toList().shuffled(Random(seed))

private fun getMap(s0: List<Int>): List<List<Int>> {

val x1 = listOf(0, 0, 1, 1, 0, 0, 0, 1, 0)
val x2 = listOf(0, 1, 0, 0, 0, 1, 1, 0, 0)

val s1 = x2 * s0
val s2 = x1 * s0
val s3 = s0 * x1
val s4 = x2 * s0 * x1
val s5 = x1 * s0 * x1
val s6 = s0 * x2
val s7 = x2 * s0 * x2
val s8 = x1 * s0 * x2

return listOf(s0, s1, s2, s3, s4, s5, s6, s7, s8)
}

private fun filterMap(map: List<List<Int>>, showCount: Int): List<List<Int>> {
var random = Random()

var cnt = 0
var ret = Array(9) { Array(9) { 0 } }

while (true) {
var i = random.nextInt(8) + 1
var j = random.nextInt(8) + 1

if (ret[i][j] == 0 && random.nextBoolean()) {
ret[i][j] = map[i][j]
cnt++
if (cnt >= showCount) {
return ret.map { it.toList() }.toList()
}
}
}
}
}
25 changes: 25 additions & 0 deletions sudoku-back/bin/main/io/sudoku/sudoku/utils/LoggerDelegate.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.sudoku.sudoku.utils

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

class LoggerDelegate : ReadOnlyProperty<Any?, Logger> {

private var logger: Logger? = null

override fun getValue(thisRef: Any?, property: KProperty<*>): Logger {
if (logger == null) {
logger = createLogger(thisRef!!.javaClass)
}

return logger!!
}

companion object {
private fun <T> createLogger(clazz: Class<T>) : Logger {
return LoggerFactory.getLogger(clazz);
}
}
}
49 changes: 49 additions & 0 deletions sudoku-back/bin/main/logger/log4j2-local.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">

<Properties>
<Property name="logFileName">log</Property>
<Property name="consoleLayout">%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable</Property>
<Property name="fileLayout">%d [%t] %-5level %c(%M:%L) - %m%n</Property>
</Properties>

<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${consoleLayout}" />
</Console>
<RollingFile name="file" fileName="logs/${logFileName}.log" filePattern="logs/${logFileName}.%d{yyyy-MM-dd-hh}.log">
<PatternLayout pattern="${fileLayout}" />
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1" /><!-- 시간별 로그 파일 생성-->
</Policies>
<DefaultRolloverStrategy max="5" fileIndex="min" /><!-- 롤링 파일 5개 까지 생성 -->
</RollingFile>
</Appenders>

<Loggers>

<!-- spring framework logging -->
<Logger name="org.springframework" level="info" additivity="false" >
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Logger>

<!-- application logging -->
<Logger name="io.sudoku.sudoku" additivity="true" >
<AppenderRef ref="console" level="info" />
<AppenderRef ref="file" level="info" />
</Logger>

<!-- jdbc logging -->
<Logger name="log4jdbc.log4j2" level="info" additivity="false">
<MarkerFilter marker="LOG4JDBC_JDBC" onMatch="DENY" onMismatch="NEUTRAL"/>
<Appender-ref ref="log4jdbc_file"/>
</Logger>

<Root level="info">
<AppenderRef ref="console"/>
</Root>

</Loggers>

</Configuration>
13 changes: 13 additions & 0 deletions sudoku-back/bin/test/io/sudoku/sudoku/SudokuApplicationTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.sudoku.sudoku

import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class SudokuApplicationTests {

@Test
fun contextLoads() {
}

}
2 changes: 2 additions & 0 deletions sudoku-back/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ dependencies {
// spring boot dependency 설정
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
// been validation
implementation("org.springframework.boot:spring-boot-starter-validation:2.6.4")

// kotlin dependency 설정
implementation("org.jetbrains.kotlin:kotlin-reflect")
Expand Down
22 changes: 22 additions & 0 deletions sudoku-back/src/main/kotlin/io/sudoku/sudoku/math/Matrix.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.sudoku.sudoku.math

operator fun List<Int>.times(other: List<Int>): List<Int> {
assert(this.size == 9)
assert(this.size == other.size)

return Array(9) { it }.let {
it[0] = this[0] * other[0] + this[1] * other[3] + this[2] * other[6]
it[1] = this[0] * other[1] + this[1] * other[4] + this[2] * other[7]
it[2] = this[0] * other[2] + this[1] * other[5] + this[2] * other[8]

it[3] = this[3] * other[0] + this[4] * other[3] + this[5] * other[6]
it[4] = this[3] * other[1] + this[4] * other[4] + this[5] * other[7]
it[5] = this[3] * other[2] + this[4] * other[5] + this[5] * other[8]

it[6] = this[6] * other[0] + this[7] * other[3] + this[8] * other[6]
it[7] = this[6] * other[1] + this[7] * other[4] + this[8] * other[7]
it[8] = this[6] * other[2] + this[7] * other[5] + this[8] * other[8]

it.toList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.sudoku.sudoku.sudoku.controller

import io.sudoku.sudoku.sudoku.model.Sudoku
import io.sudoku.sudoku.sudoku.model.SudokuRequest
import io.sudoku.sudoku.sudoku.service.SudokuService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v1/sudoku")
class SudokuController {

@Autowired
private lateinit var sudokuService: SudokuService;

@GetMapping("/random")
fun getRandomMap(sudokuRequest: SudokuRequest): Sudoku
= sudokuService.getRandomMap(sudokuRequest)
}
Loading

0 comments on commit e4f8338

Please sign in to comment.