Code review

This commit is contained in:
JustArchi
2016-02-24 06:20:35 +01:00
parent a528dee670
commit 44a595c1df
7 changed files with 115 additions and 126 deletions

View File

@@ -205,8 +205,10 @@ namespace ArchiSteamFarm {
return null;
}
var request = new ClientMsgProtobuf<CMsgClientRegisterKey>(EMsg.ClientRegisterKey);
request.SourceJobID = Client.GetNextJobID();
var request = new ClientMsgProtobuf<CMsgClientRegisterKey>(EMsg.ClientRegisterKey) {
SourceJobID = Client.GetNextJobID()
};
request.Body.key = key;
Client.Send(request);
@@ -222,7 +224,7 @@ namespace ArchiSteamFarm {
*/
public sealed override void HandleMsg(IPacketMsg packetMsg) {
public override void HandleMsg(IPacketMsg packetMsg) {
if (packetMsg == null) {
return;
}
@@ -246,10 +248,6 @@ namespace ArchiSteamFarm {
}
var response = new ClientMsgProtobuf<CMsgClientOfflineMessageNotification>(packetMsg);
if (response == null) {
return;
}
Client.PostCallback(new OfflineMessageCallback(packetMsg.TargetJobID, response.Body));
}
@@ -259,10 +257,6 @@ namespace ArchiSteamFarm {
}
var response = new ClientMsgProtobuf<CMsgClientPurchaseResponse>(packetMsg);
if (response == null) {
return;
}
Client.PostCallback(new PurchaseResponseCallback(packetMsg.TargetJobID, response.Body));
}
@@ -272,10 +266,6 @@ namespace ArchiSteamFarm {
}
var response = new ClientMsgProtobuf<CMsgClientUserNotifications>(packetMsg);
if (response == null) {
return;
}
Client.PostCallback(new NotificationsCallback(packetMsg.TargetJobID, response.Body));
}
}

View File

@@ -241,7 +241,89 @@ namespace ArchiSteamFarm {
await Start().ConfigureAwait(false);
}
internal async Task Start() {
internal async Task OnFarmingFinished(bool farmedSomething) {
if (farmedSomething && SendOnFarmingFinished) {
await ResponseSendTrade().ConfigureAwait(false);
}
if (ShutdownOnFarmingFinished) {
Shutdown();
}
}
internal async Task<string> HandleMessage(string message) {
if (string.IsNullOrEmpty(message)) {
return null;
}
if (!message.StartsWith("!")) {
return await ResponseRedeem(BotName, message, true).ConfigureAwait(false);
}
if (!message.Contains(" ")) {
switch (message) {
case "!2fa":
return Response2FA();
case "!2faoff":
return Response2FAOff();
case "!exit":
Program.Exit();
return null;
case "!rejoinchat":
return ResponseRejoinChat();
case "!restart":
Program.Restart();
return "Done";
case "!status":
return ResponseStatus();
case "!statusall":
return ResponseStatusAll();
case "!stop":
return ResponseStop();
case "!loot":
return await ResponseSendTrade().ConfigureAwait(false);
default:
return "Unrecognized command: " + message;
}
} else {
string[] args = message.Split(' ');
switch (args[0]) {
case "!2fa":
return Response2FA(args[1]);
case "!2faoff":
return Response2FAOff(args[1]);
case "!addlicense":
if (args.Length > 2) {
return await ResponseAddLicense(args[1], args[2]).ConfigureAwait(false);
} else {
return await ResponseAddLicense(BotName, args[1]).ConfigureAwait(false);
}
case "!play":
if (args.Length > 2) {
return await ResponsePlay(args[1], args[2]).ConfigureAwait(false);
} else {
return await ResponsePlay(BotName, args[1]).ConfigureAwait(false);
}
case "!redeem":
if (args.Length > 2) {
return await ResponseRedeem(args[1], args[2], false).ConfigureAwait(false);
} else {
return await ResponseRedeem(BotName, args[1], false).ConfigureAwait(false);
}
case "!start":
return await ResponseStart(args[1]).ConfigureAwait(false);
case "!stop":
return ResponseStop(args[1]);
case "!status":
return ResponseStatus(args[1]);
case "!loot":
return await ResponseSendTrade(args[1]).ConfigureAwait(false);
default:
return "Unrecognized command: " + args[0];
}
}
}
private async Task Start() {
if (SteamClient.IsConnected) {
return;
}
@@ -261,7 +343,7 @@ namespace ArchiSteamFarm {
SteamClient.Connect();
}
internal void Stop() {
private void Stop() {
if (!SteamClient.IsConnected) {
return;
}
@@ -271,22 +353,13 @@ namespace ArchiSteamFarm {
SteamClient.Disconnect();
}
internal void Shutdown() {
private void Shutdown() {
KeepRunning = false;
Stop();
Program.OnBotShutdown();
}
internal async Task OnFarmingFinished(bool farmedSomething) {
if (farmedSomething && SendOnFarmingFinished) {
await ResponseSendTrade().ConfigureAwait(false);
}
if (ShutdownOnFarmingFinished) {
Shutdown();
}
}
internal string ResponseStatus() {
private string ResponseStatus() {
if (CardsFarmer.CurrentGamesFarming.Count > 0) {
return "Bot " + BotName + " is currently farming appIDs: " + string.Join(", ", CardsFarmer.CurrentGamesFarming) + " and has a total of " + CardsFarmer.GamesToFarm.Count + " games left to farm.";
} else {
@@ -294,7 +367,7 @@ namespace ArchiSteamFarm {
}
}
internal static string ResponseStatus(string botName) {
private static string ResponseStatus(string botName) {
if (string.IsNullOrEmpty(botName)) {
return null;
}
@@ -307,7 +380,7 @@ namespace ArchiSteamFarm {
return bot.ResponseStatus();
}
internal static string ResponseStatusAll() {
private static string ResponseStatusAll() {
StringBuilder result = new StringBuilder(Environment.NewLine);
int totalBotsCount = Bots.Count;
@@ -324,7 +397,7 @@ namespace ArchiSteamFarm {
return result.ToString();
}
internal async Task<string> ResponseSendTrade() {
private async Task<string> ResponseSendTrade() {
if (SteamMasterID == 0) {
return "Trade couldn't be send because SteamMasterID is not defined!";
}
@@ -349,7 +422,7 @@ namespace ArchiSteamFarm {
}
}
internal static async Task<string> ResponseSendTrade(string botName) {
private static async Task<string> ResponseSendTrade(string botName) {
if (string.IsNullOrEmpty(botName)) {
return null;
}
@@ -362,7 +435,7 @@ namespace ArchiSteamFarm {
return await bot.ResponseSendTrade().ConfigureAwait(false);
}
internal string Response2FA() {
private string Response2FA() {
if (SteamGuardAccount == null) {
return "That bot doesn't have ASF 2FA enabled!";
}
@@ -371,7 +444,7 @@ namespace ArchiSteamFarm {
return "2FA Token: " + SteamGuardAccount.GenerateSteamGuardCode() + " (expires in " + timeLeft + " seconds)";
}
internal static string Response2FA(string botName) {
private static string Response2FA(string botName) {
if (string.IsNullOrEmpty(botName)) {
return null;
}
@@ -384,7 +457,7 @@ namespace ArchiSteamFarm {
return bot.Response2FA();
}
internal string Response2FAOff() {
private string Response2FAOff() {
if (SteamGuardAccount == null) {
return "That bot doesn't have ASF 2FA enabled!";
}
@@ -396,7 +469,7 @@ namespace ArchiSteamFarm {
}
}
internal static string Response2FAOff(string botName) {
private static string Response2FAOff(string botName) {
if (string.IsNullOrEmpty(botName)) {
return null;
}
@@ -409,7 +482,7 @@ namespace ArchiSteamFarm {
return bot.Response2FAOff();
}
internal async Task<string> ResponseRedeem(string message, bool validate) {
private async Task<string> ResponseRedeem(string message, bool validate) {
if (string.IsNullOrEmpty(message)) {
return null;
}
@@ -550,7 +623,7 @@ namespace ArchiSteamFarm {
return response.ToString();
}
internal static async Task<string> ResponseRedeem(string botName, string message, bool validate) {
private static async Task<string> ResponseRedeem(string botName, string message, bool validate) {
if (string.IsNullOrEmpty(botName) || string.IsNullOrEmpty(message)) {
return null;
}
@@ -563,7 +636,7 @@ namespace ArchiSteamFarm {
return await bot.ResponseRedeem(message, validate).ConfigureAwait(false);
}
internal static string ResponseRejoinChat() {
private static string ResponseRejoinChat() {
foreach (Bot bot in Bots.Values) {
bot.JoinMasterChat();
}
@@ -571,7 +644,7 @@ namespace ArchiSteamFarm {
return "Done!";
}
internal async Task<string> ResponseAddLicense(HashSet<uint> gameIDs) {
private async Task<string> ResponseAddLicense(HashSet<uint> gameIDs) {
if (gameIDs == null || gameIDs.Count == 0) {
return null;
}
@@ -592,7 +665,7 @@ namespace ArchiSteamFarm {
return result.ToString();
}
internal static async Task<string> ResponseAddLicense(string botName, string games) {
private static async Task<string> ResponseAddLicense(string botName, string games) {
if (string.IsNullOrEmpty(botName) || string.IsNullOrEmpty(games)) {
return null;
}
@@ -620,7 +693,7 @@ namespace ArchiSteamFarm {
return await bot.ResponseAddLicense(gamesToRedeem).ConfigureAwait(false);
}
internal async Task<string> ResponsePlay(HashSet<uint> gameIDs) {
private async Task<string> ResponsePlay(HashSet<uint> gameIDs) {
if (gameIDs == null || gameIDs.Count == 0) {
return null;
}
@@ -637,7 +710,7 @@ namespace ArchiSteamFarm {
return "Done!";
}
internal static async Task<string> ResponsePlay(string botName, string games) {
private static async Task<string> ResponsePlay(string botName, string games) {
if (string.IsNullOrEmpty(botName) || string.IsNullOrEmpty(games)) {
return null;
}
@@ -665,7 +738,7 @@ namespace ArchiSteamFarm {
return await bot.ResponsePlay(gamesToPlay).ConfigureAwait(false);
}
internal async Task<string> ResponseStart() {
private async Task<string> ResponseStart() {
if (KeepRunning) {
return "That bot instance is already running!";
}
@@ -674,7 +747,7 @@ namespace ArchiSteamFarm {
return "Done!";
}
internal static async Task<string> ResponseStart(string botName) {
private static async Task<string> ResponseStart(string botName) {
if (string.IsNullOrEmpty(botName)) {
return null;
}
@@ -687,7 +760,7 @@ namespace ArchiSteamFarm {
return await bot.ResponseStart().ConfigureAwait(false);
}
internal string ResponseStop() {
private string ResponseStop() {
if (!KeepRunning) {
return "That bot instance is already inactive!";
}
@@ -696,7 +769,7 @@ namespace ArchiSteamFarm {
return "Done!";
}
internal static string ResponseStop(string botName) {
private static string ResponseStop(string botName) {
if (string.IsNullOrEmpty(botName)) {
return null;
}
@@ -709,79 +782,6 @@ namespace ArchiSteamFarm {
return bot.ResponseStop();
}
internal async Task<string> HandleMessage(string message) {
if (string.IsNullOrEmpty(message)) {
return null;
}
if (!message.StartsWith("!")) {
return await ResponseRedeem(BotName, message, true).ConfigureAwait(false);
}
if (!message.Contains(" ")) {
switch (message) {
case "!2fa":
return Response2FA();
case "!2faoff":
return Response2FAOff();
case "!exit":
Program.Exit();
return null;
case "!rejoinchat":
return ResponseRejoinChat();
case "!restart":
Program.Restart();
return "Done";
case "!status":
return ResponseStatus();
case "!statusall":
return ResponseStatusAll();
case "!stop":
return ResponseStop();
case "!loot":
return await ResponseSendTrade().ConfigureAwait(false);
default:
return "Unrecognized command: " + message;
}
} else {
string[] args = message.Split(' ');
switch (args[0]) {
case "!2fa":
return Response2FA(args[1]);
case "!2faoff":
return Response2FAOff(args[1]);
case "!addlicense":
if (args.Length > 2) {
return await ResponseAddLicense(args[1], args[2]).ConfigureAwait(false);
} else {
return await ResponseAddLicense(BotName, args[1]).ConfigureAwait(false);
}
case "!play":
if (args.Length > 2) {
return await ResponsePlay(args[1], args[2]).ConfigureAwait(false);
} else {
return await ResponsePlay(BotName, args[1]).ConfigureAwait(false);
}
case "!redeem":
if (args.Length > 2) {
return await ResponseRedeem(args[1], args[2], false).ConfigureAwait(false);
} else {
return await ResponseRedeem(BotName, args[1], false).ConfigureAwait(false);
}
case "!start":
return await ResponseStart(args[1]).ConfigureAwait(false);
case "!stop":
return ResponseStop(args[1]);
case "!status":
return ResponseStatus(args[1]);
case "!loot":
return await ResponseSendTrade(args[1]).ConfigureAwait(false);
default:
return "Unrecognized command: " + args[0];
}
}
}
private void HandleCallbacks() {
TimeSpan timeSpan = TimeSpan.FromMilliseconds(CallbackSleep);
while (KeepRunning) {

View File

@@ -28,7 +28,7 @@ using System;
using System.IO;
namespace ArchiSteamFarm {
internal sealed class CMsgClientClanInviteAction : ISteamSerializableMessage, ISteamSerializable {
internal sealed class CMsgClientClanInviteAction : ISteamSerializableMessage {
internal ulong GroupID { get; set; } = 0;
internal bool AcceptInvite { get; set; } = true;

View File

@@ -30,6 +30,6 @@ namespace ArchiSteamFarm {
internal static readonly bool IsDebugBuild = false;
#endif
internal static bool IsReleaseBuild { get { return !IsDebugBuild; } }
internal static bool IsReleaseBuild => !IsDebugBuild;
}
}

View File

@@ -221,7 +221,7 @@ namespace ArchiSteamFarm {
}
private static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
Logging.LogGenericInfo("Archi's Steam Farm, version " + Version);
Directory.SetCurrentDirectory(ExecutableDirectory);

View File

@@ -22,7 +22,6 @@
*/
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

View File

@@ -33,7 +33,7 @@ namespace ArchiSteamFarm {
string HandleCommand(string input);
}
internal class WCF : IWCF {
internal sealed class WCF : IWCF {
private const string URL = "http://localhost:1242/ASF"; // 1242 = 1024 + A(65) + S(83) + F(70)
@@ -118,7 +118,7 @@ namespace ArchiSteamFarm {
}
}
internal class Client : ClientBase<IWCF>, IWCF {
internal sealed class Client : ClientBase<IWCF>, IWCF {
internal Client(Binding binding, EndpointAddress address) : base(binding, address) { }
public string HandleCommand(string input) {