1
0
Fork 0
vrobot3/Source/Modules/Mod_Idgames.cs

185 lines
5.3 KiB
C#
Raw Normal View History

2016-10-12 21:58:16 -07:00
//-----------------------------------------------------------------------------
//
// Copyright © 2016 Project Golan
//
// See "LICENSE" for more information.
//
//-----------------------------------------------------------------------------
2016-10-27 17:57:00 -07:00
//
2016-10-12 21:58:16 -07:00
// Idgames search module.
// .idgames
//
//-----------------------------------------------------------------------------
using System;
using System.Net;
using System.Web;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
namespace ProjectGolan.Vrobot3.Modules
{
//
// Mod_Idgames
//
public class Mod_Idgames : IBotModule
{
2016-10-27 17:57:00 -07:00
private const String APIURI =
2016-10-12 21:58:16 -07:00
"http://doomworld.com/idgames/api/api.php";
private Random rnd = Utils.GetRND();
//
// Mod_Idgames constructor
//
2016-10-27 17:57:00 -07:00
public Mod_Idgames(Bot bot) :
base(bot)
2016-10-12 21:58:16 -07:00
{
commands["idgames"] = new BotCommandStructure{
cmd = cmdIdgames,
help = "Gets an entry from the idgames archive.\n" +
"Syntax: .idgames [name or ID[, type[, position]]]\n" +
"Example: .idgames scythe, filename, 4\n" +
"Example: .idgames"
};
postSetup();
2016-10-12 21:58:16 -07:00
}
//
// cmdIdgames
//
public void cmdIdgames(User usr, Channel channel, String msg)
{
2016-10-27 17:57:00 -07:00
var args = Utils.GetArguments(msg, commands["idgames"].help, 0, 3);
2016-10-12 21:58:16 -07:00
switch(args.Length)
{
2016-10-27 17:57:00 -07:00
default:
2016-10-12 21:58:16 -07:00
case 1:
int id;
if(args[0].Trim().Length == 0)
idgamesRandom(usr, channel);
else if(Int32.TryParse(args[0], out id))
idgamesID(usr, channel, id);
else
idgames(usr, channel, args[0]);
break;
case 2: idgames(usr, channel, args[0], args[1]); break;
case 3:
2016-10-27 17:57:00 -07:00
idgames(usr, channel, args[0], args[1], args[2].Trim());
2016-10-12 21:58:16 -07:00
break;
}
}
//
// idgamesRandom
//
private void idgamesRandom(User usr, Channel channel)
{
var req = WebRequest.Create("http://doomworld.com/idgames/?random")
as HttpWebRequest;
2016-10-27 17:57:00 -07:00
if(req == null) throw new CommandArgumentException("fug it borked");
2016-10-12 21:58:16 -07:00
req.Referer = "http://doomworld.com/idgames/";
bot.message(channel,
Discord.Format.Escape(req.GetResponse().ResponseUri.ToString()));
}
//
// idgamesID
//
private void idgamesID(User usr, Channel channel, int id)
{
var req = WebRequest.Create(APIURI + "?action=get&id=" + id)
as HttpWebRequest;
2016-10-27 17:57:00 -07:00
if(req == null) throw new CommandArgumentException("fug it borked");
2016-10-12 21:58:16 -07:00
using(var response = req.GetResponse())
{
var xml = XDocument.Load(response.GetResponseStream());
var x_title =
from item in xml.Descendants("title") select item.Value;
var x_uri = from item in xml.Descendants("url") select item.Value;
if(!x_title.Any())
{
bot.message(channel, "Nothing found.");
return;
}
bot.message(channel,
Discord.Format.Escape(x_title.First() + ": " + x_uri.First()));
}
}
//
// idgames
//
private void idgames(User usr, Channel channel, String inquiry,
String type = "title", String pos = "1")
{
2016-10-27 17:57:00 -07:00
var ipos = 0;
2016-10-12 21:58:16 -07:00
2016-10-27 17:57:00 -07:00
if(pos.ToLower() != "random")
2016-10-12 21:58:16 -07:00
{
Utils.TryParse(pos, "Invalid position.", out ipos);
if(ipos < 1)
throw new CommandArgumentException("Invalid position.");
ipos = ipos - 1;
}
inquiry = HttpUtility.UrlEncode(inquiry.Trim());
type = HttpUtility.UrlEncode(type.Trim().ToLower());
if(type == "name") type = "title"; // >_>'
String[] validtypes = {
"filename", "title", "author", "email",
"description", "credits", "editors", "textfile"
};
if(!validtypes.Contains(type))
throw new CommandArgumentException("Invalid inquiry type.");
2016-10-27 17:57:00 -07:00
var uri = APIURI + "?action=search&sort=rating&query=" + inquiry +
"&type=" + type;
2016-10-12 21:58:16 -07:00
var req = WebRequest.Create(uri);
Console.WriteLine("idgames query: {0}", uri);
using(var response = req.GetResponse())
{
var xml = XDocument.Load(response.GetResponseStream());
var x_titles =
from item in xml.Descendants("title") select item.Value;
var x_uris = from item in xml.Descendants("url") select item.Value;
if(!x_titles.Any())
{
bot.message(channel, "Nothing found.");
return;
}
if(pos == "random") ipos = rnd.Next(0, x_titles.Count());
if(ipos >= x_titles.Count()) ipos = x_titles.Count() - 1;
2016-10-27 17:57:00 -07:00
var title = x_titles.ElementAtOrDefault(ipos) ?? "invalid title";
2016-10-12 21:58:16 -07:00
if(title.Trim().Length > 0) title = "[ " + title + " ] ";
bot.message(channel,
Discord.Format.Escape(String.Format("({0} of {1}{4} {2}{3}",
ipos + 1, x_titles.Count(), title,
x_uris.ElementAtOrDefault(ipos),
x_titles.Count() >= 100 ? "+)" : ")")));
}
}
}
}