コード例 #1
0
        public void Remove(Entities.User u)
        {
            if (u.Room != null)
            {
                if (u.Room.ID == this.ID)
                {
                    lock (this.RoomLock) {
                        Player p;
                        byte   oldSlot = 0;

                        lock (_playerLock) {
                            if (Players.ContainsKey(u.RoomSlot))
                            {
                                Players.TryRemove(u.RoomSlot, out p);

                                if (p != null)
                                {
                                    oldSlot = u.RoomSlot;
                                    p.User.SetRoom(null, 0);
                                }
                                else
                                {
                                    u.SetRoom(null, 0);
                                }

                                if (Players.Count > 0)
                                {
                                    // Asign new master //
                                    if (oldSlot == Master)
                                    {
                                        // Remove the suppermaster buff :)
                                        this.Supermaster = false;

                                        // Calculate the priority level.
                                        int[] priority = new int[MaximumPlayers];
                                        for (byte i = 0; i < MaximumPlayers; i++)
                                        {
                                            if (Players.ContainsKey(i))
                                            {
                                                if (Players[i] != null)
                                                {
                                                    priority[i] = 1 + (int)Players[i].User.Premium;
                                                }
                                                else
                                                {
                                                    priority[i] = 0; // no priority.
                                                }
                                            }
                                            else
                                            {
                                                priority[i] = 0; // no priority.
                                            }
                                        }

                                        // Find the new master.
                                        sbyte newMaster   = -1;
                                        int   masterPrior = 0;
                                        for (byte j = 0; j < MaximumPlayers; j++)
                                        {
                                            if (priority[j] > masterPrior) // A player with higher piority has been found.
                                            {
                                                newMaster   = (sbyte)j;    // set the master slot
                                                masterPrior = priority[j]; // store the piority.
                                            }
                                        }

                                        // We have found a new master!
                                        if (newMaster >= 0)
                                        {
                                            Master = (byte)newMaster;
                                            Players[Master].Ready = true;
                                        }
                                        else
                                        {
                                            Destroy();
                                        }
                                    }
                                }
                                else
                                {
                                    Destroy();
                                }

                                byte[] pBuffer = new Packets.RoomLeave(u, oldSlot, this).BuildEncrypted();
                                if (Players.Count > 0)
                                {
                                    Send(pBuffer);
                                }

                                u.Send(pBuffer);

                                //tell the players
                                if (State == RoomState.Playing)
                                {
                                    //     string _playerLeft = Cristina.Core.Cristina.Localization.GetLocMessageFrom("PLAYER_LEFT");

                                    //   if (_playerLeft.Contains("%/nickname/%"))
                                    //    _playerLeft = _playerLeft.Replace("/nickname/%", u.Displayname);

                                    Cristina.Core.Cristina.Chat.SaytoRoom(u.Displayname + " ha salido de la sala", this);
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void Remove(Entities.User u)
        {
            if (u.Room != null)
            {
                if (u.Room.ID == this.ID)
                {
                    lock (this.RoomLock) {
                        Player p;
                        byte   oldSlot = 0;

                        lock (_playerLock) {
                            if (Players.ContainsKey(u.RoomSlot))
                            {
                                Players.TryRemove(u.RoomSlot, out p);

                                if (p != null)
                                {
                                    oldSlot = u.RoomSlot;
                                    p.User.SetRoom(null, 0);
                                }
                                else
                                {
                                    u.SetRoom(null, 0);
                                }

                                if (Players.Count > 0)
                                {
                                    // Asign new master //
                                    if (u.RoomSlot == Master)
                                    {
                                        // Remove the suppermaster buff :)
                                        this.Supermaster = false;

                                        // Calculate the priority level.
                                        int[] priority = new int[MaximumPlayers];
                                        for (byte i = 0; i < MaximumPlayers; i++)
                                        {
                                            if (Players.ContainsKey(i))
                                            {
                                                if (Players[i] != null)
                                                {
                                                    priority[i] = 1 + (int)Players[i].User.Premium;
                                                }
                                                else
                                                {
                                                    priority[i] = 0; // no priority.
                                                }
                                            }
                                            else
                                            {
                                                priority[i] = 0; // no priority.
                                            }
                                        }

                                        // Find the new master.
                                        sbyte newMaster   = -1;
                                        int   masterPrior = 0;
                                        for (byte j = 0; j < MaximumPlayers; j++)
                                        {
                                            if (priority[j] > masterPrior) // A player with higher piority has been found.
                                            {
                                                newMaster   = (sbyte)j;    // set the master slot
                                                masterPrior = priority[j]; // store the piority.
                                            }
                                        }

                                        // We have found a new master!
                                        if (newMaster >= 0)
                                        {
                                            Master = (byte)newMaster;
                                        }
                                        else
                                        {
                                            Destory();
                                        }
                                    }
                                }
                                else
                                {
                                    Destory();
                                }

                                byte[] pBuffer = new Packets.RoomLeave(u, oldSlot, this).BuildEncrypted();
                                if (Players.Count > 0)
                                {
                                    Send(pBuffer);
                                }

                                u.Send(pBuffer);
                            }
                        }
                    }
                }
            }
        }
コード例 #3
0
        public bool Add(Entities.User u)
        {
            if (Players.Count < MaximumPlayers && !UserLimit)
            {
                if (isClanWar && u.ClanRank < 1)
                {
                    return(false);
                }

                lock (_playerLock) {
                    Team targetTeam = Team.Derbaran;

                    byte startScanIndex = 0;
                    byte endScanIndex   = (byte)(MaximumPlayers / 2);

                    var debTeam = Players.Select(n => n.Value).Where(n => n.Team == Team.Derbaran).Count();
                    var niuTeam = Players.Select(n => n.Value).Where(n => n.Team == Team.NIU).Count();

                    if (debTeam > niuTeam)
                    {
                        targetTeam     = Team.NIU;
                        startScanIndex = (byte)(MaximumPlayers / 2);
                        endScanIndex   = MaximumPlayers;
                    }

                    for (byte i = startScanIndex; i < endScanIndex; i++)
                    {
                        if (!Players.ContainsKey(i))
                        {
                            Player plr = new Player(i, u, targetTeam);

                            // Send Room //
                            if (Players.Count > 0)
                            {
                                Send((new Packets.RoomPlayers(new ArrayList()
                                {
                                    plr
                                }).BuildEncrypted()));
                            }
                            else
                            {
                                Master = i;
                            }

                            // Add Player
                            Players.TryAdd(i, plr);
                            u.SetRoom(this, i);

                            // Send Join Packet //
                            u.Send(new Packets.RoomJoin(i, this));
                            u.Send(new Packets.RoomPlayers(new ArrayList(Players.Select(n => n.Value).Where(n => n.Id != i).ToArray())));

                            // Send to all spectators
                            foreach (Entities.User Spectator in Spectators.Values)
                            {
                                u.Send(new Packets.RoomPlayers(new ArrayList(Players.Select(n => n.Value).Where(n => n.Id != i).ToArray())));
                            }

                            //Tell de players
                            if (State == RoomState.Playing)
                            {
                                //     string _message = Cristina.Core.Cristina.Localization.GetLocMessageFrom("PLAYER_JOINED");
                                //   if (_message.Contains("%/nickname/%"))
                                //     _message = _message.Replace("%/nickname/%", u.Displayname);

                                Cristina.Core.Cristina.Chat.SaytoRoom(u.Displayname + " se ha conectado", this);
                            }


                            // SEND THE ROOM UPDATE TO THE LOBBY //
                            byte roomPage   = (byte)Math.Floor((decimal)(this.ID / 8));
                            var  targetList = Managers.ChannelManager.Instance.Get(this.Channel).Users.Select(n => n.Value).Where(n => n.RoomListPage == roomPage && n.Room == null);
                            if (targetList.Count() > 0)
                            {
                                byte[] outBuffer = new Packets.RoomUpdate(this, true).BuildEncrypted();
                                foreach (Entities.User usr in targetList)
                                {
                                    usr.Send(outBuffer);
                                }
                            }
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
コード例 #4
0
        public bool Add(Entities.User u)
        {
            if (Players.Count < MaximumPlayers)
            {
                lock (_playerLock) {
                    Team targetTeam     = Team.Derbaran;
                    byte startScanIndex = 0;
                    byte endScanIndex   = (byte)(MaximumPlayers / 2);

                    var debTeam = Players.Select(n => n.Value).Where(n => n.Team == Team.Derbaran).Count();
                    var niuTeam = Players.Select(n => n.Value).Where(n => n.Team == Team.NIU).Count();

                    if (debTeam > niuTeam)
                    {
                        targetTeam     = Team.NIU;
                        startScanIndex = (byte)(MaximumPlayers / 2);
                        endScanIndex   = MaximumPlayers;
                    }

                    for (byte i = startScanIndex; i < endScanIndex; i++)
                    {
                        if (!Players.ContainsKey(i))
                        {
                            Player plr = new Player(i, u, targetTeam);

                            // Send Room //
                            if (Players.Count > 0)
                            {
                                Send((new Packets.RoomPlayers(new ArrayList()
                                {
                                    plr
                                }).BuildEncrypted()));
                            }
                            else
                            {
                                Master = i;
                            }

                            // Add Player
                            Players.TryAdd(i, plr);
                            u.SetRoom(this, i);

                            // Send Join Packet //
                            u.Send(new Packets.RoomJoin(i, this));
                            u.Send(new Packets.RoomPlayers(new ArrayList(Players.Select(n => n.Value).Where(n => n.Id != i).ToArray())));

                            // Send to all spectators: TODO //

                            // SEND THE ROOM UPDATE TO THE LOBBY //
                            byte roomPage   = (byte)Math.Floor((decimal)(this.ID / 8));
                            var  targetList = Managers.ChannelManager.Instance.Get(this.Channel).Users.Select(n => n.Value).Where(n => n.RoomListPage == roomPage && n.Room == null);
                            if (targetList.Count() > 0)
                            {
                                byte[] outBuffer = new Packets.RoomUpdate(this, true).BuildEncrypted();
                                foreach (Entities.User usr in targetList)
                                {
                                    usr.Send(outBuffer);
                                }
                            }

                            return(true);
                        }
                    }
                }
            }

            return(false);
        }