public override CSUser find(CSUser user) { if (!File.Exists(PATH)) { return(null); } string pass = user.Pass; if (encryptPassword) { pass = sha1Encrypt(pass); pass = pass.Replace("\r", @"\r").Replace("\n", @"\n"); } using (StreamReader sr = File.OpenText(PATH)) { string s = ""; while ((s = sr.ReadLine()) != null) { string[] tab = s.Split(new char[] { SEPARATOR }); if (tab[0].Equals(user.Login) && s.Contains(pass)) { int temp = 0; CSUser u = new CSUser(user.Login); u.Points = (Int32.TryParse(tab[2], out temp)) ? temp:0; u.NbParties = (Int32.TryParse(tab[3], out temp)) ? temp : 0; return(u); } } return(null); } }
public override void create(CSUser user) { string pass = user.Pass; if (encryptPassword) { pass = sha1Encrypt(pass); pass = pass.Replace("\r", @"\r").Replace("\n", @"\n"); } if (!File.Exists(PATH)) { // Create a file to write to. using (StreamWriter sw = File.CreateText(PATH)) { sw.WriteLine(user.Login + SEPARATOR + pass + SEPARATOR + user.Points + SEPARATOR + user.NbParties); } } else { using (StreamWriter sw = File.AppendText(PATH)) { sw.WriteLine(user.Login + SEPARATOR + pass + SEPARATOR + user.Points + SEPARATOR + user.NbParties); } } }
/// <summary> /// Get the unique user stored in the config file /// knowing that each file stores a single user. /// </summary> /// <returns></returns> public CSUser getUnique() { if (!File.Exists(PATH)) { return(null); } using (StreamReader sr = File.OpenText(PATH)) { string s = ""; if ((s = sr.ReadLine()) != null) { string[] tab = s.Split(new char[] { SEPARATOR }); { int temp; CSUser u = new CSUser(tab[0]); u.Pass = tab[1]; u.Points = (Int32.TryParse(tab[2], out temp)) ? temp : 0; u.NbParties = (Int32.TryParse(tab[3], out temp)) ? temp : 0; return(u); } } return(null); } }
public override CSUser find(CSUser user) { if (user == null) { throw new Exception("CSUser cannot be null !"); } try { OleDbCommand cmdFind = new OleDbCommand(); cmdFind.Connection = con; cmdFind.CommandText = "SELECT login, points, nbparties FROM [user] WHERE login = @login AND pass = @pass"; cmdFind.Parameters.Add("@login", OleDbType.VarChar, 25).Value = user.Login; cmdFind.Parameters.Add("@pass", OleDbType.VarChar, 25).Value = sha1Encrypt(user.Pass); cmdFind.Prepare(); OleDbDataReader reader = cmdFind.ExecuteReader(); CSUser logUser = null; if (reader.Read()) { logUser = new CSUser(); logUser.Login = reader.GetString(0); logUser.Points = reader.GetInt32(1); logUser.NbParties = reader.GetInt32(2); } reader.Close(); return(logUser); } catch (Exception e) { throw new Exception("Error when find user '" + user.Login + "' : " + e.Message); } }
public void createUnique(CSUser user) { // Create a file to write to. using (StreamWriter sw = File.CreateText(PATH)) { sw.WriteLine(user.Login + SEPARATOR + user.Pass + SEPARATOR + user.Points + SEPARATOR + user.NbParties); } }
public override void update(CSUser user) { if (File.Exists(PATH)) { string[] lines = File.ReadAllLines(PATH); using (StreamWriter sw = File.AppendText(PATH)) { for (int i = 0; i < lines.Length; i++) { string[] tab = lines[i].Split(new char[] { SEPARATOR }); if (tab[0].Equals(user.Login)) { lines[i] = tab[0] + SEPARATOR + tab[1] + SEPARATOR + user.Points + SEPARATOR + user.NbParties; } sw.WriteLine(lines[i]); } } } }
public override void update(CSUser user) { if (user == null) { throw new Exception("CSUser cannot be null !"); } try { OleDbCommand cmdUpdate = new OleDbCommand(); cmdUpdate.Connection = con; cmdUpdate.CommandText = "UPDATE [user] SET points = @points, nbparties = @nbparties WHERE login = @login"; cmdUpdate.Parameters.Add("@points", OleDbType.Integer, 11).Value = user.Points; cmdUpdate.Parameters.Add("@nbparties", OleDbType.Integer, 11).Value = user.NbParties; cmdUpdate.Parameters.Add("@login", OleDbType.VarChar, 20).Value = user.Login; cmdUpdate.Prepare(); cmdUpdate.ExecuteNonQuery(); } catch (Exception) { throw new Exception("Error when updating user '" + user.Login + "' !"); } }
public override void create(CSUser user) { if (user == null) { throw new Exception("CSUser cannot be null !"); } try { OleDbCommand cmdInsert = new OleDbCommand(); cmdInsert.Connection = con; cmdInsert.CommandText = "INSERT INTO [user] (login, pass, points, nbparties) VALUES(@login, @pass, @points, @nbparties)"; cmdInsert.Parameters.Add("@login", OleDbType.VarChar, 20).Value = user.Login; cmdInsert.Parameters.Add("@pass", OleDbType.VarChar, 20).Value = sha1Encrypt(user.Pass); cmdInsert.Parameters.Add("@points", OleDbType.Integer, 11).Value = user.Points; cmdInsert.Parameters.Add("@nbparties", OleDbType.Integer, 11).Value = user.NbParties; cmdInsert.Prepare(); cmdInsert.ExecuteNonQuery(); } catch (Exception) { throw new Exception("Error when inserting user '" + user.Login + "' !"); } }
public UserThread(TcpClient tcpclient) { this.tcpclient = tcpclient; this.formatter = new BinaryFormatter(); this.user = new CSUser(DEFAULTNAME); }
/// <summary> /// Find user method, it use login and password for finding. /// </summary> /// <param name="user"></param> /// <returns></returns> public abstract CSUser find(CSUser user);
/// <summary> /// Update user method. /// </summary> /// <param name="user"></param> public abstract void update(CSUser user);
/// <summary> /// Create user method, it throws an exception when user isn't correct. /// </summary> /// <param name="user"></param> public abstract void create(CSUser user);