/// <summary> /// Handler, when client requests to be put on specific level slot. /// </summary> /// <param name="sender">Client who sent request.</param> /// <param name="identifier">Level identifier usually in format levelID_version.</param> /// <param name="slot">Slot number from range 0-3.</param> public void FillSlot(ConnectedClient sender, String identifier, Int32 slot) { // First off, check whether occupancy info already exists for such a level identifier. OccupancyInfo occupancyInfo = null; lock (this.occupiedSlotsSyncLock) { this.occupiedSlots.TryGetValue(identifier, out occupancyInfo); } if (occupancyInfo == null) { // It does not, which means that noone is currently occupying any slot of the specified level, // therefore, we need to create new OccupancyInfo for it. occupancyInfo = new OccupancyInfo(identifier); lock (this.occupiedSlotsSyncLock) { this.occupiedSlots.Add(identifier, occupancyInfo); } // No need to check whether slot is free, because we just created new occupancy info, however we still // need to check if client is occupying different slot which we need to clean firstly. OccupancyInfo oldOccInfo = sender.OccupancyInfo; if (oldOccInfo != null) { // Client is already occupying a slot, lets clear it firstly. this.ClearSlot(sender, true); } // All done, lets assign occupancy info to client, fill slot and notify others. sender.OccupancyInfo = occupancyInfo; sender.OccupancyInfo.FillSlot(sender, slot); this.broadcastFillSlot(sender, identifier, slot); } else { // OccupancyInfo already exists which means that either someone else has already filled one of slots // for the specified level or it was filled in the past but it was not removed from the list. // First of all, check whether client is not occupying any other slots already. OccupancyInfo oldOccInfo = sender.OccupancyInfo; if (oldOccInfo != null) { // Client is already occupying a slot, lets clear it firstly. this.ClearSlot(sender, true); } // Next up check, whether specified slot is free. if (occupancyInfo.IsSlotFree(slot)) { // It is, we can finally assign occupancy info, fill slot and notify others. sender.OccupancyInfo = occupancyInfo; sender.OccupancyInfo.FillSlot(sender, slot); this.broadcastFillSlot(sender, identifier, slot); } } }
/// <summary> /// Checks whether it is possible to start game of specified occupancy info. /// If it is, initialize and start new race. /// </summary> /// <param name="occupancyInfo">Occupancy info to check.</param> private void checkStartGame(OccupancyInfo occupancyInfo) { if (occupancyInfo.CanStartGame()) { // We can start a new game, lets obtain list of clients. List <ConnectedClient> clients = occupancyInfo.GetClients(); // Clear appropriate slots and remove clients from this right room. foreach (ConnectedClient cli in clients) { this.ClearSlot(cli, false); this.RemoveClientFromRightRoom(cli); } Race newRace = new Race(occupancyInfo.LevelIdentifier, clients); newRace.Initialize(); newRace.StartRace(); } }