mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-26 05:14:32 -04:00
Move SimpleHttp object (#6479)
* Change TabbedPager mechanism to communicate page activation * Change TabbedPager mechanism for fixed content * OptionsPopup better use of TabbedPager * TabbedPager arrow keys * After-merge patch * Move SimpleHttp Co-authored-by: Yair Morgenstern <yairm210@hotmail.com>
This commit is contained in:
parent
8385f814a6
commit
a4babeda0b
@ -7,7 +7,6 @@ import com.unciv.logic.GameInfo
|
||||
import com.unciv.logic.GameInfoPreview
|
||||
import com.unciv.logic.GameSaver
|
||||
import com.unciv.ui.saves.Gzip
|
||||
import com.unciv.ui.worldscreen.mainmenu.OptionsPopup
|
||||
import java.util.*
|
||||
|
||||
interface IFileStorage {
|
||||
@ -25,7 +24,7 @@ interface IFileMetaData {
|
||||
|
||||
class UncivServerFileStorage(val serverUrl:String):IFileStorage {
|
||||
override fun saveFileData(fileName: String, data: String) {
|
||||
OptionsPopup.SimpleHttp.sendRequest(Net.HttpMethods.PUT, "$serverUrl/files/$fileName", data){
|
||||
SimpleHttp.sendRequest(Net.HttpMethods.PUT, "$serverUrl/files/$fileName", data){
|
||||
success: Boolean, result: String ->
|
||||
if (!success) {
|
||||
println(result)
|
||||
@ -36,7 +35,7 @@ class UncivServerFileStorage(val serverUrl:String):IFileStorage {
|
||||
|
||||
override fun loadFileData(fileName: String): String {
|
||||
var fileData = ""
|
||||
OptionsPopup.SimpleHttp.sendGetRequest("$serverUrl/files/$fileName"){
|
||||
SimpleHttp.sendGetRequest("$serverUrl/files/$fileName"){
|
||||
success: Boolean, result: String ->
|
||||
if (!success) {
|
||||
println(result)
|
||||
@ -52,7 +51,7 @@ class UncivServerFileStorage(val serverUrl:String):IFileStorage {
|
||||
}
|
||||
|
||||
override fun deleteFile(fileName: String) {
|
||||
OptionsPopup.SimpleHttp.sendRequest(Net.HttpMethods.DELETE, "$serverUrl/files/$fileName", ""){
|
||||
SimpleHttp.sendRequest(Net.HttpMethods.DELETE, "$serverUrl/files/$fileName", ""){
|
||||
success: Boolean, result: String ->
|
||||
if (!success) throw java.lang.Exception(result)
|
||||
}
|
||||
@ -66,9 +65,9 @@ class OnlineMultiplayer {
|
||||
val fileStorage: IFileStorage
|
||||
init {
|
||||
val settings = UncivGame.Current.settings
|
||||
if (settings.multiplayerServer == Constants.dropboxMultiplayerServer)
|
||||
fileStorage = DropboxFileStorage()
|
||||
else fileStorage = UncivServerFileStorage(settings.multiplayerServer)
|
||||
fileStorage = if (settings.multiplayerServer == Constants.dropboxMultiplayerServer)
|
||||
DropboxFileStorage()
|
||||
else UncivServerFileStorage(settings.multiplayerServer)
|
||||
}
|
||||
|
||||
fun tryUploadGame(gameInfo: GameInfo, withPreview: Boolean) {
|
||||
|
55
core/src/com/unciv/logic/multiplayer/SimpleHttp.kt
Normal file
55
core/src/com/unciv/logic/multiplayer/SimpleHttp.kt
Normal file
@ -0,0 +1,55 @@
|
||||
package com.unciv.logic.multiplayer
|
||||
|
||||
import com.badlogic.gdx.Net
|
||||
import java.io.BufferedReader
|
||||
import java.io.DataOutputStream
|
||||
import java.io.InputStreamReader
|
||||
import java.net.DatagramSocket
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.InetAddress
|
||||
import java.net.URI
|
||||
import java.nio.charset.Charset
|
||||
|
||||
object SimpleHttp {
|
||||
fun sendGetRequest(url: String, action: (success: Boolean, result: String)->Unit) {
|
||||
sendRequest(Net.HttpMethods.GET, url, "", action)
|
||||
}
|
||||
|
||||
fun sendRequest(method: String, url: String, content: String, action: (success: Boolean, result: String)->Unit) {
|
||||
var uri = URI(url)
|
||||
if (uri.host == null) uri = URI("http://$url")
|
||||
if (uri.port == -1) uri = URI(uri.scheme, uri.userInfo, uri.host, 8080, uri.path, uri.query, uri.fragment)
|
||||
|
||||
with(uri.toURL().openConnection() as HttpURLConnection) {
|
||||
requestMethod = method // default is GET
|
||||
|
||||
try {
|
||||
if (content.isNotEmpty()) {
|
||||
doOutput = true
|
||||
// StandardCharsets.UTF_8 requires API 19
|
||||
val postData: ByteArray = content.toByteArray(Charset.forName("UTF-8"))
|
||||
val outputStream = DataOutputStream(outputStream)
|
||||
outputStream.write(postData)
|
||||
outputStream.flush()
|
||||
}
|
||||
|
||||
val text = BufferedReader(InputStreamReader(inputStream)).readText()
|
||||
action(true, text)
|
||||
} catch (t: Throwable) {
|
||||
println(t.message)
|
||||
val errorMessageToReturn =
|
||||
if (errorStream != null) BufferedReader(InputStreamReader(errorStream)).readText()
|
||||
else t.message!!
|
||||
println(errorMessageToReturn)
|
||||
action(false, errorMessageToReturn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getIpAddress(): String? {
|
||||
DatagramSocket().use { socket ->
|
||||
socket.connect(InetAddress.getByName("8.8.8.8"), 10002)
|
||||
return socket.localAddress.hostAddress
|
||||
}
|
||||
}
|
||||
}
|
@ -3,7 +3,6 @@ package com.unciv.ui.worldscreen.mainmenu
|
||||
import com.badlogic.gdx.Application
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.Input
|
||||
import com.badlogic.gdx.Net
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Label
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
|
||||
@ -15,6 +14,7 @@ import com.unciv.MainMenuScreen
|
||||
import com.unciv.UncivGame
|
||||
import com.unciv.logic.MapSaver
|
||||
import com.unciv.logic.civilization.PlayerType
|
||||
import com.unciv.logic.multiplayer.SimpleHttp
|
||||
import com.unciv.models.UncivSound
|
||||
import com.unciv.models.metadata.BaseRuleset
|
||||
import com.unciv.models.ruleset.Ruleset
|
||||
@ -35,15 +35,7 @@ import com.unciv.ui.utils.*
|
||||
import com.unciv.ui.utils.LanguageTable.Companion.addLanguageTables
|
||||
import com.unciv.ui.utils.UncivTooltip.Companion.addTooltip
|
||||
import com.unciv.ui.worldscreen.WorldScreen
|
||||
import java.io.BufferedReader
|
||||
import java.io.DataOutputStream
|
||||
import java.io.InputStreamReader
|
||||
import java.net.DatagramSocket
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.InetAddress
|
||||
import java.net.URL
|
||||
import java.nio.charset.Charset
|
||||
import java.util.*
|
||||
import java.util.UUID
|
||||
import kotlin.math.floor
|
||||
import com.badlogic.gdx.utils.Array as GdxArray
|
||||
|
||||
@ -133,6 +125,10 @@ class OptionsPopup(val previousScreen: BaseScreen) : Popup(previousScreen) {
|
||||
(previousScreen.game.screen as BaseScreen).openOptionsPopup()
|
||||
}
|
||||
|
||||
private fun successfullyConnectedToServer(action: (Boolean, String)->Unit){
|
||||
SimpleHttp.sendGetRequest("${settings.multiplayerServer}/isalive", action)
|
||||
}
|
||||
|
||||
//region Page builders
|
||||
|
||||
private fun getAboutTab(): Table {
|
||||
@ -297,52 +293,6 @@ class OptionsPopup(val previousScreen: BaseScreen) : Popup(previousScreen) {
|
||||
}).row()
|
||||
}
|
||||
|
||||
fun getIpAddress(): String? {
|
||||
DatagramSocket().use { socket ->
|
||||
socket.connect(InetAddress.getByName("8.8.8.8"), 10002)
|
||||
return socket.localAddress.hostAddress
|
||||
}
|
||||
}
|
||||
|
||||
object SimpleHttp {
|
||||
fun sendGetRequest(url: String, action: (success: Boolean, result: String)->Unit) {
|
||||
sendRequest(Net.HttpMethods.GET, url, "", action)
|
||||
}
|
||||
|
||||
fun sendRequest(method: String, url: String, content: String, action: (success: Boolean, result: String)->Unit) {
|
||||
with(URL(url).openConnection() as HttpURLConnection) {
|
||||
requestMethod = method // default is GET
|
||||
|
||||
if (method != Net.HttpMethods.GET) doOutput = true
|
||||
|
||||
try {
|
||||
if (content.isNotEmpty()) {
|
||||
// StandardCharsets.UTF_8 requires API 19
|
||||
val postData: ByteArray = content.toByteArray(Charset.forName("UTF-8"))
|
||||
val outputStream = DataOutputStream(outputStream)
|
||||
outputStream.write(postData)
|
||||
outputStream.flush()
|
||||
}
|
||||
|
||||
val text = BufferedReader(InputStreamReader(inputStream)).readText()
|
||||
action(true, text)
|
||||
} catch (t: Throwable) {
|
||||
println(t.message)
|
||||
val errorMessageToReturn =
|
||||
if (errorStream != null) BufferedReader(InputStreamReader(errorStream)).readText()
|
||||
else t.message!!
|
||||
println(errorMessageToReturn)
|
||||
action(false, errorMessageToReturn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun successfullyConnectedToServer(action: (Boolean, String)->Unit){
|
||||
SimpleHttp.sendGetRequest("${settings.multiplayerServer}/isalive", action)
|
||||
}
|
||||
|
||||
private fun getAdvancedTab() = Table(BaseScreen.skin).apply {
|
||||
pad(10f)
|
||||
defaults().pad(5f)
|
||||
|
Loading…
x
Reference in New Issue
Block a user