Skip to content

Commit

Permalink
feat(lobby): Add battle creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Assasans committed Mar 19, 2022
1 parent aa148af commit 2a3527e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 40 deletions.
10 changes: 6 additions & 4 deletions src/main/kotlin/jp/assasans/protanki/server/ResourceConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ data class ServerMapResources(
@Json val map: List<ServerResource>
)

enum class ServerMapTheme(val key: String, val visualKey: String) {
SummerDay("summer_day", "SUMMER"),
SummerNight("summer_night", "SUMMER_NIGHT"),
Winter("winter_day", "WINTER");
enum class ServerMapTheme(val key: String, val clientKey: String, val visualKey: String) {
SummerDay("summer_day", clientKey = "SUMMER", visualKey = "SUMMER"),
SummerNight("summer_night", clientKey = "SUMMER_NIGHT", visualKey = "SUMMER_NIGHT"),
Winter("winter_day", clientKey = "WINTER", visualKey = "WINTER");

companion object {
private val map = values().associateBy(ServerMapTheme::key)
private val mapByClient = values().associateBy(ServerMapTheme::clientKey)

fun get(key: String) = map[key]
fun getByClient(key: String) = mapByClient[key]
}
}

Expand Down
31 changes: 20 additions & 11 deletions src/main/kotlin/jp/assasans/protanki/server/battles/Battle.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package jp.assasans.protanki.server.battles

import kotlin.random.Random
import kotlin.random.nextULong
import mu.KotlinLogging
import jp.assasans.protanki.server.ServerMapInfo
import jp.assasans.protanki.server.client.*
Expand Down Expand Up @@ -83,29 +85,36 @@ class Battle(
var fund: Int = 1337228
) : ITickHandler {
companion object {
private var lastId: Int = 1

fun generateId(): String {
return "test-${lastId++}"
}
fun generateId(): String = Random.nextULong().toString(16)
}

private val logger = KotlinLogging.logger { }

val players: MutableList<BattlePlayer> = mutableListOf()

fun toBattleData(): BattleData {
// TODO(Assasans)
return BattleData(
battleId = id,
battleMode = "DM",
map = map.name,
name = title,
maxPeople = 8,
minRank = 0,
maxRank = 30,
preview = map.preview,
users = listOf()
)
}

suspend fun selectFor(socket: UserSocket) {
Command(
CommandName.SelectBattle,
listOf(
id
)
CommandName.ClientSelectBattle,
listOf(id)
).send(socket)
}

suspend fun showInfoFor(socket: UserSocket) {
Command(CommandName.ClientSelectBattle, listOf(id)).send(socket)

Command(
CommandName.ShowBattleInfo,
listOf(
Expand Down
36 changes: 21 additions & 15 deletions src/main/kotlin/jp/assasans/protanki/server/client/UserSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -431,21 +431,7 @@ class UserSocket(
CommandName.InitBattleSelect,
listOf(
InitBattleSelectData(
battles = listOf(
BattleData(
battleId = "493202bf695cc88a",
battleMode = "DM",
map = "map_kungur",
name = "ProTanki Server",
maxPeople = 8,
minRank = 0,
maxRank = 30,
preview = 476411,
users = listOf(
"Luminate"
)
)
)
battles = battleProcessor.battles.map { battle -> battle.toBattleData() }
).toJson()
)
).send(this)
Expand Down Expand Up @@ -917,3 +903,23 @@ data class ShowSettingsData(
@Json val snId: String = "vkontakte",
@Json val passwordCreated: Boolean = true
)

data class BattleCreateData(
@Json val withoutCrystals: Boolean,
@Json val minRank: Int,
@Json val reArmorEnabled: Boolean,
@Json val maxPeopleCount: Int,
@Json val autoBalance: Boolean,
@Json val maxRank: Int,
@Json val battleMode: String,
@Json val mapId: String,
@Json val name: String,
@Json val scoreLimit: Int,
@Json val friendlyFire: Boolean,
@Json val withoutBonuses: Boolean,
@Json val timeLimitInSec: Int,
@Json val proBattle: Boolean,
@Json val theme: String,
@Json val withoutSupplies: Boolean,
@Json val privateBattle: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ enum class CommandCategory(val key: String) {

Battle("battle"),
BattleChat("chat"),
BattleSelect("battle_select");
BattleSelect("battle_select"),
BattleCreate("battle_create");

companion object {
private val map = values().associateBy(CommandCategory::key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ enum class CommandName(val category: CommandCategory, val key: String, val side:
ShowDamageEnabled(CommandCategory.Lobby, "showDamageEnabled", CommandSide.Server),
UpdateRankProgress(CommandCategory.Lobby, "update_rang_progress", CommandSide.Client),

AddBattle(CommandCategory.Lobby, "add_battle", CommandSide.Client),
CreateBattle(CommandCategory.BattleCreate, "battle_create", CommandSide.Server),
CheckBattleName(CommandCategory.BattleCreate, "checkBattleNameForForbiddenWords", CommandSide.Server),
SetCreateBattleName(CommandCategory.Lobby, "setFilteredBattleName", CommandSide.Client),

SelectBattle(CommandCategory.BattleSelect, "select", CommandSide.Server),
ClientSelectBattle(CommandCategory.BattleSelect, "select", CommandSide.Client),
Fight(CommandCategory.BattleSelect, "fight", CommandSide.Server),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import kotlin.io.path.readText
import mu.KotlinLogging
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import jp.assasans.protanki.server.ClientResources
import jp.assasans.protanki.server.IResourceConverter
import jp.assasans.protanki.server.IResourceManager
import jp.assasans.protanki.server.*
import jp.assasans.protanki.server.battles.Battle
import jp.assasans.protanki.server.battles.BattlePlayer
import jp.assasans.protanki.server.battles.BattleTeam
import jp.assasans.protanki.server.battles.IBattleProcessor
import jp.assasans.protanki.server.client.Screen
import jp.assasans.protanki.server.client.UserSocket
import jp.assasans.protanki.server.client.send
import jp.assasans.protanki.server.client.toJson
import jp.assasans.protanki.server.battles.map.IMapRegistry
import jp.assasans.protanki.server.battles.map.get
import jp.assasans.protanki.server.client.*
import jp.assasans.protanki.server.commands.Command
import jp.assasans.protanki.server.commands.CommandHandler
import jp.assasans.protanki.server.commands.CommandName
Expand Down Expand Up @@ -41,6 +39,8 @@ class LobbyHandler : ICommandHandler, KoinComponent {
private val battleProcessor by inject<IBattleProcessor>()
private val resourceManager by inject<IResourceManager>()
private val resourceConverter by inject<IResourceConverter>()
private val mapRegistry by inject<IMapRegistry>()
private val server by inject<ISocketServer>()

@CommandHandler(CommandName.SelectBattle)
suspend fun selectBattle(socket: UserSocket, id: String) {
Expand All @@ -49,6 +49,7 @@ class LobbyHandler : ICommandHandler, KoinComponent {
logger.debug { "Select battle $id -> ${battle.title}" }

socket.selectedBattle = battle
battle.selectFor(socket)
battle.showInfoFor(socket)
}

Expand All @@ -59,16 +60,19 @@ class LobbyHandler : ICommandHandler, KoinComponent {

@CommandHandler(CommandName.Fight)
suspend fun fight(socket: UserSocket) {
if(socket.screen == Screen.Battle) return // Client-side bug

val battle = socket.selectedBattle ?: throw Exception("Battle is not selected")

socket.screen = Screen.Battle

val player = BattlePlayer(
socket = socket,
battle = battleProcessor.battles[0],
battle = battle,
team = BattleTeam.None
)
battle.players.add(player)

socket.screen = Screen.Battle
socket.initBattleLoad()

Command(CommandName.InitShotsData, listOf(resourceManager.get("shots-data.json").readText())).send(socket)
Expand Down Expand Up @@ -189,4 +193,33 @@ class LobbyHandler : ICommandHandler, KoinComponent {
).send(socket)
}
}

@CommandHandler(CommandName.CreateBattle)
suspend fun createBattle(socket: UserSocket, data: BattleCreateData) {
// TODO(Assasans): Advanced map configuration
val battle = Battle(
id = Battle.generateId(),
title = data.name,
map = mapRegistry.get(data.mapId, ServerMapTheme.getByClient(data.theme) ?: throw Exception("Unknown theme: ${data.theme}"))
)

battleProcessor.battles.add(battle)

Command(CommandName.AddBattle, listOf(battle.toBattleData().toJson())).let { command ->
server.players
.filter { player -> player.screen == Screen.BattleSelect }
.forEach { player -> command.send(player) }
}


socket.selectedBattle = battle
battle.selectFor(socket)
battle.showInfoFor(socket)
}

@CommandHandler(CommandName.CheckBattleName)
suspend fun checkBattleName(socket: UserSocket, name: String) {
// Pass-through
Command(CommandName.SetCreateBattleName, listOf(name)).send(socket)
}
}

0 comments on commit 2a3527e

Please sign in to comment.