-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
faf4e23
commit 6b4ee8d
Showing
10 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |