1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . Composition ;
44using System . Threading . Tasks ;
88using ArchiSteamFarm . Steam . Storage ;
99using ArchiSteamFarm . Plugins . Interfaces ;
1010using ArchiSteamFarm . Localization ;
11- using JetBrains . Annotations ;
1211using SteamKit2 ;
1312using System . Linq ;
1413using System . Collections . Concurrent ;
1514
1615namespace ASFAchievementManager {
1716 [ Export ( typeof ( IPlugin ) ) ]
1817 public sealed class ASFAchievementManager : IBotSteamClient , IBotCommand {
19- private static ConcurrentDictionary < Bot , AchievementHandler > AchievementHandlers = new ConcurrentDictionary < Bot , AchievementHandler > ( ) ;
18+ private static readonly ConcurrentDictionary < Bot , AchievementHandler > AchievementHandlers = new ( ) ;
2019 public string Name => "ASF Achievement Manager" ;
2120 public Version Version => typeof ( ASFAchievementManager ) . Assembly . GetName ( ) . Version ?? new Version ( "0" ) ;
2221
23- public void OnLoaded ( ) => ASF . ArchiLogger . LogGenericInfo ( "ASF Achievement Manager Plugin by Ryzhehvost, powered by ginger cats" ) ;
22+ public Task OnLoaded ( ) {
23+ ASF . ArchiLogger . LogGenericInfo ( "ASF Achievement Manager Plugin by Ryzhehvost, powered by ginger cats" ) ;
24+ return Task . CompletedTask ;
25+ }
2426
25- public async Task < string ? > OnBotCommand ( [ NotNull ] Bot bot , ulong steamID , [ NotNull ] string message , string [ ] args ) {
27+ public async Task < string ? > OnBotCommand ( Bot bot , ulong steamID , string message , string [ ] args ) {
2628
2729 switch ( args . Length ) {
2830 case 0 :
2931 bot . ArchiLogger . LogNullError ( nameof ( args ) ) ;
3032
3133 return null ;
3234 case 1 :
33- switch ( args [ 0 ] . ToUpperInvariant ( ) ) {
34-
35- default :
36- return null ;
37- }
35+ return args [ 0 ] . ToUpperInvariant ( ) switch {
36+ _ => null ,
37+ } ;
3838 default :
39- switch ( args [ 0 ] . ToUpperInvariant ( ) ) {
40- case "ALIST" when args . Length > 2 :
41- return await ResponseAchievementList ( steamID , args [ 1 ] , Utilities . GetArgsAsText ( args , 2 , "," ) ) . ConfigureAwait ( false ) ;
42- case "ALIST" :
43- return await ResponseAchievementList ( steamID , bot , args [ 1 ] ) . ConfigureAwait ( false ) ;
44- case "ASET" when args . Length > 3 :
45- return await ResponseAchievementSet ( steamID , args [ 1 ] , args [ 2 ] , Utilities . GetArgsAsText ( args , 3 , "," ) , true ) . ConfigureAwait ( false ) ;
46- case "ASET" when args . Length > 2 :
47- return await ResponseAchievementSet ( steamID , bot , args [ 1 ] , Utilities . GetArgsAsText ( args , 2 , "," ) , true ) . ConfigureAwait ( false ) ;
48- case "ARESET" when args . Length > 3 :
49- return await ResponseAchievementSet ( steamID , args [ 1 ] , args [ 2 ] , Utilities . GetArgsAsText ( args , 3 , "," ) , false ) . ConfigureAwait ( false ) ;
50- case "ARESET" when args . Length > 2 :
51- return await ResponseAchievementSet ( steamID , bot , args [ 1 ] , Utilities . GetArgsAsText ( args , 2 , "," ) , false ) . ConfigureAwait ( false ) ;
52- default :
53- return null ;
54- }
39+ return args [ 0 ] . ToUpperInvariant ( ) switch {
40+ "ALIST" when args . Length > 2 => await ResponseAchievementList ( steamID , args [ 1 ] , Utilities . GetArgsAsText ( args , 2 , "," ) ) . ConfigureAwait ( false ) ,
41+ "ALIST" => await ResponseAchievementList ( steamID , bot , args [ 1 ] ) . ConfigureAwait ( false ) ,
42+ "ASET" when args . Length > 3 => await ResponseAchievementSet ( steamID , args [ 1 ] , args [ 2 ] , Utilities . GetArgsAsText ( args , 3 , "," ) , true ) . ConfigureAwait ( false ) ,
43+ "ASET" when args . Length > 2 => await ResponseAchievementSet ( steamID , bot , args [ 1 ] , Utilities . GetArgsAsText ( args , 2 , "," ) , true ) . ConfigureAwait ( false ) ,
44+ "ARESET" when args . Length > 3 => await ResponseAchievementSet ( steamID , args [ 1 ] , args [ 2 ] , Utilities . GetArgsAsText ( args , 3 , "," ) , false ) . ConfigureAwait ( false ) ,
45+ "ARESET" when args . Length > 2 => await ResponseAchievementSet ( steamID , bot , args [ 1 ] , Utilities . GetArgsAsText ( args , 2 , "," ) , false ) . ConfigureAwait ( false ) ,
46+ _ => null ,
47+ } ;
5548 }
5649 }
5750
58- public void OnBotSteamCallbacksInit ( [ NotNull ] Bot bot , [ NotNull ] CallbackManager callbackManager ) { }
51+ public Task OnBotSteamCallbacksInit ( Bot bot , CallbackManager callbackManager ) => Task . CompletedTask ;
5952
60- public IReadOnlyCollection < ClientMsgHandler > OnBotSteamHandlersInit ( [ NotNull ] Bot bot ) {
61- AchievementHandler CurrentBotAchievementHandler = new AchievementHandler ( ) ;
53+ public Task < IReadOnlyCollection < ClientMsgHandler > ? > OnBotSteamHandlersInit ( Bot bot ) {
54+ AchievementHandler CurrentBotAchievementHandler = new ( ) ;
6255 AchievementHandlers . TryAdd ( bot , CurrentBotAchievementHandler ) ;
63- return new HashSet < ClientMsgHandler > { CurrentBotAchievementHandler } ;
56+ return Task . FromResult < IReadOnlyCollection < ClientMsgHandler > ? > ( new HashSet < ClientMsgHandler > { CurrentBotAchievementHandler } ) ;
6457 }
6558
6659 //Responses
@@ -82,7 +75,7 @@ public IReadOnlyCollection<ClientMsgHandler> OnBotSteamHandlersInit([NotNull] Bo
8275 return null ;
8376 }
8477
85- HashSet < uint > gamesToGetAchievements = new HashSet < uint > ( ) ;
78+ HashSet < uint > gamesToGetAchievements = new ( ) ;
8679
8780 foreach ( string game in gameIDs ) {
8881 if ( ! uint . TryParse ( game , out uint gameID ) || ( gameID == 0 ) ) {
@@ -95,7 +88,7 @@ public IReadOnlyCollection<ClientMsgHandler> OnBotSteamHandlersInit([NotNull] Bo
9588
9689 IList < string > results = await Utilities . InParallel ( gamesToGetAchievements . Select ( appID => Task . Run < string > ( ( ) => AchievementHandler . GetAchievements ( bot , appID ) ) ) ) . ConfigureAwait ( false ) ;
9790
98- List < string > responses = new List < string > ( results . Where ( result => ! string . IsNullOrEmpty ( result ) ) ) ;
91+ List < string > responses = new ( results . Where ( result => ! string . IsNullOrEmpty ( result ) ) ) ;
9992
10093 return responses . Count > 0 ? bot . Commands . FormatBotResponse ( string . Join ( Environment . NewLine , responses ) ) : null ;
10194
@@ -116,19 +109,19 @@ public IReadOnlyCollection<ClientMsgHandler> OnBotSteamHandlersInit([NotNull] Bo
116109
117110 IList < string ? > results = await Utilities . InParallel ( bots . Select ( bot => ResponseAchievementList ( steamID , bot , appids ) ) ) . ConfigureAwait ( false ) ;
118111
119- List < string ? > responses = new List < string ? > ( results . Where ( result => ! string . IsNullOrEmpty ( result ) ) ) ;
112+ List < string ? > responses = new ( results . Where ( result => ! string . IsNullOrEmpty ( result ) ) ) ;
120113
121114 return responses . Count > 0 ? string . Join ( Environment . NewLine , responses ) : null ;
122115 }
123116
124117
125- private static async Task < string ? > ResponseAchievementSet ( ulong steamID , Bot bot , string appid , string AchievementNumbers , bool set = true ) {
118+ private static async Task < string ? > ResponseAchievementSet ( ulong steamID , Bot bot , string appid , string achievementNumbers , bool set = true ) {
126119 if ( ! bot . HasAccess ( steamID , BotConfig . EAccess . Master ) ) {
127120 return null ;
128121 }
129122
130- if ( string . IsNullOrEmpty ( AchievementNumbers ) ) {
131- return bot . Commands . FormatBotResponse ( string . Format ( Strings . ErrorObjectIsNull , nameof ( AchievementNumbers ) ) ) ;
123+ if ( string . IsNullOrEmpty ( achievementNumbers ) ) {
124+ return bot . Commands . FormatBotResponse ( string . Format ( Strings . ErrorObjectIsNull , nameof ( achievementNumbers ) ) ) ;
132125 }
133126 if ( ! uint . TryParse ( appid , out uint appId ) ) {
134127 return bot . Commands . FormatBotResponse ( string . Format ( Strings . ErrorIsInvalid , nameof ( appId ) ) ) ;
@@ -143,11 +136,11 @@ public IReadOnlyCollection<ClientMsgHandler> OnBotSteamHandlersInit([NotNull] Bo
143136 return null ;
144137 }
145138
146- HashSet < uint > achievements = new HashSet < uint > ( ) ;
139+ HashSet < uint > achievements = new ( ) ;
147140
148- string [ ] achievementStrings = AchievementNumbers . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
141+ string [ ] achievementStrings = achievementNumbers . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
149142
150- if ( ! AchievementNumbers . Equals ( "*" ) ) {
143+ if ( ! achievementNumbers . Equals ( "*" ) ) {
151144 foreach ( string achievement in achievementStrings ) {
152145 if ( ! uint . TryParse ( achievement , out uint achievementNumber ) || ( achievementNumber == 0 ) ) {
153146 return bot . Commands . FormatBotResponse ( string . Format ( Strings . ErrorParsingObject , achievement ) ) ;
@@ -162,17 +155,17 @@ public IReadOnlyCollection<ClientMsgHandler> OnBotSteamHandlersInit([NotNull] Bo
162155 return bot . Commands . FormatBotResponse ( await Task . Run < string > ( ( ) => AchievementHandler . SetAchievements ( bot , appId , achievements , set ) ) . ConfigureAwait ( false ) ) ;
163156 }
164157
165- private static async Task < string ? > ResponseAchievementSet ( ulong steamID , string botNames , string appid , string AchievementNumbers , bool set = true ) {
158+ private static async Task < string ? > ResponseAchievementSet ( ulong steamID , string botNames , string appid , string achievementNumbers , bool set = true ) {
166159
167160 HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
168161
169162 if ( ( bots == null ) || ( bots . Count == 0 ) ) {
170163 return Commands . FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
171164 }
172165
173- IList < string ? > results = await Utilities . InParallel ( bots . Select ( bot => ResponseAchievementSet ( steamID , bot , appid , AchievementNumbers , set ) ) ) . ConfigureAwait ( false ) ;
166+ IList < string ? > results = await Utilities . InParallel ( bots . Select ( bot => ResponseAchievementSet ( steamID , bot , appid , achievementNumbers , set ) ) ) . ConfigureAwait ( false ) ;
174167
175- List < string ? > responses = new List < string ? > ( results . Where ( result => ! string . IsNullOrEmpty ( result ) ) ) ;
168+ List < string ? > responses = new ( results . Where ( result => ! string . IsNullOrEmpty ( result ) ) ) ;
176169
177170 return responses . Count > 0 ? string . Join ( Environment . NewLine , responses ) : null ;
178171 }
0 commit comments