Ejemplo n.º 1
0
        public bool Disconnect(int token)
        {
            lock (this.state) {
                RWLock.EnterWriteLock();
                if (!this.state.Players.ContainsKey(token))
                {
                    return(false);
                }
                bool returnValue = this.state.Players.TryRemove(token, out var player);

                Callback.SendLog("Opuszczono pokój gry.");
                BroadcastMessage(player.Name + " opuścił grę.");
                BroadcastPlayerList();
                if (!IsRequiredNumberOfPlayers() && state.State != State.Lobby)
                {
                    BroadcastMessage("Brak wystarczającej liczby graczy do kontynuowania gry.");
                    StopGame();
                }
                if (state.State == State.Lobby)
                {
                    this.state.Leaderboard.TryRemove(token, out _);
                }
                if (state.Players.Keys.Count == 1 && token == state.AdminToken)
                {
                    state.AdminToken = this.state.Players.Values.First().Id;
                    state.Players[state.AdminToken].Callback.SendPlayerData(new PlayerDto(state.AdminToken, player, true));
                }
                RWLock.ExitWriteLock();
                return(returnValue);
            }
        }
        public int AddOrUpdate(T elem)
        {
            RWLock.EnterWriteLock();

            try {
                int index = InternalIndexOf(elem);
                if (index < 0)
                {
                    index = ~index;
                }

                if (index >= base.Count)
                {
                    base.Add(elem);
                }
                else
                {
                    base[index] = elem;
                }

                OnInsert(elem);
                return(index);
            }
            finally {
                RWLock.ExitWriteLock();
            }
        }
Ejemplo n.º 3
0
 public new void Clear()
 {
     RWLock.EnterWriteLock();
     try {
         base.Clear();
     } finally {
         RWLock.ExitWriteLock();
     }
 }
Ejemplo n.º 4
0
 public new bool Remove(T item)
 {
     RWLock.EnterWriteLock();
     try {
         return(base.Remove(item));
     } finally {
         RWLock.ExitWriteLock();
     }
 }
Ejemplo n.º 5
0
 public new bool Add(T item)
 {
     RWLock.EnterWriteLock();
     try {
         return(base.Add(item));
     } finally {
         RWLock.ExitWriteLock();
     }
 }
 /// <summary>
 /// You can't find or delete after this
 /// </summary>
 /// <param name="elem"></param>
 public void AddLast(T elem)
 {
     RWLock.EnterWriteLock();
     try {
         base.Add(elem);
     } finally {
         OnInsert(elem);
         RWLock.ExitWriteLock();
     }
 }
 public virtual void NaturalMergeSort(IComparer <T> comparer)
 {
     RWLock.EnterWriteLock();
     try {
         InternalNaturalMergeSort(comparer);
     }
     finally {
         RWLock.ExitWriteLock();
     }
 }
        public new void RemoveAt(int index)
        {
            RWLock.EnterWriteLock();

            try
            {
                T item = this[index];
                base.RemoveAt(index);
                OnRemove(item);
            }
            finally
            {
                RWLock.ExitWriteLock();
            }
        }
 public new void Insert(int index, T elem)
 {
     RWLock.EnterWriteLock();
     try
     {
         if (elem == null)
         {
             return;
         }
         base.Insert(index, elem);
         OnInsert(elem);
     }
     finally
     {
         RWLock.ExitWriteLock();
     }
 }
        public void RemoveLast()
        {
            RWLock.EnterWriteLock();

            try
            {
                if (base.Count > 0)
                {
                    T item = this[base.Count - 1];
                    base.RemoveAt(base.Count - 1);
                    OnRemove(item);
                }
            }
            finally
            {
                RWLock.ExitWriteLock();
            }
        }
        public void AddRangeUnsorted(IEnumerable <T> source)
        {
            RWLock.EnterWriteLock();
            try {
                if (source == null)
                {
                    return;
                }

                source.ForEach(elem => {
                    base.Add(elem);
                    OnInsert(elem);
                });
            }
            finally {
                RWLock.ExitWriteLock();
            }
        }
        public new bool Remove(T item)
        {
            RWLock.EnterWriteLock();

            try
            {
                if (base.Remove(item))
                {
                    OnRemove(item);
                    return(true);
                }
                return(false);
            }
            finally
            {
                RWLock.ExitWriteLock();
            }
        }
        public new void AddRange(IEnumerable <T> collection)
        {
            RWLock.EnterWriteLock();
            try {
                if (collection == null)
                {
                    return;
                }

                collection.ForEach(elem => {
                    base.Add(elem);
                    OnInsert(elem);
                });
                InternalNaturalMergeSort(this.Comparer);
            }
            finally {
                RWLock.ExitWriteLock();
            }
        }
Ejemplo n.º 14
0
        public void ConnectionManagementThreadMethod()
        {
            ConnectionRequest request;

            while (true)
            {
                request = connectionQueue.Dequeue();
                Console.WriteLine("Odebrano połączenie od: {0}", request.Name);
                Thread.BeginCriticalRegion();
                RWLock.EnterWriteLock();



                if (this.state.State != State.Lobby)
                {
                    request.Callback.SendLog("Gra jest w toku. Spróbuj ponownie później.");
                }
                else if (ContainsName(request.Name))
                {
                    request.Callback.SendLog("Nazwa gracza zajęta, proszę wybrać inną.");
                }
                else
                {
                    Player player = new Player(request.Context, request.Name);
                    int    token  = GenerateToken();
                    player.Id = token;
                    if (state.PlayerList.Count == 0)
                    {
                        this.state.AdminToken = token;
                        player.Callback.SendLog("Zostałeś adminem pokoju.");
                    }

                    this.state.Players.TryAdd(token, player);
                    player.Callback.SendPlayerData(new PlayerDto(token, player, token == this.state.AdminToken));
                    this.state.Leaderboard.TryAdd(token, new LeaderboardRow(player.Name, player.Points));
                    BroadcastPlayerList();
                }

                RWLock.ExitWriteLock();
                Thread.EndCriticalRegion();
            }
        }
Ejemplo n.º 15
0
        public void PlayerPickThreadMethod()
        {
            PlayerPick pick;

            while (true)
            {
                pick = picksQueue.Dequeue();

                Thread.BeginCriticalRegion();
                RWLock.EnterWriteLock();
                if (state.BannedTokens.ContainsKey(pick.PlayerToken))
                {
                    state.Players[pick.PlayerToken].Callback.SendLog("Trwa nałożona blokada.");
                }
                else
                {
                    if (this.state.CurrentCard.Symbols.Contains(pick.Pick) && state.Cards[state.Players[pick.PlayerToken].CardId].Symbols.Contains(pick.Pick))
                    {
                        this.state.State = State.WaitForReadiness;
                        this.picksQueue.Clear();
                        state.Players[pick.PlayerToken].Points++;
                        state.Leaderboard[pick.PlayerToken].Points++;
                        NotifyEveryoneAboutRoundEnd(state.RoundNumber);
                        BroadcastMessage(string.Format("Koniec rundy {0}, zwyciężył {1}", state.RoundNumber, state.Players[pick.PlayerToken].Name));
                        //this.InitializeRound();
                        state.PlayerList.ForEach(player => player.IsReady = false);
                        BroadcastPlayerList();
                    }
                    else
                    {
                        state.Players[pick.PlayerToken].Callback.LockClient();
                        BroadcastMessage(string.Format("{0}, 3 sekundy odpoczynku! (zły symbol)", state.Players[pick.PlayerToken].Name));
                        Timer timer = new Timer(UnbanToken, pick.PlayerToken, 5000, System.Threading.Timeout.Infinite);
                        state.BannedTokens.TryAdd(pick.PlayerToken, timer);
                    }
                }

                RWLock.ExitWriteLock();
                Thread.EndCriticalRegion();
            }
        }
Ejemplo n.º 16
0
            //try to write
            private void try_write()
            {
                RWLock.EnterWriteLock();

                check_pause();//system call pause;
                status = (int)global.StatusType.run;

                critical();

                RWLock.ExitWriteLock();

                check_pause();//system call pause

                global.mutexW.WaitOne();

                global.done_writer++;
                global.next_writer = CheckAndAdd(ID);

                global.mutexW.ReleaseMutex();

                status = (int)global.StatusType.done;
                Console.WriteLine(ID.ToString() + " finish the write_job");
            }
        public new void Add(T elem)
        {
            RWLock.EnterWriteLock();
            try {
                int index = InternalIndexOf(elem);
                if (index < 0)
                {
                    index = ~index;
                }

                if (index >= base.Count)
                {
                    base.Add(elem);
                }
                else
                {
                    base.Insert(index, elem);
                }
            } finally {
                OnInsert(elem);
                RWLock.ExitWriteLock();
            }
        }
Ejemplo n.º 18
0
 public override Element this[int index]
 {
     get
     {
         RWLock.EnterUpgradeableReadLock();
         try
         {
             var elem = Inner[index];
             if (elem != null && elem.Stub && elem.Owner != null)
             {
                 RWLock.EnterWriteLock();
                 try
                 {
                     elem = Inner[index] = elem.Owner.OnStubRequest(elem.ID);
                 }
                 catch (Exception err)
                 {
                     throw new DestubException(this, index, err);
                 }
                 finally
                 {
                     RWLock.ExitWriteLock();
                 }
             }
             return(elem);
         }
         finally
         {
             RWLock.ExitUpgradeableReadLock();
         }
     }
     set
     {
         base[index] = value;
     }
 }