From 607b43a84d3bc8edffa05c722c7b8c3e6f72e964 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Thu, 21 Nov 2024 21:09:52 +0200 Subject: Wooooohooo time limits and correcter printing --- src/scalevalapokalypsi/main.scala | 59 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) (limited to 'src/scalevalapokalypsi/main.scala') diff --git a/src/scalevalapokalypsi/main.scala b/src/scalevalapokalypsi/main.scala index 50e89e5..f953751 100644 --- a/src/scalevalapokalypsi/main.scala +++ b/src/scalevalapokalypsi/main.scala @@ -1,7 +1,10 @@ package scalevalapokalypsi -import scalevalapokalypsi.Client.newClient +import scalevalapokalypsi.Client.{newClient, Client, StdinLineReader, GameEvent} import scalevalapokalypsi.Server.Server +import scalevalapokalypsi.constants.* +import java.lang.Thread.sleep +import scala.util.Try import java.lang.Thread import scala.io.StdIn.readLine @@ -15,11 +18,61 @@ import scala.io.StdIn.readLine println("Server started in background.") print("Choose a name:\n> ") val name = readLine() - newClient(name, "127.0.0.1", 2267).foreach(_.startClient()) + Try(newClient(name, "127.0.0.1", 2267)) + .toOption + .flatten match + case Some(client) => + startClient(client) + case None => + println("Starting the client failed.") + case Some(2) => print("Choose a name:\n> ") val name = readLine() - newClient(name, "127.0.0.1", 2267).foreach(_.startClient()) + Try(newClient(name, "127.0.0.1", 2267)) + .toOption + .flatten match + case Some(client) => + startClient(client) + case None => + println("Starting the client failed.") case _ => println("Invalid input") +def startClient(client: Client): Unit = + var hasQuit = false + val stdinReader = StdinLineReader() + stdinReader.startReading() + val printer = Printer() + while !hasQuit do + sleep(POLL_INTERVAL) + val gameEvent = client.clientStep(stdinReader.newLine()) + printer.printGameEvent(gameEvent) + + +class Printer: + var inputIndicatorAtStartOfLine = false + def printGameEvent(gameEvent: GameEvent): Unit = + val actions = gameEvent.actions.map(_.mkString("\n")) + val roomState = gameEvent.roomState.map(_.toString) + val lineToSing = gameEvent.lineToSing + if + inputIndicatorAtStartOfLine && + (actions.isDefined || + roomState.isDefined || + lineToSing.isDefined) + then + this.printLn("") + actions.foreach(this.printLn(_)) + roomState.foreach(this.printLn(_)) + lineToSing.foreach(this.printLn(_)) + val timeLeft = s"${gameEvent.timeToNextTurn.getOrElse("∞")}" + if gameEvent.playerCanAct && !inputIndicatorAtStartOfLine then + this.inputIndicatorAtStartOfLine = true + print(s"[$timeLeft s]> ") + if gameEvent.playerCanAct then + print(s"\u001b[s\u001b[0E[$timeLeft s]\u001b[u") + + private def printLn(s: String): Unit = + println(s) + this.inputIndicatorAtStartOfLine = false -- cgit v1.2.3 From 49985d1d11c426968fc298469671326aace96d00 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Thu, 21 Nov 2024 23:19:00 +0200 Subject: Fixed singing from last commit --- src/scalevalapokalypsi/main.scala | 51 +++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 7 deletions(-) (limited to 'src/scalevalapokalypsi/main.scala') diff --git a/src/scalevalapokalypsi/main.scala b/src/scalevalapokalypsi/main.scala index f953751..e357845 100644 --- a/src/scalevalapokalypsi/main.scala +++ b/src/scalevalapokalypsi/main.scala @@ -4,6 +4,7 @@ import scalevalapokalypsi.Client.{newClient, Client, StdinLineReader, GameEvent} import scalevalapokalypsi.Server.Server import scalevalapokalypsi.constants.* import java.lang.Thread.sleep +import java.lang.System.currentTimeMillis import scala.util.Try import java.lang.Thread @@ -45,16 +46,27 @@ def startClient(client: Client): Unit = val printer = Printer() while !hasQuit do sleep(POLL_INTERVAL) - val gameEvent = client.clientStep(stdinReader.newLine()) - printer.printGameEvent(gameEvent) + val line = stdinReader.newLine() + if line.map(_.length).getOrElse(0) > 1024 then + printer.printLn("Virhe: Syötteesi oli liian pitkä.") + else if line == Some("quit") then + hasQuit = true + else + val gameEvent = client.clientStep(line) + printer.printGameEvent(gameEvent) class Printer: var inputIndicatorAtStartOfLine = false + var queriedLineToSing = false + var singStartTime: Option[Long] = None + def printGameEvent(gameEvent: GameEvent): Unit = + val actions = gameEvent.actions.map(_.mkString("\n")) val roomState = gameEvent.roomState.map(_.toString) val lineToSing = gameEvent.lineToSing + if inputIndicatorAtStartOfLine && (actions.isDefined || @@ -62,17 +74,42 @@ class Printer: lineToSing.isDefined) then this.printLn("") + actions.foreach(this.printLn(_)) + roomState.foreach(this.printLn(_)) - lineToSing.foreach(this.printLn(_)) + + lineToSing match + case Some(l) => + if this.singStartTime.isEmpty then + this.singStartTime = Some(currentTimeMillis() / 1000) + print(s"Laula: “$l”\n ") + val timeSpent = this.singStartTime.map((t: Long) => + (currentTimeMillis / 1000 - t).toString + ).getOrElse("?") + print(this.timeIndicatorUpdater(timeSpent)) + case None => + this.singStartTime = None + val timeLeft = s"${gameEvent.timeToNextTurn.getOrElse("∞")}" - if gameEvent.playerCanAct && !inputIndicatorAtStartOfLine then + + if + gameEvent.playerCanAct && + lineToSing.isEmpty && + !inputIndicatorAtStartOfLine + then this.inputIndicatorAtStartOfLine = true print(s"[$timeLeft s]> ") - if gameEvent.playerCanAct then - print(s"\u001b[s\u001b[0E[$timeLeft s]\u001b[u") - private def printLn(s: String): Unit = + if gameEvent.playerCanAct && lineToSing.isEmpty then + print(this.timeIndicatorUpdater(timeLeft)) + + end printGameEvent + + def printLn(s: String): Unit = println(s) this.inputIndicatorAtStartOfLine = false + + private def timeIndicatorUpdater(t: String): String = + s"\u001b[s\u001b[0E[$t s]> \u001b[u" -- cgit v1.2.3 From db5612ed9734d51e6fcd0d7b5a7635e49b773581 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Fri, 22 Nov 2024 22:42:22 +0200 Subject: Character safety checking, supported terminals updated --- src/scalevalapokalypsi/main.scala | 115 -------------------------------------- 1 file changed, 115 deletions(-) delete mode 100644 src/scalevalapokalypsi/main.scala (limited to 'src/scalevalapokalypsi/main.scala') diff --git a/src/scalevalapokalypsi/main.scala b/src/scalevalapokalypsi/main.scala deleted file mode 100644 index e357845..0000000 --- a/src/scalevalapokalypsi/main.scala +++ /dev/null @@ -1,115 +0,0 @@ -package scalevalapokalypsi - -import scalevalapokalypsi.Client.{newClient, Client, StdinLineReader, GameEvent} -import scalevalapokalypsi.Server.Server -import scalevalapokalypsi.constants.* -import java.lang.Thread.sleep -import java.lang.System.currentTimeMillis -import scala.util.Try - -import java.lang.Thread -import scala.io.StdIn.readLine - -// TODO: add proper logic for starting the game -@main def main(): Unit = - print("How do you want to play?\n1) Host and join local game\n2) Join local game\n> ") - readLine().toIntOption match - case Some(1) => - Thread(() => new Server(2267, 5, 30, true).startServer()).start() - println("Server started in background.") - print("Choose a name:\n> ") - val name = readLine() - Try(newClient(name, "127.0.0.1", 2267)) - .toOption - .flatten match - case Some(client) => - startClient(client) - case None => - println("Starting the client failed.") - - case Some(2) => - print("Choose a name:\n> ") - val name = readLine() - Try(newClient(name, "127.0.0.1", 2267)) - .toOption - .flatten match - case Some(client) => - startClient(client) - case None => - println("Starting the client failed.") - case _ => println("Invalid input") - -def startClient(client: Client): Unit = - var hasQuit = false - val stdinReader = StdinLineReader() - stdinReader.startReading() - val printer = Printer() - while !hasQuit do - sleep(POLL_INTERVAL) - val line = stdinReader.newLine() - if line.map(_.length).getOrElse(0) > 1024 then - printer.printLn("Virhe: Syötteesi oli liian pitkä.") - else if line == Some("quit") then - hasQuit = true - else - val gameEvent = client.clientStep(line) - printer.printGameEvent(gameEvent) - - -class Printer: - var inputIndicatorAtStartOfLine = false - var queriedLineToSing = false - var singStartTime: Option[Long] = None - - def printGameEvent(gameEvent: GameEvent): Unit = - - val actions = gameEvent.actions.map(_.mkString("\n")) - val roomState = gameEvent.roomState.map(_.toString) - val lineToSing = gameEvent.lineToSing - - if - inputIndicatorAtStartOfLine && - (actions.isDefined || - roomState.isDefined || - lineToSing.isDefined) - then - this.printLn("") - - actions.foreach(this.printLn(_)) - - roomState.foreach(this.printLn(_)) - - lineToSing match - case Some(l) => - if this.singStartTime.isEmpty then - this.singStartTime = Some(currentTimeMillis() / 1000) - print(s"Laula: “$l”\n ") - val timeSpent = this.singStartTime.map((t: Long) => - (currentTimeMillis / 1000 - t).toString - ).getOrElse("?") - print(this.timeIndicatorUpdater(timeSpent)) - case None => - this.singStartTime = None - - val timeLeft = s"${gameEvent.timeToNextTurn.getOrElse("∞")}" - - if - gameEvent.playerCanAct && - lineToSing.isEmpty && - !inputIndicatorAtStartOfLine - then - this.inputIndicatorAtStartOfLine = true - print(s"[$timeLeft s]> ") - - if gameEvent.playerCanAct && lineToSing.isEmpty then - print(this.timeIndicatorUpdater(timeLeft)) - - end printGameEvent - - def printLn(s: String): Unit = - println(s) - this.inputIndicatorAtStartOfLine = false - - private def timeIndicatorUpdater(t: String): String = - s"\u001b[s\u001b[0E[$t s]> \u001b[u" - -- cgit v1.2.3