davidwengier / Trains.NET

A simple 2D game written in C# with .NET Core. Development is streamed live on https://twitch.tv/davidwengier and past streams available on YouTube at http://bit.ly/trains-net-videos
MIT License
242 stars 31 forks source link

Add multiplayer mode by hosting the Blazor Wasm Site, inside a Blazor Server Site #181

Open lachlanwgordon opened 3 years ago

lachlanwgordon commented 3 years ago

Playing with trains all on your own during a pandemic is lonely. Let's make it multiplayer so we can all work on the same train set at the same time.

I'm experimenting with running BlazorWasm apps inside iframes on Blazor Server sites. This seems a little confusing but could open up some cool scenarios.

For Trains.Net this would mean having a Balzor server site with very little code in it. I will manages a shared set of Entities and terrain that each client renders.

The benefits of an approach like this over pure server or pure wasm blazor are:

This could all be done by having the WASM app call a server but i think that would be a lot more code, and I'm using this as an experiment because I'm interested in adding high performance Skia content to existing server side blazor applications.

Code Changes

To get this running I'll need to add a method to the WASM Site that can be called from JavaScript to pass in the new/modified entities. Most of the saving/loading and serializing is already working, but I'll need to fix issue #180 to get this work.

There will need to be a new project added to the solution to host the Blazor Server App.

POC

As an experiment I've got this map running in a Blazor server website. When ever any user modifies it, all users see the updates instantly. The only difference is that this is a Javascript Map(not WASM, but still running on the client) that gets passed lat/lon coordinates. For trains it will be the existing Trains running in skia that gets entities and terrain passed to it.

Here it is running on my Mac and on my phone over 4g.

2021-11-04 00 14 34

You can try it at https://mapcollaborate.azurewebsites.net/ and view the source at https://github.com/lachlanwgordon/MapCollaborate.

Hosting/Merging?

Are you open to this feature?

Would you want to host it on your own server, or can i have permission to run it on my own Azure App Service?

davidwengier commented 3 years ago

This is definitely on the long list of things that I would love to have, but I don't have any server running anywhere that can do it at the moment, and I'm not sure I ever will (I'm extremely cheap).

I think the form of multi-player would be interesting to talk about though. For something like you're describing, which I read as essentially "everyone sees the same gameboard, and is equally in charge", you could probably get away with just having a SignalR hub running somewhere that relays mouse messages around so that a click on one client is a click on them all. The game itself is deterministic (or should be!) in terms of movement, so the engine doesn't need to know its being multi-playered at all.

What would be more fun would be tracks owned by people, having each person have a section of a larger game board, and allowing trains to ride around across peoples areas etc. but thats a whole different kettle of fish.

lachlanwgordon commented 3 years ago

The Signal R hub is definitely a better solution, but I'm going to do it my way anyway. It may turn out to be useless but it'll be fun.

I'm just planning on putting this on a free tier Azure App service

Yeah, I'm thinking a collaborative approach, a lot like multiple people editing a Google Doc. Too many people in one google doc can be frustrating an chaoticly beautiful, I have to assume it's better with trains.

davidwengier commented 3 years ago

The Signal R hub is definitely a better solution, but I'm going to do it my way anyway.

I see you have been fully converted to the Trains ethos 🙂

ScottHolden commented 3 years ago

What would be more fun would be tracks owned by people, having each person have a section of a larger game board, and allowing trains to ride around across peoples areas etc. but thats a whole different kettle of fish.

I've had thoughts on this, but very much out of scope of this convo 😉

ADefWebserver commented 3 years ago

I feel that it is possible to have a Server Side Blazor version (with WASM Clients) run on the free Azure App Service Tier and still handle dozens of simultaneous users. In fact it would be a good demonstration of how efficient Blazor Server runs.

The Server Side Blazor app would provide Administration functions and allow users to log in using AzureB2C.

I would be happy to work on this if interested.

lachlanwgordon commented 3 years ago

If anyone's interested to see how this is going, I'm working on it in #193.

ADefWebserver commented 3 years ago

@lachlanwgordon I subscribed to that issue. If you need any help or input let me know there. Thanks.

lachlanwgordon commented 3 years ago

Thanks @ADefWebserver, will let you know if I get stuck.