-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmytro Marchuk
committed
Oct 2, 2024
1 parent
58aaf79
commit 55bbb42
Showing
13 changed files
with
192 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
@file:OptIn(ExperimentalWasmDsl::class) | ||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl | ||
|
||
plugins { | ||
alias(libs.plugins.kotlinMultiplatform) | ||
} | ||
|
||
kotlin { | ||
jvm() | ||
js { browser() } | ||
wasmJs { browser() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ korge { | |
|
||
targetJvm() | ||
targetJs() | ||
// targetWasmJs() | ||
targetWasmJs() | ||
serializationJson() | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
gui/src/commonMain/kotlin/io/github/smaugfm/game2048/search/SearchImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package io.github.smaugfm.game2048.search | ||
|
||
expect class SearchImpl(log: Boolean = true) : Search { | ||
override fun platformDepthLimit(distinctTiles: Int): Int | ||
override suspend fun getExpectimaxResults(requests: List<SearchRequest>): List<SearchResult> | ||
override fun combineStats(one: SearchStats, two: SearchStats): SearchStats | ||
|
||
public override suspend fun init() | ||
override fun platformDepthLimit(distinctTiles: Int): Int | ||
override suspend fun getExpectimaxResults(requests: List<SearchRequest>): List<SearchResult> | ||
override fun combineStats(one: SearchStats, two: SearchStats): SearchStats | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
gui/src/jsMain/kotlin/io/github/smaugfm/game2048/util/checkWasmSupport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.github.smaugfm.game2048.util | ||
|
||
import kotlinx.coroutines.delay | ||
|
||
suspend fun checkWasmSupport(): Boolean { | ||
while (true) { | ||
try { | ||
return js("hasWasmSupport").unsafeCast<Boolean>() | ||
} catch (e: Throwable) { | ||
delay(50) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
gui/src/wasmJsMain/kotlin/io/github/smaugfm/game2048/search/SearchImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.github.smaugfm.game2048.search | ||
|
||
actual class SearchImpl actual constructor(log: Boolean) : Search() { | ||
public actual override suspend fun init() { | ||
throw UnsupportedOperationException() | ||
} | ||
|
||
actual override fun platformDepthLimit(distinctTiles: Int): Int { | ||
throw UnsupportedOperationException() | ||
} | ||
|
||
actual override suspend fun getExpectimaxResults(requests: List<SearchRequest>): List<SearchResult> { | ||
throw UnsupportedOperationException() | ||
} | ||
|
||
actual override fun combineStats(one: SearchStats, two: SearchStats): SearchStats { | ||
throw UnsupportedOperationException() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
solve/src/wasmJsMain/kotlin/io/github/smaugfm/game2048/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.github.smaugfm.game2048 | ||
|
||
import io.github.smaugfm.game2048.search.ExpectimaxSearch | ||
import io.github.smaugfm.game2048.search.SearchRequest | ||
import io.github.smaugfm.game2048.transposition.Long2LongMapTranspositionTable | ||
import org.w3c.dom.DedicatedWorkerGlobalScope | ||
|
||
private fun getWorkerGlobalScope(): DedicatedWorkerGlobalScope = | ||
js("self") | ||
|
||
fun main() { | ||
val table = Long2LongMapTranspositionTable() | ||
val self = getWorkerGlobalScope() | ||
println("Web-worker (wasm) started") | ||
|
||
self.onmessage = { messageEvent -> | ||
try { | ||
val requestStr = messageEvent.data.toString() | ||
if (requestStr == "ping") | ||
self.postMessage("pong".toJsString()) | ||
else { | ||
val request = SearchRequest.deserialize(requestStr)!! | ||
|
||
val scoreResult = ExpectimaxSearch(table).score(request) | ||
self.postMessage( | ||
scoreResult | ||
?.serialize() | ||
?.toJsString() | ||
) | ||
} | ||
} catch (e: Throwable) { | ||
println("Unhandled exception in web worker (wasm):") | ||
println(e) | ||
} | ||
} | ||
} |