Home Bot: Difference between revisions

From Virtual Paradise Wiki
Jump to navigation Jump to search
JP (talk | contribs)
No edit summary
JP (talk | contribs)
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
Page is still be worked on, excuse JP's Laziness.
Home Bot is an open source C# program available free of download for anyone to use. It was created as a resource for new programmers to begin the core concepts of understanding the software development kit.
 
{| id="toc" style="width:25em; margin:0 0 0.5em 1em; float:right;"
|
|-
!colspan="3" style="text-align: center; font-size: 125%; font-weight: bold"|Home Bot
|-
!align="left" valign="top"|Programmer
|!align="left" colspan="2" valign="top"|[[JP]]
|-
!align="left" valign="top"|Created
|!align="left" colspan="2" valign="top"|March 25, 2015: 1:57 AM US EST
|-
!align="left" valign="top"|Purpose
|!align="left" colspan="2" valign="top"|Open Source
|-}


==About==
==About==
Home Bot is an open source C# program available free of download for anyone to use. It was created as a resource for new programmers to begin the core concepts of understanding the software development kit.


==Source Code==
==Source Code==
'''Bot.cs'''
<syntaxhighlight lang=CSharp>
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Threading;
using VpNet;
using VpNet.Abstract;
using VpNet.Extensions;
using homeBot;
using homeBot.Commands;
namespace homeBot
{
    public class Bot
    {
        public Config myConfig = new Config();
        public playerCommands myCommands = new playerCommands();
        Instance myBot;
        public void Initialize()
        {
            #region Login Area
            myBot = new Instance();
            try
            {
                myBot.Connect();
                myBot.Login(myConfig.Username, myConfig.Password, myConfig.Botname);
                myBot.Enter(myConfig.Worldname);
                myBot.UpdateAvatar(new Vector3(myConfig.Coordinates[0], myConfig.Coordinates[1],  myConfig.Coordinates[2]));
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.Message);
            }
            #endregion
            #region Event Controllers
            myBot.OnAvatarEnter += HandleAvatarEnter;
            myBot.OnAvatarLeave += HandleAvatarLeave;
            myBot.OnChatMessage += HandleChat;
            #endregion
            #region Wait Loop
            while (true)
            {
                myBot.Wait(0);
            }
            #endregion
        }
        #region Avatar Related Events
        void HandleAvatarEnter(Instance mrSender, AvatarEnterEventArgsT<Avatar<Vector3>, Vector3> myArgs)
        {
            if (!myArgs.Avatar.IsBot)
            {
                myBot.Say(string.Format("Welcome {0} to {1}'s beautiful home.", myArgs.Avatar.Name, myConfig.Username));
            }
        }
        void HandleAvatarLeave(Instance mrSender, AvatarLeaveEventArgsT<Avatar<Vector3>, Vector3> myArgs)
        {
        }
        void HandleChat(Instance myInstance, ChatMessageEventArgsT<Avatar<Vector3>, ChatMessage, Vector3, Color> myArgs)
        {
            if (!myArgs.Avatar.IsBot)
            {
                myCommands.myInstance = myInstance;
                myCommands.ChatArgs = myArgs;
                myCommands.ChatSelect();
            }
        }
        #endregion
    }
}
</syntaxhighlight>
'''Config.cs'''
<syntaxhighlight lang=CSharp>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace homeBot
{
    [Serializable]
    public class Config
    {
        public string Username;
        public string Password;
        public string Botname;
        public string Worldname;
        public float[] Coordinates;
        public void Load()
        {
            if (File.Exists("Configuration.xml"))
            {
                XmlSerializer MySerializer = new XmlSerializer(typeof(Config));
                StreamReader MyStreamReader = new StreamReader("Configuration.xml");
                Config myConfig = (Config)MySerializer.Deserialize(MyStreamReader);
                foreach (var Field in typeof(Config).GetFields())
                {
                    Field.SetValue(this, Field.GetValue(myConfig));
                }
                MyStreamReader.Close();
            }
            else
            {
                Save();
            }
        }
        private void Save()
        {
            Coordinates = new float[3];
            Console.Clear();
            Console.Write("Username: ");
            Username = Console.ReadLine();
            Console.Write("Password: ");
            Password = Console.ReadLine();
            Console.Clear();
            Console.Write("Worldname: ");
            Worldname = Console.ReadLine();
            Console.Write("Botname: ");
            Botname = Console.ReadLine();
            Console.Clear();
            Console.Write("X: ");
            Coordinates[0] = float.Parse(Console.ReadLine());
            Console.Write("Y: ");
            Coordinates[1] = float.Parse(Console.ReadLine());
            Console.Write("Z: ");
            Coordinates[2] = float.Parse(Console.ReadLine());
            XmlSerializer MySerializer = new XmlSerializer(typeof(Config));
            StreamWriter MyStreamWriter = new StreamWriter("Configuration.xml");
            MySerializer.Serialize(MyStreamWriter, this);
            MyStreamWriter.Close();
            Console.Clear();
        }
    }
}
</syntaxhighlight>
'''Program.cs'''
<syntaxhighlight lang=CSharp>
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using VpNet.Abstract;
using VpNet.ManagedApi.System.PluginFramework;
using VpNet.NativeApi;
using VpNet.PluginFramework;
using VpNet.Extensions;
using VpNet.PluginFramework.Interfaces;
namespace homeBot
{
    class Program
    {
        static void MainMenu()
        {
            while (true)
            {
                Console.WriteLine("0 - Exit");
                Console.WriteLine();
                Console.Write("Command: ");
                int myInput = int.Parse(Console.ReadLine());
                switch (myInput)
                {
                    case 0:
                        Environment.Exit(0);
                        break;
                    default:
                        break;
                }
                Console.Clear();
            }
        }
        static void Main(string[] args)
        {
            Console.Title = "Virtual Paradise Homebot";
            Bot HomeBot = new Bot();
            HomeBot.myConfig.Load();
            Thread T = new Thread(new ThreadStart(HomeBot.Initialize));
            T.Start();
            MainMenu();
        }
    }
}
</syntaxhighlight>
'''Commands.cs'''
<syntaxhighlight lang=CSharp>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;
using VpNet;
using VpNet.Abstract;
using VpNet.Extensions;
namespace homeBot.Commands
{
    public class playerCommands
    {
        public ChatMessageEventArgsT<Avatar<Vector3>, ChatMessage, Vector3, Color> ChatArgs;
        public Instance myInstance;
        public void ChatSelect()
        {
            string[] Messages = ChatArgs.ChatMessage.Message.Split(' ');
            for(int i = 0; i <= (Messages.Length - 1); i++)
            {
                Messages[i] = Messages[i].ToLower();
            }
            switch (Messages[0])
            {
                case "/version":
                    myInstance.Say("Version 1.0.0.0, HomeBot open source learning bot");
                    break;
                default:
                    break;
            }
        }
    }
}
</syntaxhighlight>

Latest revision as of 06:05, 25 March 2015

Home Bot is an open source C# program available free of download for anyone to use. It was created as a resource for new programmers to begin the core concepts of understanding the software development kit.

About

Home Bot is an open source C# program available free of download for anyone to use. It was created as a resource for new programmers to begin the core concepts of understanding the software development kit.

Source Code

Bot.cs

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Threading;

using VpNet;
using VpNet.Abstract;
using VpNet.Extensions;

using homeBot;
using homeBot.Commands;

namespace homeBot
{
    public class Bot
    {
        public Config myConfig = new Config();
        public playerCommands myCommands = new playerCommands();
        Instance myBot;

        public void Initialize()
        {
            #region Login Area
            myBot = new Instance();
            try
            {
                myBot.Connect();
                myBot.Login(myConfig.Username, myConfig.Password, myConfig.Botname);
                myBot.Enter(myConfig.Worldname);
                myBot.UpdateAvatar(new Vector3(myConfig.Coordinates[0], myConfig.Coordinates[1],  myConfig.Coordinates[2]));
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.Message);
            }
            #endregion

            #region Event Controllers
            myBot.OnAvatarEnter += HandleAvatarEnter;
            myBot.OnAvatarLeave += HandleAvatarLeave;
            myBot.OnChatMessage += HandleChat;
            #endregion

            #region Wait Loop
            while (true)
            {
                myBot.Wait(0);
            }
            #endregion
        }

        #region Avatar Related Events
        void HandleAvatarEnter(Instance mrSender, AvatarEnterEventArgsT<Avatar<Vector3>, Vector3> myArgs)
        {
            if (!myArgs.Avatar.IsBot)
            {
                myBot.Say(string.Format("Welcome {0} to {1}'s beautiful home.", myArgs.Avatar.Name, myConfig.Username));
            }
        }

        void HandleAvatarLeave(Instance mrSender, AvatarLeaveEventArgsT<Avatar<Vector3>, Vector3> myArgs)
        {
        }

        void HandleChat(Instance myInstance, ChatMessageEventArgsT<Avatar<Vector3>, ChatMessage, Vector3, Color> myArgs)
        {
            if (!myArgs.Avatar.IsBot)
            {
                myCommands.myInstance = myInstance;
                myCommands.ChatArgs = myArgs;

                myCommands.ChatSelect();
            }
        }
        #endregion
    }
}

Config.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace homeBot
{
    [Serializable]
    public class Config
    {
        public string Username;
        public string Password;

        public string Botname;

        public string Worldname;

        public float[] Coordinates;

        public void Load()
        {
            if (File.Exists("Configuration.xml"))
            {
                XmlSerializer MySerializer = new XmlSerializer(typeof(Config));
                StreamReader MyStreamReader = new StreamReader("Configuration.xml");
                Config myConfig = (Config)MySerializer.Deserialize(MyStreamReader);

                foreach (var Field in typeof(Config).GetFields())
                {
                    Field.SetValue(this, Field.GetValue(myConfig));
                }

                MyStreamReader.Close();
            }
            else
            {
                Save();
            }
        }

        private void Save()
        {
            Coordinates = new float[3];

            Console.Clear();
            Console.Write("Username: ");
            Username = Console.ReadLine();
            Console.Write("Password: ");
            Password = Console.ReadLine();

            Console.Clear();
            Console.Write("Worldname: ");
            Worldname = Console.ReadLine();
            Console.Write("Botname: ");
            Botname = Console.ReadLine();

            Console.Clear();
            Console.Write("X: ");
            Coordinates[0] = float.Parse(Console.ReadLine());
            Console.Write("Y: ");
            Coordinates[1] = float.Parse(Console.ReadLine());
            Console.Write("Z: ");
            Coordinates[2] = float.Parse(Console.ReadLine());

            XmlSerializer MySerializer = new XmlSerializer(typeof(Config));
            StreamWriter MyStreamWriter = new StreamWriter("Configuration.xml");

            MySerializer.Serialize(MyStreamWriter, this);
            MyStreamWriter.Close();
            Console.Clear();
        }
    }
}

Program.cs

using System;
using System.IO;
using System.Reflection;
using System.Threading;

using VpNet.Abstract;
using VpNet.ManagedApi.System.PluginFramework;
using VpNet.NativeApi;
using VpNet.PluginFramework;
using VpNet.Extensions;
using VpNet.PluginFramework.Interfaces;

namespace homeBot
{
    class Program
    {
        static void MainMenu()
        {
            while (true)
            {
                Console.WriteLine("0 - Exit");
                Console.WriteLine();

                Console.Write("Command: ");
                int myInput = int.Parse(Console.ReadLine());

                switch (myInput)
                {
                    case 0:
                        Environment.Exit(0);
                        break;
                    default:
                        break;
                }

                Console.Clear();
            }
        }

        static void Main(string[] args)
        {
            Console.Title = "Virtual Paradise Homebot";

            Bot HomeBot = new Bot();

            HomeBot.myConfig.Load();

            Thread T = new Thread(new ThreadStart(HomeBot.Initialize));

            T.Start();

            MainMenu();
        }
    }
}

Commands.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;

using VpNet;
using VpNet.Abstract;
using VpNet.Extensions;

namespace homeBot.Commands
{
    public class playerCommands
    {
        public ChatMessageEventArgsT<Avatar<Vector3>, ChatMessage, Vector3, Color> ChatArgs;
        public Instance myInstance;

        public void ChatSelect()
        {
            string[] Messages = ChatArgs.ChatMessage.Message.Split(' ');

            for(int i = 0; i <= (Messages.Length - 1); i++)
            {
                Messages[i] = Messages[i].ToLower();
            }

            switch (Messages[0])
            {
                case "/version":
                    myInstance.Say("Version 1.0.0.0, HomeBot open source learning bot");
                    break;
                default:
                    break;
            }
        }
    }
}