public override void Handle(User usr) { Subtype type = (Subtype)int.Parse(getBlock(0)); switch (type) { case Subtype.CheckForDuplicate: { string name = getBlock(1).Replace(" ", ""); if (name.Length > 4) { ClanManager.CheckForDuplicate(usr, name); } break; } case Subtype.AddClan: { string name = getBlock(1).Replace(" ", ""); if (name.Length > 4) { ClanManager.AddClan(usr, name); } break; } case Subtype.ApplyClan: { int clanId = int.Parse(getBlock(1)); if (clanId > 0) { string time = DateTime.Now.ToString("dd/MM/yyyy"); Clan clan = ClanManager.GetClan(clanId); if (clan != null) { if (clan.ClanUsers.Count >= clan.maxUsers) { return; } ClanPendingUsers pending = new ClanPendingUsers(usr.userId, usr.nickname, usr.exp.ToString(), time); clan.pendingUsers.TryAdd(usr.userId, pending); usr.clan = clan; usr.send(new SP_Clan(SP_Clan.ClanCodes.ApplyClan)); if (usr.clan != null) { DB.RunQuery("UPDATE clans_invite SET clanid='" + clanId + "' WHERE userid='" + usr.userId + "'"); } else { DB.RunQuery("INSERT INTO clans_invite (userid, clanid) VALUES ('" + usr.userId + "', '" + clanId + "')"); } DB.RunQuery("UPDATE users SET clanrank='9', clanid='" + clanId + "' WHERE id='" + usr.userId + "'"); } } break; } case Subtype.LeaveClan: { if (usr.clan != null) { Clan clan = usr.clan; if (clan.pendingUsers.ContainsKey(usr.userId)) { ClanPendingUsers u; clan.pendingUsers.TryRemove(usr.userId, out u); } int clanrank = clan.clanRank(usr); if (clanrank == 2) { foreach (User u in clan.GetUsers()) { u.clan = null; } ClanManager.RemoveClan(usr); DB.RunQuery("UPDATE users SET clanid='-1', clanrank='0' WHERE clanid='" + clan.id + "'"); DB.RunQuery("DELETE FROM clans WHERE id='" + clan.id + "'"); } else { usr.clan = null; if (clanrank == 9) { ClanPendingUsers u; clan.pendingUsers.TryRemove(usr.userId, out u); } else { ClanUsers u; clan.ClanUsers.TryRemove(usr.userId, out u); } usr.send(new SP_Clan(SP_Clan.ClanCodes.LeaveClan)); DB.RunQuery("UPDATE users SET clanid='-1', clanrank='0' WHERE id='" + usr.userId + "'"); DB.RunQuery("UPDATE clans SET count='" + usr.clan.ClanUsers.Count + "' WHERE id='" + usr.clan.id + "'"); } } break; } case Subtype.MyClan: { if (usr.clan == null) { return; } usr.send(new SP_Clan.MyClanInformation(usr)); break; } case Subtype.Members: { if (usr.clan == null) { return; } int page = int.Parse(getBlock(1)); int clanrank = usr.clan.clanRank(usr); if (page == 1) { usr.send(new SP_Clan.UserList.NormalUser(usr.clan)); } else if (page == 2 && (clanrank == 1 || clanrank == 2)) { usr.send(new SP_Clan.UserList.Pending(usr)); } break; } case Subtype.SearchClan: { int t = int.Parse(getBlock(1)); SearchType subtype = (SearchType)t; int page = int.Parse(getBlock(1)); string key = getBlock(2); List <Clan> list = new List <Clan>(); switch (subtype) { case SearchType.Name: { list = new List <Clan>(ClanManager.Clans.Values.Where(c => c != null && c.name.ToLower().Contains(key.ToLower())).ToArray()); break; } case SearchType.Master: { list = new List <Clan>(ClanManager.Clans.Values.Where(c => c != null && c.Master.ToLower().Contains(key.ToLower())).ToArray()); break; } default: { list = new List <Clan>(ClanManager.Clans.Values.Where(c => c != null && (c.Master.ToLower().Contains(key.ToLower()) || c.name.ToLower().Contains(key.ToLower()))).ToArray()); break; } } if (key.Length >= 3) { usr.send(new SP_Clan.SearchClan(list)); } else { usr.send(new SP_Clan.CheckClan(SP_Clan.CheckClan.ErrorCodes.NotFound)); } break; } case Subtype.ClanInfo: { int clanId = int.Parse(getBlock(1)); if (clanId > 0) { Clan clan = ClanManager.GetClan(clanId); if (clan != null) { usr.send(new SP_Clan.UserList.NormalUser(usr, clan)); } } break; } case Subtype.ChangeAnnDec: { if (usr.clan == null) { return; } int clanrank = usr.clan.clanRank(usr); if (clanrank < 1 || clanrank == 9 || usr.clan == null) { return; } string Message = DB.Stripslash(getBlock(2)); bool description = getBlock(1) == "0"; if (description) { usr.clan.Description = Message; } else { usr.clan.Announcment = Message; } // Send query after to let the server be lagfree DB.RunQuery("UPDATE clans SET description='" + usr.clan.Description + "', announcment='" + usr.clan.Announcment + "' WHERE id='" + usr.clan.id + "'"); break; } case Subtype.JoinAction: { if (usr.clan == null) { return; } int subtype = int.Parse(getBlock(1)); int userId = int.Parse(getBlock(2)); Clan clan = usr.clan; switch (subtype) { case 0: // Accept Join { if (clan.ClanUsers.Count >= clan.maxUsers) { usr.send(new SP_Chat("SYSTEM", SP_Chat.ChatType.Whisper, "SYSTEM >> No more slot available for the clan, please expand if is possible", usr.sessionId, usr.nickname)); return; } string time = DateTime.Now.ToString("dd/MM/yyyy"); DataTable dt = DB.RunReader("SELECT * FROM users WHERE id='" + userId + "'"); if (dt.Rows.Count > 0) { DataRow row = dt.Rows[0]; DB.RunQuery("DELETE FROM clans_invite WHERE userid='" + userId + "'"); DB.RunQuery("UPDATE clans SET count='" + clan.ClanUsers.Count + "' WHERE id='" + clan.id + "'"); DB.RunQuery("UPDATE users SET clanid='" + clan.id + "', clanrank='0', clanjoindate='" + time + "' WHERE id='" + userId + "'"); if (clan.pendingUsers.ContainsKey(userId)) { ClanPendingUsers u; clan.pendingUsers.TryRemove(userId, out u); } ClanUsers c = new ClanUsers(userId, row["nickname"].ToString(), row["exp"].ToString(), time, 0); clan.ClanUsers.TryAdd(userId, c); } User user = UserManager.GetUser(userId); if (user != null) { user.clan = clan; clan.Users.TryAdd(userId, user); } ClanPendingUsers cc; if (clan.pendingUsers.ContainsKey(userId)) { clan.pendingUsers.TryRemove(userId, out cc); } break; } case 1: // Refuse Join { DB.RunQuery("DELETE FROM clans_invite WHERE userid='" + userId + "'"); DB.RunQuery("UPDATE users SET clanid='-1', clanrank='0' WHERE id='" + userId + "'"); User u = UserManager.GetUser(userId); if (u != null) { u.clan = null; } ClanPendingUsers c; if (clan.pendingUsers.ContainsKey(userId)) { clan.pendingUsers.TryRemove(userId, out c); } break; } } DB.RunQuery("DELETE FROM clans_invite WHERE userid='" + userId + "'"); usr.send(new SP_Clan.UserList.Pending(subtype, userId)); break; } case Subtype.RankAction: { if (usr.clan == null) { return; } int subtype = int.Parse(getBlock(1)); int userId = int.Parse(getBlock(2)); int clanrank = usr.clan.clanRank(usr); if (clanrank >= 1) { clanrank = 0; switch (subtype) { case 0: { clanrank = 1; DB.RunQuery("UPDATE users SET clanrank='1' WHERE id='" + userId + "'"); break; } case 1: { clanrank = 0; DB.RunQuery("UPDATE users SET clanrank='0' WHERE id='" + userId + "'"); break; } case 2: { DB.RunQuery("UPDATE users SET clanid='-1', clanrank='0' WHERE id='" + userId + "'"); DB.RunQuery("UPDATE clans SET count='" + usr.clan.ClanUsers.Count + "' WHERE id='" + usr.clan.id + "'"); User u2; ClanUsers u; usr.clan.Users.TryRemove(userId, out u2); usr.clan.ClanUsers.TryRemove(userId, out u); break; } } if (subtype != 2) { usr.clan.ClanUsers.Values.Where(s => s.id == userId).First().clanrank = clanrank; } else { User u = UserManager.GetUser(userId); if (u != null) { u.clan = null; } } usr.send(new SP_Clan.Change(subtype, userId)); } break; } case Subtype.Promote: { if (usr.clan == null) { return; } int userId = int.Parse(getBlock(1)); Clan clan = usr.clan; if (clan != null) { DataTable dt = DB.RunReader("SELECT * FROM users WHERE id='" + userId + "'"); if (dt.Rows.Count > 0) { DataRow row = dt.Rows[0]; if (userId != usr.userId) { DB.RunQuery("UPDATE users SET clanrank='0' WHERE id='" + usr.userId + "'"); DB.RunQuery("UPDATE users SET clanrank='2' WHERE id='" + userId + "'"); clan.Master = row["nickname"].ToString(); clan.MasterEXP = row["exp"].ToString(); clan.ClanUsers.Values.Where(r => string.Compare(r.nickname, clan.Master, true) == 0).First().clanrank = 2; clan.ClanUsers.Values.Where(r => string.Compare(r.nickname, usr.nickname, true) == 0).First().clanrank = 0; byte[] buffer = (new SP_Chat("ClanSystem", SP_Chat.ChatType.Clan, "ClanSystem >> " + usr.nickname + " passed master to " + clan.Master + " :/", (uint)clan.id, "NULL")).GetBytes(); foreach (User u in clan.Users.Values) { u.sendBuffer(buffer); } usr.send(new SP_Clan.Change()); } } } break; } case Subtype.NickChange: { if (usr.clan == null) { return; } string newNick = getBlock(1); Clan c = ClanManager.GetClanByName(newNick); if (usr.clan != null) { if (c == null) { if (usr.HasItem("CB02")) //>--- CHG_CLANNICK { DB.RunQuery("UPDATE clans SET name='" + newNick + "' WHERE id='" + usr.clan.id + "'"); c.name = newNick; usr.deleteItem("CB02"); usr.send(new SP_Clan.Change(usr, true)); } } else { usr.send(new SP_Chat("SYSTEM", SP_Chat.ChatType.Whisper, "SYSTEM >> A clan has already this name, please choose another one", usr.sessionId, usr.nickname)); } } break; } case Subtype.MarkChange: { if (usr.clan == null) { return; } uint iconID = uint.Parse(getBlock(1)); if (usr.HasItem("CB54") && usr.clan != null) //>--- CHG_CLANMARK { DB.RunQuery("UPDATE clans SET iconid='" + iconID + "' WHERE id='" + usr.clan.id + "'"); usr.clan.iconid = iconID; usr.deleteItem("CB54"); usr.send(new SP_Clan.Change(usr, false)); } break; } case Subtype.DisbandClan: { if (usr.clan == null) { return; } ClanManager.RemoveClan(usr); break; } case Subtype.NewSearchClan: { int page = int.Parse(getBlock(1)); int sortType = int.Parse(getBlock(2)); List <Clan> clans = new List <Clan>(); switch (sortType) { case 0: // By rank desc { clans = ClanManager.Clans.Values.OrderByDescending(r => r.exp).Skip(page * 10).Take(10).ToList(); break; } case 1: // By rank asc { clans = ClanManager.Clans.Values.OrderBy(r => r.exp).Skip(page * 10).Take(10).ToList(); break; } } usr.send(new SP_Clan.SearchClan(page, sortType, clans)); break; } default: { Log.WriteError("Unknown Clan Subtype " + (int)Subtype.DisbandClan); break; } } }