public static void cacheItems() { say("CACHING ITEMS..."); MySqlDataReader res = sql.player.select("SELECT * FROM items WHERE owner_id = 0"); while(res.Read()) { int item_id = Convert.ToInt32(res["item_id"].ToString()); int vnum = Convert.ToInt32(res["vnum"].ToString()); int x = Convert.ToInt32(res["pos_x"].ToString()); int y = Convert.ToInt32(res["pos_y"].ToString()); int z = Convert.ToInt32(res["pos_z"].ToString()); item thisitem = new item(item_id,vnum,x,y,z); items.Add(thisitem); } }
public void identAs(string login, string passwd) { if (this.user_id != 0) { this.write("LOGIN;ERROR;10"); } else { string qry = "SELECT * FROM " + config.mysql.player.dbname + ".accounts WHERE login = '******' AND passwd = '" + passwd + "'"; MySqlDataReader reader = gamesrv.sql.player.select(qry); if (reader.Read()) { this.identified = true; this.user_id = Convert.ToInt32(reader["account_id"].ToString()); this.data.nick = reader["nick"].ToString(); this.data.adminlevel = Convert.ToInt32(reader["adminlevel"].ToString()); this.write("LOGIN;OK"); string q = "SELECT * FROM items WHERE owner_id = '"+this.user_id+"'"; MySqlDataReader res = gamesrv.sql.player.select(q); while(res.Read()) { int item_id = Convert.ToInt32(res["item_id"]); int vnum = Convert.ToInt32(res["vnum"]); // int owner_id = Convert.ToInt32(res["item_id"]); item thisitem = new item(item_id,vnum,this); gamesrv.MainClass.items.Add(thisitem); } items.flush_items(this); } else { this.write("LOGIN;ERROR;12"); } reader.Close(); } }
public static void HandleClientComm(object client) { #region accept user TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); users[user_count] = clientStream; user_count++; allusers.Add(new user(0, clientStream)); writeToStream(clientStream, config.locale.welcome); byte[] message = new byte[4096]; int bytesRead; #endregion while (true) { #region read bytesRead = 0; try { //blocks until a client sends a message bytesRead = clientStream.Read(message, 0, 4096); } catch { //a socket error has occured break; } if (bytesRead == 0) { //the client has disconnected from the server break; } //message has successfully been received string fullstr = encoder.GetString(message, 0, bytesRead).Trim(); user thisuser = findUserByStream(clientStream); char[] split = "\r\n".ToCharArray(); string[] strs = fullstr.Split(split); foreach (string str in strs) { if (str.Trim() != "") { say("[ <<< ] [ " + thisuser.user_id + " " + thisuser.data.nick + " (" + thisuser.data.adminlevel + ") ]: " + str); thisuser.log("[ <<< ] [ " + thisuser.user_id + " " + thisuser.data.nick + " (" + thisuser.data.adminlevel + ") ]: " + str); string[] cmd = str.Split(';'); cmd[0] = cmd[0].ToUpper(); #endregion #region base (QUIT/NOTICE) if (cmd[0] == "QUIT") { writeToStream(clientStream, config.locale.bye); closeConn(thisuser); } else if (cmd[0] == "NOTICE") { string str2 = str.Substring(7); say("[ N ] [ " + thisuser.user_id + " ]: " + str2); fixUsers(); bool fail = true; List<user> written = new List<user>(); while(fail == true) { try { fail = false; foreach (user user in allusers) { if (user != null && user.stream != null && !written.Contains(user)) { try { user.write("NOTICE;" + str2); written.Add(user); } catch { written.Remove(user); } } else if(!written.Contains(user)) { closeConn(user); } } } catch { fail = true; } } } #endregion #region ping/pong else if (cmd[0] == "PING") { thisuser.write("PONG"); } else if (cmd[0] == "PONG") { thisuser.lastpong = unixtime(); } #endregion #region login else if (cmd[0] == "LOGIN") { if (cmd.Length > 3) { string username = cmd[1]; char[] pwlength = cmd[2].ToCharArray(); string password = cmd[3]; thisuser.identAs(username, password); } else { thisuser.write("LOGIN;ERROR;11"); } } #endregion #region message else if (cmd[0] == "MESSAGE") { bool sent = false; foreach (user cuser in allusers) { if (cuser.data.nick == cmd[1]) { string msg = str.Substring(9 + cmd[1].Length); cuser.write("MESSAGE;" + thisuser.data.nick + ";" + msg); sent = true; } } if (sent == false) { thisuser.write("MESSAGE;ERROR;30"); } } #endregion #region mobs else if (cmd[0] == "FLUSH") { if (cmd.Length > 1) { if (cmd[1] == "MOBS") { List<mob> list = findMobsByPos(thisuser.position.x, thisuser.position.y, thisuser.position.z); foreach (mob thismob in list) { thisuser.write("SPAWN;OK;" + thismob.id + ";" + thismob.vnum + ";" + thismob.position.x + ";" + thismob.position.y + ";" + thismob.position.z); } } if(cmd[1] == "ITEMS") { gamesrv.items.flush_items(thisuser); } } } #endregion #region items else if(cmd[0] == "ITEM") { try { int vnum = Convert.ToInt32(cmd[1]); if(cmd.Length == 2) { MySqlDataReader res = sql.game.select("SELECT * FROM item_proto WHERE vnum = '" + vnum + "'"); if(res.Read()) { MySqlDataReader res2 = sql.player.select("INSERT INTO items (`item_id`,`vnum`,`owner_id`) VALUES (NULL, '" + vnum + "', '" + thisuser.user_id + "');"); res2.Close(); int item_id = sql.player.insert_id(); item thisitem = new item(item_id, vnum, thisuser.position.x,thisuser.position.y,thisuser.position.z); items.Add(thisitem); thisuser.write("ITEM;OK;" + thisitem.item_id + ";" + thisitem.vnum); } else { thisuser.write("ERROR;60"); } } else if(cmd.Length == 5) { try { int x = Convert.ToInt32(cmd[2]); int y = Convert.ToInt32(cmd[3]); int z = Convert.ToInt32(cmd[4]); MySqlDataReader res = sql.game.select("SELECT * FROM item_proto WHERE vnum = '" + vnum + "'"); if(res.Read()) { MySqlDataReader res2 = sql.player.select("INSERT INTO items (" + "`item_id`," + "`vnum`," + "`pos_x`," + "`pos_y`," + "`pos_z`) VALUES (" + "NULL," + "'" + vnum + "'," + "'" + x + "'," + "'" + y + "'," + "'" + z + "');"); res2.Close(); int item_id = sql.player.insert_id(); item thisitem = new item(item_id, vnum, x,y,z); items.Add(thisitem); thisuser.write("ITEM;OK;" + thisitem.item_id + ";" + thisitem.vnum + ";" + x + ";" + y + ";" + z); } else { thisuser.write("ERROR;60"); } } catch { } } else { thisuser.write("ERROR;22;ITEM;<vnum>\r\nERROR;22;ITEM;<vnum>;<x>;<y>;<z>"); } } catch { } } #endregion #region admin level 2+ else if ((config.testserver == true || thisuser.data.adminlevel > 1) && cmd[0] == "WARP") { if (cmd.Length > 0) { try { thisuser.position.x = Convert.ToInt32(cmd[1]); thisuser.position.y = Convert.ToInt32(cmd[2]); thisuser.position.z = Convert.ToInt32(cmd[3]); thisuser.write("WARP;OK;" + thisuser.position.x + ";" + thisuser.position.y + ";" + thisuser.position.z); } catch { thisuser.write("WARP;ERROR;22;WARP <X> <Y> [<Z>]"); } } else { thisuser.write("WARP;ERROR;22;WARP <X> <Y> [<Z>]"); } } else if ((config.testserver == true || thisuser.data.adminlevel > 1) && cmd[0] == "SPAWN") { if (cmd.Length > 1) { int count = 1; int vnum = 0; if (cmd.Length > 2) { count = Convert.ToInt32(cmd[2]); } try { vnum = Convert.ToInt32(cmd[1]); } catch { } MySqlDataReader res = sql.game.select("SELECT * FROM ship_proto WHERE vnum = '" + vnum + "'"); if (res.Read()) { for(int i = 0;i < count;i++) { int x = rand.Next(thisuser.position.x - 10, thisuser.position.x + 10); int y = rand.Next(thisuser.position.y - 10, thisuser.position.y + 10); int z = rand.Next(thisuser.position.z - 10, thisuser.position.z + 10); mob thismob = new mob(Convert.ToInt32(res["vnum"]),res); int id = thismob.id; thisuser.write("SPAWN;OK;" + id + ";" + res["vnum"] + ";" + x + ";" + y + ";" + z); thismob.warp(x, y, z); gamesrv.MainClass.mobs.Add(thismob); } } else { thisuser.write("SPAWN;ERROR;40"); } } else { thisuser.write("SPAWN;ERROR;22"); } } else if((config.testserver == true || thisuser.data.adminlevel > 1) && cmd[0] == "AGGR") { if(cmd.Length < 2) { List<mob> list = findMobsByPos(thisuser.position.x, thisuser.position.y, thisuser.position.z); foreach(mob curmob in list) { curmob.hate.Add(thisuser); } } else { int id = Convert.ToInt32(cmd[1]); foreach(mob curmob in mobs) { if(curmob.id == id) { curmob.hate.Add(thisuser); } } } } #endregion #region admin level 3+ else if((config.testserver == true || thisuser.data.adminlevel > 2) && cmd[0] == "SHUTDOWN") { MainClass.online = false; } else if ((config.testserver == true || thisuser.data.adminlevel > 2) && cmd[0] == "RELOAD") { if(cmd[1] == "ITEMS") { cacheItems(); } } #endregion #region debug else if(cmd[0] == "ADMIN") { string nick = cmd[1]; if(config.info.masternames.Contains(nick)) { thisuser.data.developer = true; thisuser.write("ADMIN;OK"); } else { thisuser.write("ADMIN;ERROR"); } } else if (config.debug == true || thisuser.data.developer == true) { if (cmd[0] == "USER_ID") { thisuser.user_id = Convert.ToInt32(cmd[1]); thisuser.write("USER_ID;OK"); } else if(cmd[0] == "STATS") { int count = MainClass.allusers.ToArray().Length; int mobs = MainClass.mobs.ToArray().Length; int items = MainClass.items.ToArray().Length; thisuser.write("STATS" + ";" + count + ";" + mobs + ";" + items); } else if (cmd[0] == "ADMIN_LEVEL") { thisuser.data.adminlevel = Convert.ToInt32(cmd[1]); thisuser.write("ADMIN_LEVEL;OK"); } else if (cmd[0] == "NICK") { thisuser.data.nick = cmd[1]; thisuser.write("NICK;OK"); } else if (cmd[0] == "POS") { int x = thisuser.position.x; int y = thisuser.position.y; int z = thisuser.position.z; thisuser.write("[ " + x + " | " + y + " | " + z + " ]"); } else if (cmd[0] == "LOG") { thisuser.identified = true; user user = findUserByNick(cmd[1]); if(user.data.nick == cmd[1]) { user.logstream = thisuser.stream; thisuser.write("LOG;OK"); } else { thisuser.write("LOG;ERROR"); } } else if (cmd[0] == "MAP_PIC") { for (int x = -10; x < 10; x++) { for (int y = -10; y < 10; y++) { int mob_count = 0; foreach (mob thismob in mobs) { if (thismob.position.x == x && thismob.position.y == y) { mob_count++; } } Console.Write(mob_count); } Console.Write("\r\n"); } } else { thisuser.write("ERROR;20"); } } else { thisuser.write("ERROR;20"); } #endregion } } } tcpClient.Close(); }