Starlight-30036225 / ChessTCP

FILLMELATER
0 stars 0 forks source link

Game rooms #31

Closed Starlight-30036225 closed 5 months ago

Starlight-30036225 commented 5 months ago

The next step is to separate multiple games into individual rooms. Allowing each server to act as its own room, which has the option of being password protected.

I have no idea where to go with this. So to keep the projects current status safe, I am going to create a new branch for this development just in case it all goes wrong.

Starlight-30036225 commented 5 months ago

The current two theoretical solutions I have to this problem are create a higher class than the server to handle multiple servers, or allow the single server to hold multiple games, and learn to distinguish between each game.

I think the simplest option is to improve the current server base to allow for multiple games to run concurrently. However, there will be problems in this with distinquishing incoming connections between different rooms.

This will take some tinkering

Starlight-30036225 commented 5 months ago

This would change the whole infastructure with how the black and white players work. This would need to be localised within a room sub-class.

This is messy, because every incoming message would need to be checked for the room. Which is likely to be messy.

I could create a map that holds the ID of the client, and the room it is in. So that it is easier to identify. There is alot to do here and I'm not happy about it.

This also for now ignores choosing which room to connect to, which i dont like.

Starlight-30036225 commented 5 months ago

Right now, when a player connects, it will look for the next room with an open space. For now this ignores passwords, and spectators. So players are not given the opportunity to chose their room, or spectate ongoing games. However, for now this will do.

This works by moving player references to the game master class, and treating that class as a room. Instead of one static game master class, a new one is created for each room. A map holds all connections, and the corresponding room.

`` protected void SendWelcomeMessage(ConnectionHandler CH) {

    Collection<GameMaster> AllRooms = roomMap.values();
    GameMaster room = null;
    for (GameMaster temproom:
         AllRooms) {
        if ((temproom.WhitePlayer == null || temproom.BlackPlayer == null)) {
            room = temproom;
            break;

        }
    }
    if (room == null){
        room = new GameMaster();
    }

``

This handles room assignment, the only other major change is replacing any reference the the game master, to the appropriate room. This is identified through this code snippit:

GameMaster room = roomMap.get(Handler);

Starlight-30036225 commented 5 months ago

To test this, i created 4 clients to connect to one server.

image

This was the result. The two windows of the left represent one room, the right another. They two rooms work perfectly independently.

This is great. I expected this to take days.