Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region license
- /************************************************************************
- --[ License ] -----------------------------------------------------------
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 3
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- You may also access the licence here: http://www.gnu.org/licenses/gpl.html
- ------------------------------------------------------------------------
- Copyright © 2009-2012 Gomez & Associates. All rights reserved.
- /************************************************************************/
- #endregion
- using System;
- using System.Collections;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace AardMapServices
- {
- public partial class QueryManager
- {
- public string getMobs(string data)
- {
- _errormsg = "";
- if (data.Length > 0)
- {
- ArrayList ur = (ArrayList) JSON.JSON.JsonDecode(data);
- if (JSON.JSON.GeterrormsgIndex() != -1)
- {
- _errormsg = "JSON failure.";
- return null;
- }
- if (ur.Count != 2)
- {
- _errormsg = "Invalid number of parameters.";
- return string.Empty;
- }
- Int64 req = (Int64) ur[0];
- Hashtable input = (Hashtable) ur[1];
- bool mode = input.ContainsKey("mode") ? ((Int64) input["mode"] != 0) : true;
- switch (req)
- {
- case 0: return dml(((string) input["mob"]).ToLower(), mode);
- case 1: return dme(((string) input["mob"]).ToLower(), mode);
- case 2: return dmr((Int64) input["room"], mode);
- case 3: return dmz((Int64) input["zone"], mode);
- case 4: return lzl(((string) input["mob"]).ToLower(), (object) input["zone"], mode);
- case 5: return lze(((string) input["mob"]).ToLower(), (object) input["zone"], mode);
- default: _errormsg = "Invalid Request";
- return string.Empty;
- }
- }
- _errormsg = "Invalid Request Format";
- return string.Empty;
- }
- public void AddMob(string name, Int16 zone, int room, int vnum, Int16 flags)
- {
- Mob mob = new Mob(name, zone, room, vnum, flags);
- int index = Mobs.BinarySearch(mob);
- if (index < 0)
- {
- Mobs.Insert(~index, mob);
- MobsThreadManager.EnqueueTask(() => InsertMob(mob));
- }
- }
- public void UpdateMob(string name, Int16 zone, Byte level)
- {
- bool state = false;
- var query = Mobs.AsParallel()
- .Where(m => m.Name.ToLower() == name.ToLower() && m.ZoneID == zone);
- if (query != null && query.Any())
- {
- foreach (var mob in query)
- {
- if (mob.MinLevel > level || mob.MinLevel == 0)
- {
- mob.MinLevel = level;
- state = true;
- }
- if (mob.MaxLevel < level || mob.MaxLevel == 0)
- {
- mob.MaxLevel = level;
- state = true;
- }
- }
- if (state) MobsThreadManager.EnqueueTask(() => ChangeMob(name, zone, level));
- }
- }
- private string dml(string mob, bool mode)
- {
- var query = Mobs.AsParallel()
- .Where(m => m.Name.ToLower().Contains(mob))
- .Select(m => new { name = m.Name, room = mode ? m.RoomID : m.Vnum, zone = m.ZoneID })
- .ToLookup(g => new { g.zone, g.name })
- .OrderBy(g => g.Key.name)
- .Select(g => new { g.Key, row = g.OrderBy(x => x.room) });
- if (query != null && query.Any())
- {
- ArrayList al = new ArrayList();
- al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
- ,{ "count", query.Count() }
- });
- foreach (var group in query)
- {
- ArrayList r = new ArrayList();
- foreach (var row in group.row) r.Add(row.room);
- al.Add(new Hashtable() { { group.Key.name, r } });
- }
- return JSON.JSON.JsonEncode(al);
- }
- _errormsg = "Mob not found";
- return string.Empty;
- }
- private string dme(string mob, bool mode)
- {
- Regex regex = new Regex(@"(?i)(^|^a\s*|^an\s*|^the\s*)" + mob + @"$");
- var query = Mobs.AsParallel()
- .Where(m => ((regex.Match(m.Name)).Success))
- .Select(m => new { name = m.Name, room = mode ? m.RoomID : m.Vnum, zone = m.ZoneID })
- .ToLookup(g => new { g.zone, g.name })
- .OrderBy(g => g.Key.name)
- .Select(g => new { g.Key, row = g.OrderBy(x => x.room) });
- if (query != null && query.Any())
- {
- ArrayList al = new ArrayList();
- al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
- ,{ "count", query.Count() }
- });
- foreach (var group in query)
- {
- ArrayList r = new ArrayList();
- foreach (var row in group.row) r.Add(row.room);
- al.Add(new Hashtable() { { group.Key.name, r } });
- }
- return JSON.JSON.JsonEncode(al);
- }
- _errormsg = "Mob not found";
- return string.Empty;
- }
- private string dmr(Int64 roomid, bool mode)
- {
- var query = Mobs.AsParallel()
- .Where(m => mode ? m.RoomID == roomid : m.Vnum == roomid)
- .Select(m => new { name = m.Name, zone = m.ZoneID })
- .Join(Mobs.AsParallel()
- ,m => new { m.name, m.zone }
- ,r => new { name = r.Name, zone = r.ZoneID }
- ,(m, r) => new { name = r.Name, room = mode ? r.RoomID : r.Vnum })
- .ToLookup(g => new { g.name })
- .OrderBy(g => g.Key.name)
- .Select(g => new { g.Key, row = g.OrderBy(r => r.room) });
- if (query != null && query.Any())
- {
- ArrayList al = new ArrayList();
- al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
- ,{ "count", query.Count() }
- });
- foreach (var group in query)
- {
- ArrayList r = new ArrayList();
- foreach (var row in group.row) r.Add(row.room);
- al.Add(new Hashtable() { { group.Key.name, r } });
- }
- return JSON.JSON.JsonEncode(al);
- }
- _errormsg = "Mob not found";
- return string.Empty;
- }
- private string dmz(Int64 zone, bool mode)
- {
- var query = Mobs.AsParallel()
- .Where(m => m.ZoneID == zone)
- .Select(m => new { name = m.Name, room = mode ? m.RoomID : m.Vnum })
- .ToLookup(g => new { g.name })
- .OrderBy(g => g.Key.name)
- .Select(g => new { g.Key, row = g.OrderBy(r => r.room) });
- if (query != null && query.Any())
- {
- ArrayList al = new ArrayList();
- al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
- ,{ "count", query.Count() }
- });
- foreach (var group in query)
- {
- ArrayList r = new ArrayList();
- foreach (var row in group.row) r.Add(row.room);
- al.Add(new Hashtable() { { group.Key.name, r } });
- }
- return JSON.JSON.JsonEncode(al);
- }
- _errormsg = "Mob not found";
- return string.Empty;
- }
- private string lzl(string mob, object zone, bool mode)
- {
- Int64 zid = (zone is Int64) ? (Int64)zone : (zone is string) ? getzone((string)zone) : -1;
- var query = Mobs.AsParallel()
- .Where(m => m.ZoneID == zid && m.Name.ToLower().Contains(mob))
- .Select(m => new { name = m.Name, room = mode ? m.RoomID : m.Vnum, zone = m.ZoneID })
- .ToLookup(g => new { g.zone, g.name })
- .OrderBy(g => g.Key.name)
- .Select(g => new { g.Key, row = g.OrderBy(x => x.room) });
- if (query != null && query.Any())
- {
- ArrayList al = new ArrayList();
- al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
- ,{ "count", query.Count() }
- });
- foreach (var group in query)
- {
- ArrayList r = new ArrayList();
- foreach (var row in group.row) r.Add(row.room);
- al.Add(new Hashtable() { { group.Key.name, r } });
- }
- return JSON.JSON.JsonEncode(al);
- }
- _errormsg = "Mob not found";
- return string.Empty;
- }
- private string lze(string mob, object zone, bool mode)
- {
- Regex regex = new Regex(@"(?i)(^|^a\s*|^an\s*|^the\s*)" + mob + @"$");
- Int64 zid = (zone is Int64) ? (Int64) zone : (zone is string) ? getzone((string) zone) : -1;
- if (zid != -1)
- {
- var query = Mobs.AsParallel()
- .Where(m => m.ZoneID == zid && (regex.Match(m.Name)).Success)
- .Select(m => new { name = m.Name, room = mode ? m.RoomID : m.Vnum, zone = m.ZoneID })
- .ToLookup(g => new { g.zone, g.name })
- .OrderBy(g => g.Key.name)
- .Select(g => new { g.Key, row = g.OrderBy(x => x.room) });
- if (query != null && query.Any())
- {
- ArrayList al = new ArrayList();
- al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
- ,{ "count", query.Count() }
- });
- foreach (var group in query)
- {
- ArrayList r = new ArrayList();
- foreach (var row in group.row) r.Add(row.room);
- al.Add(new Hashtable() { { group.Key.name, r } });
- }
- return JSON.JSON.JsonEncode(al);
- }
- }
- _errormsg = "Mob not found";
- return string.Empty;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement