Skip to content

Commit

Permalink
Close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
LaineZ committed Oct 15, 2024
1 parent 0b23e31 commit 563a3ca
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
6 changes: 0 additions & 6 deletions fs24bot3/Commands/GenericCommandsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,6 @@ public async Task Reminds(string username = "", string locale = "ru-RU")
{
if (string.IsNullOrEmpty(username))
{
if (Context.FromBridge)
{
await Context.SendSadMessage(Context.Channel, "Укажите ник пользователя");
return;
}

username = Context.User.Username;
}

Expand Down
73 changes: 51 additions & 22 deletions fs24bot3/Core/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using static fs24bot3.Models.Exceptions;

namespace fs24bot3.Core;

public class User
{
public string Username { get; }
Expand Down Expand Up @@ -48,13 +49,15 @@ public void SetContext(CommandProcessor.CustomCommandContext ctx)

public TimeZoneInfo GetTimeZone()
{
if (GetUserInfo().Timezone == null)
try
{
return TimeZoneInfo.Local;
return GetUserInfo().Timezone == null
? TimeZoneInfo.Local
: TimeZoneInfo.FindSystemTimeZoneById(GetUserInfo().Timezone);
}
else
catch (UserNotFoundException)
{
return TimeZoneInfo.FindSystemTimeZoneById(GetUserInfo().Timezone);
return TimeZoneInfo.Local;
}
}

Expand Down Expand Up @@ -126,7 +129,8 @@ public bool IncreaseXp(int count)
var nick = Connect.Table<SQL.UserStats>().Where(v => v.Nick.Equals(Username)).First();
Connect.Execute("UPDATE UserStats SET Xp = Xp + ? WHERE Nick = ?", count, nick.Nick);

Log.Verbose("{4}: Setting level: {0} {1} Current data: {2}/{3}", count, nick.Level, nick.Xp, nick.Need, nick.Nick);
Log.Verbose("{4}: Setting level: {0} {1} Current data: {2}/{3}", count, nick.Level, nick.Xp, nick.Need,
nick.Nick);

if (nick.Xp >= nick.Need)
{
Expand All @@ -141,7 +145,8 @@ public bool IncreaseXp(int count)

public void SetLastMessage()
{
Connect.Execute("UPDATE UserStats SET LastMsg = ? WHERE Nick = ?", (int)((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds(), Username);
Connect.Execute("UPDATE UserStats SET LastMsg = ? WHERE Nick = ?",
(int)((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds(), Username);
}

public DateTime GetLastMessage()
Expand All @@ -167,7 +172,11 @@ public void SetLevel(int level)
public int GetFishLevel()
{
var q = Connect.Table<SQL.Fishing>().FirstOrDefault(v => v.Nick == Username);
if (q != null) { return q.Level; }
if (q != null)
{
return q.Level;
}

return 1;
}

Expand All @@ -185,7 +194,9 @@ public string GetFishNest()
}
}

public Dictionary<string, IItem> AddRandomRarityItem(Shop shop, ItemInventory.ItemRarity rarity = ItemInventory.ItemRarity.Uncommon, int mincount = 1, int maxcount = 1, int iterations = 1)
public Dictionary<string, IItem> AddRandomRarityItem(Shop shop,
ItemInventory.ItemRarity rarity = ItemInventory.ItemRarity.Uncommon, int mincount = 1, int maxcount = 1,
int iterations = 1)
{
var rng = new Random();
var dict = new Dictionary<string, IItem>();
Expand Down Expand Up @@ -263,14 +274,16 @@ public void AddItemToInv(Shop shop, string name, int count)
{
throw new ItemNotFoundException();
}

count = (int)Math.Floor((decimal)count);
try
{
Connect.Execute("INSERT INTO Inventory VALUES(?, ?, ?)", Username, name, count);
}
catch (SQLiteException)
{
Connect.Execute("UPDATE Inventory SET Count = Count + ? WHERE Item = ? AND Nick = ?", count, name, Username);
Connect.Execute("UPDATE Inventory SET Count = Count + ? WHERE Item = ? AND Nick = ?", count, name,
Username);
}
}

Expand All @@ -283,6 +296,7 @@ public void RemoveTag(in SQL.Tag tag)
{
Connect.Execute("DELETE FROM Tags WHERE TagName = ? AND Nick = ?", tag.Name, Username);
}

/// <summary>
/// Removes item from inventory
/// </summary>
Expand All @@ -301,20 +315,24 @@ public async Task<bool> RemItemFromInv(Shop shop, string name, int count)

if (item != null && item.ItemCount >= count && count > 0)
{
Connect.Execute("UPDATE Inventory SET Count = Count - ? WHERE Item = ? AND Nick = ?", count, name, Username);
Connect.Execute("UPDATE Inventory SET Count = Count - ? WHERE Item = ? AND Nick = ?", count, name,
Username);
// clening up items with 0
Connect.Execute("DELETE FROM Inventory WHERE Count = 0");
if (Ctx != null)
{
await Ctx.SendMessage(Ctx.Channel, $" {shop.Items[name].Name} -{count} За использование данной команды");
await Ctx.SendMessage(Ctx.Channel,
$" {shop.Items[name].Name} -{count} За использование данной команды");
}

return true;
}

if (Ctx != null)
{
await Ctx.SendMessage(Ctx.Channel, $"Недостаточно {shop.Items[name].Name} x{count}");
}

return false;
}

Expand Down Expand Up @@ -342,7 +360,7 @@ public bool UpdateGoal(SQL.Goals goal)
goal.Nick = Username;
return Connect.Update(goal) > 0;
}

public bool DeleteGoal(int goalId)
{
return Connect.Delete<SQL.Goals>(goalId) > 0;
Expand All @@ -364,7 +382,7 @@ public SQL.Goals FindGoalById(int goalId)
var query = Connect.Query<SQL.Goals>("SELECT * FROM Goals WHERE Nick = ? AND Goal LIKE ?",
Username, goalSearch
);

return query;
}

Expand All @@ -391,15 +409,27 @@ public string GetCity(string def)
public List<SQL.Inventory> GetInventory()
{
var query = Connect.Table<SQL.Inventory>().Where(v => v.Nick.Equals(Username)).ToList();
if (query.Any()) { return query; }
else { return new List<SQL.Inventory>(); }
if (query.Any())
{
return query;
}
else
{
return new List<SQL.Inventory>();
}
}

public List<SQL.Tags> GetTags()
{
var query = Connect.Table<SQL.Tags>().Where(v => v.Nick.Equals(Username)).ToList();
if (query.Any()) { return query; }
else { return new List<SQL.Tags>(); }
if (query.Any())
{
return query;
}
else
{
return new List<SQL.Tags>();
}
}

public SQL.UserStats GetUserInfo()
Expand All @@ -410,10 +440,8 @@ public SQL.UserStats GetUserInfo()
{
return query;
}
else
{
throw new UserNotFoundException();
}

throw new UserNotFoundException();
}

public override string ToString()
Expand All @@ -432,6 +460,7 @@ public override bool Equals(object obj)
{
return Username == otherUser.Username;
}

return false;
}
}
}

0 comments on commit 563a3ca

Please sign in to comment.