示例#1
0
        /// <summary>
        /// Attempts to pick up hat. Once done update will be broadcasted.
        /// </summary>
        /// <param name="connectedClient">Client who wants to pick up hat.</param>
        /// <param name="hatId">Id of the hat.</param>
        public void GetHat(ConnectedClient connectedClient, String hatId)
        {
            lock (this.hatsSyncLock)
            {
                Hat foundHat;

                if (this.looseHats.TryGetValue(hatId, out foundHat))
                {
                    lock (this.racersSyncLock)
                    {
                        Racer me = this.racers.Find(racer => racer.Client == connectedClient);
                        if (me == null)
                        {
                            return;
                        }

                        if (foundHat.Type == "12")
                        {
                            // Racer picked up thief hat. Need to handle separately.

                            Racer swapRacer = this.findNextRacerForHatSwap(me);
                            if (swapRacer == null)
                            {
                                // No candidate for hat swap found. Just pick up the hat.

                                this.looseHats.Remove(hatId);
                                me.AddHat(foundHat);

                                this.BroadCast(String.Format("removeHat{0}`", foundHat.Id));
                                this.BroadCast(String.Concat("setHats", me.Position, me.GetHatsInfo()));
                            }
                            else
                            {
                                // Candidate found. Lets swap hats.

                                this.looseHats.Remove(hatId);
                                Hat swapRacerHat = swapRacer.RemoveHat();

                                if (swapRacerHat == null)
                                {
                                    me.AddHat(foundHat);
                                }
                                else
                                {
                                    me.AddHat(swapRacerHat);
                                    swapRacer.AddHat(foundHat);
                                }

                                this.BroadCast(String.Format("removeHat{0}`", foundHat.Id));
                                this.BroadCast(String.Concat("setHats", me.Position, me.GetHatsInfo()));
                                this.BroadCast(String.Concat("setHats", swapRacer.Position, swapRacer.GetHatsInfo()));
                            }
                        }
                        else
                        {
                            this.looseHats.Remove(hatId);
                            me.AddHat(foundHat);

                            this.BroadCast(String.Format("removeHat{0}`", foundHat.Id));
                            this.BroadCast(String.Concat("setHats", me.Position, me.GetHatsInfo()));
                        }
                    }
                }
            }
        }