Skip to content

Commit

Permalink
Adding randomizer machine
Browse files Browse the repository at this point in the history
  • Loading branch information
orchestr7 committed Sep 9, 2024
1 parent 6e09919 commit 9dbfae4
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.posidata.backend.controller

import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import ru.posidata.backend.service.UserService

@RestController
@RequestMapping("api")
class RandomController(
private val userService: UserService
) {
@GetMapping("/random")
fun random(): ResponseEntity<Any> {
val randomUser = userService.getRandomUser()
return ResponseEntity.ok(randomUser.toDTO())
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ru.posidata.backend.repository

import org.springframework.data.repository.CrudRepository
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import ru.posidata.backend.entity.OurUserEntityFromDb

@Repository
interface UserRepository : CrudRepository<OurUserEntityFromDb, Long> {
interface UserRepository : JpaRepository<OurUserEntityFromDb, Long> {
fun findByUsername(username: String): OurUserEntityFromDb?
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class UserService(private val userRepository: UserRepository) {
val value = userRepository.findByUsername(username)
if (value != null) {
value.updateResultIn(value.currentGameNumber(), isNextRound)
return userRepository.save(value)
return userRepository.saveAndFlush(value)
} else {
println("Not able to find $username in DB")
return null
Expand All @@ -35,4 +35,10 @@ class UserService(private val userRepository: UserRepository) {
return null
}
}

fun getRandomUser(): OurUserEntityFromDb {
val userCount = userRepository.count().toInt()
val users = userRepository.findAll().random()
return users
}
}
1 change: 1 addition & 0 deletions frontend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ kotlin {
implementation(npm("react-dom", "^18.0.0"))
implementation(npm("react-modal", "^3.0.0"))
implementation(npm("@popperjs/core", "2.11.8"))
implementation(npm("react-slot-counter", "3.0.1"))
// ====== font awesome ======
implementation(npm("@fortawesome/fontawesome-svg-core", "6.5.2"))
implementation(npm("@fortawesome/free-solid-svg-icons", "6.5.2"))
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/jsMain/kotlin/ru/posidata/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.browser.window
import react.router.dom.RouterProvider
import react.router.dom.createBrowserRouter
import ru.posidata.views.components.errorBoundary
import ru.posidata.views.main.luckyDrawCard
import ru.posidata.views.main.mainView


Expand All @@ -32,6 +33,11 @@ val App: FC<Props> = FC {
path = "/"
element = mainView.create()
errorElement = errorBoundary.create()
},
jso {
path = "/luckydraw"
element = luckyDrawCard.create()
errorElement = errorBoundary.create()
}
)
)
Expand Down
74 changes: 74 additions & 0 deletions frontend/src/jsMain/kotlin/ru/posidata/views/main/LuckyDraw.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ru.posidata.views.main

import js.objects.jso
import kotlinx.browser.window
import react.*
import react.dom.html.ReactHTML
import react.dom.html.ReactHTML.button
import react.dom.html.ReactHTML.div
import react.dom.html.ReactHTML.h1
import ru.posidata.common.UserForSerializationDTO
import ru.posidata.views.utils.externals.particles.Particles
import ru.posidata.views.utils.externals.slotmachine.Slot
import ru.posidata.views.utils.internals.*
import web.cssom.*

val luckyDrawCard = FC<LuckyDrawProps> { _ ->
val (luckyUserName, setLuckyUserName) = useState<String?>(null)

val getUser = useDeferredRequest {
val response = get(
url = "${window.location.origin}/api/random",
params = jso<dynamic> {},
headers = jsonHeaders,
loadingHandler = ::noopLoadingHandler,
responseHandler = ::noopResponseHandler,
)
setLuckyUserName(response.decodeFromJsonString<UserForSerializationDTO>().username)
}
Particles::class.react {
id = "tsparticles"
url = "${window.location.origin}/particles.json"
}

ReactHTML.div {
className = ClassName("full-width-container")
id = "back"
ReactHTML.div {
className = ClassName("row justify-content-center align-items-center text-center")
style = jso {
minHeight = "100vh".unsafeCast<MinHeight>()
}

h1 {
className = ClassName(" justify-content-center align-items-center text-center")
style = jso {
fontFamily = "'Inter', sans-serif".unsafeCast<FontFamily>()
fontSize = 200.unsafeCast<FontSize>()
color = "white".unsafeCast<Color>()
}
if (luckyUserName != null) {
Slot {
startValue = "posidata"
value = luckyUserName
speed = 3.0
duration = 7.0
}
}
}

div {
className = ClassName("col-5 zIndex1000")
button {
+"Погнали, разыграем кусок ДСП!"
className = ClassName("btn btn-outline-info zIndex1000")
onClick = {
getUser()
}
}
}
}
}
}

external interface LuckyDrawProps : Props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import ru.posidata.common.UserForSerializationDTO
import web.cssom.*

val mainView = FC {

Particles::class.react {
id = "tsparticles"
url = "${kotlinx.browser.window.location.origin}/particles.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import js.objects.jso
import kotlinx.browser.window
import react.*
import react.dom.html.ReactHTML.a
import react.dom.html.ReactHTML.button
import react.dom.html.ReactHTML.div
import react.dom.html.ReactHTML.h1
import react.dom.html.ReactHTML.h6
Expand Down Expand Up @@ -138,6 +137,7 @@ val welcomeCard = FC<WelcomeCardProps> { props ->
+". ${if (props.tgUser == null) "Чтобы участвовать в розыгрыше и рейтинге - залогинься:" else ""}"
}


if (props.tgUser == null) {
TLoginButton {
botName = "PosiDataBot"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@file:JsModule("react-slot-counter")
@file:JsNonModule

package ru.posidata.views.utils.externals.slotmachine

import react.*

@JsName("default") // Ensures you're importing the default export
external val Slot: FC<SlotProps> // Declare the component

// External Props definition that maps to your TypeScript interface
external interface SlotProps : Props {
var index: Int
var isNew: Boolean?
var charClassName: String?
var numbersRef: RefObject<dynamic>
var active: Boolean
var isChanged: Boolean
var effectiveDuration: Double
var delay: Double
var value: dynamic
var startValue: dynamic?
var disableStartValue: Boolean?
var dummyList: Array<dynamic>
var hasSequentialDummyList: Boolean?
var hasInfiniteList: Boolean?
var valueClassName: String?
var numberSlotClassName: String?
var numberClassName: String?
var reverse: Boolean?
var sequentialAnimationMode: Boolean
var useMonospaceWidth: Boolean
var maxNumberWidth: Int?
var onFontHeightChange: ((Int) -> Unit)?
var speed: Double
var duration: Double
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@file:JsModule("react-telegram-auth")
@file:JsNonModule

package ru.posidata.views.utils.externals.telegram

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@file:JsModule("react-telegram-auth")
@file:JsNonModule

package ru.posidata.views.utils.externals.telegram

Expand Down
5 changes: 5 additions & 0 deletions kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2877,6 +2877,11 @@ [email protected], react-router@^6.23.1:
dependencies:
"@remix-run/router" "1.16.1"

[email protected]:
version "3.0.1"
resolved "https://registry.yarnpkg.com/react-slot-counter/-/react-slot-counter-3.0.1.tgz#7b2becdbea1f8c83eb754371d1bd65b287ef3648"
integrity sha512-+NwzRrZ+zWErP/8nz+0Y/imtOcMDr1A2Gv8CfzXNRW/zXaxNPMpZQdWeJAX/HiSqaeGc+jxKqKscoMN101NYDg==

[email protected]:
version "1.0.4"
resolved "https://registry.yarnpkg.com/react-telegram-auth/-/react-telegram-auth-1.0.4.tgz#76443a3930497de7ab03a24d791e03aeeac47a5c"
Expand Down

0 comments on commit 9dbfae4

Please sign in to comment.