🔨 beaucoup de modifcations et changement de strategie

ServerDeployement
Bruno DA COSTA CUNHA 2 years ago
parent 89f8b31cc0
commit 533ddf1f69

@ -13,7 +13,8 @@ class Program
static void StartClient()
{
IPEndPoint serverEndPoint = new IPEndPoint(Dns.GetHostAddresses("hulivet.fr").FirstOrDefault(), 3131);
//IPEndPoint serverEndPoint = new IPEndPoint(Dns.GetHostAddresses("hulivet.fr").FirstOrDefault(), 3131);
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3131);
UdpClient client = new UdpClient();
// Send connection message to server
@ -31,7 +32,7 @@ class Program
string message = "";
while (message != "exit")
{
serverEndPoint = new IPEndPoint(Dns.GetHostAddresses("hulivet.fr").FirstOrDefault(), int.Parse(receivedPort));
serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse(receivedPort));
Console.Write("Enter message to send (or 'exit' to quit): ");
message = Console.ReadLine();

@ -8,7 +8,6 @@ namespace DataBase.Entity
public string playerId { get; set; }
public string name { get; set; }
public int nbBallTouchTotal { get; set; }
public bool ready { get; set; } = false;
public int timePlayed { get; set; }
}
}

@ -8,6 +8,7 @@ using DataBase.Entity;
using Shared.DTO;
using System.Text.Json;
using Server;
using System.ComponentModel;
public class PongServer
{
@ -38,57 +39,108 @@ public class PongServer
string fileJson = Encoding.UTF8.GetString(receivedData);
ObjectTransfert<Player> data = JsonSerializer.Deserialize<ObjectTransfert<Player>>(fileJson);
if (data.Informations.Action == Shared.DTO.Action.Create)
if (data.Informations.Action == Shared.DTO.Action.Host)
{
Room room = new Room(data.Data.playerId);
room.playerHost = data.Data;
room.nbPlayer++;
Console.WriteLine("New connection from " + remoteEndPoint.ToString());
// Assign a unique port to the client
IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, nextPort);
room.Port = nextPort;
nextPort++;
UdpClient clientSocket = new UdpClient(clientEndPoint);
clients[remoteEndPoint] = clientSocket;
// Send connection message to client
byte[] connectionData = Encoding.ASCII.GetBytes(room.Id);
serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint);
// Start thread to receive data from client
Thread receiveThread = new Thread(() => Room.ReceiveMessages(clientSocket, data.Data));
receiveThread.Start();
Host(data, remoteEndPoint, serverSocket, false);
}
if (data.Informations.Action == Shared.DTO.Action.Join)
{
Console.WriteLine("New connection from " + remoteEndPoint.ToString());
var choisenRoom = rooms.FirstOrDefault(room => room.Key == data.Informations.IdRoom);
if(choisenRoom.Value != default && choisenRoom.Value.Availaible)
{
Join(data, remoteEndPoint, serverSocket, choisenRoom.Value);
}
}
if (data.Informations.Action == Shared.DTO.Action.Connect) // Join = rejoindre un room , et Host ça va juste créer un room
{
var choisenRoom = rooms.FirstOrDefault(room => room.Value.Availaible);
if (choisenRoom.Value != default )
{
Join(data, remoteEndPoint, serverSocket, choisenRoom.Value);
}
else
{
Host(data, remoteEndPoint, serverSocket, true);
}
// Assign a unique port to the client
IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, nextPort++);
UdpClient clientSocket = new UdpClient(clientEndPoint);
clients[remoteEndPoint] = clientSocket;
}
// Send connection message to client
string connectionMessage = clientEndPoint.Port.ToString();
byte[] connectionData = Encoding.ASCII.GetBytes(connectionMessage);
serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint);
}
// Start thread to receive data from client
Thread receiveThread = new Thread(() => ReceiveMessages(clientSocket));
receiveThread.Start();
}
}
private void Host(ObjectTransfert<Player> data, IPEndPoint remoteEndPoint, UdpClient serverSocket, bool availaible)
{
Room room = new Room(data.Data.playerId, availaible);
room.playerHost = data.Data;
room.nbPlayer++;
room.PropertyChanged += OnReadyChanged;
Console.WriteLine("New connection from " + remoteEndPoint.ToString());
// Assign a unique port to the client
IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, nextPort);
room.Port = nextPort;
nextPort++;
UdpClient clientSocket = new UdpClient(clientEndPoint);
clients[remoteEndPoint] = clientSocket;
// Send connection message to client
byte[] connectionData = Encoding.ASCII.GetBytes(room.Id);
serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint);
// Start thread to receive data from client
Thread receiveThread = new Thread(() => room.ReceiveMessages(clientSocket, data.Data));
receiveThread.Start();
}
private void Join(ObjectTransfert<Player> data, IPEndPoint remoteEndPoint, UdpClient serverSocket, Room room)
{
Console.WriteLine("New connection from " + remoteEndPoint.ToString());
// Assign a unique port to the client
IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, nextPort++);
UdpClient clientSocket = new UdpClient(clientEndPoint);
clients[remoteEndPoint] = clientSocket;
// Send connection message to client
string connectionMessage = clientEndPoint.Port.ToString();
byte[] connectionData = Encoding.ASCII.GetBytes(connectionMessage);
serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint);
// Start thread to receive data from client
Thread receiveThread = new Thread(() => room.ReceiveMessages(clientSocket, data.Data));
receiveThread.Start();
}
private void OnReadyChanged(object sender, PropertyChangedEventArgs e)
{
Room nbPlayer = sender as Room;
bool maxPlayer = nbPlayer.maxPlayer;
if (room.MaxPlayers.Count == 2)
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, listenPort);
UdpClient serverSocket = new UdpClient(serverEndPoint);
if (maxPlayer)
{
while (true)
{
Console.WriteLine("Starting game...");
// Call a function to start the game
//Faut finir ça mnt
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] receivedData = serverSocket.Receive(ref remoteEndPoint);
}
}
}
}

@ -7,28 +7,54 @@ using System.Net;
using System.Text;
using System.Threading.Tasks;
using Shared.DTO;
using System.ComponentModel;
namespace Server
{
public class Room
public class Room : INotifyPropertyChanged
{
public Room(string id)
public Room(string id, bool availaible)
{
Id = id;
Availaible = availaible;
}
public bool Availaible { get; set; }
public event PropertyChangedEventHandler? PropertyChanged;
public string Id { get; set; }
public Player playerHost;
public Player playerJoin;
public int Port { get; set; }
public int nbPlayer = 0;
public int Port { get; set; }
public bool maxPlayer
{
get { return nbPlayer >= 2; }
set
{
if (nbPlayer >= 2)
{
maxPlayer = true;
NotifyPropertyChanged("Ready");
}
}
}
protected void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public static void ReceiveMessages(UdpClient clientSocket,Player player)
public void ReceiveMessages(UdpClient clientSocket,Player player)
{
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
@ -37,10 +63,7 @@ namespace Server
byte[] receivedData = clientSocket.Receive(ref remoteEndPoint);
string receivedMessage = Encoding.ASCII.GetString(receivedData);
Console.WriteLine("Received from " + remoteEndPoint.ToString() + ": " + receivedMessage);
if (receivedMessage.ToUpper().Equals("READY"))
{
player.ready = true;
}
while (true) //score
{
//cordinate paddel

@ -8,7 +8,7 @@ namespace Shared.DTO
{
public enum Action
{
Create,
Host,
Connect,
Join,
End

@ -9,5 +9,6 @@ namespace Shared.DTO
public class Informations
{
public Action Action { get; set; }
public string? IdRoom { get; set; }
}
}

Loading…
Cancel
Save