Added points system

This commit is contained in:
Ben Yost 2020-06-05 00:29:39 -04:00
parent c556edccff
commit 01e46f873f
3 changed files with 91 additions and 85 deletions

View File

@ -112,7 +112,7 @@ namespace ChaosBot.Database
SqliteCommand cmd = _conn.CreateCommand(); SqliteCommand cmd = _conn.CreateCommand();
StringBuilder commandText = new StringBuilder(); StringBuilder commandText = new StringBuilder();
commandText.Append("UPDATE OR FAIL "); commandText.Append("UPDATE ");
commandText.Append(table); commandText.Append(table);
commandText.Append(" SET "); commandText.Append(" SET ");
@ -135,11 +135,11 @@ namespace ChaosBot.Database
if (filterList.Count > 0) if (filterList.Count > 0)
{ {
commandText.Append("WHERE "); commandText.Append("WHERE ");
commandText.Append(string.Join(", ", filterList)); commandText.Append(string.Join("AND ", filterList));
} }
cmd.CommandText = commandText.ToString(); cmd.CommandText = commandText.ToString();
Console.WriteLine(commandText.ToString());
cmd.Prepare(); cmd.Prepare();
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();

View File

@ -82,37 +82,53 @@ namespace ChaosBot.Database.Repository
public static int Add(int userId, int points, int guildId) public static int Add(string userId, int points, string guildId)
{ {
Dictionary<string, object> selectfilterDict = new Dictionary<string, object>(); Dictionary<string, object> selectfilterDict = new Dictionary<string, object>();
selectfilterDict.Add("userId", userId); selectfilterDict.Add("userId", userId);
selectfilterDict.Add("guildId", guildId); selectfilterDict.Add("guildId", guildId);
DataTable dt = Controller.SelectQuery(Table, "points", selectfilterDict);
if (Convert.ToInt32(dt.Rows.Count) != 0)
{
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId);
int curPoints = Convert.ToInt32(Controller.SelectQuery(Table, "points", selectfilterDict).Rows[0]["points"]); Dictionary<string, object> updateDict = new Dictionary<string, object>();
updateDict.Add("points", Convert.ToInt32(dt.Rows[0]["points"]) + points);
Controller.UpdateQuery(Table, updateDict, filterDict);
return Convert.ToInt32(dt.Rows[0]["points"]) + points;
}
else
{
Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("userId", userId);
dict.Add("guildId", guildId);
dict.Add("points", points);
Console.WriteLine($"{curPoints}"); Controller.InsertQuery(Table, dict);
return points;
}
}
public static int Remove(string userId, int points, string guildId)
{
Dictionary<string, object> selectfilterDict = new Dictionary<string, object>();
selectfilterDict.Add("userId", userId);
selectfilterDict.Add("guildId", guildId);
DataTable dt = Controller.SelectQuery(Table, "points", selectfilterDict);
Dictionary<string, object> filterDict = new Dictionary<string, object>(); Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId); filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId); filterDict.Add("guildId", guildId);
Dictionary<string, object> updateDict = new Dictionary<string, object>(); Dictionary<string, object> updateDict = new Dictionary<string, object>();
// updateDict.Add("points", Convert.ToInt32(dataTable.Rows[0]["points"]) + points); updateDict.Add("points", Convert.ToInt32(dt.Rows[0]["points"]) - points);
return 0; Controller.UpdateQuery(Table, updateDict, filterDict);
//Controller.UpdateQuery(Table, updateDict, filterDict); return Convert.ToInt32(dt.Rows[0]["points"]) - points;
//return Convert.ToInt32(dataTable.Rows[0]["points"]) + points;
}
public static void ClearPoints(string guildId)
{
Dictionary<string,object> filterDict = new Dictionary<string, object>();
filterDict.Add("guildId", guildId);
Controller.DeleteQuery(Table, filterDict);
} }
public static void Delete(int userId) public static void Delete(int userId)
@ -123,24 +139,5 @@ namespace ChaosBot.Database.Repository
Controller.DeleteQuery(Table, filterDict); Controller.DeleteQuery(Table, filterDict);
} }
public static int Remove(int userId, int points, int guildId )
{
Dictionary<string, object> selectfilterDict = new Dictionary<string, object>();
selectfilterDict.Add("userId", userId);
selectfilterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, "points", selectfilterDict);
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId);
Dictionary<string, object> updateDict = new Dictionary<string, object>();
updateDict.Add("points", Convert.ToInt32(dataTable.Rows[0]["points"]) - points);
// Controller.UpdateQuery(Table, updateDict, filterDict);
return Convert.ToInt32(dataTable.Rows[0]["points"]) - points;
}
} }
} }

View File

@ -61,24 +61,33 @@ namespace ChaosBot.Discord.Modules
} }
/*[Command("points add")] [Command("points add")]
[RequireUserPermission(GuildPermission.ManageGuild)] [RequireUserPermission(ChannelPermission.ManageMessages)]
public async Task RaffleCommandAdd(string user, int amount = 1) public async Task RaffleCommandAdd(string user, int amount = 1)
{ {
if (Program.Cfg.GetValue<int>($"Servers:{Context.Guild.Id}:Raffle:Max") >= amount)
// await RaffleCommandHelper("add", user, amount); if (ChannelPermissions.Text.ManageMessages)
{
ulong userId = Convert.ToUInt64(user.Substring(3, user.Length - 4));
await ReplyAsync($"{Context.User.Mention} has given <@{userId}> {amount} points for a total of {PointsRepository.Add(userId.ToString(), amount, Context.Guild.Id.ToString())} points.", false);
}
else else
{ await ReplyAsync($"NO ACCESS");
await ReplyAsync(
$"You cannot give more then {Program.Cfg.GetValue<int>($"Servers:{Context.Guild.Id}:Raffle:Max").ToString()} tickets at a time", false);
_logger.Warn($"{Context.User.Username} attempted to give {amount} tickets to {user}!");
}
} }
[Command("points remove")] [Command("points remove")]
[RequireUserPermission(GuildPermission.ManageGuild)] [RequireUserPermission(ChannelPermission.ManageMessages)]
public async Task RaffleCommandClear() public async Task RaffleCommandRemove(string user, int amount = 1)
{ {
await RaffleCommandHelper("clear"); ulong userId = Convert.ToUInt64(user.Substring(3, user.Length - 4));
} */ int cur = PointsRepository.Total(Context.User.Id.ToString(), Context.Guild.Id.ToString());
if (cur > amount)
await ReplyAsync($"{Context.User.Mention} has removed {amount} points from <@{userId}> for a total of {PointsRepository.Remove(userId.ToString(), amount, Context.Guild.Id.ToString())} points.", false);
else
await ReplyAsync($"{Context.User.Mention} has tried to remove {amount} points from <@{userId}> they only had {cur} points. None were taken...", false);
}
} }
} }