2016-11-25 00:20:18 +00:00
|
|
|
using Discord.Commands;
|
|
|
|
using NadekoBot.Extensions;
|
2016-11-26 00:52:27 +00:00
|
|
|
using System.Linq;
|
|
|
|
using NadekoBot.Services;
|
|
|
|
using NadekoBot.Services.Database.Models;
|
2016-11-25 00:20:18 +00:00
|
|
|
using System.Collections.Generic;
|
2016-11-27 02:51:10 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Discord;
|
|
|
|
using System;
|
2017-07-17 19:42:36 +00:00
|
|
|
using NadekoBot.Common.Attributes;
|
|
|
|
using NadekoBot.Modules.Pokemon.Common;
|
|
|
|
using NadekoBot.Modules.Pokemon.Services;
|
2016-11-26 00:52:27 +00:00
|
|
|
|
2016-11-25 00:20:18 +00:00
|
|
|
namespace NadekoBot.Modules.Pokemon
|
|
|
|
{
|
2017-07-15 16:34:34 +00:00
|
|
|
public class Pokemon : NadekoTopLevelModule<PokemonService>
|
2016-11-26 00:52:27 +00:00
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
private readonly DbService _db;
|
2017-05-27 17:42:23 +00:00
|
|
|
private readonly BotConfig _bc;
|
2017-05-29 04:13:22 +00:00
|
|
|
private readonly CurrencyService _cs;
|
2016-11-27 02:51:10 +00:00
|
|
|
|
2017-07-15 16:34:34 +00:00
|
|
|
public Pokemon(DbService db, BotConfig bc, CurrencyService cs)
|
2016-11-26 00:52:27 +00:00
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
_db = db;
|
|
|
|
_bc = bc;
|
2017-05-29 04:13:22 +00:00
|
|
|
_cs = cs;
|
2016-11-26 00:52:27 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 00:20:18 +00:00
|
|
|
private int GetDamage(PokemonType usertype, PokemonType targetType)
|
|
|
|
{
|
|
|
|
var rng = new Random();
|
2017-02-14 23:59:10 +00:00
|
|
|
var damage = rng.Next(40, 60);
|
|
|
|
foreach (var multiplierObj in usertype.Multipliers)
|
2016-11-25 00:20:18 +00:00
|
|
|
{
|
2017-02-14 23:59:10 +00:00
|
|
|
if (multiplierObj.Type != targetType.Name) continue;
|
|
|
|
damage = (int)(damage * multiplierObj.Multiplication);
|
2016-11-25 00:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return damage;
|
2017-05-27 17:42:23 +00:00
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2017-05-27 17:42:23 +00:00
|
|
|
private PokemonType GetPokeType(ulong id)
|
2016-11-25 00:20:18 +00:00
|
|
|
{
|
|
|
|
|
2016-12-04 20:28:49 +00:00
|
|
|
Dictionary<ulong, string> setTypes;
|
2017-05-27 17:42:23 +00:00
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-11-27 02:51:10 +00:00
|
|
|
{
|
|
|
|
setTypes = uow.PokeGame.GetAll().ToDictionary(x => x.UserId, y => y.type);
|
|
|
|
}
|
|
|
|
|
2016-12-04 20:28:49 +00:00
|
|
|
if (setTypes.ContainsKey(id))
|
2016-11-25 00:20:18 +00:00
|
|
|
{
|
2016-12-04 20:28:49 +00:00
|
|
|
return StringToPokemonType(setTypes[id]);
|
2016-11-25 00:20:18 +00:00
|
|
|
}
|
2017-05-27 17:42:23 +00:00
|
|
|
var count = _service.PokemonTypes.Count;
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2017-02-14 23:59:10 +00:00
|
|
|
var remainder = Math.Abs((int)(id % (ulong)count));
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2017-05-27 17:42:23 +00:00
|
|
|
return _service.PokemonTypes[remainder];
|
2016-11-25 00:20:18 +00:00
|
|
|
}
|
2017-02-13 14:03:39 +00:00
|
|
|
|
2017-05-27 17:42:23 +00:00
|
|
|
private PokemonType StringToPokemonType(string v)
|
2016-11-25 00:20:18 +00:00
|
|
|
{
|
2016-12-04 20:28:49 +00:00
|
|
|
var str = v?.ToUpperInvariant();
|
2017-05-27 17:42:23 +00:00
|
|
|
var list = _service.PokemonTypes;
|
2017-02-14 23:59:10 +00:00
|
|
|
foreach (var p in list)
|
2016-11-25 00:20:18 +00:00
|
|
|
{
|
|
|
|
if (str == p.Name)
|
|
|
|
{
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-02-13 14:03:39 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-17 00:16:14 +00:00
|
|
|
public async Task Attack(string move, IGuildUser targetUser = null)
|
2016-11-25 00:20:18 +00:00
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
IGuildUser user = (IGuildUser)Context.User;
|
2016-11-27 02:51:10 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(move)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetUser == null)
|
2016-11-25 00:20:18 +00:00
|
|
|
{
|
2017-02-13 12:24:11 +00:00
|
|
|
await ReplyErrorLocalized("user_not_found").ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-02-13 12:24:11 +00:00
|
|
|
if (targetUser == user)
|
2016-11-27 02:51:10 +00:00
|
|
|
{
|
2017-02-13 12:24:11 +00:00
|
|
|
await ReplyErrorLocalized("cant_attack_yourself").ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
|
|
|
|
// Checking stats first, then move
|
|
|
|
//Set up the userstats
|
2017-05-27 17:42:23 +00:00
|
|
|
var userStats = _service.Stats.GetOrAdd(user.Id, new PokeStats());
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
//Check if able to move
|
|
|
|
//User not able if HP < 0, has made more than 4 attacks
|
|
|
|
if (userStats.Hp < 0)
|
|
|
|
{
|
2017-02-13 12:24:11 +00:00
|
|
|
await ReplyErrorLocalized("you_fainted").ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (userStats.MovesMade >= 5)
|
|
|
|
{
|
2017-02-13 12:24:11 +00:00
|
|
|
await ReplyErrorLocalized("too_many_moves").ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (userStats.LastAttacked.Contains(targetUser.Id))
|
|
|
|
{
|
2017-02-13 12:24:11 +00:00
|
|
|
await ReplyErrorLocalized("cant_attack_again").ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//get target stats
|
2017-05-27 17:42:23 +00:00
|
|
|
var targetStats = _service.Stats.GetOrAdd(targetUser.Id, new PokeStats());
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
//If target's HP is below 0, no use attacking
|
|
|
|
if (targetStats.Hp <= 0)
|
|
|
|
{
|
2017-02-13 12:24:11 +00:00
|
|
|
await ReplyErrorLocalized("too_many_moves", targetUser).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
//Check whether move can be used
|
|
|
|
PokemonType userType = GetPokeType(user.Id);
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
var enabledMoves = userType.Moves;
|
|
|
|
if (!enabledMoves.Contains(move.ToLowerInvariant()))
|
|
|
|
{
|
2017-02-13 20:29:06 +00:00
|
|
|
await ReplyErrorLocalized("invalid_move", Format.Bold(move), Prefix).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
//get target type
|
|
|
|
PokemonType targetType = GetPokeType(targetUser.Id);
|
|
|
|
//generate damage
|
|
|
|
int damage = GetDamage(userType, targetType);
|
|
|
|
//apply damage to target
|
|
|
|
targetStats.Hp -= damage;
|
2017-02-13 12:44:21 +00:00
|
|
|
|
2017-02-13 14:03:39 +00:00
|
|
|
var response = GetText("attack", Format.Bold(move), userType.Icon, Format.Bold(targetUser.ToString()), targetType.Icon, Format.Bold(damage.ToString()));
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
//Damage type
|
|
|
|
if (damage < 40)
|
|
|
|
{
|
2017-02-13 12:44:21 +00:00
|
|
|
response += "\n" + GetText("not_effective");
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
|
|
|
else if (damage > 60)
|
|
|
|
{
|
2017-02-13 12:44:21 +00:00
|
|
|
response += "\n" + GetText("super_effective");
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-13 12:44:21 +00:00
|
|
|
response += "\n" + GetText("somewhat_effective");
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
//check fainted
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
if (targetStats.Hp <= 0)
|
|
|
|
{
|
2017-02-14 23:59:10 +00:00
|
|
|
response += "\n" + GetText("fainted", Format.Bold(targetUser.ToString()));
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-14 23:59:10 +00:00
|
|
|
response += "\n" + GetText("hp_remaining", Format.Bold(targetUser.ToString()), targetStats.Hp);
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
//update other stats
|
|
|
|
userStats.LastAttacked.Add(targetUser.Id);
|
|
|
|
userStats.MovesMade++;
|
|
|
|
targetStats.MovesMade = 0;
|
|
|
|
if (targetStats.LastAttacked.Contains(user.Id))
|
|
|
|
{
|
|
|
|
targetStats.LastAttacked.Remove(user.Id);
|
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
//update dictionary
|
|
|
|
//This can stay the same right?
|
2017-05-27 17:42:23 +00:00
|
|
|
_service.Stats[user.Id] = userStats;
|
|
|
|
_service.Stats[targetUser.Id] = targetStats;
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2017-02-13 12:44:21 +00:00
|
|
|
await Context.Channel.SendConfirmAsync(Context.User.Mention + " " + response).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-16 18:43:57 +00:00
|
|
|
public async Task Movelist()
|
2016-11-27 02:51:10 +00:00
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
IGuildUser user = (IGuildUser)Context.User;
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
var userType = GetPokeType(user.Id);
|
|
|
|
var movesList = userType.Moves;
|
2017-02-13 12:44:21 +00:00
|
|
|
var embed = new EmbedBuilder().WithOkColor()
|
|
|
|
.WithTitle(GetText("moves", userType))
|
|
|
|
.WithDescription(string.Join("\n", movesList.Select(m => userType.Icon + " " + m)));
|
|
|
|
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-17 00:16:14 +00:00
|
|
|
public async Task Heal(IGuildUser targetUser = null)
|
2016-11-27 02:51:10 +00:00
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
IGuildUser user = (IGuildUser)Context.User;
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2017-02-13 14:03:39 +00:00
|
|
|
if (targetUser == null)
|
|
|
|
{
|
2017-02-13 12:24:11 +00:00
|
|
|
await ReplyErrorLocalized("user_not_found").ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2017-05-27 17:42:23 +00:00
|
|
|
if (_service.Stats.ContainsKey(targetUser.Id))
|
2016-11-27 02:51:10 +00:00
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
var targetStats = _service.Stats[targetUser.Id];
|
2016-11-27 02:51:10 +00:00
|
|
|
if (targetStats.Hp == targetStats.MaxHp)
|
|
|
|
{
|
2017-02-13 14:03:39 +00:00
|
|
|
await ReplyErrorLocalized("already_full", Format.Bold(targetUser.ToString())).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Payment~
|
|
|
|
var amount = 1;
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-12-04 20:28:49 +00:00
|
|
|
var target = (targetUser.Id == user.Id) ? "yourself" : targetUser.Mention;
|
2016-11-27 02:51:10 +00:00
|
|
|
if (amount > 0)
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
if (!await _cs.RemoveAsync(user, $"Poke-Heal {target}", amount, true).ConfigureAwait(false))
|
2017-02-13 12:24:11 +00:00
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
await ReplyErrorLocalized("no_currency", _bc.CurrencySign).ConfigureAwait(false);
|
2017-02-13 12:24:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
//healing
|
|
|
|
targetStats.Hp = targetStats.MaxHp;
|
|
|
|
if (targetStats.Hp < 0)
|
|
|
|
{
|
|
|
|
//Could heal only for half HP?
|
2017-05-27 17:42:23 +00:00
|
|
|
_service.Stats[targetUser.Id].Hp = (targetStats.MaxHp / 2);
|
2016-11-27 02:51:10 +00:00
|
|
|
if (target == "yourself")
|
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
await ReplyConfirmLocalized("revive_yourself", _bc.CurrencySign).ConfigureAwait(false);
|
2017-02-13 12:24:11 +00:00
|
|
|
return;
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
2017-02-13 12:24:11 +00:00
|
|
|
|
2017-05-27 17:42:23 +00:00
|
|
|
await ReplyConfirmLocalized("revive_other", Format.Bold(targetUser.ToString()), _bc.CurrencySign).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
2017-05-27 17:42:23 +00:00
|
|
|
await ReplyConfirmLocalized("healed", Format.Bold(targetUser.ToString()), _bc.CurrencySign).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-13 14:03:39 +00:00
|
|
|
await ErrorLocalized("already_full", Format.Bold(targetUser.ToString()));
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
|
|
|
|
|
2016-11-27 02:51:10 +00:00
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-17 00:16:14 +00:00
|
|
|
public async Task Type(IGuildUser targetUser = null)
|
2016-11-27 02:51:10 +00:00
|
|
|
{
|
2017-02-13 12:24:11 +00:00
|
|
|
targetUser = targetUser ?? (IGuildUser)Context.User;
|
2016-11-27 02:51:10 +00:00
|
|
|
var pType = GetPokeType(targetUser.Id);
|
2017-02-13 14:03:39 +00:00
|
|
|
await ReplyConfirmLocalized("type_of_user", Format.Bold(targetUser.ToString()), pType).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
|
2016-11-25 00:20:18 +00:00
|
|
|
}
|
2016-11-27 02:51:10 +00:00
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-17 00:16:14 +00:00
|
|
|
public async Task Settype([Remainder] string typeTargeted = null)
|
2016-11-27 02:51:10 +00:00
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
IGuildUser user = (IGuildUser)Context.User;
|
2016-11-27 02:51:10 +00:00
|
|
|
|
2016-12-04 20:28:49 +00:00
|
|
|
var targetType = StringToPokemonType(typeTargeted);
|
2016-11-27 02:51:10 +00:00
|
|
|
if (targetType == null)
|
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
await Context.Channel.EmbedAsync(_service.PokemonTypes.Aggregate(new EmbedBuilder().WithDescription("List of the available types:"),
|
2016-12-17 00:16:14 +00:00
|
|
|
(eb, pt) => eb.AddField(efb => efb.WithName(pt.Name)
|
|
|
|
.WithValue(pt.Icon)
|
|
|
|
.WithIsInline(true)))
|
|
|
|
.WithColor(NadekoBot.OkColor)).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (targetType == GetPokeType(user.Id))
|
|
|
|
{
|
2017-02-13 14:03:39 +00:00
|
|
|
await ReplyErrorLocalized("already_that_type", targetType).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Payment~
|
|
|
|
var amount = 1;
|
|
|
|
if (amount > 0)
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
if (!await _cs.RemoveAsync(user, $"{user} change type to {typeTargeted}", amount, true).ConfigureAwait(false))
|
2016-11-27 02:51:10 +00:00
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
await ReplyErrorLocalized("no_currency", _bc.CurrencySign).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Actually changing the type here
|
|
|
|
|
2017-05-27 17:42:23 +00:00
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-11-27 02:51:10 +00:00
|
|
|
{
|
2017-02-14 23:59:10 +00:00
|
|
|
var pokeUsers = uow.PokeGame.GetAll().ToArray();
|
|
|
|
var setTypes = pokeUsers.ToDictionary(x => x.UserId, y => y.type);
|
2016-11-27 02:51:10 +00:00
|
|
|
var pt = new UserPokeTypes
|
|
|
|
{
|
2016-12-04 20:28:49 +00:00
|
|
|
UserId = user.Id,
|
2016-11-27 02:51:10 +00:00
|
|
|
type = targetType.Name,
|
|
|
|
};
|
2016-12-04 20:28:49 +00:00
|
|
|
if (!setTypes.ContainsKey(user.Id))
|
2016-11-27 02:51:10 +00:00
|
|
|
{
|
|
|
|
//create user in db
|
|
|
|
uow.PokeGame.Add(pt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//update user in db
|
2017-02-14 23:59:10 +00:00
|
|
|
var pokeUserCmd = pokeUsers.FirstOrDefault(p => p.UserId == user.Id);
|
2016-11-28 01:15:08 +00:00
|
|
|
pokeUserCmd.type = targetType.Name;
|
|
|
|
uow.PokeGame.Update(pokeUserCmd);
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
|
|
|
await uow.CompleteAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Now for the response
|
2017-02-13 12:24:11 +00:00
|
|
|
await ReplyConfirmLocalized("settype_success",
|
2017-02-13 14:03:39 +00:00
|
|
|
targetType,
|
2017-05-27 17:42:23 +00:00
|
|
|
_bc.CurrencySign).ConfigureAwait(false);
|
2016-11-27 02:51:10 +00:00
|
|
|
}
|
2016-11-25 00:20:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|