working invitation code via portal

This commit is contained in:
minjaesong
2023-09-04 21:48:28 +09:00
parent 7b3ac9cd06
commit c45cab388e
12 changed files with 276 additions and 216 deletions

View File

@@ -32,63 +32,47 @@ class UIImportAvatar(val remoCon: UIRemoCon) : Advanceable() {
private val drawX = (Toolkit.drawWidth - width) / 2
private val drawY = (App.scr.height - height) / 2
private val cols = 80
private val rows = 30
private val goButtonWidth = 180
private val descStartY = 24 * 4
private val lh = App.fontGame.lineHeight.toInt()
// private val codeBox = UIItemCodeBox(this, (Toolkit.drawWidth - App.fontSmallNumbers.W * cols) / 2, drawY, cols, rows)
private val inputWidth = 340
private val filenameInput = UIItemTextLineInput(this,
(Toolkit.drawWidth - inputWidth) / 2, (App.scr.height - height) / 2 + descStartY + (5) * lh, inputWidth,
maxLen = InputLenCap(256, InputLenCap.CharLenUnit.UTF8_BYTES)
)
/*
private val clearButton = UIItemTextButton(this,
{ Lang["MENU_IO_CLEAR"] }, drawX + (width/2 - goButtonWidth) / 2, drawY + height - 24 - 34, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true)
private val pasteButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_PASTE"] }, drawX + width/2 + (width/2 - goButtonWidth) / 2, drawY + height - 24 - 34, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true)
*/
).also {
// reset importReturnCode if the text input has changed
it.onKeyDown = { _ ->
importReturnCode = 0
}
}
private val backButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_BACK"] }, drawX + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true)
private val goButton = UIItemTextButton(this,
{ Lang["MENU_IO_IMPORT"] }, drawX + width/2 + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true)
{ Lang["MENU_LABEL_BACK"] }, drawX + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
private var importReturnCode = 0
init {
// addUIitem(codeBox)
// addUIitem(clearButton)
// addUIitem(pasteButton)
addUIitem(filenameInput)
addUIitem(backButton)
addUIitem(goButton)
/*clearButton.clickOnceListener = { _,_ ->
codeBox.clearTextBuffer()
}
pasteButton.clickOnceListener = { _,_ ->
codeBox.pasteFromClipboard()
}*/
backButton.clickOnceListener = { _,_ ->
it.clickOnceListener = { _,_ ->
remoCon.openUI(UILoadSavegame(remoCon))
}
goButton.clickOnceListener = { _,_ ->
}
private val goButton = UIItemTextButton(this,
{ Lang["MENU_IO_IMPORT"] }, drawX + width/2 + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _,_ ->
if (filenameInput.getText().isNotBlank()) {
importReturnCode = doImport()
if (importReturnCode == 0) remoCon.openUI(UILoadSavegame(remoCon))
}
}
}
private var importReturnCode = 0
init {
addUIitem(filenameInput)
addUIitem(backButton)
addUIitem(goButton)
// reset importReturnCode if the text input has changed
filenameInput.onKeyDown = { _ ->
importReturnCode = 0
}
}
// private var textX = 0

View File

@@ -1,6 +1,5 @@
package net.torvald.terrarum.modulebasegame.ui
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.Texture
@@ -21,8 +20,11 @@ import net.torvald.terrarum.serialise.Common
import net.torvald.terrarum.modulebasegame.serialise.ReadActor
import net.torvald.terrarum.savegame.DiskSkimmer
import net.torvald.terrarum.savegame.VDFileID.SAVEGAMEINFO
import net.torvald.terrarum.serialise.toBigInt64
import net.torvald.terrarum.ui.*
import net.torvald.terrarum.utils.PasswordBase32
import net.torvald.terrarum.utils.RandomWordsName
import java.util.UUID
/**
* Created by minjaesong on 2021-10-25.
@@ -56,7 +58,7 @@ class UINewWorld(val remoCon: UIRemoCon) : UICanvas() {
private val radioCellWidth = 120
private val inputWidth = 350
private val radioX = (width - (radioCellWidth * NEW_WORLD_SIZE.size + 9)) / 2
private val inputX = width - inputWidth
private val inputX = drawX + width - inputWidth + 5
private val sizeSelY = 186 + 40
@@ -85,74 +87,166 @@ class UINewWorld(val remoCon: UIRemoCon) : UICanvas() {
private val inputLineY1 = 90
private val inputLineY2 = 130
private val goButtonWidth = 180
private val gridGap = 10
private val buttonBaseX = (Toolkit.drawWidth - 3 * goButtonWidth - 2 * gridGap) / 2
private val buttonY = drawY + height - 24
private var mode = 0 // 0: new world, 1: use invitation
private var uiItemsChangeRequest: (() -> Unit)? = null
private val nameInput = UIItemTextLineInput(this,
drawX + width - inputWidth + 5, drawY + sizeSelY + inputLineY1, inputWidth,
inputX, drawY + sizeSelY + inputLineY1, inputWidth,
{ RandomWordsName(4) }, InputLenCap(VirtualDisk.NAME_LENGTH, InputLenCap.CharLenUnit.UTF8_BYTES))
private val seedInput = UIItemTextLineInput(this,
drawX + width - inputWidth + 5, drawY + sizeSelY + inputLineY2, inputWidth,
inputX, drawY + sizeSelY + inputLineY2, inputWidth,
{ rng.nextLong().toString() }, InputLenCap(256, InputLenCap.CharLenUnit.CODEPOINTS))
private val goButtonWidth = 180
private val codeInput = UIItemTextLineInput(this,
inputX, drawY + sizeSelY, inputWidth,
{ "AAAA BB CCCCC DDDDD EEEEE FFFFF" }, InputLenCap(31, InputLenCap.CharLenUnit.CODEPOINTS)).also {
// reset importReturnCode if the text input has changed
it.onKeyDown = { _ ->
importReturnCode = 0
}
}
private val backButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_BACK"] }, drawX + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
{ Lang["MENU_LABEL_BACK"] }, buttonBaseX, buttonY, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _, _ ->
remoCon.openUI(UILoadSavegame(remoCon))
}
}
private val useInvitationButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_USE_CODE"] }, buttonBaseX + goButtonWidth + gridGap, buttonY, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _, _ ->
if (mode == 0) {
it.textfun = { Lang["CONTEXT_WORLD_NEW"] }
uiItemsChangeRequest = {
uiItems.clear()
addUIitem(codeInput)
addUIitem(goButton)
addUIitem(it)
addUIitem(backButton)
}
mode = 1
}
else if (mode == 1) {
it.textfun = { Lang["MENU_LABEL_USE_CODE"] }
uiItemsChangeRequest = {
uiItems.clear()
addUIitem(sizeSelector)
addUIitem(seedInput) // order is important
addUIitem(nameInput) // because of the IME candidates overlay
addUIitem(goButton)
addUIitem(it)
addUIitem(backButton)
}
mode = 0
}
}
}
private val goButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_CONFIRM_BUTTON"] }, drawX + width/2 + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
{ Lang["MENU_LABEL_CONFIRM_BUTTON"] }, buttonBaseX + (goButtonWidth + gridGap) * 2, buttonY, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _, _ ->
// after the save is complete, proceed to new world generation
if (existingPlayer == null) {
newPlayerCreationThread.start()
newPlayerCreationThread.join()
}
if (mode == 0) {
// after the save is complete, proceed to new world generation
if (existingPlayer == null) {
newPlayerCreationThread.start()
newPlayerCreationThread.join()
}
printdbg(this, "generate! Size=${sizeSelector.selection}, Name=${nameInput.getTextOrPlaceholder()}, Seed=${seedInput.getTextOrPlaceholder()}")
printdbg(this, "generate! Size=${sizeSelector.selection}, Name=${nameInput.getTextOrPlaceholder()}, Seed=${seedInput.getTextOrPlaceholder()}")
val ingame = TerrarumIngame(App.batch)
val playerDisk = existingPlayer ?: App.savegamePlayers[UILoadGovernor.playerUUID]!!.loadable()
val player = ReadActor.invoke(playerDisk, ByteArray64Reader(playerDisk.getFile(SAVEGAMEINFO)!!.bytes, Common.CHARSET)) as IngamePlayer
val seed = try {
seedInput.getTextOrPlaceholder().toLong()
}
catch (e: NumberFormatException) {
XXHash64.hash(seedInput.getTextOrPlaceholder().toByteArray(Charsets.UTF_8), 10000)
}
val (wx, wy) = TerrarumIngame.NEW_WORLD_SIZE[sizeSelector.selection]
val worldParam = TerrarumIngame.NewGameParams(
val ingame = TerrarumIngame(App.batch)
val playerDisk = existingPlayer ?: App.savegamePlayers[UILoadGovernor.playerUUID]!!.loadable()
val player = ReadActor.invoke(
playerDisk,
ByteArray64Reader(playerDisk.getFile(SAVEGAMEINFO)!!.bytes, Common.CHARSET)
) as IngamePlayer
val seed = try {
seedInput.getTextOrPlaceholder().toLong()
}
catch (e: NumberFormatException) {
XXHash64.hash(seedInput.getTextOrPlaceholder().toByteArray(Charsets.UTF_8), 10000)
}
val (wx, wy) = TerrarumIngame.NEW_WORLD_SIZE[sizeSelector.selection]
val worldParam = TerrarumIngame.NewGameParams(
player, TerrarumIngame.NewWorldParameters(
wx, wy, seed, nameInput.getTextOrPlaceholder()
)
)
ingame.gameLoadInfoPayload = worldParam
ingame.gameLoadMode = TerrarumIngame.GameLoadMode.CREATE_NEW
)
ingame.gameLoadInfoPayload = worldParam
ingame.gameLoadMode = TerrarumIngame.GameLoadMode.CREATE_NEW
Terrarum.setCurrentIngameInstance(ingame)
val loadScreen = WorldgenLoadScreen(ingame, wx, wy)
App.setLoadScreen(loadScreen)
Terrarum.setCurrentIngameInstance(ingame)
val loadScreen = WorldgenLoadScreen(ingame, wx, wy)
App.setLoadScreen(loadScreen)
}
else {
val code = codeInput.getText().replace(" ", "")
val uuid = PasswordBase32.decode(code, 16).let {
UUID(it.toBigInt64(0), it.toBigInt64(8))
}
val world = App.savegameWorlds[uuid]
printdbg(this, "Decoded UUID=$uuid")
// world exists?
if (world == null) {
importReturnCode = 1
}
else {
TODO()
// after the save is complete, proceed to importing
if (existingPlayer == null) {
newPlayerCreationThread.start()
newPlayerCreationThread.join()
}
}
}
}
}
private var importReturnCode = 0
private val errorMessages = listOf(
"", // 0
Lang["ERROR_WORLD_NOT_FOUND"], // 1
)
init {
addUIitem(sizeSelector)
addUIitem(seedInput) // order is important
addUIitem(nameInput) // because of the IME candidates overlay
addUIitem(goButton)
addUIitem(useInvitationButton)
addUIitem(backButton)
}
override fun show() {
super.show()
seedInput.clearText()
nameInput.clearText()
codeInput.clearText()
importReturnCode = 0
}
override fun updateUI(delta: Float) {
if (uiItemsChangeRequest != null) {
uiItemsChangeRequest!!()
uiItemsChangeRequest = null
}
uiItems.forEach { it.update(delta) }
}
@@ -162,23 +256,41 @@ class UINewWorld(val remoCon: UIRemoCon) : UICanvas() {
// val titlestr = Lang["CONTEXT_WORLD_NEW"]
// App.fontUITitle.draw(batch, titlestr, drawX + (width - App.fontUITitle.getWidth(titlestr)).div(2).toFloat(), titleTextPosY.toFloat())
// draw size previews
val texture = tex[sizeSelector.selection.coerceAtMost(tex.lastIndex)]
val tx = drawX + (width - texture.regionWidth) / 2
val ty = drawY + (160 - texture.regionHeight) / 2
batch.draw(texture, tx.toFloat(), ty.toFloat())
// border
batch.color = Toolkit.Theme.COL_INACTIVE
Toolkit.drawBoxBorder(batch, tx - 1, ty - 1, texture.regionWidth + 2, texture.regionHeight + 2)
if (mode == 0) {
// draw size previews
val texture = tex[sizeSelector.selection.coerceAtMost(tex.lastIndex)]
val tx = drawX + (width - texture.regionWidth) / 2
val ty = drawY + (160 - texture.regionHeight) / 2
batch.draw(texture, tx.toFloat(), ty.toFloat())
// border
batch.color = Toolkit.Theme.COL_INACTIVE
Toolkit.drawBoxBorder(batch, tx - 1, ty - 1, texture.regionWidth + 2, texture.regionHeight + 2)
batch.color = Color.WHITE
// size selector title
val sizestr = Lang["MENU_OPTIONS_SIZE"]
App.fontGame.draw(batch, sizestr, drawX + (width - App.fontGame.getWidth(sizestr)).div(2).toFloat(), drawY + sizeSelY - 40f)
batch.color = Color.WHITE
// size selector title
val sizestr = Lang["MENU_OPTIONS_SIZE"]
App.fontGame.draw(
batch,
sizestr,
drawX + (width - App.fontGame.getWidth(sizestr)).div(2).toFloat(),
drawY + sizeSelY - 40f
)
// name/seed input labels
App.fontGame.draw(batch, Lang["MENU_NAME"], drawX - 4, drawY + sizeSelY + inputLineY1)
App.fontGame.draw(batch, Lang["CONTEXT_GENERATOR_SEED"], drawX - 4, drawY + sizeSelY + inputLineY2)
// name/seed input labels
App.fontGame.draw(batch, Lang["MENU_NAME"], drawX - 4, drawY + sizeSelY + inputLineY1)
App.fontGame.draw(batch, Lang["CONTEXT_GENERATOR_SEED"], drawX - 4, drawY + sizeSelY + inputLineY2)
}
else if (mode == 1) {
// code input labels
App.fontGame.draw(batch, Lang["CREDITS_CODE"], drawX - 4, drawY + sizeSelY)
if (importReturnCode != 0) {
batch.color = Toolkit.Theme.COL_RED
val tby = codeInput.posY
val btny = backButton.posY
Toolkit.drawTextCentered(batch, App.fontGame, errorMessages[importReturnCode], Toolkit.drawWidth, 0, (tby + btny) / 2)
}
}
uiItems.forEach { it.render(batch, camera) }
}

View File

@@ -30,12 +30,13 @@ class UIShare : UICanvas() {
override fun show() {
shareCode = PasswordBase32.encode(
INGAME.world.worldIndex.mostSignificantBits.toBig64() +
INGAME.world.worldIndex.mostSignificantBits.toBig64()
).let { it.substring(0, it.indexOf('=')) }.let {
INGAME.world.worldIndex.leastSignificantBits.toBig64()
).let {
"${it.substring(0..3)}$dash${it.substring(4..5)}$dash${it.substring(6..10)}$dash${it.substring(11..15)}$dash${it.substring(16..20)}$dash${it.substring(21)}"
}
App.printdbg(this, shareCode)
App.printdbg(this, INGAME.world.worldIndex)
wotKeys = (1..4).map { Lang["CONTEXT_WORLD_CODE_SHARE_$it", false] }
}

View File

@@ -49,8 +49,8 @@ class UIWorldPortalShare(private val full: UIWorldPortal) : UICanvas() {
override fun show() {
shareCode = PasswordBase32.encode(
INGAME.world.worldIndex.mostSignificantBits.toBig64() +
INGAME.world.worldIndex.mostSignificantBits.toBig64()
).let { it.substring(0, it.indexOf('=')) }.let {
INGAME.world.worldIndex.leastSignificantBits.toBig64()
).let {
"${it.substring(0..3)}$dash${it.substring(4..5)}$dash${it.substring(6..10)}$dash${it.substring(11..15)}$dash${it.substring(16..20)}$dash${it.substring(21)}"
}

View File

@@ -4,12 +4,16 @@ import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.App
import net.torvald.terrarum.INGAME
import net.torvald.terrarum.gamecontroller.TerrarumKeyboardEvent
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.printStackTrace
import net.torvald.terrarum.savegame.VirtualDisk
import net.torvald.terrarum.serialise.toBigInt64
import net.torvald.terrarum.ui.*
import net.torvald.terrarum.utils.PasswordBase32
import net.torvald.terrarum.utils.RandomWordsName
import java.util.*
/**
* Created by minjaesong on 2023-09-03.
@@ -34,9 +38,14 @@ class UIWorldPortalUseInvitation(val full: UIWorldPortal) : UICanvas() {
private val codeInput = UIItemTextLineInput(this,
drawX + width - inputWidth + 5, drawY + sizeSelY + inputLineY1, inputWidth,
{ "AAAA BB CCCCC DDDDD EEEEE FFFFF" }, InputLenCap(VirtualDisk.NAME_LENGTH, InputLenCap.CharLenUnit.UTF8_BYTES)
)
drawX + width - inputWidth + 5, drawY + sizeSelY, inputWidth,
{ "AAAA BB CCCCC DDDDD EEEEE FFFFF" }, InputLenCap(31, InputLenCap.CharLenUnit.CODEPOINTS)
).also {
// reset importReturnCode if the text input has changed
it.onKeyDown = { _ ->
importReturnCode = 0
}
}
@@ -59,10 +68,35 @@ class UIWorldPortalUseInvitation(val full: UIWorldPortal) : UICanvas() {
{ Lang["MENU_LABEL_CONFIRM_BUTTON"] }, buttonBaseX + (goButtonWidth + gridGap) * 2, buttonY, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _, _ ->
val code = codeInput.getText().replace(" ", "")
val uuid = PasswordBase32.decode(code, 16).let {
UUID(it.toBigInt64(0), it.toBigInt64(8))
}
val world = App.savegameWorlds[uuid]
App.printdbg(this, "Decoded UUID=$uuid")
// world exists?
if (world == null) {
importReturnCode = 1
}
else {
// add the world to the player's worldbook
// TODO memory cap check? or disable check against shared worlds?
full.addWorldToPlayersDict(uuid)
full.cleanUpWorldDict()
full.requestTransition(0)
}
}
}
private var importReturnCode = 0
private val errorMessages = listOf(
"", // 0
Lang["ERROR_WORLD_NOT_FOUND"], // 1
)
init {
addUIitem(backButton)
addUIitem(searchWorldButton)
@@ -70,20 +104,32 @@ class UIWorldPortalUseInvitation(val full: UIWorldPortal) : UICanvas() {
addUIitem(codeInput)
}
override fun show() {
super.show()
codeInput.clearText()
importReturnCode = 0
}
override fun updateUI(delta: Float) {
uiItems.forEach { it.update(delta) }
}
override fun renderUI(batch: SpriteBatch, camera: OrthographicCamera) {
// error messages
if (importReturnCode != 0) {
batch.color = Toolkit.Theme.COL_RED
val tby = codeInput.posY
val btny = backButton.posY
Toolkit.drawTextCentered(batch, App.fontGame, errorMessages[importReturnCode], Toolkit.drawWidth, 0, (tby + btny) / 2)
}
// input labels
batch.color = Color.WHITE
App.fontGame.draw(batch, Lang["CREDITS_CODE"], drawX - 4, drawY + sizeSelY + inputLineY1)
App.fontGame.draw(batch, Lang["CREDITS_CODE"], drawX - 4, drawY + sizeSelY)
// control hints
App.fontGame.draw(batch, full.portalListingControlHelp, 2 + (Toolkit.drawWidth - 560)/2 + 2, (full.yEnd - 20).toInt())
uiItems.forEach { it.render(batch, camera) }
}