Changed server connection to URL-based to allow connection to uncivserver.xyz

This commit is contained in:
Yair Morgenstern
2022-03-31 23:03:14 +03:00
parent 279178f103
commit 6315a16d98
6 changed files with 15 additions and 35 deletions

View File

@ -1251,8 +1251,7 @@ Invalid ID! =
# Multiplayer options menu # Multiplayer options menu
Current IP address = Server address =
Server's IP address =
Reset to Dropbox = Reset to Dropbox =
Check connection to server = Check connection to server =
Awaiting response... = Awaiting response... =

View File

@ -6,7 +6,6 @@ import com.unciv.UncivGame
import com.unciv.logic.GameInfo import com.unciv.logic.GameInfo
import com.unciv.logic.GameInfoPreview import com.unciv.logic.GameInfoPreview
import com.unciv.logic.GameSaver import com.unciv.logic.GameSaver
import com.unciv.models.metadata.checkMultiplayerServerWithPort
import com.unciv.ui.saves.Gzip import com.unciv.ui.saves.Gzip
import com.unciv.ui.worldscreen.mainmenu.OptionsPopup import com.unciv.ui.worldscreen.mainmenu.OptionsPopup
import java.util.* import java.util.*
@ -24,8 +23,7 @@ interface IFileMetaData {
class UncivServerFileStorage(serverIpWithPort:String):IFileStorage { class UncivServerFileStorage(val serverUrl:String):IFileStorage {
val serverUrl = "http://$serverIpWithPort"
override fun saveFileData(fileName: String, data: String) { override fun saveFileData(fileName: String, data: String) {
OptionsPopup.SimpleHttp.sendRequest(Net.HttpMethods.PUT, "$serverUrl/files/$fileName", data){ OptionsPopup.SimpleHttp.sendRequest(Net.HttpMethods.PUT, "$serverUrl/files/$fileName", data){
success: Boolean, result: String -> success: Boolean, result: String ->
@ -70,10 +68,7 @@ class OnlineMultiplayer {
val settings = UncivGame.Current.settings val settings = UncivGame.Current.settings
if (settings.multiplayerServer == Constants.dropboxMultiplayerServer) if (settings.multiplayerServer == Constants.dropboxMultiplayerServer)
fileStorage = DropboxFileStorage() fileStorage = DropboxFileStorage()
else { else fileStorage = UncivServerFileStorage(settings.multiplayerServer)
val serverIpWithPort = settings.multiplayerServer.checkMultiplayerServerWithPort()
fileStorage = UncivServerFileStorage(serverIpWithPort)
}
} }
fun tryUploadGame(gameInfo: GameInfo, withPreview: Boolean) { fun tryUploadGame(gameInfo: GameInfo, withPreview: Boolean) {

View File

@ -1,7 +0,0 @@
package com.unciv.models.metadata
/** Check the url, if no port, add [defaultPort], then return the url. */
fun String.checkMultiplayerServerWithPort(defaultPort: Int = 8080): String {
return if (contains(":")) this
else "$this:$defaultPort"
}

View File

@ -16,7 +16,6 @@ import com.unciv.UncivGame
import com.unciv.logic.MapSaver import com.unciv.logic.MapSaver
import com.unciv.logic.civilization.PlayerType import com.unciv.logic.civilization.PlayerType
import com.unciv.models.UncivSound import com.unciv.models.UncivSound
import com.unciv.models.metadata.checkMultiplayerServerWithPort
import com.unciv.models.ruleset.Ruleset import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.Ruleset.RulesetError import com.unciv.models.ruleset.Ruleset.RulesetError
import com.unciv.models.ruleset.Ruleset.RulesetErrorSeverity import com.unciv.models.ruleset.Ruleset.RulesetErrorSeverity
@ -260,18 +259,17 @@ class OptionsPopup(val previousScreen: BaseScreen) : Popup(previousScreen) {
val connectionToServerButton = "Check connection to server".toTextButton() val connectionToServerButton = "Check connection to server".toTextButton()
val ipAddress = getIpAddress() val textToShowForMultiplayerAddress =
add("{Current IP address}: $ipAddress".toTextButton().onClick { if (settings.multiplayerServer != Constants.dropboxMultiplayerServer) settings.multiplayerServer
Gdx.app.clipboard.contents = ipAddress.toString() else "https://..."
}).row() val multiplayerServerTextField = TextField(textToShowForMultiplayerAddress, BaseScreen.skin)
val multiplayerServerTextField = TextField(settings.multiplayerServer, BaseScreen.skin)
multiplayerServerTextField.programmaticChangeEvents = true multiplayerServerTextField.programmaticChangeEvents = true
multiplayerServerTextField.width = screen.stage.width / 2
val serverIpTable = Table() val serverIpTable = Table()
serverIpTable.add("Server's IP address".toLabel().onClick { serverIpTable.add("Server address".toLabel().onClick {
multiplayerServerTextField.text = Gdx.app.clipboard.contents multiplayerServerTextField.text = Gdx.app.clipboard.contents
}).padRight(10f) }).row()
multiplayerServerTextField.onChange { multiplayerServerTextField.onChange {
settings.multiplayerServer = multiplayerServerTextField.text settings.multiplayerServer = multiplayerServerTextField.text
settings.save() settings.save()
@ -345,7 +343,7 @@ class OptionsPopup(val previousScreen: BaseScreen) : Popup(previousScreen) {
} }
fun successfullyConnectedToServer(action: (Boolean, String)->Unit){ fun successfullyConnectedToServer(action: (Boolean, String)->Unit){
SimpleHttp.sendGetRequest("http://${settings.multiplayerServer.checkMultiplayerServerWithPort()}/isalive", action) SimpleHttp.sendGetRequest("${settings.multiplayerServer}/isalive", action)
} }
private fun getAdvancedTab() = Table(BaseScreen.skin).apply { private fun getAdvancedTab() = Table(BaseScreen.skin).apply {

View File

@ -26,7 +26,7 @@ private class UncivServerRunner : CliktCommand() {
"-p", "-port", "-p", "-port",
envvar = "UncivServerPort", envvar = "UncivServerPort",
help = "Server port" help = "Server port"
).int().restrictTo(1024..49151).default(8080) ).int().restrictTo(1024..49151).default(80)
private val folder by option( private val folder by option(
"-f", "-folder", "-f", "-folder",

View File

@ -22,17 +22,12 @@ Your server has now started!
In Unciv itself, from the same computer, enter Options > Multiplayer. In Unciv itself, from the same computer, enter Options > Multiplayer.
Click the first text (Current IP address) to copy the IP to the clipboard. Enter the URL of the computer you ran the server on (or http://localhost)
Then, click the second the second (Server IP address) to put your computer's IP in the "Server IP" slot.
If you click "check connection to server" you should now get "Return result: true", which means it's working! If you click "check connection to server" you should now get "Return result: true", which means it's working!
So far you ran the server and connected yourself to it, but now for the interesting part - connecting other people! For other devices, you'll need an external IP, which is out of scope for this documentation since there are many ways of achieving it.
The IP should still be in your clipboard - if not, just click the 'copy to clipboard' button again. On the other device, do the same - enter the URL, click 'check connection' from the new device, and if you got the same result - congratulations, you're both connected to the same server and can start a multiplayer game on the server!
Send the IP to the other device, there - copy it, and click 'copy from clipboard'.
You can of course enter the IP manually if that's easier for you.
Click 'check connection' from the new device, and if you got the same result - congratulations, you're both connected to the same server and can start a multiplayer game on the server!
Please note that devices NOT connected to the same server will NOT be able to participate in multiplayer games together! Please note that devices NOT connected to the same server will NOT be able to participate in multiplayer games together!