Skip to content

Commit

Permalink
Makes connections a bit more smooth, adds port selection with proper …
Browse files Browse the repository at this point in the history
…IP parsing
  • Loading branch information
MarkSuckerberg committed Nov 2, 2023
1 parent 7c9926f commit fbadb57
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .run/Blocktest (Local Multiplayer).run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<configuration default="false" name="Blocktest (Local Multiplayer)" type="DotNetProject" factoryName=".NET Project"
singleton="false">
<option name="EXE_PATH" value="$PROJECT_DIR$/Blocktest/bin/Debug/net7.0/Blocktest.exe" />
<option name="PROGRAM_PARAMETERS" value="connect 127.0.0.1" />
<option name="PROGRAM_PARAMETERS" value="connect 127.0.0.1:9050"/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/Blocktest/bin/Debug/net7.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
Expand Down
2 changes: 1 addition & 1 deletion .run/DedicatedServer.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="DedicatedServer" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/DedicatedServer/bin/Debug/net7.0/DedicatedServer" />
<option name="EXE_PATH" value="$PROJECT_DIR$/DedicatedServer/bin/Debug/net7.0/DedicatedServer.exe"/>
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/DedicatedServer/bin/Debug/net7.0" />
<option name="PASS_PARENT_ENVS" value="1" />
Expand Down
5 changes: 3 additions & 2 deletions Blocktest/BlocktestGame.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net;
using Blocktest.Block_System;
using Blocktest.Scenes;
using Myra;
Expand All @@ -7,12 +8,12 @@ namespace Blocktest;
/// <inheritdoc />
public sealed class BlocktestGame : Game {
private readonly bool _connect;
private readonly string? _ip;
private readonly IPEndPoint? _ip;
private IScene? _currentScene;
private GraphicsDeviceManager _graphics;

/// <inheritdoc />
public BlocktestGame(string? newIp = null) {
public BlocktestGame(IPEndPoint? newIp = null) {
_connect = newIp != null;
_ip = newIp;
_graphics = new GraphicsDeviceManager(this);
Expand Down
15 changes: 12 additions & 3 deletions Blocktest/Code/Networking/Client.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Net;
using Blocktest.Rendering;
using Blocktest.Scenes;
using Blocktest.UI;
using LiteNetLib;
using LiteNetLib.Utils;
using Shared.Code;
Expand All @@ -16,9 +19,12 @@ public sealed class Client {
private readonly WorldState _worldState;
public TickBuffer ClientTickBuffer;

public Client(WorldState worldState, Camera camera) {
private readonly BlocktestGame _game;

public Client(WorldState worldState, Camera camera, BlocktestGame game) {
_worldState = worldState;
_camera = camera;
_game = game;
ClientTickBuffer = new TickBuffer(0, _worldState);
_listener = new EventBasedNetListener();
_manager = new NetManager(_listener);
Expand All @@ -30,8 +36,8 @@ public Client(WorldState worldState, Camera camera) {

public NetPeer? Server { get; private set; }

public void Start(string ip, int port, string key) {
_manager.Connect(ip, port, key);
public void Start(IPEndPoint ipEndPoint, string key) {
_manager.Connect(ipEndPoint, key);
}

public void Stop() {
Expand All @@ -54,6 +60,9 @@ private void PeerConnected(NetPeer peer) {

private void PeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) {
Console.WriteLine("Disconnected from server");

_game.SetScene(new MainMenuScene(_game,
new DialogueWindow("Disconnected from server.", $"{disconnectInfo.Reason}")));
}

/// <summary>
Expand Down
7 changes: 4 additions & 3 deletions Blocktest/Code/Scenes/GameScene.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Net;
using Blocktest.Block_System;
using Blocktest.Misc;
using Blocktest.Networking;
Expand Down Expand Up @@ -28,7 +29,7 @@ public sealed class GameScene : IScene {

private KeyboardState _previousKeyboardState;

public GameScene(BlocktestGame game, bool doConnect, string? ip) {
public GameScene(BlocktestGame game, bool doConnect, IPEndPoint? ip) {
_connect = doConnect;
_spriteBatch = new SpriteBatch(game.GraphicsDevice);
_game = game;
Expand All @@ -37,12 +38,12 @@ public GameScene(BlocktestGame game, bool doConnect, string? ip) {

_backgroundTilemapSprites = new RenderableTilemap(_worldState.Foreground, _camera);
_foregroundTilemapSprites = new RenderableTilemap(_worldState.Background, _camera);
_networkingClient = new Client(_worldState, _camera);
_networkingClient = new Client(_worldState, _camera, game);

_blockStrings = BlockManagerShared.AllBlocks.Keys.ToArray();

if (_connect && ip != null) {
_networkingClient.Start(ip, 9050, "testKey");
_networkingClient.Start(ip, "testKey");
return;
}

Expand Down
17 changes: 9 additions & 8 deletions Blocktest/Code/Scenes/MainMenuScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ namespace Blocktest.Scenes;

public class MainMenuScene : IScene {
private readonly Desktop _desktop;
private readonly VerticalStackPanel _mainMenu;

public MainMenuScene(BlocktestGame game) {
_mainMenu = new VerticalStackPanel {
public MainMenuScene(BlocktestGame game, Window? modal = null) {
VerticalStackPanel mainMenu = new() {
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
BorderThickness = new Thickness(1),
Expand All @@ -23,33 +22,35 @@ public MainMenuScene(BlocktestGame game) {
VerticalAlignment = VerticalAlignment.Center,
Padding = new Thickness(8)
};
_mainMenu.Widgets.Add(titleLabel);
mainMenu.Widgets.Add(titleLabel);

TextButton newGameButton = new() {
Text = "New Game",
HorizontalAlignment = HorizontalAlignment.Stretch,
Padding = new Thickness(5)
};
newGameButton.Click += (_, _) => { game.SetScene(new GameScene(game, false, null)); };
_mainMenu.Widgets.Add(newGameButton);
mainMenu.Widgets.Add(newGameButton);

TextButton connectButton = new() {
Text = "Connect",
HorizontalAlignment = HorizontalAlignment.Stretch,
Padding = new Thickness(5)
};
connectButton.Click += (_, _) => { new ConnectionWindow(game).ShowModal(_desktop); };
_mainMenu.Widgets.Add(connectButton);
mainMenu.Widgets.Add(connectButton);

TextButton exitButton = new() {
Text = "Exit",
HorizontalAlignment = HorizontalAlignment.Stretch,
Padding = new Thickness(5)
};
exitButton.Click += (_, _) => { game.Exit(); };
_mainMenu.Widgets.Add(exitButton);
mainMenu.Widgets.Add(exitButton);

_desktop = new Desktop { Root = _mainMenu };
_desktop = new Desktop { Root = mainMenu };

modal?.ShowModal(_desktop);
}

public void Update(GameTime gameTime) { }
Expand Down
16 changes: 12 additions & 4 deletions Blocktest/Code/UI/ConnectionWindow.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net;
using Blocktest.Scenes;
using Myra.Graphics2D;
using Myra.Graphics2D.UI;
Expand All @@ -20,13 +21,13 @@ public ConnectionWindow(BlocktestGame game) {
};
windowGrid.Widgets.Add(label1);

TextBox textBox = new() {
Text = "127.0.0.1",
TextBox ipBox = new() {
Text = "127.0.0.1:9050",
Padding = new Thickness(5),
GridColumn = 1,
GridRow = 0
};
windowGrid.Widgets.Add(textBox);
windowGrid.Widgets.Add(ipBox);

TextButton button = new() {
Text = "Connect",
Expand All @@ -37,9 +38,16 @@ public ConnectionWindow(BlocktestGame game) {
HorizontalAlignment = HorizontalAlignment.Stretch
};

button.Click += (_, _) => { game.SetScene(new GameScene(game, true, textBox.Text)); };
button.Click += (_, _) => {
if (!IPEndPoint.TryParse(ipBox.Text, out IPEndPoint? ip)) {
new DialogueWindow("Connect to server", "Invalid IP address.").ShowModal(Desktop);
return;
}
game.SetScene(new GameScene(game, true, ip));
};
windowGrid.Widgets.Add(button);

Title = "Connect to server";
Content = windowGrid;
}
}
4 changes: 2 additions & 2 deletions Blocktest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public static void Main(string[] args) {
Console.WriteLine("Invalid argument.");
return;
}
if (!IPAddress.TryParse(args[1], out IPAddress? _)) {
if (!IPEndPoint.TryParse(args[1], out IPEndPoint? ip)) {
Console.WriteLine("Invalid IP address.");
return;
}
using BlocktestGame game = new(args[1]);
using BlocktestGame game = new(ip);
game.Run();
} else {
using BlocktestGame game = new();
Expand Down

0 comments on commit fbadb57

Please sign in to comment.