~ruther/NosTale-Gfless

d5a07bd2633006a840adbf743758cab24bf56b9d — František Boháček 4 years ago 1fb9e06
feat(example): add option for more accounts
2 files changed, 35 insertions(+), 19 deletions(-)

M NostaleGfless.Example/Options.cs
M NostaleGfless.Example/Program.cs
M NostaleGfless.Example/Options.cs => NostaleGfless.Example/Options.cs +2 -1
@@ 1,3 1,4 @@
using System.Collections.Generic;
using CommandLine;
using Newtonsoft.Json;



@@ 11,7 12,7 @@ namespace NostaleGfless.Example
        [Option('n', "nostale", Required = true, HelpText = "Path to nostale folder or NostaleClientX.exe")]
        public string NostalePath { get; set; }

        [Option('a', "account", Required = false, HelpText = "Name of the account to connect to. Otherwise the first one will be used.")]
        [Option('a', "account", Required = false, HelpText = "Name of the account to connect to. Otherwise the first one will be used. For more accounts split them using coma")]
        public string AccountName { get; set; }

        [Value(0, MetaName = "Email", Required = true, HelpText = "Gameforge account email")]

M NostaleGfless.Example/Program.cs => NostaleGfless.Example/Program.cs +33 -18
@@ 1,4 1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommandLine;


@@ 33,41 34,55 @@ namespace NostaleGfless.Example
                var authenticator = new GameforgeAuthenticator();
                authenticator.InstallationId = installationId;
                var launcher  = await authenticator.Authenticate(options.Email, options.Password);

                GameforgeAccount account = launcher.Accounts.FirstOrDefault();
                if (account == null)
                if (launcher.Accounts.Count == 0)
                {
                    throw new InvalidOperationException("There are no nostale account on the gameforge account");
                }

                List<GameforgeAccount> accounts = new List<GameforgeAccount>();

                if (options.AccountName != null)
                {
                    account = launcher.Accounts.FirstOrDefault(x => x.Name == options.AccountName);

                    if (account == null)
                    foreach (string accountName in options.AccountName.Split(','))
                    {
                        account = launcher.Accounts.FirstOrDefault(x => x.Name.Contains(options.AccountName));
                        GameforgeAccount account = launcher.Accounts.FirstOrDefault(x => x.Name == accountName);

                        if (account == null)
                        {
                            throw new InvalidOperationException("Account not found");
                            account = launcher.Accounts.FirstOrDefault(x => x.Name.Contains(accountName));

                            if (account == null)
                            {
                                throw new InvalidOperationException($"Account {accountName} not found");
                            }
                        }
                        
                        accounts.Add(account);
                    }
                }
                
                Console.WriteLine($"Selected account: {account.Name}");
                Console.WriteLine("Launching nostale with the selected account ...");
                    
                Console.WriteLine("Waiting for nostale ...");
                var result = await launcher.Launch(account, options.NostalePath);

                if (result != null && result.Initialized)
                if (accounts.Count == 0)
                {
                    Console.WriteLine($"Nostale launched successfully. Process id: {result.ProcessId}, Session id: {result.SessionId}");
                    accounts.Add(launcher.Accounts.FirstOrDefault());
                }
                else

                foreach (GameforgeAccount account in accounts)
                {
                    Console.WriteLine("There was an error launching nostale");
                    Console.WriteLine($"Selected account: {account.Name}");
                    Console.WriteLine("Launching nostale with the selected account ...");

                    Console.WriteLine("Waiting for nostale ...");
                    var result = await launcher.Launch(account, options.NostalePath);

                    if (result != null && result.Initialized)
                    {
                        Console.WriteLine(
                            $"Nostale launched successfully. Process id: {result.ProcessId}, Session id: {result.SessionId}");
                    }
                    else
                    {
                        Console.WriteLine("There was an error launching nostale");
                    }
                }

                ewh.Set();

Do not follow this link