Skip to content

Commit

Permalink
Merge branch 'SA08_MicroService'
Browse files Browse the repository at this point in the history
  • Loading branch information
SailReal committed Nov 3, 2018
2 parents 0abc13a + d623c89 commit 8f8f6e6
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 50 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ target/

# Maven
log/
target/

# Gradle
.gradle/
Expand All @@ -24,15 +23,12 @@ pom.xml

# sbt
.cache
.history
.lib/
dist/*
target/
lib_managed/
src_managed/
project/boot/
project/plugins/project/
project/target/
project/project/
logs/
project/.sbtserver
Expand All @@ -47,3 +43,6 @@ project/.sbtserver

# Application log
sudoku.log

# Application backup
grid.json
11 changes: 8 additions & 3 deletions src/main/scala/de/htwg/se/sudoku/Sudoku.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import com.google.inject.{Guice, Injector}
import de.htwg.se.sudoku.aview.gui.SwingGui
import de.htwg.se.sudoku.aview.{HttpServer, Tui}
import de.htwg.se.sudoku.controller.controllerComponent.ControllerInterface
import de.htwg.se.sudoku.model.fileIoComponent.FileIOInterface
import de.htwg.se.sudoku.model.fileIoComponent.fileIoMicroImpl.FileIoHttpServer

import scala.io.StdIn.readLine

object Sudoku {
val injector: Injector = Guice.createInjector(new SudokuModule)
val injector: Injector = Guice.createInjector(new MicroSudokuModule)
val controller: ControllerInterface = injector.getInstance(classOf[ControllerInterface])
val tui = new Tui(controller)

if (!GraphicsEnvironment.isHeadless) {
val gui = new SwingGui(controller)
}


val fileIoHttpServer: FileIoHttpServer = injector.getInstance(classOf[FileIoHttpServer])
val webserver = new HttpServer(controller)

controller.createNewGrid
Expand All @@ -32,5 +35,7 @@ object Sudoku {
}
} while (input != "q")
webserver.unbind()
fileIoHttpServer.unbind()
controller.finish()
}
}
27 changes: 25 additions & 2 deletions src/main/scala/de/htwg/se/sudoku/SudokuModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package de.htwg.se.sudoku

import com.google.inject.AbstractModule
import com.google.inject.name.Names
import net.codingwell.scalaguice.ScalaModule
import de.htwg.se.sudoku.controller.controllerComponent._
import de.htwg.se.sudoku.model.fileIoComponent._
import de.htwg.se.sudoku.model.gridComponent.GridInterface
import de.htwg.se.sudoku.model.gridComponent.gridAdvancedImpl.Grid
import net.codingwell.scalaguice.ScalaModule

class SudokuModule extends AbstractModule with ScalaModule {

val defaultSize: Int = 9

def configure() = {
def configure(): Unit = {
bindConstant().annotatedWith(Names.named("DefaultSize")).to(defaultSize)
bind[GridInterface].to[Grid]
bind[ControllerInterface].to[controllerBaseImpl.Controller]
Expand All @@ -26,3 +26,26 @@ class SudokuModule extends AbstractModule with ScalaModule {
}

}

class MicroSudokuModule extends AbstractModule with ScalaModule {

val defaultSize: Int = 9
val defaultHostname: String = "localhost"
val defaultFilePort: Int = 8089

def configure(): Unit = {
bindConstant().annotatedWith(Names.named("DefaultSize")).to(defaultSize)
bind[GridInterface].to[Grid]
bind[ControllerInterface].to[controllerBaseImpl.Controller]

bind[GridInterface].annotatedWithName("tiny").toInstance(new Grid(1))
bind[GridInterface].annotatedWithName("small").toInstance(new Grid(4))
bind[GridInterface].annotatedWithName("normal").toInstance(new Grid(9))

bindConstant().annotatedWith(Names.named("FileHost")).to(defaultHostname)
bindConstant().annotatedWith(Names.named("FilePort")).to(defaultFilePort)

bind[FileIOInterface].to[fileIoMicroImpl.FileIO]
}

}
2 changes: 1 addition & 1 deletion src/main/scala/de/htwg/se/sudoku/aview/HttpServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scala.concurrent.{ExecutionContextExecutor, Future}

class HttpServer(controller: ControllerInterface) {

implicit val system: ActorSystem = ActorSystem("system")
implicit val system: ActorSystem = ActorSystem("RestHttpServerSystem")
implicit val materializer: ActorMaterializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext: ExecutionContextExecutor = system.dispatcher
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package de.htwg.se.sudoku.controller.controllerComponent

import de.htwg.se.sudoku.controller.controllerComponent.GameStatus.GameStatus
import de.htwg.se.sudoku.model.gridComponent.CellInterface
import de.htwg.se.sudoku.model.gridComponent.{CellInterface, GridInterface}
import play.api.libs.json.JsValue

import scala.swing.Publisher

trait ControllerInterface extends Publisher {

def gridSize: Int

def blockSize: Int
Expand Down Expand Up @@ -56,8 +55,9 @@ trait ControllerInterface extends Publisher {

def statusText: String

def toJson: JsValue
def gridToJson: JsValue

def finish(): Unit
}

trait ControllerIoInterface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package de.htwg.se.sudoku.controller.controllerComponent.controllerBaseImpl

import com.google.inject.name.Names
import com.google.inject.{Guice, Inject}
import net.codingwell.scalaguice.InjectorExtensions._
import de.htwg.se.sudoku.SudokuModule
import com.google.inject.{Guice, Inject, Injector}
import com.typesafe.scalalogging.LazyLogging
import de.htwg.se.sudoku.MicroSudokuModule
import de.htwg.se.sudoku.controller.controllerComponent.GameStatus._
import de.htwg.se.sudoku.controller.controllerComponent._
import de.htwg.se.sudoku.model.fileIoComponent.FileIOInterface
import de.htwg.se.sudoku.model.gridComponent.GridInterface
import de.htwg.se.sudoku.model.gridComponent.{CellInterface, GridInterface}
import de.htwg.se.sudoku.util.UndoManager
import net.codingwell.scalaguice.InjectorExtensions._
import play.api.libs.json.JsValue

import scala.util.{Success, Failure}
import com.typesafe.scalalogging.{LazyLogging, Logger}
import scala.util.{Failure, Success}

class Controller @Inject()(var grid: GridInterface)
extends ControllerInterface
extends ControllerInterface
with ControllerIoInterface
with LazyLogging {

var gameStatus: GameStatus = IDLE
var showAllCandidates: Boolean = false
private val undoManager = new UndoManager
val injector = Guice.createInjector(new SudokuModule)
val fileIo = injector.instance[FileIOInterface]
val injector: Injector = Guice.createInjector(new MicroSudokuModule)
val fileIo: FileIOInterface = injector.instance[FileIOInterface]

def createEmptyGrid: Unit = {
grid.size match {
Expand Down Expand Up @@ -77,7 +78,7 @@ class Controller @Inject()(var grid: GridInterface)
publish(new CellChanged)
}

def toJson = grid.toJson
def gridToJson: JsValue = grid.toJson

def load: Unit = {
val gridOptionResult = fileIo.load
Expand Down Expand Up @@ -114,29 +115,40 @@ class Controller @Inject()(var grid: GridInterface)
publish(new CellChanged)
}

def cell(row:Int, col:Int) = grid.cell(row,col)
def cell(row:Int, col:Int): CellInterface = grid.cell(row,col)

def isGiven(row: Int, col: Int): Boolean = grid.cell(row, col).given

def isSet(row: Int, col: Int): Boolean = grid.cell(row, col).isSet

def isGiven(row: Int, col: Int):Boolean = grid.cell(row, col).given
def isSet(row:Int, col:Int):Boolean = grid.cell(row, col).isSet
def available(row:Int, col:Int):Set[Int] = grid.available(row, col)
def showCandidates(row:Int, col:Int):Unit = {
grid=grid.setShowCandidates(row, col)
def available(row: Int, col: Int): Set[Int] = grid.available(row, col)

def showCandidates(row: Int, col: Int): Unit = {
grid = grid.setShowCandidates(row, col)
gameStatus = CANDIDATES
publish(new CandidatesChanged)
}

def isShowCandidates(row:Int, col:Int):Boolean = grid.cell(row, col).showCandidates
def gridSize:Int = grid.size
def blockSize:Int = Math.sqrt(grid.size).toInt
def isShowAllCandidates:Boolean = showAllCandidates
def toggleShowAllCandidates:Unit = {
def isShowCandidates(row: Int, col: Int): Boolean =
grid.cell(row, col).showCandidates

def gridSize: Int = grid.size

def blockSize: Int = Math.sqrt(grid.size).toInt

def isShowAllCandidates: Boolean = showAllCandidates

def toggleShowAllCandidates: Unit = {
showAllCandidates = !showAllCandidates
gameStatus = CANDIDATES
publish(new CellChanged)
}
def isHighlighted(row:Int, col: Int):Boolean = grid.isHighlighted(row, col)
def statusText:String = GameStatus.message(gameStatus)
def highlight(index:Int):Unit = {

def isHighlighted(row: Int, col: Int): Boolean = grid.isHighlighted(row, col)

def statusText: String = GameStatus.message(gameStatus)

def highlight(index: Int): Unit = {
grid = grid.highlight(index)
publish(new CellChanged)
}
Expand All @@ -148,4 +160,6 @@ class Controller @Inject()(var grid: GridInterface)
override def setShowCandidates(row: Int, col: Int): Unit = {
grid = grid.setShowCandidates(row, col)
}

override def finish(): Unit = fileIo.unbind()
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Controller(var grid: GridInterface) extends ControllerInterface {

override def load: Unit = {}

override def toJson: JsValue = grid.toJson()
override def gridToJson: JsValue = grid.toJson

override def finish(): Unit = {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ trait FileIOInterface {

def load: Try[Option[GridInterface]]
def save(grid: GridInterface): Try[Unit]
def unbind()

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package de.htwg.se.sudoku.model.fileIoComponent.fileIoJsonImpl

import com.google.inject.Guice
import com.google.inject.name.Names
import net.codingwell.scalaguice.InjectorExtensions._
import de.htwg.se.sudoku.SudokuModule
import de.htwg.se.sudoku.model.fileIoComponent.FileIOInterface
import de.htwg.se.sudoku.model.gridComponent.{CellInterface, GridInterface}
import de.htwg.se.sudoku.model.gridComponent.GridInterface
import net.codingwell.scalaguice.InjectorExtensions._
import play.api.libs.json._

import scala.util.{Try, Success, Failure}
import scala.io.Source
import scala.util.Try

class FileIO extends FileIOInterface {

Expand All @@ -23,7 +23,8 @@ class FileIO extends FileIOInterface {

val json: JsValue = Json.parse(source)
val size = (json \ "grid" \ "size").get.toString.toInt
val injector = Guice.createInjector(new SudokuModule)
val injector:ScalaInjector = Guice.createInjector(new SudokuModule)

size match {
case 1 =>
gridOption =
Expand All @@ -40,9 +41,9 @@ class FileIO extends FileIOInterface {
case Some(grid) => {
var _grid = grid
for (index <- 0 until size * size) {
val row = (json \\ "row")(index).as[Int]
val col = (json \\ "col")(index).as[Int]
val cell = (json \\ "cell")(index)
val row = (json \\ "row") (index).as[Int]
val col = (json \\ "col") (index).as[Int]
val cell = (json \\ "cell") (index)
val value = (cell \ "value").as[Int]
_grid = _grid.set(row, col, value)
val given = (cell \ "given").as[Boolean]
Expand Down Expand Up @@ -70,7 +71,5 @@ class FileIO extends FileIOInterface {

def gridToJson(grid: GridInterface) = grid.toJson




override def unbind(): Unit = {}
}
Loading

0 comments on commit 8f8f6e6

Please sign in to comment.