-
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.
🌿 Optimize and consolidate code for Grass and TwoTripleThree
- Loading branch information
1 parent
b47e661
commit 8301a97
Showing
2 changed files
with
50 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,45 @@ | ||
using CubicBot.Telegram.Utils; | ||
using System.Linq; | ||
using System.Numerics; | ||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace CubicBot.Telegram.Stats; | ||
|
||
public sealed class Grass : IStatsCollector | ||
public sealed partial class Grass : IStatsCollector | ||
{ | ||
private static readonly string[] s_grassSeeds = | ||
[ | ||
"cao", "艹", "草", "c奥", "c嗷", | ||
]; | ||
[GeneratedRegex("(cao|c奥|c嗷|艹|草)")] | ||
private static partial Regex GrassSeedsRegex(); | ||
|
||
private static bool IsGrowingGrass(string msg) => msg.Length > 0 && s_grassSeeds.Any(msg.Contains); | ||
private static bool IsGrowingGrass(string msg) => GrassSeedsRegex().IsMatch(msg); | ||
|
||
public Task CollectAsync(MessageContext messageContext, CancellationToken cancellationToken = default) | ||
{ | ||
if (IsGrowingGrass(messageContext.Text)) | ||
{ | ||
var grassGrown = messageContext.MemberOrUserData.GrassGrown; | ||
grassGrown++; | ||
messageContext.MemberOrUserData.GrassGrown = grassGrown; | ||
if (!IsGrowingGrass(messageContext.Text)) | ||
return Task.CompletedTask; | ||
|
||
var grassGrown = messageContext.MemberOrUserData.GrassGrown; | ||
grassGrown++; | ||
messageContext.MemberOrUserData.GrassGrown = grassGrown; | ||
|
||
if (messageContext.GroupData is GroupData groupData) | ||
groupData.GrassGrown++; | ||
if (messageContext.GroupData is GroupData groupData) | ||
groupData.GrassGrown++; | ||
|
||
if ((grassGrown & (grassGrown - 1UL)) == 0UL && grassGrown > 4UL) // 8, 16, 32... | ||
return messageContext.ReplyWithTextMessageAndRetryAsync($"🏆 Achievement Unlocked: {grassGrown} Grass Grown", cancellationToken: cancellationToken); | ||
} | ||
// Assign achievement on 8, 16, 32... | ||
if (!BitOperations.IsPow2(grassGrown) || grassGrown <= 4UL) | ||
return Task.CompletedTask; | ||
|
||
return Task.CompletedTask; | ||
const string msgPrefix = "🏆 Achievement Unlocked: "; | ||
const string herb = "🌿"; | ||
var herbCount = BitOperations.TrailingZeroCount(grassGrown); | ||
var msg = string.Create(msgPrefix.Length + herb.Length * herbCount, herbCount, (buf, _) => | ||
{ | ||
msgPrefix.CopyTo(buf); | ||
for (var i = msgPrefix.Length; i < buf.Length; i += herb.Length) | ||
{ | ||
herb.CopyTo(buf[i..]); | ||
} | ||
}); | ||
return messageContext.ReplyWithTextMessageAndRetryAsync(msg, cancellationToken: cancellationToken); | ||
} | ||
} |
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