Trying to make a server

This commit is contained in:
2022-10-23 20:58:01 +02:00
parent 9d04246d75
commit 7a60703eb8
9 changed files with 125 additions and 16 deletions
+36
View File
@@ -0,0 +1,36 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "server", "server\server.csproj", "{AB011134-CF45-4362-B3AC-5F11D9C39B80}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "client", "client\client.csproj", "{5AE7A447-37B1-45D1-91D3-664049C1645F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shared", "shared\shared.csproj", "{EB920FE1-25C7-4AB2-8DC2-A712904E9ABA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AB011134-CF45-4362-B3AC-5F11D9C39B80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB011134-CF45-4362-B3AC-5F11D9C39B80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB011134-CF45-4362-B3AC-5F11D9C39B80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB011134-CF45-4362-B3AC-5F11D9C39B80}.Release|Any CPU.Build.0 = Release|Any CPU
{5AE7A447-37B1-45D1-91D3-664049C1645F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5AE7A447-37B1-45D1-91D3-664049C1645F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5AE7A447-37B1-45D1-91D3-664049C1645F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5AE7A447-37B1-45D1-91D3-664049C1645F}.Release|Any CPU.Build.0 = Release|Any CPU
{EB920FE1-25C7-4AB2-8DC2-A712904E9ABA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB920FE1-25C7-4AB2-8DC2-A712904E9ABA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB920FE1-25C7-4AB2-8DC2-A712904E9ABA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB920FE1-25C7-4AB2-8DC2-A712904E9ABA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {431509A6-DE8B-46C1-AC0D-5FF9344BD6FB}
EndGlobalSection
EndGlobal
+8 -6
View File
@@ -61,8 +61,10 @@ namespace client
// Start | Stop ball moving // Start | Stop ball moving
public Ball StartMoving() public Ball StartMoving()
{ {
MoveX = random.Next((int)-Speed, (int)Speed); /*MoveX = random.Next((int)-Speed, (int)Speed);
MoveY = random.Next((int)-Speed, (int)Speed); MoveY = 1000f;*/
MoveX = Speed;
MoveY = Speed;
CanMove = true; CanMove = true;
return this; return this;
} }
@@ -71,7 +73,7 @@ namespace client
{ {
if (CanMove) if (CanMove)
{ {
// X /*// X
if (Position.X < 0) if (Position.X < 0)
{ {
MoveX -= Speed; MoveX -= Speed;
@@ -88,10 +90,10 @@ namespace client
else else
{ {
MoveY += Speed; MoveY += Speed;
} }*/
Position.X += MoveX; Position.X += MoveX + Speed;
Position.Y += MoveY; Position.Y += MoveY + Speed;
// Collisions // Collisions
if (Position.Y < Radius * 2 && MoveY < 0) if (Position.Y < Radius * 2 && MoveY < 0)
+28 -5
View File
@@ -1,9 +1,10 @@
using LiteNetLib; using System;
using System.Collections.Generic;
using LiteNetLib;
using LiteNetLib.Utils; using LiteNetLib.Utils;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System; using shared;
using System.Collections.Generic;
namespace client namespace client
{ {
@@ -13,6 +14,7 @@ namespace client
EventBasedNetListener listener = new(); EventBasedNetListener listener = new();
NetManager client; NetManager client;
NetPeer server; NetPeer server;
NetPacketProcessor processor = new();
// Game instances // Game instances
private Player pl1; private Player pl1;
@@ -57,6 +59,7 @@ namespace client
// TODO: Replace by a error message :) // TODO: Replace by a error message :)
Exit(); Exit();
} }
processor.SubscribeReusable<Assignation>(AssignationHandler);
// Window // Window
Window.Title = "Pong"; Window.Title = "Pong";
@@ -70,7 +73,7 @@ namespace client
15, 15,
100, 100,
100f 100f
).SetControllable(true); );
pl2 = new Player( pl2 = new Player(
new Vector2(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50), new Vector2(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50),
@@ -83,7 +86,7 @@ namespace client
ball = new Ball( ball = new Ball(
new Vector2(_graphics.PreferredBackBufferWidth / 2 - 5, _graphics.PreferredBackBufferHeight / 2 - 5), new Vector2(_graphics.PreferredBackBufferWidth / 2 - 5, _graphics.PreferredBackBufferHeight / 2 - 5),
10, 10,
0.2f 1f
); );
// KILLZONE // KILLZONE
@@ -143,5 +146,25 @@ namespace client
_spriteBatch.End(); _spriteBatch.End();
base.Draw(gameTime); base.Draw(gameTime);
} }
private void AssignationHandler(Assignation assignation)
{
if (assignation != null)
{
if (assignation.controller == 0) {
pl1.SetControllable(true);
pl2.SetControllable(false);
}
else
{
pl1.SetControllable(false);
pl2.SetControllable(true);
}
}
else
{
Console.Error.WriteLine("Packet malformed!");
}
}
} }
} }
+2 -2
View File
@@ -50,14 +50,14 @@ namespace client
{ {
if (ball.Position.X < Position.X + Width && ball.CanMove) if (ball.Position.X < Position.X + Width && ball.CanMove)
{ {
ball.StopMoving(); //ball.StopMoving();
} }
} }
else else
{ {
if (ball.Position.X > Position.X && ball.CanMove) if (ball.Position.X > Position.X && ball.CanMove)
{ {
ball.StopMoving(); //ball.StopMoving();
} }
} }
} }
+4
View File
@@ -25,6 +25,10 @@
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" /> <PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
<PackageReference Include="MonoGame.UI.Forms" Version="1.0.1" /> <PackageReference Include="MonoGame.UI.Forms" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\shared\shared.csproj" />
</ItemGroup> </ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="Restore"> <Target Name="RestoreDotnetTools" BeforeTargets="Restore">
<Message Text="Restoring dotnet tools" Importance="High" /> <Message Text="Restoring dotnet tools" Importance="High" />
+25 -3
View File
@@ -1,5 +1,6 @@
using LiteNetLib; using LiteNetLib;
using LiteNetLib.Utils; using LiteNetLib.Utils;
using shared;
namespace Server namespace Server
{ {
@@ -7,10 +8,15 @@ namespace Server
{ {
static EventBasedNetListener listener = new(); static EventBasedNetListener listener = new();
static NetManager? server; static NetManager? server;
static NetPacketProcessor processor = new();
static NetSerializer serializer = new();
static string? port; static string? port;
static string? pswd; static string? pswd;
static string gameState = "Idle"; // Idle || Playing || Paused || Ended
static List<NetPeer> players;
public static void Main(string[] args) public static void Main(string[] args)
{ {
server = new NetManager(listener); server = new NetManager(listener);
@@ -28,6 +34,8 @@ namespace Server
return; return;
} }
serializer.Register<Assignation>();
listener.ConnectionRequestEvent += request => listener.ConnectionRequestEvent += request =>
{ {
@@ -38,19 +46,33 @@ namespace Server
listener.PeerConnectedEvent += peer => listener.PeerConnectedEvent += peer =>
{ {
Console.WriteLine("Connection accepted for: {0}", peer.EndPoint); Console.WriteLine("Connection accepted for: {0}", peer.EndPoint);
/* NetDataWriter writer = new(); if (peer != null)
writer.Put("Hello client!"); {
peer.Send(writer, DeliveryMethod.ReliableOrdered); */ players.Add(peer);
}
}; };
listener.PeerDisconnectedEvent += (peer, infos) => listener.PeerDisconnectedEvent += (peer, infos) =>
{ {
Console.WriteLine($"{peer.EndPoint} has left.\nError code: {infos.SocketErrorCode}\nReason: {infos.Reason}"); Console.WriteLine($"{peer.EndPoint} has left.\nError code: {infos.SocketErrorCode}\nReason: {infos.Reason}");
players.Remove(peer);
}; };
while (!Console.KeyAvailable) while (!Console.KeyAvailable)
{ {
server.PollEvents(); server.PollEvents();
if (server.GetPeersCount(ConnectionState.Connected) >= 2)
{
// Differents game states
if (gameState == "Idle")
{
Assignation packet = new() { controller = 2, ballX = 0, ballY = 0 };
processor.Send(players[0], packet, DeliveryMethod.ReliableOrdered);
processor.Send(players[1], packet, DeliveryMethod.ReliableOrdered);
Console.WriteLine("Assignation sended");
gameState = "Playing";
}
}
Thread.Sleep(15); Thread.Sleep(15);
} }
+4
View File
@@ -12,4 +12,8 @@
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.1" /> <PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.1" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\shared\shared.csproj" />
</ItemGroup>
</Project> </Project>
+9
View File
@@ -0,0 +1,9 @@
namespace shared
{
public class Assignation
{
public int controller { get; set; } // 0 || 1
public float ballX { get; set; }
public float ballY { get; set; }
}
}
+9
View File
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>