Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Security.Cryptography;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Net;
- using System.Net.Sockets;
- namespace Spine
- {
- public class Objects
- {
- static DataBaseRunner.DatabaseRunner DB = new DataBaseRunner.DatabaseRunner("all");
- static public byte[] ObjectToByteArray(Object obj)
- {
- if (obj == null)
- return null;
- BinaryFormatter bf = new BinaryFormatter();
- MemoryStream ms = new MemoryStream();
- bf.Serialize(ms, obj);
- return ms.ToArray();
- }
- // Convert a byte array to an Object
- static public Object ByteArrayToObject(byte[] arrBytes)
- {
- MemoryStream memStream = new MemoryStream();
- BinaryFormatter binForm = new BinaryFormatter();
- memStream.Write(arrBytes, 0, arrBytes.Length);
- memStream.Seek(0, SeekOrigin.Begin);
- Object obj = (Object)binForm.Deserialize(memStream);
- return obj;
- }
- [Serializable]
- public class LoginSocket
- {
- public Spine.Objects.Player P = new Spine.Objects.Player();
- public Spine.Objects.logininfo L;
- public string username;
- public string password;
- }
- public List<monster> MonstersBuffer = new List<monster>();
- public List<spell> SpellBuffer = new List<spell>();
- public List<Player> PlayerBuffer = new List<Player>();
- Dictionary<string, int> PlayerSession = new Dictionary<string, int>();
- public Random n = new Random();
- static public Byte[] BuildRequest(object o, string Type)
- {
- byte[] obj = Spine.Objects.ObjectToByteArray(o);
- byte[] nul = System.Text.Encoding.ASCII.GetBytes("\r\n");
- byte[] type = System.Text.Encoding.ASCII.GetBytes(Type);
- byte[] senddata = new byte[obj.Length + nul.Length + type.Length];
- int x = 0;
- for (int y = 0; y < Type.Length; y++)
- {
- senddata[x] = type[y];
- x++;
- }
- for (int y = 0; y < obj.Length; y++)
- {
- senddata[x] = obj[y];
- x++;
- }
- for (int y = 0; y < nul.Length; y++)
- {
- senddata[x] = nul[y];
- x++;
- }
- return senddata;
- }
- public string SendToServer(object o, string Type)
- {
- string response = "";
- Socket M = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
- try
- {
- M.Connect("127.0.0.1", 1000);
- }
- catch
- {
- return "Failed To Connect";
- }
- M.Send(BuildRequest(o,Type));
- int bytes = 0;
- byte[] b = new byte[1024];
- do
- {
- try
- {
- bytes = M.Receive(b);
- }
- catch
- {
- return "Server Force Closed";
- }
- response += Encoding.ASCII.GetString(b, 0, bytes);
- } while (bytes > 0 && !response.Contains("\r\n"));
- return response;
- }
- [Serializable]
- public class SendToClientMe
- {
- }
- public class SendToClientView
- {
- public List<_monster> mlist = new List<_monster>();
- public List<_player> plist = new List<_player>();
- public SendToClientView(List<Player> p, List<monster> m, location l)
- {
- foreach (Player play in p)
- {
- if (play.loc.AREAID == l.AREAID)
- {
- _player P = new _player();
- P.l = l;
- P.playerinfo = play.Info;
- plist.Add(P);
- }
- }
- foreach (monster mon in m)
- {
- if (mon.LOC.AREAID == l.AREAID)
- {
- _monster M = new _monster();
- M.l = l;
- M.monsterinfo = mon.Info;
- mlist.Add(M);
- }
- }
- }
- [Serializable]
- public class SendToClientMe
- {
- Player me = new Player();
- public SendToClientMe(Player p)
- {
- p = me;
- }
- }
- public class _monster
- {
- public location l = new location();
- public int hp;
- public info monsterinfo = new info();
- }
- public class _player
- {
- public location l = new location();
- public int hp;
- public int image;
- public info playerinfo = new info();
- }
- }
- public static Stats GenerateStatsFromDataRow(DataRow DR)
- {
- Stats s = new Stats();
- s.MAXHP = int.Parse(DR["MAXHP"].ToString());
- s.XP = int.Parse(DR["XP"].ToString());
- s.MAN = int.Parse(DR["MAN"].ToString());
- s.MAXMAN = int.Parse(DR["MAXMAN"].ToString());
- s.STR = int.Parse(DR["STR"].ToString());
- s.DEF = int.Parse(DR["DEF"].ToString());
- s.MAG = int.Parse(DR["MAG"].ToString());
- s.SPD = int.Parse(DR["SPD"].ToString());
- s.ACC = int.Parse(DR["ACC"].ToString());
- s.LVL = int.Parse(DR["LVL"].ToString());
- s.CLS = DR["CLS"].ToString();
- s.WIS = int.Parse(DR["WIS"].ToString());
- s.DEX = int.Parse(DR["DEX"].ToString());
- s.ELE = DR["ELE"].ToString();
- return s;
- }
- public static info GenerateInfoFromDR(DataRow DR)
- {
- info i = new info();
- i.ImageID = int.Parse(DR["IMG"].ToString());
- i.name = DR["NAME"].ToString();
- return i;
- }
- public static location GenerateLocationFromDR(DataRow DR)
- {
- location l = new location();
- l.XLOC = int.Parse(DR["XLOC"].ToString());
- l.YLOC = int.Parse(DR["YLOC"].ToString());
- l.AreaX = int.Parse(DR["AREAX"].ToString());
- l.AreaY = int.Parse(DR["AREAY"].ToString());
- l.AREAID = int.Parse(DR["AREAID"].ToString());
- return l;
- }
- public void SpawnMonster(location l, int ID)
- {
- monster m = new monster(l, ID);
- m.Info.ID = MonstersBuffer.Count;
- MonstersBuffer.Add(m);
- }
- public Player SpawnPlayer(int ID)
- {
- Player p = new Player();
- DataRow DR = DB.ExecSProcDS("GMGetPlayerById", ID).Tables[0].Rows[0];
- p.stat = GenerateStatsFromDataRow(DR);
- p.Info = GenerateInfoFromDR(DR);
- p.Info.ID = int.Parse(DR["PlayerID"].ToString());
- DataTable Items = DB.ExecSProcDS("GMGetPlayerInventory", p.Info.ID).Tables[0];
- p.inventory = FillInventoryFromDT(Items);
- p.equip = FillEquipFromDT(Items);
- p.loc = GenerateLocationFromDR(DR);
- p.Session = CalculateMD5Hash(DR["PlayerID"].ToString() + DR["Password"].ToString());
- p.quest = GenerateQuestListFromDT(DB.ExecSProcDS("GMGetPlayerQuestById", ID).Tables[0]);
- ///////finish here, generate quest by id, add to list. finish player generation
- return p;
- }
- public quests GenerateQuestFromDR(DataRow dr)
- {
- quests Q = new quests();
- Q.require = GenerateReqInfoFromDR(dr);
- Q.reward.Add(new item(int.Parse(dr["REW1"].ToString())));
- Q.reward.Add(new item(int.Parse(dr["REW2"].ToString())));
- Q.reward.Add(new item(int.Parse(dr["REW3"].ToString())));
- Q.Goal = int.Parse(dr["REW3"].ToString());
- Q.OwnerID = int.Parse(dr["OWNERID"].ToString());
- Q.Info = GenerateInfoFromDR(dr);
- Q.Progress = int.Parse(dr["Progress"].ToString());
- Q.RequirementID = int.Parse(dr["REQ"].ToString());
- Q.rewardXP = int.Parse(dr["XP"].ToString());
- Q.Type = dr["TYPE"].ToString();
- return Q;
- }
- public List<quests> GenerateQuestListFromDT(DataTable DT)
- {
- List<quests> li = new List<quests>();
- foreach (DataRow DR in DT.Rows)
- {
- li.Add(GenerateQuestFromDR(DR));
- }
- return li;
- }
- [Serializable]
- public class logininfo
- {
- public string session;
- public int playerid;
- public logininfo(string Session, int Playerid)
- {
- session = Session;
- playerid = Playerid;
- }
- }
- public string CalculateMD5Hash(string input)
- {
- // step 1, calculate MD5 hash from input
- MD5 md5 = System.Security.Cryptography.MD5.Create();
- byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
- byte[] hash = md5.ComputeHash(inputBytes);
- // step 2, convert byte array to hex string
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < hash.Length; i++)
- {
- sb.Append(hash[i].ToString("X2"));
- }
- return sb.ToString();
- }
- public List<item> FillInventoryFromDT(DataTable DT)
- {
- List<item> li = new List<item>();
- foreach (DataRow DR in DT.Rows)
- {
- if (DR["LOC"].ToString() != "E")
- {
- li.Add(GenerateItemFromDR(DR));
- }
- }
- return li;
- }
- public item GenerateItemFromDR(DataRow DR)
- {
- item i = new item();
- i.stat = GenerateStatsFromDataRow(DR);
- i.requirements = GenerateReqInfoFromDR(DR);
- i.Info = GenerateInfoFromDR(DR);
- i.ItemID = int.Parse(DR["ItemID"].ToString());
- i.ItemType = DR["MAINTYPE"].ToString();
- i.SubType = DR["SUBTYPE"].ToString();
- return i;
- }
- public List<item> FillEquipFromDT(DataTable DT)
- {
- List<item> li = new List<item>();
- foreach (DataRow DR in DT.Rows)
- {
- if (DR["LOC"].ToString() == "E")
- {
- li.Add(GenerateItemFromDR(DR));
- }
- }
- return li;
- }
- public void SpawnNPC(location l, int ID)
- {
- monster m = new monster(l, ID);
- DataRow DR = DB.ExecSProcDS("GMGetNPCById", ID).Tables[0].Rows[0];
- m.stat = GenerateStatsFromDataRow(DR);
- m.LOC = l;
- m.Info = GenerateInfoFromDR(DR);
- m.align = 0;
- }
- [Serializable]
- public class spell
- {
- public info Info = new info();
- public string Caster;
- public string Target;
- public string CoolDown;
- public bool Cast;
- public Stats stat = new Stats();
- }
- [Serializable]
- public class Requirements
- {
- public Stats stat = new Stats();
- public location loc = new location();
- }
- [Serializable]
- public class Actions
- {
- Random n = new Random();
- public Stats ownerstats = new Stats();
- int damage = 0;
- public Actions(Stats s)
- {
- ownerstats = s;
- }
- public void Damage(Player p, spell s)
- {
- if (s.Info.ID == 0)
- {
- damage = ownerstats.STR - p.stat.DEF;
- if (damage < 0) { damage = 0; }
- damage += n.Next(1, ownerstats.STR / 10);
- p.stat.HP = p.stat.HP - damage;
- }
- }
- public void Damage(monster m, spell s)
- {
- if (s.Info.ID == 0)
- {
- damage = ownerstats.STR - m.stat.DEF;
- if (damage < 0) { damage = 0; }
- damage += n.Next(1, ownerstats.STR / 10);
- m.stat.HP = m.stat.HP - damage;
- }
- }
- }
- [Serializable]
- public class Stats
- {
- public int MAXHP;
- public int XP;
- public int HP;
- public int MAN;
- public int STR;
- public int DEF;
- public int MAG;
- public int SPD;
- public int ACC;
- public int LVL;
- public string CLS;//W,S,M,R
- public int WIS;
- public int DEX;
- public String ELE;//I, F, P, D, E, W, A, B
- public int MAXMAN;
- }
- [Serializable]
- public class info
- {
- public string name;
- public int ID;
- public int ImageID;
- }
- [Serializable]
- public class Player
- {
- public string Session;
- public info Info = new info();
- public bool IsDead = false;
- public location loc = new location();
- public List<spell> spells = new List<spell>();
- public List<item> equip = new List<item>();
- public List<item> inventory = new List<item>();
- public Stats stat = new Stats();
- public List<quests> quest = new List<quests>();
- }
- [Serializable]
- public class quests
- {
- public info Info = new info();
- public string Ownername;
- public int OwnerID;
- public string Type;
- public int rewardXP;
- public Requirements require = new Requirements();
- public List<item> reward = new List<item>();
- public bool inprogress = false;
- public int RequirementID;
- public int Progress = 0;
- public int Goal;
- }
- [Serializable]
- public class monster
- {
- public monster() { }
- public monster(location l, int ID)
- {
- DataRow DR = DB.ExecSProcDS("GMGetMonsterById", ID).Tables[0].Rows[0];
- stat = GenerateStatsFromDataRow(DR);
- LOC = l;
- Info = GenerateInfoFromDR(DR);
- align = 1;
- }
- public info Info = new info();
- public int align; //0 Good, 1 Bad
- public Stats stat = new Stats();
- public location LOC = new location();
- }
- public static Requirements GenerateReqInfoFromDR(DataRow DR)
- {
- Requirements r = new Requirements();
- Stats s = new Stats();
- s.MAN = int.Parse(DR["RMAN"].ToString());
- s.STR = int.Parse(DR["RSTR"].ToString());
- s.DEF = int.Parse(DR["RDEF"].ToString());
- s.MAG = int.Parse(DR["RMAG"].ToString());
- s.SPD = int.Parse(DR["RSPD"].ToString());
- s.ACC = int.Parse(DR["RACC"].ToString());
- s.LVL = int.Parse(DR["RLVL"].ToString());
- s.CLS = DR["RCLS"].ToString();
- s.WIS = int.Parse(DR["RWIS"].ToString());
- s.DEX = int.Parse(DR["RDEX"].ToString());
- s.ELE = DR["RELE"].ToString();
- r.stat = s;
- return r;
- }
- [Serializable]
- public class item
- {
- public item() { }
- public item(int itemid)
- {
- DataRow DR = DB.ExecSProcDS("GMGetItemById", itemid).Tables[0].Rows[0];
- ItemID = itemid;
- stat = GenerateStatsFromDataRow(DR);
- Info = GenerateInfoFromDR(DR);
- requirements = GenerateReqInfoFromDR(DR);
- ItemType = DR["MAINTYPE"].ToString();
- SubType = DR["SUBTYPE"].ToString();
- }
- public info Info = new info();
- public int ItemID;
- public int Quantity;
- public string ItemType; //Weapon, Armor, Disposable, Permanent, Quest;
- public string SubType;
- public Stats stat = new Stats();
- public Requirements requirements = new Requirements();
- }
- [Serializable]
- public class location
- {
- public int XLOC;
- public int YLOC;
- public int AREAID;
- public int AreaX;
- public int AreaY;
- public int Direction;
- }
- public Player login(loginpackage l)
- {
- DataTable DT = DB.ExecSProcDS("GMLogin", l.username, l.password).Tables[0];
- DataRow DR = DT.Rows[0];
- if (DT.Rows.Count > 0)
- {
- return SpawnPlayer(int.Parse(DR["PlayerID"].ToString()));
- }
- return new Player();
- }
- [Serializable]
- public class loginpackage
- {
- public string username;
- public string password;
- public loginpackage(string u, string p)
- {
- username = u;
- password = p;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement