.novel done, waifutransfer almost done

This commit is contained in:
Master Kwoth 2017-11-02 18:22:17 +01:00
parent e581419945
commit 6aeec724c0
9 changed files with 1013 additions and 922 deletions

View File

@ -9,13 +9,15 @@ namespace NadekoBot.Modules.Gambling.Services
public class WaifuService : INService
{
private readonly DbService _db;
private readonly CurrencyService _cs;
public ConcurrentDictionary<ulong, DateTime> DivorceCooldowns { get; } = new ConcurrentDictionary<ulong, DateTime>();
public ConcurrentDictionary<ulong, DateTime> AffinityCooldowns { get; } = new ConcurrentDictionary<ulong, DateTime>();
public WaifuService(DbService db)
public WaifuService(DbService db, CurrencyService cs)
{
_db = db;
_cs = cs;
}
public async Task<bool> WaifuTransfer(IUser owner, ulong waifuId, IUser newOwner)
@ -29,12 +31,22 @@ namespace NadekoBot.Modules.Gambling.Services
if (waifu.ClaimerId != ownerUser.Id)
return false;
if (!await _cs.RemoveAsync(owner.Id,
"Waifu Transfer",
waifu.Price / 10,
uow).ConfigureAwait(false))
{
return false;
}
//new claimerId is the id of the new owner
var newOwnerUser = uow.DiscordUsers.GetOrCreate(newOwner);
waifu.ClaimerId = newOwnerUser.Id;
await uow.CompleteAsync().ConfigureAwait(false);
}
return true;
}
}
}

View File

@ -190,9 +190,9 @@ namespace NadekoBot.Modules.Gambling
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task WaifuTransfer(IGuildUser waifu, IGuildUser newOwner)
public async Task WaifuTransfer(IUser waifu, IUser newOwner)
{
if(!await _service.WaifuTransfer(waifu.Id, newOwner.Id)
if(!await _service.WaifuTransfer(Context.User, waifu.Id, newOwner)
.ConfigureAwait(false))
{
await ReplyErrorLocalized("waifu_transfer_fail").ConfigureAwait(false);

View File

@ -37,7 +37,7 @@ namespace NadekoBot.Modules.Searches
.WithImageUrl(novelData.ImageUrl)
.AddField(efb => efb.WithName(GetText("authors")).WithValue(String.Join("\n", novelData.Authors)).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("status")).WithValue(novelData.Status).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("genres")).WithValue(string.Join(",\n", novelData.Genres.Any() ? novelData.Genres : new[] { "none" })).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("genres")).WithValue(string.Join(" ", novelData.Genres.Any() ? novelData.Genres : new[] { "none" })).WithIsInline(true))
.WithFooter(efb => efb.WithText(GetText("score") + " " + novelData.Score));
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace NadekoBot.Modules.Searches.Common
{
public class NovelData
public class NovelResult
{
public string Description { get; set; }
public string Title { get; set; }

View File

@ -5,6 +5,9 @@ using System;
using System.Net.Http;
using System.Threading.Tasks;
using NadekoBot.Modules.Searches.Common;
using AngleSharp;
using AngleSharp.Dom.Html;
using System.Linq;
namespace NadekoBot.Modules.Searches.Services
{
@ -46,7 +49,7 @@ namespace NadekoBot.Modules.Searches.Services
}
}
public async Task<NovelData> GetNovelData(string query)
public async Task<NovelResult> GetNovelData(string query)
{
if (string.IsNullOrWhiteSpace(query))
throw new ArgumentNullException(nameof(query));
@ -57,18 +60,66 @@ namespace NadekoBot.Modules.Searches.Services
var link = "http://www.novelupdates.com/series/" + Uri.EscapeDataString(query.Replace("/", " "));
link = link.ToLowerInvariant();
var (ok, data) = await _cache.TryGetAnimeDataAsync(link).ConfigureAwait(false);
var (ok, data) = await _cache.TryGetNovelDataAsync(link).ConfigureAwait(false);
if (!ok)
{
data = await _http.GetStringAsync(link).ConfigureAwait(false);
await _cache.SetAnimeDataAsync(link, data).ConfigureAwait(false);
}
var config = Configuration.Default.WithDefaultLoader();
var document = await BrowsingContext.New(config).OpenAsync(link);
var imageElem = document.QuerySelector("div.seriesimg > img");
var imageUrl = ((IHtmlImageElement)imageElem).Source;
return JsonConvert.DeserializeObject<MangaResult>(data);
}
catch
var descElem = document.QuerySelector("div#editdescription > p");
var desc = descElem.InnerHtml;
var genres = document.QuerySelector("div#seriesgenre").Children
.Select(x => x as IHtmlAnchorElement)
.Where(x => x != null)
.Select(x => $"[{x.InnerHtml}]({x.Href})")
.ToArray();
var authors = document
.QuerySelector("div#showauthors")
.Children
.Select(x => x as IHtmlAnchorElement)
.Where(x => x != null)
.Select(x => $"[{x.InnerHtml}]({x.Href})")
.ToArray();
var score = ((IHtmlSpanElement)document
.QuerySelector("h5.seriesother > span.uvotes"))
.InnerHtml;
var status = document
.QuerySelector("div#editstatus")
.InnerHtml;
var title = document
.QuerySelector("div.w-blog-content > div.seriestitlenu")
.InnerHtml;
var obj = new NovelResult()
{
Description = desc,
Authors = authors,
Genres = genres,
ImageUrl = imageUrl,
Link = link,
Score = score,
Status = status,
Title = title,
};
await _cache.SetNovelDataAsync(link,
JsonConvert.SerializeObject(obj)).ConfigureAwait(false);
return obj;
}
return JsonConvert.DeserializeObject<NovelResult>(data);
}
catch (Exception ex)
{
_log.Error(ex);
return null;
}
}

View File

@ -9,8 +9,10 @@ namespace NadekoBot.Core.Services
ConnectionMultiplexer Redis { get; }
Task<(bool Success, byte[] Data)> TryGetImageDataAsync(string key);
Task<(bool Success, string Data)> TryGetAnimeDataAsync(string key);
Task<(bool Success, string Data)> TryGetNovelDataAsync(string key);
Task SetImageDataAsync(string key, byte[] data);
Task SetAnimeDataAsync(string link, string data);
Task SetNovelDataAsync(string link, string data);
TimeSpan? AddTimelyClaim(ulong id, int period);
void RemoveAllTimelyClaims();
}

View File

@ -46,6 +46,17 @@ namespace NadekoBot.Core.Services.Impl
return _db.StringSetAsync("anime_" + key, data);
}
public async Task<(bool Success, string Data)> TryGetNovelDataAsync(string key)
{
string x = await _db.StringGetAsync("novel_" + key);
return (x != null, x);
}
public Task SetNovelDataAsync(string key, string data)
{
return _db.StringSetAsync("novel_" + key, data);
}
private readonly object timelyLock = new object();
public TimeSpan? AddTimelyClaim(ulong id, int period)
{

View File

@ -498,6 +498,7 @@
"searches_failed_finding_anime": "Failed finding that animu.",
"searches_failed_finding_manga": "Failed finding that mango.",
"searches_genres": "Genres",
"searches_authors": "Authors",
"searches_hashtag_error": "Failed finding a definition for that tag.",
"searches_height_weight": "Height/Weight",
"searches_height_weight_val": "{0}m/{1}kg",

View File

@ -2314,6 +2314,13 @@
"{0}claim 50 @Himesama"
]
},
"waifutransfer": {
"cmd": "waifutransfer",
"desc": "Transfer the ownership of one of your waifus to another user. You must pay 10% of your waifu's value.",
"usage": [
"{0}waifutransfer @ExWaifu @NewOwner"
]
},
"waifugift": {
"Cmd": "waifugift gift gifts",
"Desc": "Gift an item to someone. This will increase their waifu value by 50% of the gifted item's value if they don't have affinity set towards you, or 100% if they do. Provide no arguments to see a list of items that you can gift.",
@ -3076,5 +3083,12 @@
"usage": [
"{0}timelyreset"
]
},
"novel": {
"cmd": "novel",
"desc": "Searches for a novel on `http://novelupdates.com/`. You have to provide an exact name.",
"usage": [
"{0}novel the nine cauldrons"
]
}
}