Ejemplo n.º 1
0
        /// <summary>
        /// Broadcasts state change of respective racer.
        /// </summary>
        /// <param name="connectedClient">Client who sent state update message.</param>
        /// <param name="msgSegments">Raw data of message set_var.</param>
        public void BroadCastVar(ConnectedClient connectedClient, String[] msgSegments)
        {
            StringBuilder strBuilder = new StringBuilder();

            for (int i = 3; i < msgSegments.Length; i++)
            {
                strBuilder.Append(Separators.ARG_CHAR);
                strBuilder.Append(msgSegments[i]);
            }
            String varData = strBuilder.ToString();

            lock (this.racersSyncLock)
            {
                Racer me = this.racers.Find(racer => racer.Client == connectedClient);

                foreach (Racer racer in this.racers)
                {
                    if (racer != me)
                    {
                        racer.SendMessage(String.Format("var{0}{1}", me.Position, varData));
                    }
                }
            }
        }
Ejemplo 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()));
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Sends finishDrawing message to all racers.
 /// </summary>
 /// <param name="racer">Racer, who finished drawing.</param>
 private void sendFinishDrawing(Racer racer)
 {
     this.BroadCast(String.Concat("finishDrawing`", racer.Position));
 }