Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions InsightLogParser.Client/Spider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void SetServer(string serverAddress, DateTimeOffset timestamp)
_sessionStart = timestamp;
_serverTracker.Connected(serverAddress);
_messageWriter.ConnectedToServer(serverAddress, timestamp);
_uiCommands.SetConnection(true, serverAddress);
}

public void ConnectingToServer(string serverAddress)
Expand All @@ -78,6 +79,7 @@ public void EndSession(DateTimeOffset timestamp)
_messageWriter.SessionEnded(timestamp, _serverAddress);
_sessionStart = null;
_serverAddress = null;
_uiCommands.SetConnection(false, string.Empty);
}

public async Task SessionBeingDisconnectedAsync()
Expand Down
28 changes: 21 additions & 7 deletions InsightLogParser.Client/Websockets/ISocketUiCommands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using InsightLogParser.Common.PuzzleParser;
using System.Net.WebSockets;
using InsightLogParser.Common.PuzzleParser;
using InsightLogParser.Common.World;

namespace InsightLogParser.Client.Websockets;
Expand All @@ -8,24 +9,37 @@ public interface ISocketUiCommands
/// <summary>
/// Sets the current target
/// </summary>
/// <param name="coordinate">The coordinate of the target</param>
/// <param name="target">The coordinate of the target</param>
/// <param name="puzzle">The target puzzle</param>
/// <param name="routeNumber">The number of the current route node or 0 if not on route</param>
/// <param name="routeLength">The length of the route or 0 if not on route</param>
void SetTarget(Coordinate coordinate, InsightPuzzle puzzle, int routeNumber, int routeLength);
/// <param name="webSocket">The websocket to send to, or everyone if not specified</param>
void SetTarget(Coordinate target, InsightPuzzle puzzle, int routeNumber, int routeLength, WebSocket? webSocket = null);

/// <summary>
/// Updates the player position
/// </summary>
/// <param name="coordinate">The coordinate of the last known player position</param>
void MovePlayer(Coordinate coordinate);
/// <param name="destination">The coordinate of the last known player position</param>
/// <param name="webSocket">The websocket to send to, or everyone if not specified</param>
void MovePlayer(Coordinate destination, WebSocket? webSocket = null);

/// <summary>
/// Updates the connection state
/// </summary>
/// <param name="isConnected">Whether the user is connected to the game</param>
/// <param name="ipAddress">The IP address of the server the user is connected to</param>
/// <param name="webSocket">The websocket to send to, or everyone if not specified</param>
void SetConnection(bool isConnected, string ipAddress, WebSocket? webSocket = null ) { }
}

internal class DummySocketUiCommands : ISocketUiCommands
{
//No-op
public void SetTarget(Coordinate coordinate, InsightPuzzle puzzle, int routeNumber, int routeLength) { }
public void SetTarget(Coordinate target, InsightPuzzle puzzle, int routeNumber, int routeLength, WebSocket? webSocket) { }

//No-op
public void MovePlayer(Coordinate destination, WebSocket? webSocket) { }

//No-op
public void MovePlayer(Coordinate coordinate) { }
public void SetConnection(bool isConnected, string ipAddress, WebSocket? webSocket) { }
}
86 changes: 71 additions & 15 deletions InsightLogParser.Client/Websockets/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ internal class Server : ISocketUiCommands
private ISocketParserCommands _parserCommands;
private Task _listenerTask;

// setTarget data
private Coordinate _target = new Coordinate(0, 0, 0);
private PuzzleType _puzzleType = PuzzleType.Unknown;
private int _puzzleId = 0;
private int _routeNumber = 0;
private int _routeLength = 0;

// movePlayer data
private Coordinate _destination = new Coordinate(0, 0, 0);

// setConnected data
private bool _isConnected = false;
private string _ipAddress = string.Empty;

public Server(CancellationToken forcedCancellationToken)
{
_stopTokenSource = CancellationTokenSource.CreateLinkedTokenSource(forcedCancellationToken);
Expand Down Expand Up @@ -106,6 +120,14 @@ private async Task HandleWebSocketConnection(WebSocket webSocket, CancellationTo
// Add the connection.
_clients.Add(webSocket);

// Initialize the data for the UI.
if (webSocket.State == WebSocketState.Open)
{
SetTarget(_target, new InsightPuzzle { Type = _puzzleType, KrakenId = _puzzleId }, _routeNumber, _routeLength, webSocket);
MovePlayer(_destination, webSocket);
SetConnection(_isConnected, _ipAddress, webSocket);
}

while (webSocket.State == WebSocketState.Open && !cancellationToken.IsCancellationRequested)
{
var buffer = new byte[1024];
Expand Down Expand Up @@ -190,50 +212,84 @@ private async Task HandleWebSocketConnection(WebSocket webSocket, CancellationTo
_clients.TryTake(out _);
}

private async Task SendAsync(object message)
private async Task SendAsync(object message, WebSocket? webSocket = null)
{
// Encode the message.
var messageBuffer = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message));
var messageSegment = new ArraySegment<byte>(messageBuffer);

// Send the message to all clients.
foreach (var client in _clients)
if (webSocket == null)
{
// Send the message to all clients.
foreach (var client in _clients)
{
if (client.State != WebSocketState.Open) continue;
await client.SendAsync(messageSegment, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
else
{
if (client.State != WebSocketState.Open) continue;
await client.SendAsync(messageSegment, WebSocketMessageType.Text, true, CancellationToken.None);
// Send the message to a specific client.
if (webSocket.State == WebSocketState.Open)
{
await webSocket.SendAsync(messageSegment, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
}

#region ISocketUiCommands

public void SetTarget(Coordinate coordinate, InsightPuzzle puzzle, int routeNumber, int routeLength)
public void SetTarget(Coordinate target, InsightPuzzle puzzle, int routeNumber, int routeLength, WebSocket? websocket)
{
if (target.X == 0 && target.Y == 0 && target.Z == 0) return;

_ = SendAsync(new {
type = "setTarget",
target = new {
coordinate.X,
coordinate.Y,
coordinate.Z
target.X,
target.Y,
target.Z
},
puzzleType = (int)puzzle.Type,
puzzleId = puzzle.KrakenId,
routeNumber = routeNumber,
routeLength = routeLength,
});
}, websocket);

_target = target;
_puzzleType = puzzle.Type;
_puzzleId = puzzle.KrakenId;
_routeNumber = routeNumber;
_routeLength = routeLength;
}

public void MovePlayer(Coordinate coordinate)
public void MovePlayer(Coordinate destination, WebSocket? websocket)
{
if (destination.X == 0 && destination.Y == 0 && destination.Z == 0) return;

_ = SendAsync(new {
type = "movePlayer",
destination = new {
coordinate.X,
coordinate.Y,
coordinate.Z
destination.X,
destination.Y,
destination.Z
}
});
}, websocket);

_destination = destination;
}

public void SetConnection(bool isConnected, string ipAddress, WebSocket? websocket)
{
_ = SendAsync(new {
type = "setConnection",
isConnected = isConnected,
ipAddress = ipAddress
}, websocket);

_ipAddress = ipAddress;
_isConnected = isConnected;
}

#endregion
}
104 changes: 67 additions & 37 deletions InsightLogParser.UI/Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading