Skip to content
Snippets Groups Projects
Commit dc9e147d authored by Hoai Viet Nguyen's avatar Hoai Viet Nguyen
Browse files

initial commit

parent d30f4c34
No related branches found
No related tags found
No related merge requests found
Showing
with 167 additions and 38 deletions
...@@ -24,10 +24,13 @@ dependencies { ...@@ -24,10 +24,13 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-websocket' implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin' implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect' implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5' testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
runtimeOnly 'com.h2database:h2'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher' testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.json:org.json:chargebee-1.0'
} }
kotlin { kotlin {
......
package de.thk.gm.remotecontrol.configs package de.thk.gm.remotecontrol.configs
import de.thk.gm.remotecontrol.handlers.ColorGameHandler import de.thk.gm.remotecontrol.handlers.GameHandler
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.web.socket.config.annotation.EnableWebSocket import org.springframework.web.socket.config.annotation.EnableWebSocket
import org.springframework.web.socket.config.annotation.WebSocketConfigurer import org.springframework.web.socket.config.annotation.WebSocketConfigurer
...@@ -10,6 +10,6 @@ import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry ...@@ -10,6 +10,6 @@ import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry
@EnableWebSocket @EnableWebSocket
class WebSocketsConfig : WebSocketConfigurer { class WebSocketsConfig : WebSocketConfigurer {
override fun registerWebSocketHandlers(registry: WebSocketHandlerRegistry) { override fun registerWebSocketHandlers(registry: WebSocketHandlerRegistry) {
registry.addHandler(ColorGameHandler(), "/ws/s") registry.addHandler(GameHandler(), "/game")
} }
} }
\ No newline at end of file
package de.thk.gm.remotecontrol.controllers
import de.thk.gm.remotecontrol.models.Game
import de.thk.gm.remotecontrol.services.GamesService
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import java.util.*
@Controller
class GameController (private val gamesService: GamesService) {
@GetMapping("/")
fun index(model: Model): String {
model.addAttribute("games",gamesService.getAllGames())
return "index"
}
@GetMapping("/games/{id}")
fun game(model: Model, @PathVariable id: UUID): String {
model.addAttribute("id", id)
return "game"
}
@PostMapping("/games")
fun createGame(): String {
var game = Game()
gamesService.saveGame(game)
return "redirect:/"
}
@GetMapping("/controller/{id}")
fun controller(model: Model, @PathVariable id: String): String {
model.addAttribute("id", id)
return "controller"
}
}
\ No newline at end of file
package de.thk.gm.remotecontrol.controllers
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
@Controller
class WelcomeController {
@GetMapping("/")
fun index(model: Model): String {
return "index"
}
@GetMapping("/colorgame")
fun colorgame(model: Model): String {
return "colorgame"
}
}
\ No newline at end of file
package de.thk.gm.remotecontrol.handlers
import org.springframework.web.socket.CloseStatus
import org.springframework.web.socket.TextMessage
import org.springframework.web.socket.WebSocketSession
import org.springframework.web.socket.handler.TextWebSocketHandler
class ColorGameHandler : TextWebSocketHandler() {
override fun afterConnectionEstablished(session: WebSocketSession) {
session.sendMessage(TextMessage(session.id))
}
}
\ No newline at end of file
package de.thk.gm.remotecontrol.handlers
import org.json.JSONObject
import org.springframework.web.socket.CloseStatus
import org.springframework.web.socket.TextMessage
import org.springframework.web.socket.WebSocketSession
import org.springframework.web.socket.handler.TextWebSocketHandler
import org.springframework.web.util.UriComponents
import org.springframework.web.util.UriComponentsBuilder
class GameHandler : TextWebSocketHandler() {
private val hashMapOfSessions : HashMap<String, ArrayList<WebSocketSession>> = HashMap()
override fun afterConnectionEstablished(session: WebSocketSession) {
var uri : UriComponents = UriComponentsBuilder.fromUri(session.uri!!).build()
var side = uri.queryParams.getFirst("side")
if(side != null && side == "game") {
var id = uri.queryParams.getFirst("id")
if(id != null) {
var sessions = hashMapOfSessions[id]
if(sessions == null) {
sessions = ArrayList()
}
sessions.add(session)
hashMapOfSessions[id] = sessions
}
}
}
override fun handleTextMessage(session: WebSocketSession, message: TextMessage) {
var uri : UriComponents = UriComponentsBuilder.fromUri(session.uri!!).build()
var side = uri.queryParams.getFirst("side")
if(side != null && side == "controller") {
var id = uri.queryParams.getFirst("id")
var sessions = hashMapOfSessions[id]
if(sessions != null) {
for (gameSession in sessions) {
gameSession.sendMessage(message)
}
}
}
}
override fun afterConnectionClosed(session: WebSocketSession, status: CloseStatus) {
var uri : UriComponents = UriComponentsBuilder.fromUri(session.uri!!).build()
var side = uri.queryParams.getFirst("side")
if(side != null && side == "game") {
var id = uri.queryParams.getFirst("id")
var sessions = hashMapOfSessions[id]
sessions?.remove(session)
}
}
}
\ No newline at end of file
package de.thk.gm.remotecontrol.models
import jakarta.persistence.Entity
import jakarta.persistence.Id
import java.util.*
@Entity
class Game {
@Id
var id: UUID = UUID.randomUUID()
}
\ No newline at end of file
package de.thk.gm.remotecontrol.repositories
import de.thk.gm.remotecontrol.models.Game
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
import java.util.*
@Repository
interface GamesRepository : CrudRepository<Game, UUID>
\ No newline at end of file
package de.thk.gm.remotecontrol.services
import de.thk.gm.remotecontrol.models.Game
interface GamesService {
fun getAllGames() : List<Game>
fun saveGame(game: Game)
}
\ No newline at end of file
package de.thk.gm.remotecontrol.services
import de.thk.gm.remotecontrol.models.Game
import de.thk.gm.remotecontrol.repositories.GamesRepository
import org.springframework.stereotype.Service
@Service
class GamesServiceImpl (private val gamesRepository: GamesRepository) : GamesService {
override fun getAllGames(): List<Game> {
return gamesRepository.findAll().toList()
}
override fun saveGame(game: Game) {
gamesRepository.save(game)
}
}
\ No newline at end of file
...@@ -8,9 +8,15 @@ ...@@ -8,9 +8,15 @@
<title>Controller</title> <title>Controller</title>
</head> </head>
<body> <body>
<button onclick="sendBackgroundColor('red')" type="button">Red</button>
<button onclick="sendBackgroundColor('blue')" type="button">Blue</button>
<button onclick="sendBackgroundColor('yellow')" type="button">Yellow</button>
<button onclick="sendBackgroundColor('green')" type="button">Green</button>
</body> </body>
<script> <script>
function sendBackgroundColor(color){
ws.send(color)
}
var ws = new WebSocket("/game?id=${id}&side=controller")
</script> </script>
</html> </html>
\ No newline at end of file
...@@ -8,13 +8,18 @@ ...@@ -8,13 +8,18 @@
<title>Color Game</title> <title>Color Game</title>
</head> </head>
<body> <body>
<a href="/controller/${id}" id="linkToController" target="_blank">Link to Controller: <b>/controller/${id}</b></a><br>
<a href="/">Back</a>
</body> </body>
<script> <script>
function setBackgroundColor(color){ function setBackgroundColor(color){
document.getElementsByTagName("body")[0].setAttribute("style","background-color:" + color) document.body.style.background = color;
}
var ws = new WebSocket("/game?id=${id}&side=game")
ws.onmessage = function(msg){
setBackgroundColor(msg.data)
} }
var ws = new WebSocket("/game")
</script> </script>
</html> </html>
\ No newline at end of file
...@@ -8,6 +8,14 @@ ...@@ -8,6 +8,14 @@
<title>Document</title> <title>Document</title>
</head> </head>
<body> <body>
<a href="/colorgame">Start game</a> <ul>
<#list games as game>
<li><a href="/games/${game.id}">${game.id}</a></li>
</#list>
</ul>
<form action="/games" method="post">
<button>Create game</button>
</form>
</body> </body>
</html> </html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment