Skip to content

Commit

Permalink
release: 1.0.2 (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Dec 25, 2024
2 parents 97a3f3a + 00089f9 commit 053dd41
Show file tree
Hide file tree
Showing 102 changed files with 2,397 additions and 104 deletions.
25 changes: 25 additions & 0 deletions src/main/kotlin/org/gitanimals/guild/app/DrawGuildProxy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.gitanimals.guild.app

import org.gitanimals.guild.domain.GuildService
import org.gitanimals.guild.domain.GuildService.Companion.loadMembers
import org.gitanimals.render.app.DrawGuildFacade
import org.springframework.stereotype.Service

@Service
class DrawGuildProxy(
private val guildService: GuildService,
private val renderDrawGuildFacade: DrawGuildFacade,
) {

fun drawGuild(id: Long): String {
val guild = guildService.getGuildById(id, loadMembers)

return renderDrawGuildFacade.drawGuild(
title = guild.getTitle(),
totalContributions = guild.getTotalContributions(),
guildFarmType = guild.getGuildFarmType(),
userIds = guild.getMembers().map { it.userId } + guild.getLeaderUserId(),
personaIds = guild.getMembers().map { it.personaId } + guild.getLeaderPersonaId(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class JoinGuildFacade(
) {
sagaManager.startSync(
InboxInputEvent.guildJoinRequest(
userId = guild.getLeaderId(),
userId = guild.getLeaderUserId(),
newUserImage = member.profileImage,
newUserName = member.username,
guildTitle = guild.getTitle(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class GuildController(
private val searchGuildFacade: SearchGuildFacade,
private val changeMainPersonaFacade: ChangeMainPersonaFacade,
private val leaveGuildFacade: LeaveGuildFacade,
private val drawGuildProxy: DrawGuildProxy,
) {

@ResponseStatus(HttpStatus.OK)
Expand Down Expand Up @@ -135,4 +136,9 @@ class GuildController(
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String,
@PathVariable("guildId") guildId: Long,
) = leaveGuildFacade.leave(token, guildId)

@GetMapping("/guilds/{guildId}/draw")
fun draw(
@PathVariable("guildId") guildId: Long,
) = drawGuildProxy.drawGuild(guildId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ data class GuildResponse(
body = guild.getBody(),
guildIcon = guild.getGuildIcon(),
leader = Leader(
userId = guild.getLeaderId().toString(),
userId = guild.getLeaderUserId().toString(),
name = guild.getLeaderName(),
contributions = guild.getContributions().toString(),
personaId = guild.getLeaderPersonaId().toString(),
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/gitanimals/guild/domain/Guild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Guild(
waitMembers.add(waitMember)
}

fun getLeaderId(): Long = leader.userId
fun getLeaderUserId(): Long = leader.userId

fun accept(acceptUserId: Long) {
val acceptUser = waitMembers.firstOrNull { it.userId == acceptUserId } ?: return
Expand Down
137 changes: 136 additions & 1 deletion src/main/kotlin/org/gitanimals/guild/domain/GuildFarmType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,141 @@ package org.gitanimals.guild.domain

enum class GuildFarmType {

DUMMY,
DUMMY {
override fun loadComponent(name: String, commit: Long): String {
return dummyGuildFieldSvg.replace(NAME_FIX, name.toSvg(0.0, 3.0))
.replace(COMMIT_FIX, commit.toSvg("commit", 260.0, 4.0))
}

override fun fillBackground(): String =
"<rect x=\"0.5\" y=\"0.5\" width=\"599\" height=\"299\" rx=\"4.5\" fill=\"white\"/>"

override fun drawBorder(): String =
"<rect x=\"0.5\" y=\"0.5\" width=\"599\" height=\"299\" rx=\"4.5\" stroke=\"#D9D9D9\" fill=\"none\"/>"
},
;

abstract fun loadComponent(name: String, commit: Long): String

abstract fun fillBackground(): String

abstract fun drawBorder(): String

private companion object {
private const val NAME_FIX = "*{username}"
private const val COMMIT_FIX = "*{commit-count}"
private const val VISIT_FIX = "*{visit-count}"

private val largeTextWidth = mapOf(
"A" to 20.0,
"B" to 20.0,
"C" to 20.0,
"D" to 20.0,
"E" to 20.0,
"F" to 20.0,
"G" to 20.0,
"H" to 20.0,
"I" to 14.0,
"J" to 20.0,
"K" to 20.0,
"L" to 20.0,
"M" to 23.0,
"N" to 23.0,
"O" to 20.0,
"P" to 20.0,
"Q" to 20.0,
"R" to 20.0,
"S" to 20.0,
"T" to 20.0,
"U" to 20.0,
"V" to 20.0,
"W" to 23.0,
"X" to 20.0,
"Y" to 20.0,
"Z" to 20.0,
"-" to 20.0,
"a" to 20.0,
"b" to 20.0,
"c" to 20.0,
"d" to 20.0,
"e" to 20.0,
"f" to 20.0,
"g" to 20.0,
"h" to 20.0,
"i" to 20.0,
"j" to 17.0,
"k" to 20.0,
"l" to 20.0,
"m" to 23.0,
"n" to 20.0,
"o" to 20.0,
"p" to 20.0,
"q" to 20.0,
"r" to 20.0,
"s" to 20.0,
"t" to 20.0,
"u" to 20.0,
"v" to 20.0,
"w" to 23.0,
"x" to 20.0,
"y" to 23.0,
"z" to 20.0,
"0" to 20.0,
"1" to 17.0,
"2" to 20.0,
"3" to 20.0,
"4" to 23.0,
"5" to 20.0,
"6" to 20.0,
"7" to 20.0,
"8" to 20.0,
"9" to 20.0,
)

private val mediumNumberWidth = listOf(
11.0,
9.0,
11.0,
11.0,
12.0,
11.0,
11.0,
11.0,
11.0,
11.0,
)

private fun String.toSvg(startX: Double, xIncrease: Double): String {
val builder = StringBuilder()
var currentX = startX
this.forEach { char ->
val largeTextSvg = largeTextSvgs[char.toString()]
val index = char - 'A'

val charWidth = largeTextWidth[char.toString()]
?: throw IllegalArgumentException("Cannot find matched charWidth by \"$char\"")

builder.append("<g id=\"name$index\" transform=\"translate($currentX, 2)\">")
.append(largeTextSvg)
.append("</g>")

currentX += xIncrease + charWidth
}
return builder.toString()
}

private fun Long.toSvg(id: String, startX: Double, xIncrease: Double): String {
val builder = StringBuilder()
var currentX = startX
this.toString().forEach { char ->
val index = char.digitToInt()
val mediumNumberSvg = mediumNumberSvgs[index]
builder.append("<g id=\"$id$index\" transform=\"translate($currentX, 0)\">")
.append(mediumNumberSvg)
.append("</g>")
currentX += xIncrease + mediumNumberWidth[index]
}
return builder.toString()
}
}
}
54 changes: 54 additions & 0 deletions src/main/kotlin/org/gitanimals/guild/domain/Svgs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.gitanimals.guild.domain

import org.springframework.core.io.ClassPathResource
import java.nio.charset.Charset

val dummyGuildFieldSvg: String = ClassPathResource("persona/field/white-field.svg")
.getContentAsString(Charset.defaultCharset())

val largeTextSvgs = lazy {
val map = mutableMapOf<String, String>()
for (i in 'A'..'Z') {
val path = "persona/text/large/$i.svg"
map[i.toString()] = ClassPathResource(path)
.getContentAsString(Charset.defaultCharset())
}
for (i in 'a'..'z') {
val path = "persona/text/large/_$i.svg"
map[i.toString()] = ClassPathResource(path)
.getContentAsString(Charset.defaultCharset())
}
for (i in 0..9) {
val path = "persona/text/large/$i.svg"
map[i.toString()] = ClassPathResource(path)
.getContentAsString(Charset.defaultCharset())
}
map["-"] = ClassPathResource("persona/text/large/hyphens.svg")
.getContentAsString(Charset.defaultCharset())
map
}.value

val mediumNumberSvgs = lazy {
val list = mutableListOf<String>()
for (i in 0..9) {
val path = "persona/text/medium/$i.svg"
list.add(
ClassPathResource(path)
.getContentAsString(Charset.defaultCharset())
)
}
list
}.value

val numberSvgs = lazy {
val list = mutableListOf<String>()
for (i in 0..9) {
val path = "persona/text/small/$i.svg"
list.add(
ClassPathResource(path)
.getContentAsString(Charset.defaultCharset())
)
}
list
}.value

49 changes: 49 additions & 0 deletions src/main/kotlin/org/gitanimals/render/app/DrawGuildFacade.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.gitanimals.render.app

import org.gitanimals.guild.domain.GuildFarmType
import org.gitanimals.render.domain.Mode
import org.gitanimals.render.domain.UserService
import org.springframework.stereotype.Service

@Service
class DrawGuildFacade(
private val userService: UserService,
) {

fun drawGuild(
title: String,
totalContributions: Long,
guildFarmType: GuildFarmType,
userIds: List<Long>,
personaIds: List<Long>,
): String {
val users = userService.findAllUsersByIdWithContributions(userIds.toSet())

val svgBuilder = StringBuilder().openGuild()
.append(guildFarmType.fillBackground())

val personaSvgs = users.flatMap { user ->
user.personas.filter { persona ->
persona.id in personaIds
}.map { persona ->
persona.toSvgForce(Mode.NAME_WITH_LEVEL)
}
}

personaSvgs.forEach { svgBuilder.append(it) }

return svgBuilder.append(guildFarmType.loadComponent(title, totalContributions))
.append(guildFarmType.drawBorder())
.closeGuild()
}

private fun StringBuilder.openGuild(): StringBuilder =
this.append("<svg width=\"600\" height=\"300\" viewBox=\"0 0 600 300\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">")

private fun StringBuilder.closeGuild(): String = this
.append("</svg>")
.toString()

}


1 change: 1 addition & 0 deletions src/main/kotlin/org/gitanimals/render/domain/Mode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ enum class Mode {
FARM,
LINE,
LINE_NO_CONTRIBUTION,
NAME_WITH_LEVEL,
;
}
Loading

0 comments on commit 053dd41

Please sign in to comment.