fCraft.PlayerDB.SwapPlayerInfo C# (CSharp) Method

SwapPlayerInfo() static private method

static private SwapPlayerInfo ( [ p1, [ p2 ) : void
p1 [
p2 [
return void
        internal static void SwapPlayerInfo( [NotNull] PlayerInfo p1, [NotNull] PlayerInfo p2 ) {
            if( p1 == null ) throw new ArgumentNullException( "p1" );
            if( p2 == null ) throw new ArgumentNullException( "p2" );
            using( GetWriteLock() ) {
                if( p1.IsOnline || p2.IsOnline ) {
                    throw new InvalidOperationException( "Both players must be offline to swap info." );
                }

                string tempString = p1.Name;
                p1.Name = p2.Name;
                p2.Name = tempString;

                DateTime tempDate = p1.LastLoginDate;
                p1.LastLoginDate = p2.LastLoginDate;
                p2.LastLoginDate = tempDate;

                tempDate = p1.LastSeen;
                p1.LastSeen = p2.LastSeen;
                p2.LastSeen = tempDate;

                LeaveReason tempLeaveReason = p1.LeaveReason;
                p1.LeaveReason = p2.LeaveReason;
                p2.LeaveReason = tempLeaveReason;

                IPAddress tempIP = p1.LastIP;
                p1.LastIP = p2.LastIP;
                p2.LastIP = tempIP;

                bool tempBool = p1.IsHidden;
                p1.IsHidden = p2.IsHidden;
                p2.IsHidden = tempBool;
            }
        }

Usage Example

        static void InfoSwapHandler(Player player, CommandReader cmd)
        {
            string p1Name = cmd.Next();
            string p2Name = cmd.Next();

            if (p1Name == null || p2Name == null)
            {
                CdInfoSwap.PrintUsage(player);
                return;
            }

            PlayerInfo p1 = PlayerDB.FindByPartialNameOrPrintMatches(player, p1Name);

            if (p1 == null)
            {
                return;
            }
            PlayerInfo p2 = PlayerDB.FindByPartialNameOrPrintMatches(player, p2Name);

            if (p2 == null)
            {
                return;
            }

            if (p1 == p2)
            {
                player.Message("InfoSwap: Please specify 2 different players.");
                return;
            }

            if (p1.IsOnline || p2.IsOnline)
            {
                player.Message("InfoSwap: Both players must be offline to swap info.");
                return;
            }

            if (!cmd.IsConfirmed)
            {
                player.Confirm(cmd, "InfoSwap: Swap stats of players {0}&S and {1}&S?", p1.ClassyName, p2.ClassyName);
            }
            else
            {
                PlayerDB.SwapPlayerInfo(p1, p2);
                player.Message("InfoSwap: Stats of {0}&S and {1}&S have been swapped.",
                               p1.ClassyName, p2.ClassyName);
            }
        }