Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

查询功能优化 #297

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/kotlin/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ 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 }, p.originIdentity))
playerGameResultList.add(PlayerGameResult(p.playerName, winners.any { it === p },
p.originIdentity, p.originSecretTask))
}
Statistics.addPlayerGameCount(playerGameResultList)
Statistics.calculateRankList()
Expand Down
149 changes: 126 additions & 23 deletions src/main/kotlin/Statistics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.fengsheng
import com.fengsheng.ScoreFactory.addScore
import com.fengsheng.protos.Common.*
import com.fengsheng.protos.Common.color.*
import com.fengsheng.protos.Common.secret_task.*
import com.fengsheng.protos.getRecordListToc
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
Expand Down Expand Up @@ -79,38 +80,93 @@ object Statistics {
var rbgame = 0
var blackwin = 0
var blackgame = 0
var killerwin = 0
var killergame = 0
var stealerwin = 0
var stealergame = 0
var collectorwin = 0
var collectorgame = 0
var mutatorwin = 0
var mutatorgame = 0
var pioneerwin = 0
var pioneergame = 0
var disturberwin = 0
var disturbergame = 0
var sweeperwin = 0
var sweepergame = 0
var updateTrial = false
for (count in playerGameResultList) {
if (count.isWin) {
when (count.identity) {
Black -> {
blackwin++
if (count.identity == Black) {
blackwin++
when (count.secret_task) {
Killer -> killerwin++
Stealer -> stealerwin++
Collector -> collectorwin++
Mutator -> mutatorwin++
Pioneer -> pioneerwin++
Disturber -> disturberwin++
Sweeper -> sweeperwin++
else -> {}
}
else -> {
rbwin++
}
}
} else rbwin++
win++
if (trialStartTime.remove(count.playerName) != null) updateTrial = true
}
when (count.identity) {
Black -> {
blackgame++
if (count.identity == Black) {
blackgame++
when (count.secret_task) {
Killer -> killergame++
Stealer -> stealergame++
Collector -> collectorgame++
Mutator -> mutatorgame++
Pioneer -> pioneergame++
Disturber -> disturbergame++
Sweeper -> sweepergame++
else -> {}
}
else -> {
rbgame++
}
}
} else rbgame++
game++
playerInfoMap.computeIfPresent(count.playerName) { _, v ->
val addWin = if (count.isWin) 1 else 0
val addRbWin = if (count.isWin && count.identity != Black) 1 else 0
val addBlackWin = if (count.isWin && count.identity == Black) 1 else 0
val addKillerWin = if (count.isWin && count.identity == Black && count.secret_task == Killer) 1 else 0
val addStealerWin = if (count.isWin && count.identity == Black && count.secret_task == Stealer) 1 else 0
val addCollectorWin = if (count.isWin && count.identity == Black && count.secret_task == Collector) 1 else 0
val addMutatorWin = if (count.isWin && count.identity == Black && count.secret_task == Mutator) 1 else 0
val addPioneerWin = if (count.isWin && count.identity == Black && count.secret_task == Pioneer) 1 else 0
val addDisturberWin = if (count.isWin && count.identity == Black && count.secret_task == Disturber) 1 else 0
val addSweeperWin = if (count.isWin && count.identity == Black && count.secret_task == Sweeper) 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)
val addKillerGame = if (count.identity == Black && count.secret_task == Killer) 1 else 0
val addStealerGame = if (count.identity == Black && count.secret_task == Stealer) 1 else 0
val addCollectorGame = if (count.identity == Black && count.secret_task == Collector) 1 else 0
val addMutatorGame = if (count.identity == Black && count.secret_task == Mutator) 1 else 0
val addPioneerGame = if (count.identity == Black && count.secret_task == Pioneer) 1 else 0
val addDisturberGame = if (count.identity == Black && count.secret_task == Disturber) 1 else 0
val addSweeperGame = if (count.identity == Black && count.secret_task == Sweeper) 1 else 0
v.copy(winCount = v.winCount + addWin,
gameCount = v.gameCount + 1, lastTime = now,
rbWinCount = v.rbWinCount + addRbWin,
blackWinCount = v.blackWinCount + addBlackWin,
killerWinCount = v.killerWinCount + addKillerWin,
stealerWinCount = v.stealerWinCount + addStealerWin,
collectorWinCount = v.collectorWinCount + addCollectorWin,
mutatorWinCount = v.mutatorWinCount + addMutatorWin,
pioneerWinCount = v.pioneerWinCount + addPioneerWin,
disturberWinCount = v.disturberWinCount + addDisturberWin,
sweeperWinCount = v.sweeperWinCount + addSweeperWin,
rbGameCount = v.rbGameCount + addRbGame,
blackGameCount = v.blackGameCount + addBlackGame,
killerGameCount = v.killerGameCount + addKillerGame,
stealerGameCount = v.stealerGameCount + addStealerGame,
collectorGameCount = v.collectorGameCount + addCollectorGame,
mutatorGameCount = v.mutatorGameCount + addMutatorGame,
pioneerGameCount = v.pioneerGameCount + addPioneerGame,
disturberGameCount = v.disturberGameCount + addDisturberGame,
sweeperGameCount = v.sweeperGameCount + addSweeperGame)
}
}
totalWinCount.addAndGet(win)
Expand All @@ -126,7 +182,8 @@ 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, 0, 0, 0, 0)) == null
val result = playerInfoMap.putIfAbsent(name, PlayerInfo(name, 0, "", 0, 0, 0, "", now, 10, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == null
if (result) pool.trySend(::savePlayerInfo)
return result
}
Expand Down Expand Up @@ -313,7 +370,21 @@ object Statistics {
sb.append(info.rbWinCount).append(',')
sb.append(info.rbGameCount).append(',')
sb.append(info.blackWinCount).append(',')
sb.append(info.blackGameCount).append('\n')
sb.append(info.blackGameCount).append(',')
sb.append(info.killerWinCount).append(',')
sb.append(info.killerGameCount).append(',')
sb.append(info.stealerWinCount).append(',')
sb.append(info.stealerGameCount).append(',')
sb.append(info.collectorWinCount).append(',')
sb.append(info.collectorGameCount).append(',')
sb.append(info.mutatorWinCount).append(',')
sb.append(info.mutatorGameCount).append(',')
sb.append(info.pioneerWinCount).append(',')
sb.append(info.pioneerGameCount).append(',')
sb.append(info.disturberWinCount).append(',')
sb.append(info.disturberGameCount).append(',')
sb.append(info.sweeperWinCount).append(',')
sb.append(info.sweeperGameCount).append('\n')
}
writeFile("playerInfo.csv", sb.toString().toByteArray())
sb.clear()
Expand Down Expand Up @@ -342,7 +413,7 @@ object Statistics {
var line: String
while (true) {
line = reader.readLine() ?: break
val a = line.split(",".toRegex(), limit = 14)
val a = line.split(",".toRegex(), limit = 28)
val pwd = a[4]
val score = if (a[3].length < 6) a[3].toInt() else 0 // 以前这个位置是deviceId
val name = a[2]
Expand All @@ -357,8 +428,26 @@ object Statistics {
val rbGameCount = a.getOrNull(11)?.toInt() ?: 0
val blackWinCount = a.getOrNull(12)?.toInt() ?: 0
val blackGameCount = a.getOrNull(13)?.toInt() ?: 0
val killerWinCount = a.getOrNull(14)?.toInt() ?: 0
val killerGameCount = a.getOrNull(15)?.toInt() ?: 0
val stealerWinCount = a.getOrNull(16)?.toInt() ?: 0
val stealerGameCount = a.getOrNull(17)?.toInt() ?: 0
val collectorWinCount = a.getOrNull(18)?.toInt() ?: 0
val collectorGameCount = a.getOrNull(19)?.toInt() ?: 0
val mutatorWinCount = a.getOrNull(20)?.toInt() ?: 0
val mutatorGameCount = a.getOrNull(21)?.toInt() ?: 0
val pioneerWinCount = a.getOrNull(22)?.toInt() ?: 0
val pioneerGameCount = a.getOrNull(23)?.toInt() ?: 0
val disturberWinCount = a.getOrNull(24)?.toInt() ?: 0
val disturberGameCount = a.getOrNull(25)?.toInt() ?: 0
val sweeperWinCount = a.getOrNull(26)?.toInt() ?: 0
val sweeperGameCount = a.getOrNull(27)?.toInt() ?: 0
val p = PlayerInfo(name, score, pwd, win, game, forbid, title, lt, energy, maxScore,
rbWinCount, rbGameCount, blackWinCount, blackGameCount)
rbWinCount, rbGameCount, blackWinCount, blackGameCount,
killerWinCount, killerGameCount, stealerWinCount, stealerGameCount,
collectorWinCount, collectorGameCount, mutatorWinCount, mutatorGameCount,
pioneerWinCount, pioneerGameCount, disturberWinCount, disturberGameCount,
sweeperWinCount, sweeperGameCount)
if (playerInfoMap.put(name, p) != null)
throw RuntimeException("数据错误,有重复的玩家name")
winCount += win
Expand Down Expand Up @@ -443,7 +532,7 @@ object Statistics {
val totalPlayerCount: Int
)

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

data class PlayerGameCount(val winCount: Int, val gameCount: Int) {
fun random(): PlayerGameCount {
Expand All @@ -470,7 +559,21 @@ object Statistics {
val rbWinCount: Int,
val rbGameCount: Int,
val blackWinCount: Int,
val blackGameCount: Int
val blackGameCount: Int,
val killerWinCount: Int,
val killerGameCount: Int,
val stealerWinCount: Int,
val stealerGameCount: Int,
val collectorWinCount: Int,
val collectorGameCount: Int,
val mutatorWinCount: Int,
val mutatorGameCount: Int,
val pioneerWinCount: Int,
val pioneerGameCount: Int,
val disturberWinCount: Int,
val disturberGameCount: Int,
val sweeperWinCount: Int,
val sweeperGameCount: Int
) : Comparable<PlayerInfo> {
val scoreWithDecay: Int
get() {
Expand Down
Loading