aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/UI
diff options
context:
space:
mode:
Diffstat (limited to 'src/scalevalapokalypsi/UI')
-rw-r--r--src/scalevalapokalypsi/UI/StdinLineReader.scala20
-rw-r--r--src/scalevalapokalypsi/UI/main.scala7
2 files changed, 16 insertions, 11 deletions
diff --git a/src/scalevalapokalypsi/UI/StdinLineReader.scala b/src/scalevalapokalypsi/UI/StdinLineReader.scala
index 4d0f778..1509f75 100644
--- a/src/scalevalapokalypsi/UI/StdinLineReader.scala
+++ b/src/scalevalapokalypsi/UI/StdinLineReader.scala
@@ -12,18 +12,22 @@ class StdinLineReader:
private var nextLine: Future[String] = Future.failed(Exception())
- /** Returns a new line of input if there are any. */
- def newLine(): Option[String] =
+ /** Returns a new line of input as a Right when there are any.
+ * If there is no new line due to EOF, returns Left(true),
+ * if there is no new line due to some other error, returns Left(false)
+ */
+ def newLine(): Either[Boolean, String] =
this.nextLine.value match
case Some(Success(s)) =>
- if s.contains("\u0000") then
- println("End of stream!")
- this.startReading()
- Some(s)
+ if s == null then
+ Left(true)
+ else
+ this.startReading()
+ Right(s)
case Some(Failure(e)) =>
this.startReading()
- None
- case None => None
+ Left(false)
+ case None => Left(false)
/** Discards the line that is currently being read and restarts reading */
def startReading(): Unit =
diff --git a/src/scalevalapokalypsi/UI/main.scala b/src/scalevalapokalypsi/UI/main.scala
index 7368803..e172f24 100644
--- a/src/scalevalapokalypsi/UI/main.scala
+++ b/src/scalevalapokalypsi/UI/main.scala
@@ -121,12 +121,13 @@ def startClient(client: Client): Unit =
while !hasQuit do
sleep(POLL_INTERVAL)
val line = stdinReader.newLine()
- if line.map(_.length).getOrElse(0) > 1024 then
+ if line.toOption.map(_.length).getOrElse(0) > 1024 then
Printer.printLn("Virhe: Syötteesi oli liian pitkä.")
- else if line == Some("quit") then
+ else if line == Right("quit") || line == Left(true) then
hasQuit = true
+ println("Poistut pelistä.")
else
- val gameEvent = client.clientStep(line)
+ val gameEvent = client.clientStep(line.toOption)
Printer.printGameEvent(gameEvent)
if !gameEvent.gameIsOn then
hasQuit = true