Skip to content

Commit

Permalink
release: 0.1.2 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Apr 12, 2024
2 parents eacc69d + 16a7e2e commit f0ad53b
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 35 deletions.
45 changes: 40 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<img src="./docs/logo.svg" width="30000"/>

##
<a href="https://github.com/devxb/gitanimals">
<img src="https://render.gitanimals.org/lines/devxb" width="2000" height="60"/>
</a>

<div align = "center">
⭐스타를 눌러주세요 개발에 큰 도움이 됩니다!⭐️<br>
Expand All @@ -26,7 +29,15 @@

## 빠르게 사용하기

아래의 MARKDOWN 혹은 HTML 방식중 하나를 붙여넣기 하는것으로 쉽게 적용할 수 있어요.
아래의 MARKDOWN 혹은 HTML 중 하나를 깃허브 Readme 에 붙여넣기 하는것으로 쉽게 적용할 수 있어요.

> [!IMPORTANT]
> {username} 은 자신의 깃허브 닉네임 (ex. devxb) 으로 변경해주세요.
> 이때, {username} 은 반드시 자신의 깃허브 이름이 들어가야 합니다.
### farm mode

farm mode는 갖고있는 모든 동물과 추가적인 정보를 보여줘요.

```markdown
[![gitanimals](https://render.gitanimals.org/farms/{username})](https://github.com/devxb/gitanimals)
Expand All @@ -37,12 +48,36 @@
<img src = "https://render.gitanimals.org/farms/{username}"/>
</a>
```

> [!IMPORTANT]
> {username} 은 자신의 깃허브 닉네임 (ex. devxb) 으로 변경해주세요.
> 이때, {username} 은 반드시 자신의 깃허브 이름이 들어가야 합니다.

### line mode

line mode는 자신이 갖고있는 펫중 하나를 지정해서, 지정한 width, height범위에서 움직이게 해요.
펫이 Readme에서 자유롭게 움직이는모습을 표현할 수 있어요.
pet-id에 아무값도 입력하지 않으면, 첫번째 펫이 가져와져요.
> [!TIP]
> **Img의 width와 height를 조절해서 펫의 이동영역을 조절할 수 있어요.**
> width를 길게 height를 작게하면 (width = 1000, height = 60) 가로로 길게 움직이게 할 수 있어요.
> 반대로, width를 작게 height를 길게하면 (width = 60, height = 1000) 세로로 길게 움직이게 할 수 있어요.
```markdown
[![gitanimals](https://render.gitanimals.org/farms/{username}?mode=line)](https://github.com/devxb/gitanimals)
```

```html
<a href="https://github.com/devxb/gitanimals">
<img src = "https://render.gitanimals.org/lines/{username}" width="1000" height="60"/>
</a>
```

펫은 다음 url을 웹에 요청 후, `$.personas.[].id` 에 해당하는 값을 url의 pet-id param에 입력하면서 변경할 수 있어요.
사용 가능한 펫을 확인하려면, `https://render.gitanimals.org/users/{username}` 을 url에 붙여넣으세요.
```html
예시)

<a href="https://github.com/devxb/gitanimals">
<img src = "https://render.gitanimals.org/lines/devxb?pet-id=1" width="1000" height="60"/>
</a>
```

## TIPS

Expand Down
24 changes: 19 additions & 5 deletions src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.gitanimals.render.app

import jakarta.annotation.PostConstruct
import org.gitanimals.render.domain.AnimationMode
import org.gitanimals.render.domain.User
import org.gitanimals.render.domain.UserService
import org.gitanimals.render.domain.event.Visited
Expand All @@ -18,19 +17,34 @@ class AnimationFacade(

private lateinit var registerNewUserOrchestrator: Orchestrator<String, User>

fun getSvgAnimation(username: String, mode: String): String {
val animationMode = AnimationMode.valueOf(mode.uppercase())
fun getFarmAnimation(username: String): String {
return when (userService.existsByName(username)) {
true -> {
val svgAnimation = userService.getSvgAnimationByUsername(username, animationMode)
val svgAnimation = userService.getFarmAnimationByUsername(username)
sagaManager.startSync(Visited(username))
svgAnimation
}

false -> {
registerNewUserOrchestrator.sagaSync(10000, username)

userService.getSvgAnimationByUsername(username, animationMode)
userService.getFarmAnimationByUsername(username)
}
}
}

fun getLineAnimation(username: String, personaId: Long): String {
return when(userService.existsByName(username)) {
true -> {
val svgAnimation = userService.getLineAnimationByUsername(username, personaId)
sagaManager.startSync(Visited(username))
svgAnimation
}

false -> {
registerNewUserOrchestrator.sagaSync(10000, username)

userService.getLineAnimationByUsername(username ,personaId)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@ import jakarta.servlet.http.HttpServletResponse
import org.gitanimals.render.app.AnimationFacade
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/farms")
class AnimationController(
private val animationFacade: AnimationFacade,
) {

@GetMapping(value = ["/{username}"], produces = ["image/svg+xml"])
fun getSvgAnimation(
@GetMapping(value = ["/farms/{username}"], produces = ["image/svg+xml"])
fun getFarmSvgAnimation(
@PathVariable("username") username: String,
@RequestParam(name = "mode", defaultValue = "FARM") mode: String,
response: HttpServletResponse
): String {
response.noCache()
return animationFacade.getSvgAnimation(username, mode)
return animationFacade.getFarmAnimation(username)
}

@GetMapping(value = ["/lines/{username}"], produces = ["image/svg+xml"])
fun getLineSvgAnimation(
@PathVariable("username") username: String,
@RequestParam(name = "pet-id", defaultValue = "0") personaId: Long,
response: HttpServletResponse,
): String {
response.noCache()
return animationFacade.getLineAnimation(username, personaId)
}

fun HttpServletResponse.noCache(): HttpServletResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.gitanimals.render.controller

import org.gitanimals.render.controller.response.UserResponse
import org.gitanimals.render.domain.UserService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class PersonaController(
private val userService: UserService
) {

@GetMapping("/users/{username}")
fun getUserByName(@PathVariable("username") username: String): UserResponse {
return UserResponse.from(userService.getUserByName(username))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.gitanimals.render.controller.response

import org.gitanimals.render.domain.PersonaType

data class PersonaResponse(
val id: String,
val type: PersonaType,
val level: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.gitanimals.render.controller.response

import org.gitanimals.render.domain.User

data class UserResponse(
val id: String,
val name: String,
private val personas: List<PersonaResponse>,
) {

companion object {
fun from(user: User): UserResponse {
return UserResponse(
user.id.toString(),
user.name,
user.personas.map {
PersonaResponse(it.id.toString(), it.type, it.level().toString())
}.toList()
)
}
}
}
11 changes: 0 additions & 11 deletions src/main/kotlin/org/gitanimals/render/domain/AnimationMode.kt

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/kotlin/org/gitanimals/render/domain/Persona.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ class Persona(


fun toSvg(): String = type.load(this)

fun level(): Long = level.value
}
4 changes: 2 additions & 2 deletions src/main/kotlin/org/gitanimals/render/domain/PersonaType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ enum class PersonaType(private val weight: Double) {
duration: String,
personaWidth: Long,
): StringBuilder {
var currentY = Random.nextInt(10, 70)
var currentY = Random.nextInt(10, 90)
var currentX = Random.nextInt(10, 90)
var currentAngle = (Random.nextDouble() * 10).toInt()
var currentScale = Random.nextInt(0, 2) - 1
Expand All @@ -973,7 +973,7 @@ enum class PersonaType(private val weight: Double) {
val beforeAnimationPercentage = animationPercentage
animationPercentage += Random.nextInt(2, 6)
val nextY =
Random.nextInt(max(0, min(79, currentY - speed)), min(70, currentY + speed))
Random.nextInt(max(10, min(79, currentY - speed)), min(80, currentY + speed))
val nextX =
Random.nextInt(max(10, min(79, currentX - speed)), min(80, currentX + speed))
val nextAngle = (atan2(
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/org/gitanimals/render/domain/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class User(
val name: String,

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
private val personas: MutableList<Persona> = mutableListOf(),
val personas: MutableList<Persona> = mutableListOf(),

@BatchSize(size = 20)
@ElementCollection(fetch = FetchType.LAZY)
Expand Down Expand Up @@ -101,10 +101,11 @@ class User(
visit += 1
}

fun createLineAnimation(): String {
fun createLineAnimation(personaId: Long): String {
val builder = StringBuilder().openLine()

val persona = personas.first()
val persona = personas.find { it.id!! >= personaId }
?: throw IllegalArgumentException("Cannot find persona by id \"$personaId\"")
builder.append(persona.toSvg())

return builder.closeSvg()
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/org/gitanimals/render/domain/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ class UserService(

fun existsByName(name: String): Boolean = userRepository.existsByName(name)

fun getSvgAnimationByUsername(username: String, mode: AnimationMode): String {
val user = getUserByName(username)
return mode.createAnimation(user)
fun getFarmAnimationByUsername(username: String): String {
return getUserByName(username).createFarmAnimation()
}

fun getLineAnimationByUsername(username: String, personaId: Long): String {
return getUserByName(username).createLineAnimation(personaId)
}

@Retryable(retryFor = [OptimisticLockingFailureException::class], maxAttempts = 10)
Expand Down

0 comments on commit f0ad53b

Please sign in to comment.