Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT!] Multiplayer with Blazor Server #193

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
35 changes: 35 additions & 0 deletions .github/workflows/BlazingServerTrains.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Build and deploy .NET Core application to Web App BlazingServerTrains
env:
AZURE_WEBAPP_NAME: BlazingServerTrains
AZURE_WEBAPP_PACKAGE_PATH: src\BlazingServerTrains\published
CONFIGURATION: Release
DOTNET_CORE_VERSION: 6.0.x
WORKING_DIRECTORY: src\BlazingServerTrains
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ env.DOTNET_CORE_VERSION }}
- name: Restore
run: dotnet restore "${{ env.WORKING_DIRECTORY }}"
- name: Build
run: dotnet build "${{ env.WORKING_DIRECTORY }}" --configuration ${{ env.CONFIGURATION }} --no-restore
- name: Test
run: dotnet test "${{ env.WORKING_DIRECTORY }}" --no-build
- name: Publish
run: dotnet publish "${{ env.WORKING_DIRECTORY }}" --configuration ${{ env.CONFIGURATION }} --no-build --output "${{ env.AZURE_WEBAPP_PACKAGE_PATH }}"
- name: Deploy to Azure WebApp
uses: azure/webapps-deploy@v2
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.BLAZINGSERVERTRAINS_FFFF }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
- name: Publish Artifacts
uses: actions/[email protected]
with:
name: webapp
path: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
7 changes: 7 additions & 0 deletions Trains.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinTrains", "src\WinTrains\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazingTrains", "src\BlazingTrains\BlazingTrains.csproj", "{79D851AC-3713-4582-A20A-5E6C24FEAE6B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazingServerTrains", "src\BlazingServerTrains\BlazingServerTrains.csproj", "{EAECA630-1A76-476F-B6B9-66ADB1BBFF28}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -95,6 +97,10 @@ Global
{79D851AC-3713-4582-A20A-5E6C24FEAE6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79D851AC-3713-4582-A20A-5E6C24FEAE6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79D851AC-3713-4582-A20A-5E6C24FEAE6B}.Release|Any CPU.Build.0 = Release|Any CPU
{EAECA630-1A76-476F-B6B9-66ADB1BBFF28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EAECA630-1A76-476F-B6B9-66ADB1BBFF28}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EAECA630-1A76-476F-B6B9-66ADB1BBFF28}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EAECA630-1A76-476F-B6B9-66ADB1BBFF28}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -111,6 +117,7 @@ Global
{10E5BE95-85C2-456C-A32B-A77E7EB702A1} = {6D7A257F-24F9-43A2-870A-14DFCA32DDD2}
{894082D5-C407-476F-9EAA-B412411E6E1F} = {77E57704-D3D8-4228-A195-1F90FD62539A}
{79D851AC-3713-4582-A20A-5E6C24FEAE6B} = {77E57704-D3D8-4228-A195-1F90FD62539A}
{EAECA630-1A76-476F-B6B9-66ADB1BBFF28} = {77E57704-D3D8-4228-A195-1F90FD62539A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8834C621-39E4-4AD6-85D0-97F46ADC88D6}
Expand Down
12 changes: 12 additions & 0 deletions src/BlazingServerTrains/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
22 changes: 22 additions & 0 deletions src/BlazingServerTrains/BlazingServerTrains.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.0" />
<PackageReference Include="Refractored.MvvmHelpers" Version="1.6.2" />

<PackageReference Include="SkiaSharp" />
<PackageReference Include="SkiaSharp.Views.Blazor" />

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BlazingTrains\BlazingTrains.csproj" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions src/BlazingServerTrains/Data/SharedObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using MvvmHelpers;

namespace BlazingServerTrains.Data;

public class SharedObject : ObservableObject
{
//public event PropertyChangedEventHandler? PropertyChanged;
//public
//public string Key { get; init; }
//public string Value
//{
// get
// {
// return Items.FirstOrDefault(x => x.key == this.Key)?.value;
// }
// set
// {
// if(Items.Contains()
// }
//}

//public static Con<(string key, string value)> Items = new();

}
13 changes: 13 additions & 0 deletions src/BlazingServerTrains/Data/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace BlazingServerTrains.Data
{
public class WeatherForecast
{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/BlazingServerTrains/Data/WeatherForecastService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace BlazingServerTrains.Data
{
public class WeatherForecastService
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
{
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray());
}
}
}
18 changes: 18 additions & 0 deletions src/BlazingServerTrains/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
private static int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
42 changes: 42 additions & 0 deletions src/BlazingServerTrains/Pages/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@page
@model BlazingServerTrains.Pages.ErrorModel

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Error</title>
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="~/css/site.css" rel="stylesheet" asp-append-version="true" />
</head>

<body>
<div class="main">
<div class="content px-4">
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
</div>
</div>
</body>

</html>
27 changes: 27 additions & 0 deletions src/BlazingServerTrains/Pages/Error.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Diagnostics;

namespace BlazingServerTrains.Pages
{
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
}
21 changes: 21 additions & 0 deletions src/BlazingServerTrains/Pages/FetchData.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@page "/fetchdata"

<PageTitle>Weather forecast</PageTitle>

@using BlazingServerTrains.Data
@inject WeatherForecastService ForecastService

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from a service.</p>
<iframe src="https://localhost:7043/" width="500" height="500"/>
<iframe src="https://localhost:7043/" width="500" height="500"/>

@code {
private WeatherForecast[]? forecasts;

protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
}
21 changes: 21 additions & 0 deletions src/BlazingServerTrains/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@page "/"

<PageTitle>Server Trains.Net</PageTitle>

<iframe id="wasmFrame" src="/index.html" width="500" height="500" />

<div style="display: flex; flex-direction: column; background-color: #BBBBBB;">
<h3>This area is rendered server side</h3>
<h4>These feilds share values, you can edit them on different devices, like a lo-fi google doc </h4>
<label>TerrainMap</label>
<input type="number" @oninput="@(e => TerrainMap = e?.Value?.ToString() ?? string.Empty)" value="@TerrainMap" />

<label>ILayout</label>
<input @oninput="@(e => ILayout = e?.Value?.ToString() ?? string.Empty)" value="@ILayout" />

</div>


<Counter></Counter>


46 changes: 46 additions & 0 deletions src/BlazingServerTrains/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//This file runs on the server and contains an iframe that runs the whole BlazingTrains App on the client
//This file has one instance per user


using BlazingServerTrains.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;

namespace BlazingServerTrains.Pages;

public partial class Index
{
[Inject]
public SharedMemory SharedMemory { get; set; }

[Inject]
public IJSRuntime JSRuntime { get; set; }

public string TerrainMap
{
get => this.SharedMemory!.TerrainMap;
set => this.SharedMemory!.TerrainMap = value;
}

public string ILayout
{
get => this.SharedMemory!.ILayout;
set => this.SharedMemory!.ILayout = value;
}

protected override void OnInitialized()
{
base.OnInitialized();
this.SharedMemory!.PropertyChanged += Index_PropertyChanged;
}

private async void Index_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
await InvokeAsync(StateHasChanged);
var prop = typeof(SharedMemory).GetProperty(e.PropertyName);
var val = prop?.GetValue(this.SharedMemory);
// _ = this.JSRuntime.InvokeVoidAsync("MessageOuterJS", $"TO Outer PropertyChanged - {e.PropertyName}: {val}");
// _ = this.JSRuntime.InvokeVoidAsync("MessageInnerJS", $"TO Inner PropertyChanged - {e.PropertyName}: {val}");
_ = this.JSRuntime.InvokeVoidAsync("UpdateProperty", e.PropertyName, val);
}
}
8 changes: 8 additions & 0 deletions src/BlazingServerTrains/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page "/"
@namespace BlazingServerTrains.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = "_Layout";
}

<component type="typeof(App)" render-mode="ServerPrerendered" />
33 changes: 33 additions & 0 deletions src/BlazingServerTrains/Pages/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@using Microsoft.AspNetCore.Components.Web
@namespace BlazingServerTrains.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<link href="BlazingServerTrains.styles.css" rel="stylesheet" />
<script src="scripts/Interop.js"></script>
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
</head>
<body>
@RenderBody()

<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.server.js"></script>
</body>
</html>
Loading