Skip to content

Commit

Permalink
punch more aggressively
Browse files Browse the repository at this point in the history
  • Loading branch information
pschichtel committed Oct 5, 2024
1 parent 88ddd2f commit 47a9406
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/service/Ld56MasterServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import java.nio.channels.DatagramChannel
import java.nio.charset.StandardCharsets
import java.time.{Duration, Instant}
import java.util.concurrent.ConcurrentHashMap
import scala.jdk.CollectionConverters.{CollectionHasAsScala, EnumerationHasAsScala}
import scala.jdk.CollectionConverters.CollectionHasAsScala
import scala.math.Ordering.Implicits.infixOrderingOps
import scala.util.Random

sealed trait ResponseMessage
final case class HostResponseMessage(host: String, port: Int) extends ResponseMessage
final case class JoinResponseMessage(host: String, port: Int, playerCount: Int) extends ResponseMessage
final case class PunchRequestMessage(host: String, port: Int) extends ResponseMessage

object ResponseMessage {
implicit val hostResponseFormat: Format[HostResponseMessage] = Json.format
implicit val joinResponseFormat: Format[JoinResponseMessage] = Json.format
implicit val punchRequestFormat: Format[PunchRequestMessage] = Json.format
}

sealed trait Message
Expand Down Expand Up @@ -48,7 +50,13 @@ object Message {
}
}

final case class HostingHost(host: String, port: Int, playerCount: Int, lastHosted: Instant)
final case class Host(addr: String, port: Int)

object Host {
implicit val format: Format[Host] = Json.format
}

final case class HostingHost(gameHost: Host, c2Host: Host, playerCount: Int, lastHosted: Instant)

class Ld56MasterServer {
private val logger = Logger(classOf[Ld56MasterServer])
Expand Down Expand Up @@ -92,7 +100,7 @@ class Ld56MasterServer {
sendMessage(HostResponseMessage(sourceAddr.getAddress.getHostAddress, sourceAddr.getPort), sourceAddr)
case HostingMessage(host, port, playerCount) =>
val hostPort = s"$host:$port"
hostMap.put(hostPort, HostingHost(host, port, playerCount, now))
hostMap.put(hostPort, HostingHost(Host(host, port), Host(sourceAddr.getAddress.getHostAddress, sourceAddr.getPort), playerCount, now))
case JoinMessage() =>
hostMap.entrySet().removeIf(entry => {
entry.getValue.lastHosted < now.minus(Duration.ofSeconds(20))
Expand All @@ -101,6 +109,7 @@ class Ld56MasterServer {
if hosts.nonEmpty then
val randomHost = hosts(Random.nextInt(hosts.length))

sendMessage(JoinResponseMessage(randomHost.host, randomHost.port, randomHost.playerCount), sourceAddr)
sendMessage(PunchRequestMessage(sourceAddr.getAddress.getHostAddress, sourceAddr.getPort), InetSocketAddress.createUnresolved(randomHost.c2Host.addr, randomHost.c2Host.port))
sendMessage(JoinResponseMessage(randomHost.gameHost.addr, randomHost.gameHost.port, randomHost.playerCount), sourceAddr)
}
}

0 comments on commit 47a9406

Please sign in to comment.