Skip to content

Commit

Permalink
增加查询个人身份胜率功能 (#296)
Browse files Browse the repository at this point in the history
* 增加查询个人身份胜率功能

* 修改代码错误
  • Loading branch information
zc-meng authored Sep 18, 2024
1 parent a2e54cf commit 18a3628
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class Game(val id: Int, totalPlayerCount: Int, val actorRef: ActorRef) {
for (p in humanPlayers) {
if (humanPlayers.size <= 1) Statistics.addEnergy(p.playerName, -1)
else Statistics.addEnergy(p.playerName, humanPlayers.size * 2)
playerGameResultList.add(PlayerGameResult(p.playerName, winners.any { it === p }))
playerGameResultList.add(PlayerGameResult(p.playerName, winners.any { it === p }, p.originIdentity))
}
Statistics.addPlayerGameCount(playerGameResultList)
Statistics.calculateRankList()
Expand Down
52 changes: 46 additions & 6 deletions src/main/kotlin/Statistics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.fengsheng

import com.fengsheng.ScoreFactory.addScore
import com.fengsheng.protos.Common.*
import com.fengsheng.protos.Common.color.*
import com.fengsheng.protos.getRecordListToc
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
Expand Down Expand Up @@ -74,16 +75,42 @@ object Statistics {
val now = System.currentTimeMillis()
var win = 0
var game = 0
var rbwin = 0
var rbgame = 0
var blackwin = 0
var blackgame = 0
var updateTrial = false
for (count in playerGameResultList) {
if (count.isWin) {
when (count.identity) {
Black -> {
blackwin++
}
else -> {
rbwin++
}
}
win++
if (trialStartTime.remove(count.playerName) != null) updateTrial = true
}
when (count.identity) {
Black -> {
blackgame++
}
else -> {
rbgame++
}
}
game++
playerInfoMap.computeIfPresent(count.playerName) { _, v ->
val addWin = if (count.isWin) 1 else 0
v.copy(winCount = v.winCount + addWin, gameCount = v.gameCount + 1, lastTime = now)
val addRbWin = if (count.isWin && count.identity != Black) 1 else 0
val addBlackWin = if (count.isWin && count.identity == Black) 1 else 0
val addRbGame = if (count.identity != Black) 1 else 0
val addBlackGame = if (count.identity == Black) 1 else 0
v.copy(winCount = v.winCount + addWin, gameCount = v.gameCount + 1, lastTime = now,
rbWinCount = v.rbWinCount + addRbWin, blackWinCount = v.blackWinCount + addBlackWin,
rbGameCount = v.rbGameCount + addRbGame, blackGameCount = v.blackGameCount + addBlackGame)
}
}
totalWinCount.addAndGet(win)
Expand All @@ -99,7 +126,7 @@ object Statistics {

fun register(name: String): Boolean {
val now = System.currentTimeMillis()
val result = playerInfoMap.putIfAbsent(name, PlayerInfo(name, 0, "", 0, 0, 0, "", now, 10, 0)) == null
val result = playerInfoMap.putIfAbsent(name, PlayerInfo(name, 0, "", 0, 0, 0, "", now, 10, 0, 0, 0, 0, 0)) == null
if (result) pool.trySend(::savePlayerInfo)
return result
}
Expand Down Expand Up @@ -282,7 +309,11 @@ object Statistics {
sb.append(info.title).append(',')
sb.append(info.lastTime).append(',')
sb.append(info.energy).append(',')
sb.append(info.maxScore).append('\n')
sb.append(info.maxScore).append(',')
sb.append(info.rbWinCount).append(',')
sb.append(info.rbGameCount).append(',')
sb.append(info.blackWinCount).append(',')
sb.append(info.blackGameCount).append('\n')
}
writeFile("playerInfo.csv", sb.toString().toByteArray())
sb.clear()
Expand Down Expand Up @@ -311,7 +342,7 @@ object Statistics {
var line: String
while (true) {
line = reader.readLine() ?: break
val a = line.split(",".toRegex(), limit = 10)
val a = line.split(",".toRegex(), limit = 14)
val pwd = a[4]
val score = if (a[3].length < 6) a[3].toInt() else 0 // 以前这个位置是deviceId
val name = a[2]
Expand All @@ -322,7 +353,12 @@ object Statistics {
val lt = (a.getOrNull(7)?.toLong() ?: 0).let { if (it == 0L) System.currentTimeMillis() else it }
val energy = a.getOrNull(8)?.toInt() ?: 0
val maxScore = a.getOrNull(9)?.toInt() ?: score
val p = PlayerInfo(name, score, pwd, win, game, forbid, title, lt, energy, maxScore)
val rbWinCount = a.getOrNull(10)?.toInt() ?: 0
val rbGameCount = a.getOrNull(11)?.toInt() ?: 0
val blackWinCount = a.getOrNull(12)?.toInt() ?: 0
val blackGameCount = a.getOrNull(13)?.toInt() ?: 0
val p = PlayerInfo(name, score, pwd, win, game, forbid, title, lt, energy, maxScore,
rbWinCount, rbGameCount, blackWinCount, blackGameCount)
if (playerInfoMap.put(name, p) != null)
throw RuntimeException("数据错误,有重复的玩家name")
winCount += win
Expand Down Expand Up @@ -407,7 +443,7 @@ object Statistics {
val totalPlayerCount: Int
)

class PlayerGameResult(val playerName: String, val isWin: Boolean)
class PlayerGameResult(val playerName: String, val isWin: Boolean, val identity: color)

data class PlayerGameCount(val winCount: Int, val gameCount: Int) {
fun random(): PlayerGameCount {
Expand All @@ -431,6 +467,10 @@ object Statistics {
val lastTime: Long,
val energy: Int,
val maxScore: Int,
val rbWinCount: Int,
val rbGameCount: Int,
val blackWinCount: Int,
val blackGameCount: Int
) : Comparable<PlayerInfo> {
val scoreWithDecay: Int
get() {
Expand Down
12 changes: 11 additions & 1 deletion src/main/kotlin/gm/Getscore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ class Getscore : Function<Map<String, String>, Any> {
if (playerInfo.gameCount == 0) "0.00%"
else "%.2f%%".format(playerInfo.winCount * 100.0 / total)
val energy = playerInfo.energy
var s = "$name·$rank·$score,总场次:$total,胜率:$winRate,精力:$energy"
val rbGameCount = playerInfo.rbGameCount
val winRbRate =
if (playerInfo.rbGameCount == 0) "0.00%"
else "%.2f%%".format(playerInfo.rbWinCount * 100.0 / rbGameCount)
val blackGameCount = playerInfo.blackGameCount
val winBlackRate =
if (playerInfo.blackGameCount == 0) "0.00%"
else "%.2f%%".format(playerInfo.blackWinCount * 100.0 / blackGameCount)
var s1 = "$name·$rank·$score,总场次:$total(军潜:$rbGameCount,神秘人:$blackGameCount),"
var s2 = "胜率:$winRate(军潜:$winRbRate,神秘人:$winBlackRate),精力:$energy"
var s = s1 + s2
if (playerInfo.score != score) s += "(长期不打会掉分,打一场即可全部恢复)"
val history = QQPusher.getHistory(name)
if (history.isNotEmpty())
Expand Down

0 comments on commit 18a3628

Please sign in to comment.