Skip to content

Commit

Permalink
🥈 Add 2333 stats
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Mar 20, 2024
1 parent faf4e23 commit 6b4ee8d
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CubicBot.Telegram/CLI/ConfigCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static async Task<int> GetAsync(CancellationToken cancellationToken = def
Console.WriteLine($"{"Enable Grass Stats",-36}{config.Stats.EnableGrass}");
Console.WriteLine($"{"Enable Command Stats",-36}{config.Stats.EnableCommandStats}");
Console.WriteLine($"{"Enable Message Counter",-36}{config.Stats.EnableMessageCounter}");
Console.WriteLine($"{"Enable Two Triple Three (2333) Counter",-36}{config.Stats.EnableTwoTripleThree}");
Console.WriteLine($"{"Enable Parenthesis Enclosure",-36}{config.Stats.EnableParenthesisEnclosure}");

return 0;
Expand All @@ -61,6 +62,7 @@ public static async Task<int> SetAsync(
bool? enableGrassStats,
bool? enableCommandStats,
bool? enableMessageCounter,
bool? enableTwoTripleThree,
bool? enableParenthesisEnclosure,
CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -110,6 +112,8 @@ public static async Task<int> SetAsync(
config.Stats.EnableCommandStats = eCS;
if (enableMessageCounter is bool eMC)
config.Stats.EnableMessageCounter = eMC;
if (enableTwoTripleThree is bool eTTT)
config.Stats.EnableTwoTripleThree = eTTT;
if (enableParenthesisEnclosure is bool ePE)
config.Stats.EnableParenthesisEnclosure = ePE;

Expand Down
17 changes: 17 additions & 0 deletions CubicBot.Telegram/Commands/QueryStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public QueryStats(Config config, List<CubicBotCommand> commands)
commands.Add(new("leaderboard_talkative", "🗣️ Who's the most talkative person in this chat?", SendTalkativeLeaderboardAsync));
}

if (config.Stats.EnableTwoTripleThree)
{
commands.Add(new("leaderboard_2333", "🥈 2333 排行榜", SendTwoTripleThreeLeaderboardAsync));
}

if (config.Stats.EnableParenthesisEnclosure)
{
commands.Add(new("leaderboard_half_parentheses", "🌓 括号发一半排行榜", SendParenthesesUnenclosedLeaderboardAsync));
Expand Down Expand Up @@ -108,6 +113,11 @@ public Task QueryUserStats(CommandContext commandContext, CancellationToken canc
responseSB.AppendLine($"生草数量: {targetUserData.GrassGrown}");
}

if (_config.Stats.EnableTwoTripleThree)
{
responseSB.AppendLine($"2333 发送数量: {targetUserData.TwoTripleThreesUsed}");
}

if (_config.Stats.EnableParenthesisEnclosure)
{
responseSB.AppendLine($"括号发一半数量: {targetUserData.ParenthesesUnenclosed}");
Expand Down Expand Up @@ -401,6 +411,13 @@ public static Task SendTalkativeLeaderboardAsync(CommandContext commandContext,
x => x.MessagesProcessed,
cancellationToken);

public static Task SendTwoTripleThreeLeaderboardAsync(CommandContext commandContext, CancellationToken cancellationToken = default)
=> SendLeaderboardAsync(commandContext,
"🥈 2333 排行榜",
x => x.TwoTripleThreesUsed,
x => x.TwoTripleThreesUsed,
cancellationToken);

public static Task SendParenthesesUnenclosedLeaderboardAsync(CommandContext commandContext, CancellationToken cancellationToken = default)
=> SendLeaderboardAsync(commandContext,
"🌓 括号发一半排行榜",
Expand Down
3 changes: 2 additions & 1 deletion CubicBot.Telegram/Commands/Systemd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;

namespace CubicBot.Telegram.Commands;
Expand Down Expand Up @@ -134,7 +135,7 @@ await commandContext.EditMessageTextWithRetryAsync(
cancellationToken: cancellationToken);
}

private static Task SendHelpAsync(CommandContext commandContext, string errMsgMarkdownV2, CancellationToken cancellationToken = default)
private static Task<Message> SendHelpAsync(CommandContext commandContext, string errMsgMarkdownV2, CancellationToken cancellationToken = default)
=> commandContext.ReplyWithTextMessageAndRetryAsync(errMsgMarkdownV2 + HelpMarkdownV2, parseMode: ParseMode.MarkdownV2, cancellationToken: cancellationToken);

public static void CountSystemctlCalls(CommandContext commandContext) => commandContext.MemberOrUserData.SystemctlCommandsUsed++;
Expand Down
6 changes: 6 additions & 0 deletions CubicBot.Telegram/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ private static Task<int> Main(string[] args)
{
Description = "Whether to enable message counter.",
};
var enableTwoTripleThreeOption = new CliOption<bool?>("--enable-two-triple-three")
{
Description = "Whether to enable two triple three (2333) counter.",
};
var enableParenthesisEnclosureOption = new CliOption<bool?>("--enable-parenthesis-enclosure")
{
Description = "Whether to enable parenthesis enclosure.",
Expand Down Expand Up @@ -123,6 +127,7 @@ private static Task<int> Main(string[] args)
var enableGrassStats = parseResult.GetValue(enableGrassStatsOption);
var enableCommandStats = parseResult.GetValue(enableCommandStatsOption);
var enableMessageCounter = parseResult.GetValue(enableMessageCounterOption);
var enableTwoTripleThree = parseResult.GetValue(enableTwoTripleThreeOption);
var enableParenthesisEnclosure = parseResult.GetValue(enableParenthesisEnclosureOption);
return ConfigCommand.SetAsync(
botToken,
Expand All @@ -141,6 +146,7 @@ private static Task<int> Main(string[] args)
enableGrassStats,
enableCommandStats,
enableMessageCounter,
enableTwoTripleThree,
enableParenthesisEnclosure,
cancellationToken);
});
Expand Down
2 changes: 2 additions & 0 deletions CubicBot.Telegram/Stats/ChatData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ public abstract class ChatData
public bool EnsureParenthesisEnclosure { get; set; }

public ulong ParenthesesUnenclosed { get; set; }

public ulong TwoTripleThreesUsed { get; set; }
}
6 changes: 3 additions & 3 deletions CubicBot.Telegram/Stats/Grass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public sealed class Grass : IStatsCollector
"cao", "艹", "草", "c奥", "c嗷",
];

private static bool IsGrowingGrass(string msg) => msg.Length > 0 && s_grassSeeds.Any(seed => msg.Contains(seed));
private static bool IsGrowingGrass(string msg) => msg.Length > 0 && s_grassSeeds.Any(msg.Contains);

public Task CollectAsync(MessageContext messageContext, CancellationToken cancellationToken)
public Task CollectAsync(MessageContext messageContext, CancellationToken cancellationToken = default)
{
if (IsGrowingGrass(messageContext.Text))
{
Expand All @@ -26,7 +26,7 @@ public Task CollectAsync(MessageContext messageContext, CancellationToken cancel
groupData.GrassGrown++;

if ((grassGrown & (grassGrown - 1UL)) == 0UL && grassGrown > 4UL) // 8, 16, 32...
return messageContext.ReplyWithTextMessageAndRetryAsync($"🏆 Achievement Unlocked: {grassGrown} Grass Grown", cancellationToken: cancellationToken); ;
return messageContext.ReplyWithTextMessageAndRetryAsync($"🏆 Achievement Unlocked: {grassGrown} Grass Grown", cancellationToken: cancellationToken);
}

return Task.CompletedTask;
Expand Down
2 changes: 1 addition & 1 deletion CubicBot.Telegram/Stats/ParenthesisEnclosure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public string GetCompensationString()
return compensation;
}

public Task CollectAsync(MessageContext messageContext, CancellationToken cancellationToken)
public Task CollectAsync(MessageContext messageContext, CancellationToken cancellationToken = default)
{
var task = Task.CompletedTask;

Expand Down
2 changes: 2 additions & 0 deletions CubicBot.Telegram/Stats/StatsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public sealed class StatsConfig

public bool EnableMessageCounter { get; set; } = true;

public bool EnableTwoTripleThree { get; set; } = true;

public bool EnableParenthesisEnclosure { get; set; } = true;
}
6 changes: 6 additions & 0 deletions CubicBot.Telegram/Stats/StatsDispatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public StatsDispatch(StatsConfig config)
_collectors.Add(messageCounter);
}

if (config.EnableTwoTripleThree)
{
var twoTripleThree = new TwoTripleThree();
_collectors.Add(twoTripleThree);
}

if (config.EnableParenthesisEnclosure)
{
var parenthesisEnclosure = new ParenthesisEnclosure();
Expand Down
53 changes: 53 additions & 0 deletions CubicBot.Telegram/Stats/TwoTripleThree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using CubicBot.Telegram.Utils;
using System;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;

namespace CubicBot.Telegram.Stats;

public sealed class TwoTripleThree : IStatsCollector
{
private const string TwoDoubleThree = "233";

/// <summary>
/// Checks whether the message starts with or ends with "233".
/// </summary>
/// <param name="msg">The text message to check.</param>
/// <returns>
/// True if the message starts with or ends with "233".
/// Otherwise, false.
/// </returns>
private static bool ContainsTwoDoubleThree(ReadOnlySpan<char> msg) => msg.Length >= TwoDoubleThree.Length &&
(msg[..TwoDoubleThree.Length] == TwoDoubleThree || msg[(msg.Length - TwoDoubleThree.Length)..] == TwoDoubleThree);

public Task CollectAsync(MessageContext messageContext, CancellationToken cancellationToken = default)
{
if (ContainsTwoDoubleThree(messageContext.Text))
{
var twoTripleThreesUsed = messageContext.MemberOrUserData.TwoTripleThreesUsed;
twoTripleThreesUsed++;
messageContext.MemberOrUserData.TwoTripleThreesUsed = twoTripleThreesUsed;

if (messageContext.GroupData is GroupData groupData)
groupData.TwoTripleThreesUsed++;

if ((twoTripleThreesUsed & (twoTripleThreesUsed - 1UL)) == 0UL && twoTripleThreesUsed > 4UL) // 8, 16, 32...
{
const string msgPrefix = "🏆 Achievement Unlocked: 2";
var threes = BitOperations.Log2(twoTripleThreesUsed);
var msg = string.Create(msgPrefix.Length + threes, threes, (buf, _) =>
{
msgPrefix.CopyTo(buf);
for (var i = msgPrefix.Length; i < buf.Length; i++)
{
buf[i] = '3';
}
});
return messageContext.ReplyWithTextMessageAndRetryAsync(msg, cancellationToken: cancellationToken);
}
}

return Task.CompletedTask;
}
}

0 comments on commit 6b4ee8d

Please sign in to comment.