Bläddra i källkod

feat: add auth and create profile

klmnvan 5 månader sedan
förälder
incheckning
7e814d0e23

+ 5 - 6
HelloItQuantum/Function/WorkWithFile.cs

@@ -6,7 +6,7 @@ using System.Linq;
 
 namespace HelloItQuantum.Function
 {
-	public class WorkWithFile
+	public static class WorkWithFile
 	{
 		private static string filePath = "user.csv";
 
@@ -14,14 +14,13 @@ namespace HelloItQuantum.Function
 		/// Считывает пользователей из файла
 		/// </summary>
 		/// <returns>Возвращает null - если файл пустой (не существует) или возвращает модель</returns>
-		static public List<User>? ReadFile()
+		static public List<User>? GetAllUsers()
 		{
 			if (!File.Exists(filePath))
 			{
 				FileStream fs = File.Create(filePath);
 				return null;
 			}
-
 			List<User> users = new List<User>();
 			using (StreamReader read = new StreamReader(filePath))
 			{
@@ -46,9 +45,9 @@ namespace HelloItQuantum.Function
 		/// </summary>
 		/// <param name="newUser">Новый пользователь</param>
 		/// <returns>true если пользователь добавлен, false - пользователь уже существует</returns>
-		static public bool WriteFile(User newUser)
+		static public bool IsWriteUserInFile(User newUser)
 		{
-			List<User> users = ReadFile();
+			List<User>? users = GetAllUsers();
 			bool exists = users.Any(item => item.Nickname == newUser.Nickname);
 			if (exists)
 			{
@@ -56,7 +55,7 @@ namespace HelloItQuantum.Function
 			}
 			using (StreamWriter writer = new StreamWriter(filePath, true))
 			{
-				writer.WriteLine(string.Join(";", newUser));
+				writer.WriteLine($"{newUser.Nickname};{newUser.Name};{newUser.Surname};{newUser.GameHotkeys};{newUser.GameCreateFriend};{newUser.GameLabyrinth}");
 			}
 			return true;
 		}

+ 7 - 0
HelloItQuantum/Models/User.cs

@@ -14,5 +14,12 @@ namespace HelloItQuantum.Models
 		public int GameHotkeys { get; set; }
 		public int GameCreateFriend { get; set; }
 		public int GameLabyrinth { get; set; }
+
+		public User()
+		{
+			GameHotkeys = 0;
+            GameCreateFriend = 0;
+            GameLabyrinth = 0;
+        }
 	}
 }

+ 24 - 2
HelloItQuantum/ViewModels/AuthViewModel.cs

@@ -1,5 +1,8 @@
 using System;
 using System.Collections.Generic;
+using System.Linq;
+using HelloItQuantum.Function;
+using HelloItQuantum.Models;
 using HelloItQuantum.Views;
 using ReactiveUI;
 
@@ -9,6 +12,8 @@ namespace HelloItQuantum.ViewModels
     {
         #region
         string nickname = "";
+
+        List<string>? userNicknames = new List<string>();
         public string Nickname { get => nickname; set => SetProperty(ref nickname, value); }
         #endregion
 
@@ -17,8 +22,25 @@ namespace HelloItQuantum.ViewModels
         /// </summary>
         public void Auth()
         {
-            HomeVM = new HomeViewModel();
-            PageSwitch.View = new HomeView();
+            List<User>? users = WorkWithFile.GetAllUsers();
+            if(users.Count != 0)
+            {
+                userNicknames = users.Select(it => it.Nickname).ToList();
+                if (userNicknames.Contains(Nickname))
+                {
+                    HomeVM = new HomeViewModel();
+                    PageSwitch.View = new HomeView();
+                }
+                else
+                {
+                    Nickname = "";
+                }
+            }
+            else
+            {
+                Nickname = "";
+            }
+
         }
 
         /// <summary>

+ 15 - 2
HelloItQuantum/ViewModels/CreateProfileViewModel.cs

@@ -1,5 +1,7 @@
 using System;
 using System.Collections.Generic;
+using HelloItQuantum.Function;
+using HelloItQuantum.Models;
 using HelloItQuantum.Views;
 using ReactiveUI;
 
@@ -21,8 +23,19 @@ namespace HelloItQuantum.ViewModels
         /// </summary>
         public void CreateProfile()
         {
-            AuthVM = new AuthViewModel();
-            PageSwitch.View = new AuthView();
+            var newUser = new User();
+            newUser.Nickname = Nickname;
+            newUser.Name = Name;
+            newUser.Surname = Name;
+            if(WorkWithFile.IsWriteUserInFile(newUser))
+            {
+                AuthVM = new AuthViewModel();
+                PageSwitch.View = new AuthView();
+            }
+            else
+            {
+                //Òåõíè÷åñêèå øîêîëàäêè
+            }
         }
 
         /// <summary>

+ 0 - 11
HelloItQuantum/ViewModels/LabyrinthViewModel.cs

@@ -10,16 +10,6 @@ namespace HelloItQuantum.ViewModels
 {
     public class LabyrinthViewModel : MainWindowViewModel
     {
-
-        /*private List<NameComand> listCommandForRobots = new List<NameComand>();
-
-        public List<NameComand> ListCommandForRobots { get => listCommandForRobots; set => listCommandForRobots = value; }
-
-        public void AddRight() {
-            ListCommandForRobots.Add(new NameComand { name = "go right" });
-
-        }*/
-
         StackPanel listCommandForRobots = new StackPanel();
         public StackPanel ListCommandForRobots { get => listCommandForRobots; set => SetProperty(ref listCommandForRobots, value); }
 
@@ -28,7 +18,6 @@ namespace HelloItQuantum.ViewModels
             TextBox tb = new TextBox();
             tb.Text = "Ïðèâèâè";
             ListCommandForRobots.Children.Add(tb);
-
         }
     }