public static bool BankAccount(CommandProcess process, string[] command) { BankClient client = (BankClient)process; BankDaemon daemon = (BankDaemon)client.Daemon; var bankFolder = process.computer.fileSystem.rootFile.GetFile("bank"); var accountFile = bankFolder.GetFile("accounts.db"); if (command[0] == "account") { if (command.Length < 2) { process.Print("Usage : account [create/login/resetpass/balance/transfer/transactions/close]"); return(true); } // TODO: Implement Transaction Log var cmdArgs = command[1].Split(' '); if (cmdArgs[0] == "create") { // TODO: When mail daemon is implemented, require an email address for password reset if (cmdArgs.Length < 3) { process.Print("Usage : account create [accountname] [password]"); return(true); } List <string> accounts = new List <string>(); var accountsFile = accountFile.Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); if (accountsFile.Length != 0) { foreach (string line in accountFile.Content.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries)) { var data = line.Split(','); if (data.Length < 4) { continue; } accounts.Add(data[0]); } } if (accounts.Contains(cmdArgs[1])) { process.Print("This account name is not available"); return(true); } daemon.accounts.Add(new BankAccount(cmdArgs[1], 0, cmdArgs[2], client.Session.owner.username)); daemon.UpdateAccountDatabase(); process.Print("Your account has been opened. Use account login [accountname] [password] to login."); } if (cmdArgs[0] == "login") { if (cmdArgs.Length < 3) { process.Print("Usage : account login [accountname] [password]"); return(true); } foreach (var account in daemon.accounts) { if (account.accountName == cmdArgs[1] && account.password == cmdArgs[2]) { client.loggedInAccount = account; daemon.computer.Log(Log.LogEvents.Login, daemon.computer.logs.Count + 1 + " " + client.Session.owner.homeComputer.ip + " logged in as bank account " + account.accountName, client.Session.sessionId, client.Session.owner.homeComputer.ip); process.Print($"Logged into bank account {account.accountName} successfully"); return(true); } } process.Print("Invalid account name or password"); } if (cmdArgs[0] == "resetpass") { if (cmdArgs.Length < 3) { process.Print("Usage : account resetpass [accountname] [newpassword]"); return(true); } // TODO: When mail daemon is implemented, change it to verify using email so players can hack by password reset foreach (var account in daemon.accounts) { if (account.accountName == cmdArgs[1]) { if (account.clientUsername == client.Session.owner.username) { account.password = cmdArgs[2]; daemon.UpdateAccountDatabase(); process.Print("Your password has been changed"); } else { process.Print("You are not the owner of the account"); } break; } } return(true); } if (cmdArgs[0] == "balance") { if (client.loggedInAccount == null) { process.Print("You are not logged in"); return(true); } process.Print($"Account balance for {client.loggedInAccount.accountName} is {client.loggedInAccount.balance}"); } if (cmdArgs[0] == "transfer") { if (cmdArgs.Length < 4) { process.Print("Usage : account transfer [receivingaccountname] [receivingbankip] [amount]"); return(true); } if (client.loggedInAccount == null) { process.Print("You are not logged in"); return(true); } if (client.loggedInAccount.balance < Convert.ToInt32(cmdArgs[3])) { process.Print("Account does not have enough balance"); return(true); } if (Server.Instance.GetComputerManager().GetNodeByIp(cmdArgs[2]) == null) { process.Print("The receiving computer does not exist"); return(true); } BankDaemon targetBank = null; foreach (var computer in Server.Instance.GetComputerManager().NodeList) { if (computer.ip == cmdArgs[2]) { Daemon targetDaemon = computer.GetDaemon("Bank"); if (targetDaemon == null) { process.Print("The receiving computer does not have a bank daemon"); return(true); } targetBank = (BankDaemon)targetDaemon; break; } } BankAccount accountTo = null; foreach (var account in targetBank.accounts) { if (account.accountName == cmdArgs[1]) { accountTo = account; break; } } if (accountTo == null) { process.Print("The receiving account does not exist"); return(true); } targetBank.ProcessBankTransfer(client.loggedInAccount, accountTo, cmdArgs[2], int.Parse(cmdArgs[3]), client.Session); daemon.LogTransaction($"{client.loggedInAccount.accountName},{client.Session.owner.homeComputer.ip} transferred {cmdArgs[3]} from {client.loggedInAccount.accountName} to {accountTo.accountName}@{targetBank.computer.ip}", client.Session.sessionId, client.Session.owner.homeComputer.ip); } if (cmdArgs[0] == "transactions") { if (client.loggedInAccount == null) { process.Print("You are not logged in"); return(true); } File transactionLog = bankFolder.GetFile("transactionlog.db"); if (transactionLog == null) { process.Print("This bank does not keep transaction logs"); return(true); } string[] transactions = transactionLog.Content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); if (transactions.Length == 0) { process.Print("The transaction log database is empty"); return(true); } string transactionLogForClient = ""; foreach (var transactionUnsplit in transactions) { string[] transaction = transactionUnsplit.Split(','); if (transaction[0] == client.loggedInAccount.accountName) { transactionLogForClient += transaction[1] + "\n"; } } if (transactionLogForClient == "") { transactionLogForClient += "Your transaction log is empty"; } File transactionFileForClient = client.Session.owner.homeComputer.fileSystem.rootFile.GetFile("Bank_Transaction_Log_For_" + client.loggedInAccount.accountName); if (transactionFileForClient == null) { transactionFileForClient = client.Session.owner.homeComputer.fileSystem.CreateFile(client.Session.owner.homeComputer, client.Session.owner.homeComputer.fileSystem.rootFile, "Bank_Transaction_Log_For_" + client.loggedInAccount.accountName); transactionFileForClient.Content = transactionLogForClient; transactionFileForClient.OwnerId = 0; transactionFileForClient.Permissions.SetPermission(FilePermissions.PermissionType.User, true, true, true); transactionFileForClient.Permissions.SetPermission(FilePermissions.PermissionType.Group, true, true, true); transactionFileForClient.Group = transactionFileForClient.Parent.Group; process.Print("A file containing your transaction log has been uploaded to your computer"); return(true); } transactionFileForClient.Content = transactionLogForClient; process.Print("A file containing your transaction log has been uploaded to your computer"); } if (cmdArgs[0] == "close") { if (client.loggedInAccount == null) { process.Print("You are not logged in"); return(true); } if (client.loggedInAccount.balance != 0) { process.Print("Your account balance must be zero before you can close your account.\nUse account transfer to transfer your money out of your account"); return(true); } daemon.accounts.Remove(client.loggedInAccount); daemon.UpdateAccountDatabase(); client.loggedInAccount = null; process.Print("Your account has been closed"); } return(true); } return(false); }
public static bool BankAccount(CommandProcess process, string[] command) { BankClient client = (BankClient)process; BankDaemon daemon = (BankDaemon)client.Daemon; var bankFolder = process.computer.fileSystem.rootFile.GetFile("bank"); var accountFile = bankFolder.GetFile("accounts.db"); if (command[0] == "account") { if (command.Length < 2) { process.Print("Usage : account [create/login/resetpass/balance/transfer/transactions/close]"); return(true); } var cmdArgs = command[1].Split(' '); if (cmdArgs[0] == "create") { if (cmdArgs.Length < 4) { process.Print("Usage : account create [accountname] [email] [password]"); return(true); } string[] emailArgs = cmdArgs[2].Split('@'); if (emailArgs.Length != 2) { process.Print("Not a valid email address"); return(true); } Node emailServer = Server.Instance.GetComputerManager().GetNodeByIp(emailArgs[1]); if (emailServer == null) { process.Print("The email server does not exist!\nMake sure you use the IP of the mail server, not the domain."); } MailDaemon mailDaemon = (MailDaemon)emailServer.GetDaemon("mail"); if (mailDaemon == null) { process.Print("IP of the email address does not have an email server installed!"); return(true); } if (!mailDaemon.accounts.Any(x => x.accountName == emailArgs[0])) { process.Print("The email account on that email server does not exist!"); return(true); } List <string> accounts = new List <string>(); var accountsFile = accountFile.Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); if (accountsFile.Length != 0) { foreach (string line in accountFile.Content.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries)) { var data = line.Split(','); if (data.Length < 5) { continue; } accounts.Add(data[0]); } } if (accounts.Contains(cmdArgs[1])) { process.Print("This account name is not available"); return(true); } daemon.accounts.Add(new BankAccount(cmdArgs[1], 0, cmdArgs[3], client.Session.owner.username, cmdArgs[2])); daemon.UpdateAccountDatabase(); process.Print("Your account has been opened. Use account login [accountname] [password] to login."); } if (cmdArgs[0] == "login") { if (cmdArgs.Length < 3) { process.Print("Usage : account login [accountname] [password]"); return(true); } foreach (var account in daemon.accounts) { if (account.accountName == cmdArgs[1] && account.password == cmdArgs[2]) { client.loggedInAccount = account; daemon.computer.Log(Log.LogEvents.Login, daemon.computer.logs.Count + 1 + " " + client.Session.owner.homeComputer.ip + " logged in as bank account " + account.accountName, client.Session.sessionId, client.Session.owner.homeComputer.ip); process.Print($"Logged into bank account {account.accountName} successfully"); return(true); } } process.Print("Invalid account name or password"); } if (cmdArgs[0] == "resetpass") { if (cmdArgs.Length == 2) { BankAccount account = daemon.accounts.Where(x => x.accountName == cmdArgs[1]).DefaultIfEmpty(null).First(); if (account == null) { process.Print("That account does not exist!"); return(true); } if (!MailDaemon.SendPasswordResetEmail(process.computer, account.email, account.accountName)) { process.Print("The email failed to send! Either the account no longer exists, or an authentication code has already been sent to this email less than an hour ago!"); return(true); } process.Print("Password reset email sent to the email associated with this account!"); return(true); } if (cmdArgs.Length < 4) { process.Print("Usage : account resetpass [accountname] [authentication code] [newpassword]"); return(true); } if (!int.TryParse(cmdArgs[2], out int authCode)) { process.Print("Please use a valid authentication code!"); return(true); } if (!MailDaemon.CheckIfAuthRequestIsValid(process.computer, cmdArgs[1], authCode) || !daemon.accounts.Any(x => x.accountName == cmdArgs[1])) { process.Print("Something went wrong when trying to authenticate!\nEither the username does not exist, or the authentication code is invalid!"); return(true); } foreach (BankAccount account in daemon.accounts) { if (account.accountName == cmdArgs[1]) { account.password = cmdArgs[3]; daemon.UpdateAccountDatabase(); } } process.Print("Password reset successful!"); return(true); } if (cmdArgs[0] == "balance") { if (client.loggedInAccount == null) { process.Print("You are not logged in"); return(true); } process.Print($"Account balance for {client.loggedInAccount.accountName} is {client.loggedInAccount.balance}"); } if (cmdArgs[0] == "transfer") { if (cmdArgs.Length < 4) { process.Print("Usage : account transfer [receivingaccountname] [receivingbankip] [amount]"); return(true); } if (client.loggedInAccount == null) { process.Print("You are not logged in"); return(true); } if (client.loggedInAccount.balance < Convert.ToInt32(cmdArgs[3])) { process.Print("Account does not have enough balance"); return(true); } if (Server.Instance.GetComputerManager().GetNodeByIp(cmdArgs[2]) == null) { process.Print("The receiving computer does not exist"); return(true); } BankDaemon targetBank = null; foreach (var computer in Server.Instance.GetComputerManager().NodeList) { if (computer.ip == cmdArgs[2]) { Daemon targetDaemon = computer.GetDaemon("Bank"); if (targetDaemon == null) { process.Print("The receiving computer does not have a bank daemon"); return(true); } targetBank = (BankDaemon)targetDaemon; break; } } BankAccount accountTo = null; foreach (var account in targetBank.accounts) { if (account.accountName == cmdArgs[1]) { accountTo = account; break; } } if (accountTo == null) { process.Print("The receiving account does not exist"); return(true); } targetBank.ProcessBankTransfer(client.loggedInAccount, accountTo, cmdArgs[2], int.Parse(cmdArgs[3]), client.Session); daemon.LogTransaction($"{client.loggedInAccount.accountName},{client.Session.owner.homeComputer.ip} transferred {cmdArgs[3]} from {client.loggedInAccount.accountName} to {accountTo.accountName}@{targetBank.computer.ip}", client.Session.sessionId, client.Session.owner.homeComputer.ip); } if (cmdArgs[0] == "transactions") { if (client.loggedInAccount == null) { process.Print("You are not logged in"); return(true); } File transactionLog = bankFolder.GetFile("transactionlog.db"); if (transactionLog == null) { process.Print("This bank does not keep transaction logs"); return(true); } List <string> transactions = transactionLog.Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (transactions.Count == 0) { process.Print("The transaction log database is empty"); return(true); } int pageNumber = 1; int totalPages = transactions.Count / 10 == 0 ? 1 : transactions.Count / 10 + transactions.Count % 10 >= 1 ? 1 : 0; if (cmdArgs.Length > 1) { if (int.TryParse(cmdArgs[1], out pageNumber)) { if (pageNumber != 1) { if (pageNumber <= 0 || pageNumber > totalPages) { process.Print("Invalid page number"); return(true); } } } } string transactionLogForClient = "------ Transaction Log for " + client.loggedInAccount.accountName + " ------\n"; if (pageNumber == 1) { transactions = (List <string>)transactions.Where(transaction => transaction.Split(',')[0] == client.loggedInAccount.accountName).Take(10); } else { transactions = (List <string>)transactions.Where(transaction => transaction.Split(',')[0] == client.loggedInAccount.accountName).Skip(pageNumber * 10).Take(10); } //for (int i = transactions.Length - pageModifier; transactions.Length - pageModifier < i; i--) //{ // string[] transaction = transactions[currentTransaction].Split(','); // if (transaction[0] == client.loggedInAccount.accountName) // { // transactionLogForClient += transaction[1] + "\n"; // } // else // i++; // currentTransaction--; //} foreach (var transaction in transactions) { transactionLogForClient += transaction[1] + "\n"; } if (transactionLogForClient == "------ Transaction Log for " + client.loggedInAccount.accountName + " ------\n") { transactionLogForClient += "\nThis account's transaction log is empty\n"; } transactionLogForClient += "------ Page " + pageNumber + "/" + totalPages + " ------"; process.Print(transactionLogForClient); } if (cmdArgs[0] == "close") { if (client.loggedInAccount == null) { process.Print("You are not logged in"); return(true); } if (client.loggedInAccount.balance != 0) { process.Print("Your account balance must be zero before you can close your account.\nUse account transfer to transfer your money out of your account"); return(true); } daemon.accounts.Remove(client.loggedInAccount); daemon.UpdateAccountDatabase(); client.loggedInAccount = null; process.Print("Your account has been closed"); } return(true); } return(false); }
public static bool Account(CommandProcess process, string[] command) { BankClient client = (BankClient)process; BankDaemon daemon = (BankDaemon)client.Daemon; var bankFolder = process.computer.fileSystem.rootFile.GetFile("bank"); var accountFile = bankFolder.GetFile("accounts.db"); if (command[0] == "account") { if (command.Length < 2) { process.Print("Usage : account [create/login/resetpass/balance/transfer/transactions/close]"); return(true); } // TODO: Implement Transaction Log var cmdArgs = command[1].Split(' '); if (cmdArgs[0] == "create") { // TODO: When mail daemon is implemented, require an email address for password reset if (cmdArgs.Length < 3) { process.Print("Usage : account create [accountname] [password]"); return(true); } List <string> accounts = new List <string>(); var accountsFile = accountFile.Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); if (accountsFile.Length != 0) { foreach (var account in accountsFile) { string[] accountData = account.Split('='); string accountPassword = accountData[1]; // Update temporary holding array accountData = accountData[0].Split(','); accounts.Add(accountData[accountData.Length - 1]); } } if (accounts.Contains(cmdArgs[1])) { process.Print("This account name is not available"); return(true); } daemon.accounts.Add(new Account(cmdArgs[1], 0, cmdArgs[2], client.Session.owner.username)); daemon.UpdateAccountDatabase(); process.Print("Your account has been opened. Use account login [accountname] [password] to login."); } if (cmdArgs[0] == "login") { if (cmdArgs.Length < 3) { process.Print("Usage : account login [accountname] [password]"); return(true); } foreach (var account in daemon.accounts) { if (account.accountName == cmdArgs[1] && account.password == cmdArgs[2]) { client.loggedInAccount = account; daemon.computer.Log(Log.LogEvents.Login, daemon.computer.logs.Count + 1 + " " + client.Session.owner.homeComputer.ip + " logged in as bank account " + account.accountName, client.Session.sessionId, client.Session.owner.homeComputer.ip); process.Print($"Logged into bank account {account.accountName} successfully"); break; } } } if (cmdArgs[0] == "resetpass") { if (cmdArgs.Length < 3) { process.Print("Usage : account resetpass [accountname] [newpassword]"); return(true); } // TODO: When mail daemon is implemented, change it to verify using email so players can hack by password reset foreach (var account in daemon.accounts) { if (account.accountName == cmdArgs[1]) { if (account.clientUsername == client.Session.owner.username) { account.password = cmdArgs[2]; daemon.UpdateAccountDatabase(); process.Print("Your password has been changed"); } else { process.Print("You are not the owner of the account"); } break; } } return(true); } if (cmdArgs[0] == "balance") { if (client.loggedInAccount == null) { process.Print("You are not logged in"); return(true); } process.Print($"Account balance for {client.loggedInAccount.accountName} is {client.loggedInAccount.balance}"); } if (cmdArgs[0] == "transfer") { if (cmdArgs.Length < 4) { process.Print("Usage : account transfer [receivingaccountname] [receivingbankip] [amount]"); return(true); } if (client.loggedInAccount.balance < Convert.ToInt32(cmdArgs[3])) { process.Print("Account does not have enough balance"); return(true); } if (Server.Instance.GetComputerManager().GetNodeByIp(cmdArgs[2]) == null) { process.Print("The receiving computer does not exist"); return(true); } BankDaemon targetBank = null; foreach (var computer in Server.Instance.GetComputerManager().NodeList) { if (computer.ip == cmdArgs[2]) { Daemon targetDaemon = computer.GetDaemon("Bank"); if (targetDaemon == null) { process.Print("The receiving computer does not have a bank daemon"); return(true); } targetBank = (BankDaemon)targetDaemon; break; } } Account accountTo = null; foreach (var account in targetBank.accounts) { if (account.accountName == cmdArgs[1]) { accountTo = account; break; } } if (accountTo == null) { process.Print("The receiving account does not exist"); return(true); } daemon.computer.Log(Log.LogEvents.BankTransfer, $"{client.computer.ip} transferred {cmdArgs[3]} from {client.loggedInAccount.accountName} to {accountTo.accountName}@{targetBank.computer.ip}", client.Session.sessionId, client.computer.ip); targetBank.ProcessBankTransfer(client.loggedInAccount, accountTo, cmdArgs[2], int.Parse(cmdArgs[3]), client.Session); } return(true); } return(false); }