Basic Public Match Making Setup

This commit is contained in:
brausjonas
2023-04-10 15:22:03 +02:00
parent 106535cb19
commit e60b637e20
7 changed files with 366 additions and 21 deletions
+1
View File
@@ -98,6 +98,7 @@
<workItem from="1680352829832" duration="680000" />
<workItem from="1680353695912" duration="624000" />
<workItem from="1680470998642" duration="1402000" />
<workItem from="1681130452664" duration="2086000" />
</task>
<servers />
</component>
+3 -3
View File
@@ -14,11 +14,11 @@ import java.util.TimerTask;
public class Room
{
//tells how many clients can connect to a room
private Client[] players = new Client[2];
public Client[] players = new Client[2];
//the array of players start positions
private Vector3[] startPositions;
//the generated roomcode
private String roomCode;
public String roomCode;
//the instance to the main server
private Server server;
@@ -26,7 +26,7 @@ public class Room
private List<Thread> threads = new ArrayList<>();
//count of players in the game
private int playerCount = 0;
public int playerCount = 0;
//tells players turn (array index)
private int currentPlayer = 0;
+33
View File
@@ -6,7 +6,9 @@ import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Server
{
@@ -14,6 +16,7 @@ public class Server
private ServerSocket serverSocket;
//Roomcodes and Room objects are mapped here
private HashMap<String, Room> rooms = new HashMap<>();
private List<Room> publicRooms = new ArrayList<>();
//Server reference
private final Server server = this;
@@ -153,6 +156,36 @@ public class Server
}
break;
//For Public Match
case 4:
boolean success = false;
for(int i = 0; i < publicRooms.size(); i++)
{
Room temp = publicRooms.get(i);
if(temp.playerCount < temp.players.length)
{
String sendRoomCode = temp.roomCode;
output.write(sendRoomCode.getBytes(StandardCharsets.US_ASCII));
success = true;
client.close();
client = null;
break;
}
}
if(!success)
{
byte[] tempRoomCode = generateRoomCode();
Room tempRoom = new Room(new String(tempRoomCode, StandardCharsets.US_ASCII), server);
publicRooms.add(tempRoom);
output.write(tempRoomCode);
System.out.println("Room " + new String(tempRoomCode, StandardCharsets.US_ASCII) + " successfully created!");
client.close();
client = null;
break;
}
}
System.out.println("Init Thread finished");