Skip to content

Commit

Permalink
Merge pull request #9 from MichaelEpicA/development
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
CRD716 authored Aug 9, 2021
2 parents 4a89171 + 6575ec7 commit fbc190f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
39 changes: 33 additions & 6 deletions VM Battle Royale Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static void AcceptCallBack(IAsyncResult ar)
private static void RecieveCallBack(IAsyncResult ar)
{
//How the server recieves things.
int recieved = new int();
int recieved = new int();
Socket socket = (Socket)ar.AsyncState;
try
{
Expand Down Expand Up @@ -114,7 +114,7 @@ private static void RecieveCallBack(IAsyncResult ar)
//This is sent when the vm monitor starts up.
if (gameState == GameState.Start)
{
IPEndPoint end = (IPEndPoint)socket.LocalEndPoint;
IPEndPoint end = (IPEndPoint)socket.RemoteEndPoint;

VMAndPass convert = new VMAndPass
{
Expand All @@ -124,7 +124,14 @@ private static void RecieveCallBack(IAsyncResult ar)
};
//Check if the client disconnected. (please someone make this better holy crap)
Task.Run(() => CheckIfDisconnected(socket));
vmandpass.Add(end.Address, convert);
try
{
vmandpass.Add(end.Address, convert);
}
catch
{

}
}
else if (gameState == GameState.Play)
{
Expand Down Expand Up @@ -276,7 +283,7 @@ private static void RecieveCallBack(IAsyncResult ar)
if (command == "startgame")
{
//Starts the game, pretty obvious.
if (usernames.Count == vmandpass.Count && usernames.Count > 2 && gameState == GameState.Start && usernames.ElementAt(0).Value == socket)
if (usernames.Count == vmandpass.Count && usernames.Count >= 2 && gameState == GameState.Start && usernames.ElementAt(0).Value == socket)
{
gameState = GameState.Grace;
foreach (KeyValuePair<string, Socket> kvp in usernames)
Expand Down Expand Up @@ -441,15 +448,35 @@ private static void RecieveCallBack(IAsyncResult ar)
//Disconnect function, is used a lot, if you wanna change what the server does when a client disconnects, use this.
public static void Disconnect(Socket socket)
{
bool error = false;
foreach (KeyValuePair<string, Socket> kvp in usernames)
{
if (kvp.Value == socket)
{
usernames.Remove(kvp.Key);
}
}
socket.Disconnect(false);
_clientSockets.Remove(socket);
try
{
IPEndPoint end = (IPEndPoint)socket.RemoteEndPoint;
vmandpass[end.Address].Disconnected = true;
error = false;
} catch
{
error = true;
}
if(error)
{
socket.Disconnect(false);
_clientSockets.Remove(socket);
} else
{
IPEndPoint end = (IPEndPoint)socket.RemoteEndPoint;
vmandpass.Remove(end.Address);
socket.Disconnect(false);
_clientSockets.Remove(socket);
}

}

public static void CheckIfDisconnected(Socket socket)
Expand Down
4 changes: 1 addition & 3 deletions VM Battle Royale VM/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,14 @@ static void SetupPassword() {

public static string RandomString(int length = 7)
{
const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@";
Random rand = new Random();

string generatedString = "";

using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
{
byte[] randomBytes = new byte[sizeof(int)];
crypto.GetBytes(randomBytes);
generatedString = "" + BitConverter.ToInt32(randomBytes);
generatedString = "" + BitConverter.ToString(randomBytes);
}

return generatedString;
Expand Down

0 comments on commit fbc190f

Please sign in to comment.