} // login command // function that try login and produces the changes acordingly (change to main window or warn the user about the error) private void Login(object parameter) { // get password PasswordBox pBox = parameter as PasswordBox; string password = pBox.Password; if (Username.Length == 0 || password.Length == 0) // if one of the fields isn't fulfilled { MessageBox.Show(Application.Current.MainWindow, "Provide username and password in order to login!", "Missing fields", MessageBoxButton.OK, MessageBoxImage.Error); } else if (client.Login(Username, Encrypt(password))) // if succesfully logged { NotificationMessenger.sendNotification(this, new NotificationType(NotifType.Login, null), "DEFAULT"); } else // else { if (!noserver) // wrong login info or already logged { MessageBox.Show(Application.Current.MainWindow, "Wrong user/password information or already logged in.\nPlease try again!", "Wrong login", MessageBoxButton.OK, MessageBoxImage.Error); } else // can't reach server { MessageBox.Show(Application.Current.MainWindow, "Can't reach server! Exiting Application!", "No server", MessageBoxButton.OK, MessageBoxImage.Exclamation); Environment.Exit(-1); } } }
// function that performs the logout on server internal void Logout() { try { if (this.user != null) // if not already logged out { // logout in server diginoteSystem.Logout(user); // unsubscribe changes evRepeater.ChangeEvent -= ChangeHandler; diginoteSystem.ChangeEvent -= evRepeater.Repeater; // reset data user = null; Quotation = 0.0; Diginotes = null; Orders = null; QuotationEvolution = null; TransactionsPerMin = null; } } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } }
// function that gets the statistics information in the server internal void GetStatistics() { try { // initialize lists QuotationEvolution = new ObservableCollection <DataPoint>(); TransactionsPerMin = new ObservableCollection <DataPoint>(); // add stats to the list foreach (Pair <DateTime, double> pair in diginoteSystem.GetQuotationEvolution()) { QuotationEvolution.Add(new DataPoint(DateTimeAxis.ToDouble(pair.first), pair.second)); } // add stats to the list foreach (Pair <DateTime, int> pair in diginoteSystem.GetTransactionsPerMin()) { TransactionsPerMin.Add(new DataPoint(DateTimeAxis.ToDouble(pair.first), pair.second)); } } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } }
// function that performs the logout private void Logout(object parameter) { // stop diggings StopDigging(); // logout client.Logout(); // notify NotificationMessenger.sendNotification(this, new NotificationType(NotifType.LogOut, null), ""); }
// function that shows the user the dialog to approve keeping an order after login private void QueryApproval(object sender, EventArgs e) { // stop the timer DispatcherTimer timer = (DispatcherTimer)sender; timer.Tick -= QueryApproval; timer.Stop(); // notify main window to show the dialog NotificationMessenger.sendNotification(this, new NotificationType(NotifType.AskApprove, null), ""); }
// function that gets the time to dig a new diginote internal void GetDigSeed() { try { // get time DigTime = diginoteSystem.GetDigtime(); } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } }
// function that suggests the new quotation in the server internal void SetNewQuotation(double value) { try { // suggest quotation diginoteSystem.SuggestNewQuotation(user, value); } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } }
// function that warns main window to show change quotation dialog to user private void ChangeQuotation(object parameter) { // get operation string operation = parameter as string; // show the dialog with the correct parameter if (operation == "Lower") { NotificationMessenger.sendNotification(this, new NotificationType(NotifType.AskQuotation, null), "-" + Quotation); } else { NotificationMessenger.sendNotification(this, new NotificationType(NotifType.AskQuotation, null), "+" + Quotation); } }
// function that add a new buy order in server internal void BuyDiginotes(int quantity) { try { // add sell order in server and add the returned order to orders list Orders.Insert(0, diginoteSystem.AddBuyOrder(user, quantity, OrderType.Buy)); // notify udpdate NotificationMessenger.sendNotification(this, new NotificationType(NotifType.Order, null), ""); } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } }
// function that gets the information about diginotes owned by the logged user in the server internal void GetDiginotes() { try { // initialize list Diginotes = new ObservableCollection <DiginoteInfo>(); // get diginotes info and adds them to the list diginoteSystem.DiginotesFromUser(user).ForEach(Diginotes.Add); } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } }
// function that performs the registration on server internal bool Register(string name, string user, string password) { try { if (diginoteSystem.RegisterUser(name, user, password)) // try register in server { // if success then login Login(user, password); return(true); } } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } return(false); }
// function that performs the login on server internal bool Login(string user, string password) { try { Pair <bool, User> result = diginoteSystem.Login(user, password); // try login in server if (result.first) { // set user this.user = result.second; // get actual quotation Quotation = diginoteSystem.GetQuotation(); // get diginotes GetDiginotes(); // get orders GetOrders(); // get info GetInfo(); // get statistics GetStatistics(); // subscribe changes evRepeater.ChangeEvent += ChangeHandler; diginoteSystem.ChangeEvent += evRepeater.Repeater; // return success return(true); } } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), "DEFAULT"); } return(false); }
// function that gets the information about system in the server internal void GetInfo() { try { // get info Tuple <int, int, int, int, int> info = diginoteSystem.GetSystemInfo(); // set data NumUsers = info.Item1; NumLoggedUsers = info.Item2; NumSysDiginotes = info.Item3; DiginotesOffer = info.Item4; DiginotesDemand = info.Item5; } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } }
// function that gets the new diginote digged and the new time needed to dig another diginote internal void GetDiginoteDigged() { try { if (user != null) // if not already logged out { // add new diginote Diginotes.Add(diginoteSystem.DigDiginote(user)); // notify update because we have a new diginote NotificationMessenger.sendNotification(this, new NotificationType(NotifType.Diginotes, null), ""); // get new time needed to dig DigTime = diginoteSystem.GetDigtime(); } } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } }
// function that gets orders made by the logged user in the server internal void GetOrders() { try { // initialize list Orders = new ObservableCollection <Order>(); // get the orders List <Order> reversed = diginoteSystem.OrdersFromUser(user); // reverse reversed.Reverse(); // adds them to the list reversed.ForEach(Orders.Add); } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } }
// function that sends the approval or disapproval of keeping an order active to the server internal void ChangeQuotationApproval(bool approve) { try { // get last order Order lastOrder = Orders[0]; // send approval to the server Order changedOrder = diginoteSystem.ReceiveApproval(user, lastOrder, approve); // update list with the changed order, we need to remove and add to cast the listview update Orders.Remove(lastOrder); Orders.Insert(0, changedOrder); // notify update NotificationMessenger.sendNotification(this, new NotificationType(NotifType.Order, null), ""); } catch { // couldn't reach server NotificationMessenger.sendNotification(this, new NotificationType(NotifType.NoServer, null), ""); } }
// handler for incoming messenger notifications private void NotificationMessageHandler(NotificationMessage <NotificationType> msg) { if (msg.Content.Type == NotifType.LogOut) // logout { // open login window new LoginWindow().Show(); // unregister Messenger.Default.Unregister <NotificationMessage <NotificationType> >(this); // close current this.Close(); } else if (msg.Content.Type == NotifType.AskQuotation) // ask user for new quotation { double quot = Double.Parse(msg.Notification); // get value // create new dialog ChangeQuotationDialog dialog = new ChangeQuotationDialog(this, Math.Abs(quot), quot >= 0); if (dialog.ShowDialog() == true) // show dialog, if user clicks ok then suggest new quotation { NotificationMessenger.sendNotification(this, new NotificationType(NotifType.SetQuotation, null), dialog.NewQuotation.ToString()); } } else if (msg.Content.Type == NotifType.AskApprove) // ask user to approve keeping the offer and send to server the result { if (new ApproveChangeDialog(this, 60d).ShowDialog() == true) { NotificationMessenger.sendNotification(this, new NotificationType(NotifType.ApproveQuotation, null), "Approve"); } else { NotificationMessenger.sendNotification(this, new NotificationType(NotifType.ApproveQuotation, null), "Disapprove"); } } }
} // register command // function that try resgiter and produces the changes acordingly (change to main window or warn the user about the error) private void Register(object parameter) { // get passwords Tuple <PasswordBox, PasswordBox> param = parameter as Tuple <PasswordBox, PasswordBox>; string password = ((PasswordBox)param.Item1).Password; string passwordConfirm = ((PasswordBox)param.Item2).Password; if (UsernameRegister == "" || NameRegister == "" || password == "" || passwordConfirm == "") // if one of the fields isn't fulfilled { MessageBox.Show(Application.Current.MainWindow, "Please provide all fields in order to register!", "Missing fields", MessageBoxButton.OK, MessageBoxImage.Error); } else if (UsernameRegister.Length < 3) // if username is too short { MessageBox.Show(Application.Current.MainWindow, "Your username must at least 3 characters!", "Bad username", MessageBoxButton.OK, MessageBoxImage.Error); } else if (password != passwordConfirm) // if passwords don't match { MessageBox.Show(Application.Current.MainWindow, "Passwords don't match! Please make to confirm your password!", "Wrong passwords", MessageBoxButton.OK, MessageBoxImage.Error); } else if (client.Register(NameRegister, UsernameRegister, Encrypt(password))) // if successfully logged { NotificationMessenger.sendNotification(this, new NotificationType(NotifType.Login, null), "DEFAULT"); } else // else { if (!noserver) // username already taken { MessageBox.Show(Application.Current.MainWindow, "Username already taken! Please choose another username!", "Username taken", MessageBoxButton.OK, MessageBoxImage.Error); } else // can't reach server { MessageBox.Show(Application.Current.MainWindow, "Can't reach server! Exiting Application!", "No server", MessageBoxButton.OK, MessageBoxImage.Exclamation); Environment.Exit(-1); } } }
// handler for incoming messenger notifications private void ChangeHandler(ChangeArgs args) { if (args.User1 == null || args.User1 == user.Username || args.User2 == user.Username) // if the logged user can receive the message { if (args.Type == ChangeType.QuotationUp || args.Type == ChangeType.QuotationDown) // quotation changed { App.Current.Dispatcher.Invoke((System.Action)(() => { // set new quotation value Quotation = args.QuotationValue; // notify change NotificationMessenger.sendNotification(this, new NotificationType(NotifType.Quotation, null), ""); // add new stat QuotationEvolution.Add(new DataPoint(DateTimeAxis.ToDouble(args.QuotationStat.first), args.QuotationStat.second)); if (Orders.Count > 0 && Orders[0].State == OrderState.Pending) // if last order is pending { if (args.User2 != user.Username && ((args.Type == ChangeType.QuotationUp && Orders[0].Type == OrderType.Buy) || (args.Type == ChangeType.QuotationDown && Orders[0].Type == OrderType.Sell))) // if the new quotation needs to be appproved by the user { // get last order Order lastOrder = Orders[0]; // remove and set new state Orders.Remove(lastOrder); lastOrder.State = OrderState.WaitApproval; // add to the list, we need to remove and add to cast the listview update Orders.Insert(0, lastOrder); // notify main window to show approve dialog NotificationMessenger.sendNotification(this, new NotificationType(NotifType.AskApprove, null), ""); } } })); } else if (args.Type == ChangeType.Transaction) // transaction occurred { App.Current.Dispatcher.Invoke((System.Action)(() => { // get last order Order lastOrder = Orders[0]; // remove order Orders.Remove(lastOrder); if (args.User1 == user.Username) // if the user logged sold the diginotes { // insert the changed order, we need to remove and add to cast the listview update Orders.Insert(0, args.Order1); // look for and remove the diginotes traded List <DiginoteInfo> toRemove = new List <DiginoteInfo>(); foreach (DiginoteInfo dInfo in Diginotes) { if (args.DiginotesTraded.Exists(d => d.Serial == dInfo.Serial)) { toRemove.Add(dInfo); } } foreach (DiginoteInfo dInfo in toRemove) { Diginotes.Remove(dInfo); } } else if (args.User2 == user.Username) // if the user logged bought the diginotes { // insert the changed order, we need to remove and add to cast the listview update Orders.Insert(0, args.Order2); // add the diginotes traded args.DiginotesTraded.ForEach(Diginotes.Add); } // update transaction statistics if (TransactionsPerMin.Count > 0) { DataPoint lastStatItem = TransactionsPerMin[TransactionsPerMin.Count - 1]; if (lastStatItem.X == DateTimeAxis.ToDouble(args.TransactionStat.first)) { TransactionsPerMin.Remove(lastStatItem); lastStatItem.Y++; TransactionsPerMin.Add(lastStatItem); } else { TransactionsPerMin.Add(new DataPoint(DateTimeAxis.ToDouble(args.TransactionStat.first), args.TransactionStat.second)); } } else { TransactionsPerMin.Add(new DataPoint(DateTimeAxis.ToDouble(args.TransactionStat.first), args.TransactionStat.second)); } // notify changes NotificationMessenger.sendNotification(this, new NotificationType(NotifType.Transaction, null), ""); NotificationMessenger.sendNotification(this, new NotificationType(NotifType.Diginotes, null), ""); // if the order of the logged user wasn't totally satifiest, notify main window to show the dialog to ask for a new quotation if (Orders[0].Quantity != 0) { if (Orders[0].Type == OrderType.Buy) { NotificationMessenger.sendNotification(this, new NotificationType(NotifType.AskQuotation, null), "+" + Quotation); } else { NotificationMessenger.sendNotification(this, new NotificationType(NotifType.AskQuotation, null), "-" + Quotation); } } })); } else if (args.Type == ChangeType.Login) // login occurred { // set data NumUsers = args.NumUsers; NumLoggedUsers = args.NumLoggedUsers; NumSysDiginotes = args.NumSysDiginotes; // notify update NotificationMessenger.sendNotification(this, new NotificationType(NotifType.SystemInfo, null), ""); } else if (args.Type == ChangeType.Logout) // logout occurred { // set data NumLoggedUsers = args.NumLoggedUsers; // notify update NotificationMessenger.sendNotification(this, new NotificationType(NotifType.SystemInfo, null), ""); } else if (args.Type == ChangeType.SysDiginotes) // update on number of diginotes in system { // set data NumSysDiginotes = args.NumSysDiginotes; // notify update NotificationMessenger.sendNotification(this, new NotificationType(NotifType.SystemInfo, null), ""); } else if (args.Type == ChangeType.OfferDemand) // update in number of diginotes to buy or sell { // set data DiginotesOffer = args.DiginotesOffer; DiginotesDemand = args.DiginotesDemand; // notify update NotificationMessenger.sendNotification(this, new NotificationType(NotifType.SystemInfo, null), ""); } } }