Skip to content

Commit

Permalink
勋章根据分数大小排序 (#301)
Browse files Browse the repository at this point in the history
* 勋章根据分数大小排序

* 增加单元测试

* 增加单元测试到工作流

* 解决处理字符串问题

---------

Co-authored-by: 奇葩の灵梦 <[email protected]>
  • Loading branch information
zc-meng and CuteReimu authored Sep 27, 2024
1 parent aee4784 commit 447159a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3

- name: Execute Gradle test
env:
GRADLE_OPTS: '-Xmx4g'
run: ./gradlew test

- name: Execute Gradle build
env:
GRADLE_OPTS: '-Xmx4g'
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ repositories {
}

dependencies {
testImplementation("junit:junit:4.9")
implementation("com.typesafe.akka:akka-actor_2.13:2.8.5")
implementation("io.netty:netty-all:4.1.108.Final")
implementation("io.ktor:ktor-server-netty:2.3.10")
Expand Down
29 changes: 28 additions & 1 deletion src/main/kotlin/Statistics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,11 @@ object Statistics {
playerInfoMap.keys.forEach {
playerInfoMap.computeIfPresent(it) { _, v ->
if (v.score <= 1) return@computeIfPresent null
val newTitle = v.title + getSeasonTitleByScore(v.maxScore)
v.copy(
winCount = 0,
gameCount = 0,
title = v.title + getSeasonTitleByScore(v.maxScore),
title = sortTitles(newTitle),
score = v.score / 2,
energy = v.energy.coerceAtLeast(10),
maxScore = v.score / 2,
Expand All @@ -342,6 +343,32 @@ object Statistics {
val totalPlayerGameCount: PlayerGameCount
get() = PlayerGameCount(totalWinCount.get(), totalGameCount.get())

fun getTitleRank(title: String): Int = when (title) {
"\u2B50" -> 1 // score >= 2900
"\uD83D\uDC51" -> 2 // score >= 1900
"\uD83D\uDCA0" -> 3 // score >= 1400
"\uD83D\uDC8D" -> 4 // score >= 920
"\uD83E\uDD47" -> 5 // score >= 520
else -> 6 // Lower than 520
}

fun sortTitles(titles: String): String {
val titleList = mutableListOf<String>()
var i = 0
while (i < titles.length) {
if (titles[i] == '\u2B50') {
// 如果是单字符 emoji(⭐)
titleList.add(titles[i].toString())
i += 1
} else {
// 处理其他双字符 emoji
titleList.add(titles.substring(i, i + 2))
i += 2
}
}
return titleList.sortedBy { getTitleRank(it) }.joinToString("")
}

private fun savePlayerInfo() {
val sb = StringBuilder()
for ((_, info) in playerInfoMap) {
Expand Down
12 changes: 12 additions & 0 deletions src/test/kotlin/Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.fengsheng

import org.junit.Assert.assertEquals
import org.junit.Test

class Test {
@Test
fun sortTitlesTest() {
val s = Statistics.sortTitles("💠👑🏅👑🏅💍💠🏅💠")
assertEquals("👑👑💠💠💠💍🏅🏅🏅", s)
}
}

0 comments on commit 447159a

Please sign in to comment.