Basic server hosting done
This commit is contained in:
@@ -11,6 +11,10 @@ text.level.mode=Gamemode:
|
|||||||
text.savegame=Save Game
|
text.savegame=Save Game
|
||||||
text.loadgame=Load Game
|
text.loadgame=Load Game
|
||||||
text.quit=Quit
|
text.quit=Quit
|
||||||
|
text.hostserver=Host Server
|
||||||
|
text.server.port=Port:
|
||||||
|
text.server.invalidport=Invalid port number!
|
||||||
|
text.server.error=[crimson]Error hosting server: [orange]{0}
|
||||||
text.tutorial.back=< Prev
|
text.tutorial.back=< Prev
|
||||||
text.tutorial.next=Next >
|
text.tutorial.next=Next >
|
||||||
text.save.new=New Save
|
text.save.new=New Save
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ public class Mindustry extends ModuleCore {
|
|||||||
module(Vars.control = new Control());
|
module(Vars.control = new Control());
|
||||||
module(Vars.renderer = new Renderer());
|
module(Vars.renderer = new Renderer());
|
||||||
module(Vars.ui = new UI());
|
module(Vars.ui = new UI());
|
||||||
|
module(Vars.network = new Network());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadBundle(){
|
public void loadBundle(){
|
||||||
|
|||||||
@@ -73,10 +73,14 @@ public class Vars{
|
|||||||
|
|
||||||
public static final int tilesize = 8;
|
public static final int tilesize = 8;
|
||||||
|
|
||||||
|
//server port
|
||||||
|
public static final int port = 6567;
|
||||||
|
|
||||||
public static Control control;
|
public static Control control;
|
||||||
public static Renderer renderer;
|
public static Renderer renderer;
|
||||||
public static UI ui;
|
public static UI ui;
|
||||||
public static World world;
|
public static World world;
|
||||||
|
public static Network network;
|
||||||
|
|
||||||
public static Player player;
|
public static Player player;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,36 @@
|
|||||||
package io.anuke.mindustry.core;
|
package io.anuke.mindustry.core;
|
||||||
|
|
||||||
|
import io.anuke.mindustry.Vars;
|
||||||
|
import io.anuke.mindustry.core.GameState.State;
|
||||||
|
import io.anuke.mindustry.net.Net;
|
||||||
import io.anuke.ucore.modules.Module;
|
import io.anuke.ucore.modules.Module;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class Network extends Module{
|
public class Network extends Module{
|
||||||
//TODO implementation
|
private boolean isHosting;
|
||||||
|
|
||||||
|
public Network(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(){
|
||||||
|
if(isHosting && GameState.is(State.menu)){
|
||||||
|
Net.closeServer();
|
||||||
|
isHosting = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void hostServer(int port) throws IOException{
|
||||||
|
if(isHosting){
|
||||||
|
throw new IOException("Already hosting a server!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Net.host(port);
|
||||||
|
isHosting = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isHosting(){
|
||||||
|
return isHosting;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import io.anuke.ucore.scene.builders.label;
|
|||||||
import io.anuke.ucore.scene.builders.table;
|
import io.anuke.ucore.scene.builders.table;
|
||||||
import io.anuke.ucore.scene.event.Touchable;
|
import io.anuke.ucore.scene.event.Touchable;
|
||||||
import io.anuke.ucore.scene.ui.*;
|
import io.anuke.ucore.scene.ui.*;
|
||||||
|
import io.anuke.ucore.scene.ui.TextField.TextFieldFilter;
|
||||||
import io.anuke.ucore.scene.ui.Window.WindowStyle;
|
import io.anuke.ucore.scene.ui.Window.WindowStyle;
|
||||||
import io.anuke.ucore.scene.ui.layout.Table;
|
import io.anuke.ucore.scene.ui.layout.Table;
|
||||||
import io.anuke.ucore.scene.ui.layout.Unit;
|
import io.anuke.ucore.scene.ui.layout.Unit;
|
||||||
@@ -371,12 +372,12 @@ public class UI extends SceneModule{
|
|||||||
configtable.setVisible(false);
|
configtable.setVisible(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showTextInput(String title, String text, String def, Consumer<String> confirmed){
|
public void showTextInput(String title, String text, String def, TextFieldFilter filter, Consumer<String> confirmed){
|
||||||
new Dialog(title, "dialog"){{
|
new Dialog(title, "dialog"){{
|
||||||
content().margin(30);
|
content().margin(30);
|
||||||
content().add(text).padRight(6f);
|
content().add(text).padRight(6f);
|
||||||
TextField field = content().addField(def, t->{}).size(170f, 50f).get();
|
TextField field = content().addField(def, t->{}).size(170f, 50f).get();
|
||||||
field.setTextFieldFilter((f, c) -> field.getText().length() < 12);
|
field.setTextFieldFilter((f, c) -> field.getText().length() < 12 && filter.acceptChar(f, c));
|
||||||
Mindustry.platforms.addDialog(field);
|
Mindustry.platforms.addDialog(field);
|
||||||
buttons().defaults().size(120, 54).pad(4);
|
buttons().defaults().size(120, 54).pad(4);
|
||||||
buttons().addButton("$text.ok", () -> {
|
buttons().addButton("$text.ok", () -> {
|
||||||
@@ -387,6 +388,10 @@ public class UI extends SceneModule{
|
|||||||
}}.show();
|
}}.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void showTextInput(String title, String text, String def, Consumer<String> confirmed){
|
||||||
|
showTextInput(title, text, def, (field, c) -> true, confirmed);
|
||||||
|
}
|
||||||
|
|
||||||
public void showError(String text){
|
public void showError(String text){
|
||||||
new Dialog("$text.error.title", "dialog"){{
|
new Dialog("$text.error.title", "dialog"){{
|
||||||
content().margin(15);
|
content().margin(15);
|
||||||
|
|||||||
@@ -23,9 +23,18 @@ public class Net{
|
|||||||
|
|
||||||
/**Host a server at an address*/
|
/**Host a server at an address*/
|
||||||
public static void host(int port) throws IOException{
|
public static void host(int port) throws IOException{
|
||||||
|
active = true;
|
||||||
|
server = true;
|
||||||
serverProvider.host(port);
|
serverProvider.host(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**Closes the server.*/
|
||||||
|
public static void closeServer(){
|
||||||
|
serverProvider.close();
|
||||||
|
server = false;
|
||||||
|
active = false;
|
||||||
|
}
|
||||||
|
|
||||||
/**Send an object to all connected clients, or to the server if this is a client.*/
|
/**Send an object to all connected clients, or to the server if this is a client.*/
|
||||||
public static void send(Object object, SendMode mode){
|
public static void send(Object object, SendMode mode){
|
||||||
if(server){
|
if(server){
|
||||||
@@ -113,6 +122,8 @@ public class Net{
|
|||||||
public void sendTo(int id, Object object, SendMode mode);
|
public void sendTo(int id, Object object, SendMode mode);
|
||||||
/**Send an object to everyone <i>except</i> a client ID.*/
|
/**Send an object to everyone <i>except</i> a client ID.*/
|
||||||
public void sendExcept(int id, Object object, SendMode mode);
|
public void sendExcept(int id, Object object, SendMode mode);
|
||||||
|
/**Close the server connection.*/
|
||||||
|
public void close();
|
||||||
/**Register classes to be sent.*/
|
/**Register classes to be sent.*/
|
||||||
public void register(Class<?>... types);
|
public void register(Class<?>... types);
|
||||||
}
|
}
|
||||||
|
|||||||
16
core/src/io/anuke/mindustry/net/Packets.java
Normal file
16
core/src/io/anuke/mindustry/net/Packets.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package io.anuke.mindustry.net;
|
||||||
|
|
||||||
|
/**Class for storing all packets.*/
|
||||||
|
public class Packets {
|
||||||
|
|
||||||
|
public static class Connect {
|
||||||
|
public int id;
|
||||||
|
public String addressTCP;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Disconnect {
|
||||||
|
public int id;
|
||||||
|
public String addressTCP;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package io.anuke.mindustry.net.packets;
|
|
||||||
|
|
||||||
public class Connect {
|
|
||||||
public int id;
|
|
||||||
public String addressTCP;
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package io.anuke.mindustry.net.packets;
|
|
||||||
|
|
||||||
public class Disconnect {
|
|
||||||
public int id;
|
|
||||||
public String addressTCP;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package io.anuke.mindustry.net.packets;
|
|
||||||
|
|
||||||
public class Packets {
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,12 @@ import io.anuke.ucore.scene.builders.build;
|
|||||||
import io.anuke.ucore.scene.builders.imagebutton;
|
import io.anuke.ucore.scene.builders.imagebutton;
|
||||||
import io.anuke.ucore.scene.ui.ConfirmDialog;
|
import io.anuke.ucore.scene.ui.ConfirmDialog;
|
||||||
import io.anuke.ucore.scene.ui.ImageButton;
|
import io.anuke.ucore.scene.ui.ImageButton;
|
||||||
|
import io.anuke.ucore.scene.ui.TextField.TextFieldFilter.DigitsOnlyFilter;
|
||||||
import io.anuke.ucore.scene.ui.layout.Cell;
|
import io.anuke.ucore.scene.ui.layout.Cell;
|
||||||
|
import io.anuke.ucore.util.Bundles;
|
||||||
|
import io.anuke.ucore.util.Strings;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class MenuDialog extends FloatingDialog{
|
public class MenuDialog extends FloatingDialog{
|
||||||
private SaveDialog save = new SaveDialog();
|
private SaveDialog save = new SaveDialog();
|
||||||
@@ -55,6 +60,24 @@ public class MenuDialog extends FloatingDialog{
|
|||||||
}
|
}
|
||||||
|
|
||||||
content().row();
|
content().row();
|
||||||
|
|
||||||
|
content().addButton("$text.hostserver", () -> {
|
||||||
|
Vars.ui.showTextInput("$text.hostserver", "$text.server.port", Vars.port + "", new DigitsOnlyFilter(), text -> {
|
||||||
|
int result = Strings.parseInt(text);
|
||||||
|
if(result == Integer.MIN_VALUE || result >= 65535){
|
||||||
|
Vars.ui.showError("$text.server.invalidport");
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
Vars.network.hostServer(result);
|
||||||
|
}catch (IOException e){
|
||||||
|
Vars.ui.showError(Bundles.format("text.server.error", Strings.parseException(e, false)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}).disabled(b -> Vars.network.isHosting());
|
||||||
|
|
||||||
|
content().row();
|
||||||
|
|
||||||
content().addButton("$text.quit", () -> {
|
content().addButton("$text.quit", () -> {
|
||||||
new ConfirmDialog("$text.confirm", "$text.quit.confirm", () -> {
|
new ConfirmDialog("$text.confirm", "$text.quit.confirm", () -> {
|
||||||
hide();
|
hide();
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ import io.anuke.mindustry.net.Net.SendMode;
|
|||||||
import io.anuke.mindustry.Vars;
|
import io.anuke.mindustry.Vars;
|
||||||
import io.anuke.mindustry.io.PlatformFunction;
|
import io.anuke.mindustry.io.PlatformFunction;
|
||||||
import io.anuke.mindustry.net.Net.ServerProvider;
|
import io.anuke.mindustry.net.Net.ServerProvider;
|
||||||
import io.anuke.mindustry.net.packets.Connect;
|
import io.anuke.mindustry.net.Packets.Connect;
|
||||||
import io.anuke.mindustry.net.packets.Disconnect;
|
import io.anuke.mindustry.net.Packets.Disconnect;
|
||||||
import io.anuke.ucore.scene.ui.TextField;
|
import io.anuke.ucore.scene.ui.TextField;
|
||||||
|
|
||||||
public class DesktopLauncher {
|
public class DesktopLauncher {
|
||||||
@@ -133,7 +133,9 @@ public class DesktopLauncher {
|
|||||||
|
|
||||||
{
|
{
|
||||||
server = new Server();
|
server = new Server();
|
||||||
server.start();
|
Thread thread = new Thread(server, "Kryonet Server");
|
||||||
|
thread.setDaemon(true);
|
||||||
|
thread.start();
|
||||||
server.addListener(new Listener(){
|
server.addListener(new Listener(){
|
||||||
@Override
|
@Override
|
||||||
public void connected (Connection connection) {
|
public void connected (Connection connection) {
|
||||||
@@ -163,6 +165,11 @@ public class DesktopLauncher {
|
|||||||
server.bind(port, port);
|
server.bind(port, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
server.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void send(Object object, SendMode mode) {
|
public void send(Object object, SendMode mode) {
|
||||||
if(mode == SendMode.tcp){
|
if(mode == SendMode.tcp){
|
||||||
|
|||||||
Reference in New Issue
Block a user