Advertisement
Anaristos

Using LINQ for mob object table search

Aug 18th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.46 KB | None | 0 0
  1. #region license
  2. /************************************************************************
  3. --[ License ] -----------------------------------------------------------
  4.  
  5.   This program is free software; you can redistribute it and/or
  6.   modify it under the terms of the GNU General Public License
  7.   as published by the Free Software Foundation; either version 3
  8.   of the License, or (at your option) any later version.
  9.  
  10.   This program is distributed in the hope that it will be useful,
  11.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.   GNU General Public License for more details.
  14.  
  15.   You should have received a copy of the GNU General Public License
  16.   along with this program; if not, write to the Free Software
  17.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.    
  19.   You may also access the licence here: http://www.gnu.org/licenses/gpl.html
  20.  
  21.  ------------------------------------------------------------------------
  22.   Copyright © 2009-2012 Gomez & Associates. All rights reserved.
  23. /************************************************************************/
  24. #endregion
  25.  
  26. using System;
  27. using System.Collections;
  28. using System.Linq;
  29. using System.Text.RegularExpressions;
  30.  
  31. namespace AardMapServices
  32. {
  33.     public partial class QueryManager
  34.     {
  35.         public string getMobs(string data)
  36.         {
  37.             _errormsg = "";
  38.  
  39.             if (data.Length > 0)
  40.             {
  41.                 ArrayList ur = (ArrayList) JSON.JSON.JsonDecode(data);
  42.  
  43.                 if (JSON.JSON.GeterrormsgIndex() != -1)
  44.                 {
  45.                     _errormsg = "JSON failure.";
  46.  
  47.                     return null;
  48.                 }
  49.  
  50.                 if (ur.Count != 2)
  51.                 {
  52.                     _errormsg = "Invalid number of parameters.";
  53.  
  54.                     return string.Empty;
  55.                 }
  56.  
  57.                 Int64 req = (Int64) ur[0];
  58.  
  59.                 Hashtable input = (Hashtable) ur[1];
  60.  
  61.                 bool mode = input.ContainsKey("mode") ? ((Int64) input["mode"] != 0) : true;
  62.  
  63.                 switch (req)
  64.                 {
  65.                     case 0: return dml(((string) input["mob"]).ToLower(), mode);
  66.  
  67.                     case 1: return dme(((string) input["mob"]).ToLower(), mode);
  68.  
  69.                     case 2: return dmr((Int64)   input["room"], mode);
  70.  
  71.                     case 3: return dmz((Int64)   input["zone"], mode);
  72.  
  73.                     case 4: return lzl(((string) input["mob"]).ToLower(), (object) input["zone"], mode);
  74.  
  75.                     case 5: return lze(((string) input["mob"]).ToLower(), (object) input["zone"], mode);
  76.  
  77.                    
  78.  
  79.                     default: _errormsg = "Invalid Request";
  80.  
  81.                         return string.Empty;
  82.                 }
  83.             }
  84.  
  85.             _errormsg = "Invalid Request Format";
  86.  
  87.             return string.Empty;
  88.         }
  89.  
  90.         public void AddMob(string name, Int16 zone, int room, int vnum, Int16 flags)
  91.         {
  92.             Mob mob = new Mob(name, zone, room, vnum, flags);
  93.  
  94.             int index = Mobs.BinarySearch(mob);
  95.  
  96.             if (index < 0)
  97.             {
  98.                 Mobs.Insert(~index, mob);
  99.  
  100.                 MobsThreadManager.EnqueueTask(() => InsertMob(mob));
  101.             }
  102.         }
  103.  
  104.         public void UpdateMob(string name, Int16 zone, Byte level)
  105.         {
  106.             bool state = false;
  107.  
  108.             var query = Mobs.AsParallel()
  109.                             .Where(m => m.Name.ToLower() == name.ToLower() && m.ZoneID == zone);
  110.  
  111.             if (query != null && query.Any())
  112.             {
  113.                 foreach (var mob in query)
  114.                 {
  115.                     if (mob.MinLevel > level || mob.MinLevel == 0)
  116.                     {
  117.                         mob.MinLevel = level;
  118.  
  119.                         state = true;
  120.                     }
  121.  
  122.                     if (mob.MaxLevel < level || mob.MaxLevel == 0)
  123.                     {
  124.                         mob.MaxLevel = level;
  125.  
  126.                         state = true;
  127.                     }
  128.                 }
  129.  
  130.                 if (state) MobsThreadManager.EnqueueTask(() => ChangeMob(name, zone, level));
  131.             }
  132.         }
  133.  
  134.         private string dml(string mob, bool mode)
  135.         {
  136.             var query = Mobs.AsParallel()
  137.                             .Where(m => m.Name.ToLower().Contains(mob))
  138.                             .Select(m => new { name = m.Name, room = mode ? m.RoomID : m.Vnum, zone = m.ZoneID })
  139.                             .ToLookup(g => new { g.zone, g.name })
  140.                             .OrderBy(g => g.Key.name)
  141.                             .Select(g => new { g.Key, row = g.OrderBy(x => x.room) });
  142.  
  143.             if (query != null && query.Any())
  144.             {
  145.                 ArrayList al = new ArrayList();
  146.  
  147.                 al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
  148.                                         ,{ "count", query.Count() }
  149.                                        });
  150.  
  151.                 foreach (var group in query)
  152.                 {
  153.                     ArrayList r = new ArrayList();
  154.  
  155.                     foreach (var row in group.row) r.Add(row.room);
  156.  
  157.                     al.Add(new Hashtable() { { group.Key.name, r } });
  158.                 }
  159.  
  160.                 return JSON.JSON.JsonEncode(al);
  161.             }
  162.  
  163.             _errormsg = "Mob not found";
  164.  
  165.             return string.Empty;
  166.         }
  167.  
  168.         private string dme(string mob, bool mode)
  169.         {
  170.             Regex regex = new Regex(@"(?i)(^|^a\s*|^an\s*|^the\s*)" + mob + @"$");
  171.  
  172.             var query = Mobs.AsParallel()
  173.                             .Where(m => ((regex.Match(m.Name)).Success))
  174.                             .Select(m => new { name = m.Name, room = mode ? m.RoomID : m.Vnum, zone = m.ZoneID })
  175.                             .ToLookup(g => new { g.zone, g.name })
  176.                             .OrderBy(g => g.Key.name)
  177.                             .Select(g => new { g.Key, row = g.OrderBy(x => x.room) });
  178.  
  179.             if (query != null && query.Any())
  180.             {
  181.                 ArrayList al = new ArrayList();
  182.  
  183.                 al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
  184.                                         ,{ "count", query.Count() }
  185.                                        });
  186.  
  187.                 foreach (var group in query)
  188.                 {
  189.                     ArrayList r = new ArrayList();
  190.  
  191.                     foreach (var row in group.row) r.Add(row.room);
  192.  
  193.                     al.Add(new Hashtable() { { group.Key.name, r } });
  194.                 }
  195.  
  196.                 return JSON.JSON.JsonEncode(al);
  197.             }
  198.  
  199.             _errormsg = "Mob not found";
  200.  
  201.             return string.Empty;
  202.         }
  203.  
  204.         private string dmr(Int64 roomid, bool mode)
  205.         {
  206.             var query = Mobs.AsParallel()
  207.                             .Where(m => mode ? m.RoomID == roomid : m.Vnum == roomid)
  208.                             .Select(m => new { name = m.Name, zone = m.ZoneID })
  209.                             .Join(Mobs.AsParallel()
  210.                                   ,m => new { m.name, m.zone }
  211.                                   ,r => new { name = r.Name, zone = r.ZoneID }
  212.                                   ,(m, r) => new { name = r.Name, room = mode ? r.RoomID : r.Vnum })
  213.                             .ToLookup(g => new { g.name })
  214.                             .OrderBy(g => g.Key.name)
  215.                             .Select(g => new { g.Key, row = g.OrderBy(r => r.room) });
  216.  
  217.             if (query != null && query.Any())
  218.             {
  219.                 ArrayList al = new ArrayList();
  220.  
  221.                 al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
  222.                                         ,{ "count", query.Count() }
  223.                                        });
  224.  
  225.                 foreach (var group in query)
  226.                 {
  227.                     ArrayList r = new ArrayList();
  228.  
  229.                     foreach (var row in group.row) r.Add(row.room);
  230.  
  231.                     al.Add(new Hashtable() { { group.Key.name, r } });
  232.                 }
  233.  
  234.                 return JSON.JSON.JsonEncode(al);
  235.             }
  236.  
  237.             _errormsg = "Mob not found";
  238.  
  239.             return string.Empty;
  240.         }
  241.  
  242.         private string dmz(Int64 zone, bool mode)
  243.         {
  244.             var query = Mobs.AsParallel()
  245.                             .Where(m => m.ZoneID == zone)
  246.                             .Select(m => new { name = m.Name, room = mode ? m.RoomID : m.Vnum })
  247.                             .ToLookup(g => new { g.name })
  248.                             .OrderBy(g => g.Key.name)
  249.                             .Select(g => new { g.Key, row = g.OrderBy(r => r.room) });
  250.  
  251.             if (query != null && query.Any())
  252.             {
  253.                 ArrayList al = new ArrayList();
  254.  
  255.                 al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
  256.                                         ,{ "count", query.Count() }
  257.                                        });
  258.  
  259.                 foreach (var group in query)
  260.                 {
  261.                     ArrayList r = new ArrayList();
  262.  
  263.                     foreach (var row in group.row) r.Add(row.room);
  264.  
  265.                     al.Add(new Hashtable() { { group.Key.name, r } });
  266.                 }
  267.  
  268.                 return JSON.JSON.JsonEncode(al);
  269.             }
  270.  
  271.             _errormsg = "Mob not found";
  272.  
  273.             return string.Empty;
  274.         }
  275.  
  276.         private string lzl(string mob, object zone, bool mode)
  277.         {
  278.             Int64 zid = (zone is Int64) ? (Int64)zone : (zone is string) ? getzone((string)zone) : -1;
  279.  
  280.             var query = Mobs.AsParallel()
  281.                             .Where(m => m.ZoneID == zid && m.Name.ToLower().Contains(mob))
  282.                             .Select(m => new { name = m.Name, room = mode ? m.RoomID : m.Vnum, zone = m.ZoneID })
  283.                             .ToLookup(g => new { g.zone, g.name })
  284.                             .OrderBy(g => g.Key.name)
  285.                             .Select(g => new { g.Key, row = g.OrderBy(x => x.room) });
  286.  
  287.             if (query != null && query.Any())
  288.             {
  289.                 ArrayList al = new ArrayList();
  290.  
  291.                 al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
  292.                                         ,{ "count", query.Count() }
  293.                                        });
  294.  
  295.                 foreach (var group in query)
  296.                 {
  297.                     ArrayList r = new ArrayList();
  298.  
  299.                     foreach (var row in group.row) r.Add(row.room);
  300.  
  301.                     al.Add(new Hashtable() { { group.Key.name, r } });
  302.                 }
  303.  
  304.                 return JSON.JSON.JsonEncode(al);
  305.             }
  306.  
  307.             _errormsg = "Mob not found";
  308.  
  309.             return string.Empty;
  310.         }
  311.  
  312.         private string lze(string mob, object zone, bool mode)
  313.         {
  314.             Regex regex = new Regex(@"(?i)(^|^a\s*|^an\s*|^the\s*)" + mob + @"$");
  315.  
  316.             Int64 zid = (zone is Int64) ? (Int64) zone : (zone is string) ? getzone((string) zone) : -1;
  317.  
  318.             if (zid != -1)
  319.             {
  320.                 var query = Mobs.AsParallel()
  321.                                 .Where(m => m.ZoneID == zid && (regex.Match(m.Name)).Success)
  322.                                 .Select(m => new { name = m.Name, room = mode ? m.RoomID : m.Vnum, zone = m.ZoneID })
  323.                                 .ToLookup(g => new { g.zone, g.name })
  324.                                 .OrderBy(g => g.Key.name)
  325.                                 .Select(g => new { g.Key, row = g.OrderBy(x => x.room) });
  326.  
  327.                 if (query != null && query.Any())
  328.                 {
  329.                     ArrayList al = new ArrayList();
  330.  
  331.                     al.Add(new Hashtable() { { "maxlen", query.Max(q => q.Key.name.Length) }
  332.                                             ,{ "count", query.Count() }
  333.                                            });
  334.  
  335.                     foreach (var group in query)
  336.                     {
  337.                         ArrayList r = new ArrayList();
  338.  
  339.                         foreach (var row in group.row) r.Add(row.room);
  340.  
  341.                         al.Add(new Hashtable() { { group.Key.name, r } });
  342.                     }
  343.  
  344.                     return JSON.JSON.JsonEncode(al);
  345.                 }
  346.             }
  347.  
  348.             _errormsg = "Mob not found";
  349.  
  350.             return string.Empty;
  351.         }
  352.     }  
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement