Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Security.Cryptography;
  7. using System.IO;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Net;
  10. using System.Net.Sockets;
  11.  
  12. namespace Spine
  13. {
  14.     public class Objects
  15.     {
  16.  
  17.         static DataBaseRunner.DatabaseRunner DB = new DataBaseRunner.DatabaseRunner("all");
  18.         static public byte[] ObjectToByteArray(Object obj)
  19.         {
  20.             if (obj == null)
  21.                 return null;
  22.             BinaryFormatter bf = new BinaryFormatter();
  23.             MemoryStream ms = new MemoryStream();
  24.             bf.Serialize(ms, obj);
  25.             return ms.ToArray();
  26.         }
  27.         // Convert a byte array to an Object
  28.         static public Object ByteArrayToObject(byte[] arrBytes)
  29.         {
  30.             MemoryStream memStream = new MemoryStream();
  31.             BinaryFormatter binForm = new BinaryFormatter();
  32.             memStream.Write(arrBytes, 0, arrBytes.Length);
  33.             memStream.Seek(0, SeekOrigin.Begin);
  34.             Object obj = (Object)binForm.Deserialize(memStream);
  35.             return obj;
  36.         }
  37.         [Serializable]
  38.         public class LoginSocket
  39.         {
  40.             public Spine.Objects.Player P = new Spine.Objects.Player();
  41.             public Spine.Objects.logininfo L;
  42.             public string username;
  43.             public string password;
  44.         }
  45.         public List<monster> MonstersBuffer = new List<monster>();
  46.         public List<spell> SpellBuffer = new List<spell>();
  47.         public List<Player> PlayerBuffer = new List<Player>();
  48.         Dictionary<string, int> PlayerSession = new Dictionary<string, int>();
  49.         public Random n = new Random();
  50.         static public Byte[] BuildRequest(object o, string Type)
  51.         {
  52.             byte[] obj = Spine.Objects.ObjectToByteArray(o);
  53.             byte[] nul = System.Text.Encoding.ASCII.GetBytes("\r\n");
  54.             byte[] type = System.Text.Encoding.ASCII.GetBytes(Type);
  55.             byte[] senddata = new byte[obj.Length + nul.Length + type.Length];
  56.             int x = 0;
  57.             for (int y = 0; y < Type.Length; y++)
  58.             {
  59.                 senddata[x] = type[y];
  60.                 x++;
  61.  
  62.             }
  63.             for (int y = 0; y < obj.Length; y++)
  64.             {
  65.                 senddata[x] = obj[y];
  66.                 x++;
  67.  
  68.             }
  69.             for (int y = 0; y < nul.Length; y++)
  70.             {
  71.                 senddata[x] = nul[y];
  72.                 x++;
  73.             }
  74.             return senddata;
  75.  
  76.         }
  77.         public string SendToServer(object o, string Type)
  78.         {
  79.             string response = "";
  80.             Socket M = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  81.  
  82.  
  83.             try
  84.             {
  85.                 M.Connect("127.0.0.1", 1000);
  86.             }
  87.             catch
  88.             {
  89.                 return "Failed To Connect";
  90.             }
  91.             M.Send(BuildRequest(o,Type));
  92.             int bytes = 0;
  93.             byte[] b = new byte[1024];
  94.             do
  95.             {
  96.                 try
  97.                 {
  98.                     bytes = M.Receive(b);
  99.                 }
  100.                 catch
  101.                 {
  102.                     return "Server Force Closed";
  103.                 }
  104.                 response += Encoding.ASCII.GetString(b, 0, bytes);
  105.             } while (bytes > 0 && !response.Contains("\r\n"));
  106.  
  107.  
  108.             return response;
  109.         }
  110.         [Serializable]
  111.         public class SendToClientMe
  112.         {
  113.  
  114.  
  115.  
  116.  
  117.  
  118.         }
  119.         public class SendToClientView
  120.         {
  121.             public List<_monster> mlist = new List<_monster>();
  122.             public List<_player> plist = new List<_player>();
  123.  
  124.  
  125.             public SendToClientView(List<Player> p, List<monster> m, location l)
  126.             {
  127.  
  128.                 foreach (Player play in p)
  129.                 {
  130.                     if (play.loc.AREAID == l.AREAID)
  131.                     {
  132.                         _player P = new _player();
  133.                         P.l = l;
  134.                         P.playerinfo = play.Info;
  135.                         plist.Add(P);
  136.                     }
  137.  
  138.                 }
  139.                 foreach (monster mon in m)
  140.                 {
  141.                     if (mon.LOC.AREAID == l.AREAID)
  142.                     {
  143.                         _monster M = new _monster();
  144.                         M.l = l;
  145.                         M.monsterinfo = mon.Info;
  146.                         mlist.Add(M);
  147.                     }
  148.  
  149.  
  150.                 }
  151.  
  152.             }
  153.             [Serializable]
  154.             public class SendToClientMe
  155.             {
  156.                 Player me = new Player();
  157.                 public SendToClientMe(Player p)
  158.                 {
  159.                     p = me;
  160.  
  161.                 }
  162.             }
  163.  
  164.  
  165.             public class _monster
  166.             {
  167.                 public location l = new location();
  168.                 public int hp;
  169.                 public info monsterinfo = new info();
  170.  
  171.             }
  172.             public class _player
  173.             {
  174.                 public location l = new location();
  175.                 public int hp;
  176.                 public int image;
  177.                 public info playerinfo = new info();
  178.  
  179.             }
  180.  
  181.  
  182.  
  183.  
  184.  
  185.         }
  186.         public static Stats GenerateStatsFromDataRow(DataRow DR)
  187.         {
  188.             Stats s = new Stats();
  189.             s.MAXHP = int.Parse(DR["MAXHP"].ToString());
  190.             s.XP = int.Parse(DR["XP"].ToString());
  191.             s.MAN = int.Parse(DR["MAN"].ToString());
  192.             s.MAXMAN = int.Parse(DR["MAXMAN"].ToString());
  193.             s.STR = int.Parse(DR["STR"].ToString());
  194.             s.DEF = int.Parse(DR["DEF"].ToString());
  195.             s.MAG = int.Parse(DR["MAG"].ToString());
  196.             s.SPD = int.Parse(DR["SPD"].ToString());
  197.             s.ACC = int.Parse(DR["ACC"].ToString());
  198.             s.LVL = int.Parse(DR["LVL"].ToString());
  199.             s.CLS = DR["CLS"].ToString();
  200.             s.WIS = int.Parse(DR["WIS"].ToString());
  201.             s.DEX = int.Parse(DR["DEX"].ToString());
  202.             s.ELE = DR["ELE"].ToString();
  203.  
  204.  
  205.  
  206.             return s;
  207.  
  208.  
  209.         }    
  210.         public static info GenerateInfoFromDR(DataRow DR)
  211.         {
  212.             info i = new info();
  213.  
  214.             i.ImageID = int.Parse(DR["IMG"].ToString());
  215.             i.name = DR["NAME"].ToString();
  216.             return i;
  217.  
  218.         }
  219.         public static location GenerateLocationFromDR(DataRow DR)
  220.         {
  221.             location l = new location();
  222.  
  223.             l.XLOC = int.Parse(DR["XLOC"].ToString());
  224.             l.YLOC = int.Parse(DR["YLOC"].ToString());
  225.             l.AreaX = int.Parse(DR["AREAX"].ToString());
  226.             l.AreaY = int.Parse(DR["AREAY"].ToString());
  227.             l.AREAID = int.Parse(DR["AREAID"].ToString());
  228.             return l;
  229.  
  230.         }
  231.         public void SpawnMonster(location l, int ID)
  232.         {
  233.             monster m = new monster(l, ID);
  234.             m.Info.ID = MonstersBuffer.Count;
  235.             MonstersBuffer.Add(m);
  236.  
  237.  
  238.         }
  239.         public Player SpawnPlayer(int ID)
  240.         {
  241.             Player p = new Player();
  242.             DataRow DR = DB.ExecSProcDS("GMGetPlayerById", ID).Tables[0].Rows[0];
  243.             p.stat = GenerateStatsFromDataRow(DR);
  244.             p.Info = GenerateInfoFromDR(DR);
  245.             p.Info.ID = int.Parse(DR["PlayerID"].ToString());
  246.             DataTable Items = DB.ExecSProcDS("GMGetPlayerInventory", p.Info.ID).Tables[0];
  247.  
  248.  
  249.             p.inventory = FillInventoryFromDT(Items);
  250.             p.equip = FillEquipFromDT(Items);
  251.             p.loc = GenerateLocationFromDR(DR);
  252.             p.Session = CalculateMD5Hash(DR["PlayerID"].ToString() + DR["Password"].ToString());
  253.  
  254.             p.quest = GenerateQuestListFromDT(DB.ExecSProcDS("GMGetPlayerQuestById", ID).Tables[0]);
  255.             ///////finish here, generate quest by id, add to list. finish player generation
  256.             return p;
  257.  
  258.  
  259.         }
  260.         public quests GenerateQuestFromDR(DataRow dr)
  261.         {
  262.            
  263.             quests Q = new quests();
  264.             Q.require = GenerateReqInfoFromDR(dr);
  265.             Q.reward.Add(new item(int.Parse(dr["REW1"].ToString())));
  266.             Q.reward.Add(new item(int.Parse(dr["REW2"].ToString())));
  267.             Q.reward.Add(new item(int.Parse(dr["REW3"].ToString())));
  268.             Q.Goal = int.Parse(dr["REW3"].ToString());
  269.             Q.OwnerID = int.Parse(dr["OWNERID"].ToString());
  270.             Q.Info = GenerateInfoFromDR(dr);
  271.             Q.Progress = int.Parse(dr["Progress"].ToString());
  272.             Q.RequirementID = int.Parse(dr["REQ"].ToString());
  273.             Q.rewardXP = int.Parse(dr["XP"].ToString());
  274.             Q.Type = dr["TYPE"].ToString();
  275.  
  276.  
  277.             return Q;
  278.  
  279.         }
  280.         public List<quests> GenerateQuestListFromDT(DataTable DT)
  281.         {
  282.             List<quests> li = new List<quests>();
  283.  
  284.             foreach (DataRow DR in DT.Rows)
  285.             {
  286.                 li.Add(GenerateQuestFromDR(DR));
  287.  
  288.  
  289.             }
  290.  
  291.             return li;
  292.  
  293.         }
  294.         [Serializable]
  295.         public class logininfo
  296.         {
  297.             public string session;
  298.             public int playerid;
  299.             public logininfo(string Session, int Playerid)
  300.             {
  301.                 session = Session;
  302.                 playerid = Playerid;
  303.             }
  304.  
  305.         }
  306.         public string CalculateMD5Hash(string input)
  307.         {
  308.             // step 1, calculate MD5 hash from input
  309.             MD5 md5 = System.Security.Cryptography.MD5.Create();
  310.             byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
  311.             byte[] hash = md5.ComputeHash(inputBytes);
  312.  
  313.             // step 2, convert byte array to hex string
  314.             StringBuilder sb = new StringBuilder();
  315.             for (int i = 0; i < hash.Length; i++)
  316.             {
  317.                 sb.Append(hash[i].ToString("X2"));
  318.             }
  319.             return sb.ToString();
  320.         }
  321.         public List<item> FillInventoryFromDT(DataTable DT)
  322.         {
  323.             List<item> li = new List<item>();
  324.             foreach (DataRow DR in DT.Rows)
  325.             {
  326.                
  327.                 if (DR["LOC"].ToString() != "E")
  328.                 {
  329.                    
  330.                     li.Add(GenerateItemFromDR(DR));
  331.                 }
  332.  
  333.             }
  334.             return li;
  335.  
  336.         }
  337.         public item GenerateItemFromDR(DataRow DR)
  338.         {
  339.             item i = new item();
  340.             i.stat = GenerateStatsFromDataRow(DR);
  341.             i.requirements = GenerateReqInfoFromDR(DR);
  342.             i.Info = GenerateInfoFromDR(DR);
  343.             i.ItemID = int.Parse(DR["ItemID"].ToString());
  344.             i.ItemType = DR["MAINTYPE"].ToString();
  345.             i.SubType = DR["SUBTYPE"].ToString();
  346.            
  347.  
  348.             return i;
  349.         }
  350.         public List<item> FillEquipFromDT(DataTable DT)
  351.         {
  352.             List<item> li = new List<item>();
  353.             foreach (DataRow DR in DT.Rows)
  354.             {
  355.  
  356.                 if (DR["LOC"].ToString() == "E")
  357.                 {
  358.  
  359.                     li.Add(GenerateItemFromDR(DR));
  360.                 }
  361.  
  362.             }
  363.             return li;
  364.  
  365.         }
  366.         public void SpawnNPC(location l, int ID)
  367.         {
  368.             monster m = new monster(l, ID);
  369.  
  370.             DataRow DR = DB.ExecSProcDS("GMGetNPCById", ID).Tables[0].Rows[0];
  371.  
  372.             m.stat = GenerateStatsFromDataRow(DR);
  373.             m.LOC = l;
  374.             m.Info = GenerateInfoFromDR(DR);
  375.             m.align = 0;
  376.         }
  377.         [Serializable]
  378.         public class spell
  379.         {
  380.             public info Info = new info();
  381.             public string Caster;
  382.             public string Target;
  383.             public string CoolDown;
  384.             public bool Cast;
  385.             public Stats stat = new Stats();
  386.         }
  387.         [Serializable]
  388.         public class Requirements
  389.         {
  390.             public Stats stat = new Stats();
  391.             public location loc = new location();
  392.  
  393.  
  394.         }
  395.         [Serializable]
  396.         public class Actions
  397.         {
  398.             Random n = new Random();
  399.  
  400.             public Stats ownerstats = new Stats();
  401.             int damage = 0;
  402.             public Actions(Stats s)
  403.             {
  404.                 ownerstats = s;
  405.             }
  406.  
  407.  
  408.             public void Damage(Player p, spell s)
  409.             {
  410.  
  411.                 if (s.Info.ID == 0)
  412.                 {
  413.                     damage = ownerstats.STR - p.stat.DEF;
  414.                     if (damage < 0) { damage = 0; }
  415.                     damage += n.Next(1, ownerstats.STR / 10);
  416.                     p.stat.HP = p.stat.HP - damage;
  417.                 }
  418.  
  419.             }
  420.             public void Damage(monster m, spell s)
  421.             {
  422.  
  423.                 if (s.Info.ID == 0)
  424.                 {
  425.                     damage = ownerstats.STR - m.stat.DEF;
  426.                     if (damage < 0) { damage = 0; }
  427.                     damage += n.Next(1, ownerstats.STR / 10);
  428.                     m.stat.HP = m.stat.HP - damage;
  429.                 }
  430.  
  431.  
  432.             }
  433.  
  434.  
  435.         }
  436.         [Serializable]
  437.         public class Stats
  438.         {
  439.             public int MAXHP;
  440.             public int XP;
  441.             public int HP;
  442.             public int MAN;
  443.             public int STR;
  444.             public int DEF;
  445.             public int MAG;
  446.             public int SPD;
  447.             public int ACC;
  448.             public int LVL;
  449.             public string CLS;//W,S,M,R
  450.             public int WIS;
  451.             public int DEX;
  452.             public String ELE;//I, F, P, D, E, W, A, B
  453.             public int MAXMAN;
  454.  
  455.         }
  456.         [Serializable]
  457.         public class info
  458.         {
  459.             public string name;
  460.             public int ID;
  461.             public int ImageID;
  462.  
  463.  
  464.  
  465.         }
  466.         [Serializable]
  467.         public class Player
  468.         {
  469.             public string Session;
  470.             public info Info = new info();
  471.             public bool IsDead = false;
  472.             public location loc = new location();
  473.             public List<spell> spells = new List<spell>();
  474.             public List<item> equip = new List<item>();
  475.             public List<item> inventory = new List<item>();
  476.             public Stats stat = new Stats();
  477.             public List<quests> quest = new List<quests>();
  478.  
  479.         }
  480.         [Serializable]
  481.         public class quests
  482.         {
  483.             public info Info = new info();
  484.             public string Ownername;
  485.             public int OwnerID;
  486.             public string Type;
  487.             public int rewardXP;
  488.             public Requirements require = new Requirements();
  489.             public List<item> reward = new List<item>();
  490.             public bool inprogress = false;
  491.             public int RequirementID;
  492.  
  493.             public int Progress = 0;
  494.             public int Goal;
  495.  
  496.  
  497.  
  498.         }
  499.         [Serializable]
  500.         public class monster
  501.         {
  502.             public monster() { }
  503.             public monster(location l, int ID)
  504.             {
  505.  
  506.                 DataRow DR = DB.ExecSProcDS("GMGetMonsterById", ID).Tables[0].Rows[0];
  507.  
  508.                 stat = GenerateStatsFromDataRow(DR);
  509.                 LOC = l;
  510.                 Info = GenerateInfoFromDR(DR);
  511.                 align = 1;
  512.  
  513.             }
  514.             public info Info = new info();
  515.             public int align; //0 Good, 1 Bad
  516.             public Stats stat = new Stats();
  517.             public location LOC = new location();
  518.  
  519.         }
  520.         public static Requirements GenerateReqInfoFromDR(DataRow DR)
  521.         {
  522.             Requirements r = new Requirements();
  523.             Stats s = new Stats();
  524.  
  525.  
  526.             s.MAN = int.Parse(DR["RMAN"].ToString());
  527.             s.STR = int.Parse(DR["RSTR"].ToString());
  528.             s.DEF = int.Parse(DR["RDEF"].ToString());
  529.             s.MAG = int.Parse(DR["RMAG"].ToString());
  530.             s.SPD = int.Parse(DR["RSPD"].ToString());
  531.             s.ACC = int.Parse(DR["RACC"].ToString());
  532.             s.LVL = int.Parse(DR["RLVL"].ToString());
  533.             s.CLS = DR["RCLS"].ToString();
  534.             s.WIS = int.Parse(DR["RWIS"].ToString());
  535.             s.DEX = int.Parse(DR["RDEX"].ToString());
  536.             s.ELE = DR["RELE"].ToString();
  537.             r.stat = s;
  538.  
  539.  
  540.  
  541.  
  542.  
  543.             return r;
  544.  
  545.         }
  546.         [Serializable]
  547.         public class item
  548.         {
  549.             public item() { }
  550.             public item(int itemid)
  551.             {
  552.                
  553.                 DataRow DR = DB.ExecSProcDS("GMGetItemById", itemid).Tables[0].Rows[0];
  554.                 ItemID = itemid;
  555.  
  556.                 stat = GenerateStatsFromDataRow(DR);
  557.                 Info = GenerateInfoFromDR(DR);
  558.                 requirements = GenerateReqInfoFromDR(DR);
  559.                 ItemType = DR["MAINTYPE"].ToString();
  560.                 SubType = DR["SUBTYPE"].ToString();
  561.  
  562.  
  563.             }
  564.             public info Info = new info();
  565.             public int ItemID;
  566.             public int Quantity;
  567.             public string ItemType; //Weapon, Armor, Disposable, Permanent, Quest;
  568.             public string SubType;
  569.             public Stats stat = new Stats();
  570.             public Requirements requirements = new Requirements();
  571.  
  572.         }
  573.         [Serializable]
  574.         public class location
  575.         {
  576.             public int XLOC;
  577.             public int YLOC;
  578.             public int AREAID;
  579.             public int AreaX;
  580.             public int AreaY;
  581.             public int Direction;
  582.  
  583.  
  584.         }
  585.         public Player login(loginpackage l)
  586.         {
  587.             DataTable DT = DB.ExecSProcDS("GMLogin", l.username, l.password).Tables[0];
  588.             DataRow DR = DT.Rows[0];
  589.  
  590.             if (DT.Rows.Count > 0)
  591.             {
  592.                 return SpawnPlayer(int.Parse(DR["PlayerID"].ToString()));
  593.  
  594.             }
  595.  
  596.             return new Player();
  597.  
  598.         }
  599.         [Serializable]
  600.         public class loginpackage
  601.         {
  602.             public string username;
  603.             public string password;
  604.             public loginpackage(string u, string p)
  605.             {
  606.                 username = u;
  607.                 password = p;
  608.             }
  609.  
  610.  
  611.         }
  612.  
  613.  
  614.  
  615.  
  616.     }
  617. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement