Exemplo n.º 1
0
        /// <summary>
        /// Makes client drop his hat and broadcast changes to everyone.
        /// Dropped hat will be added to dictionary dropped hats.
        /// </summary>
        /// <param name="connectedClient">Client who lost hat.</param>
        /// <param name="msgSegments">Message loose_hat in its raw format.</param>
        public void LooseHat(ConnectedClient connectedClient, String[] msgSegments)
        {
            lock (this.racersSyncLock)
            {
                Racer me = this.racers.Find(racer => racer.Client == connectedClient);
                if (me == null)
                {
                    return;
                }

                Hat droppedHat = me.RemoveHat();

                if (droppedHat != null)
                {
                    lock (this.hatsSyncLock)
                    {
                        this.looseHats.Add(droppedHat.Id, droppedHat);
                    }

                    this.BroadCast(
                        String.Format(
                            "addEffect`Hat`{0}`{1}`{2}`{3}`{4}",
                            msgSegments[3],
                            msgSegments[4],
                            msgSegments[5],
                            droppedHat.ToString(),
                            droppedHat.Id
                            ));
                    this.BroadCast(String.Concat("setHats", me.Position, me.GetHatsInfo()));
                }
            }
        }
Exemplo n.º 2
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()));
                        }
                    }
                }
            }
        }