Exemplo n.º 1
0
        /// <summary>
        /// Stores level id, initializes list of racers and sets status to INITIALIZED.
        /// </summary>
        public void Initialize()
        {
            if (this.gameStatus < GameStatus.INITIALIZED)
            {
                // Update levelId from levelId_version format to levelId.
                if (this.levelId.Contains('_'))
                {
                    this.levelId = this.levelId.Split(Separators.UNDERSCORE_SEPARATOR)[0];
                }

                // Fill list of racers and assign hats.
                lock (this.clientsSyncLock)
                {
                    lock (this.racersSyncLock)
                    {
                        lock (this.hatsSyncLock)
                        {
                            Int32 startingHatId = this.looseHats.Count;

                            for (Int32 idx = 0; idx < this.clients.Count; idx++)
                            {
                                this.clients[idx].Game = this;
                                Racer newRacer = new Racer(idx.ToString(), this.clients[idx]);
                                Hat   newHat   = new Hat
                                {
                                    Id             = startingHatId.ToString(),
                                    Type           = newRacer.AccData.Hat.ToString(),
                                    PrimaryColor   = newRacer.AccData.HatColor.ToString(),
                                    SecondaryColor = newRacer.AccData.HatColor2.ToString()
                                };
                                newRacer.AddHat(newHat);
                                this.racers.Add(newRacer);
                                startingHatId++;
                            }
                        }
                    }
                }

                this.gameStatus = GameStatus.INITIALIZED;
            }
            else
            {
                // TODO - Log this. Initialize should only be called once.
            }
        }
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()));
                        }
                    }
                }
            }
        }