Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Threading;
- using System.IO;
- using System.Net.Sockets;
- using System.Configuration;
- using System.Security.Cryptography;
- using MUD_Crypto;
- using MySql.Data.MySqlClient;
- namespace MUD_DB
- {
- class MUD_Database
- {
- static MySqlConnection conn = new MySqlConnection();
- static MySqlDataReader reader;
- public static void Connect()
- {
- conn.ConnectionString =
- "Data Source=192.168.1.9;" +
- "Initial catalog=MUDyo;" +
- "User id=herp;" +
- "Password=derp;";
- try
- {
- conn.Open();
- Console.WriteLine("Connected to database!\n");
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- public static string LogIn(string name, string password)
- {
- if (name.Length <= 3)
- {
- return "Name too short";
- }
- else if (name.Length > 15)
- {
- return "Name too long";
- }
- foreach (char c in name)
- {
- if (c < 48)
- return "Only a-z, A-Z, 0-9";
- if (c > 122)
- return "Only a-z, A-Z, 0-9";
- if (c > 57 && c < 65)
- return "Only a-z, A-Z, 0-9";
- if (c > 90 && c < 97)
- return "Only a-z, A-Z, 0-9";
- }
- string loginQuery = string.Format("Select * from users where username='{0}' and password='{1}';", name, password);
- MySqlCommand command = new MySqlCommand(loginQuery, conn);
- reader = command.ExecuteReader();
- if (reader.Read())
- {
- reader.Close();
- command.Dispose();
- Console.WriteLine("New user logged in as \'{0}\'!", name);
- return "logInAccepted";
- }
- else
- {
- reader.Close();
- command.Dispose();
- return "logInDenied";
- }
- }
- public static string Register(string name, string password)
- {
- if (name.Length <= 3)
- {
- return "Name too short";
- }
- else if (name.Length > 15)
- {
- return "Name too long";
- }
- foreach (char c in name)
- {
- if (c < 48)
- return "Only a-z, A-Z, 0-9";
- if (c > 122)
- return "Only a-z, A-Z, 0-9";
- if (c > 57 && c < 65)
- return "Only a-z, A-Z, 0-9";
- if (c > 90 && c < 97)
- return "Only a-z, A-Z, 0-9";
- }
- string registerQuery = string.Format("Select * from users where username='{0}';", name);
- MySqlCommand command = new MySqlCommand(registerQuery, conn);
- reader = command.ExecuteReader();
- if (reader.Read())
- {
- reader.Close();
- command.Dispose();
- return "regTaken";
- }
- else
- {
- reader.Close();
- registerQuery = string.Format("Insert into users (username, password, level, exp, health, strength) values ('{0}', '{1}', '1', '0', '100', '10');", name, password);
- command = new MySqlCommand(registerQuery, conn);
- reader = command.ExecuteReader();
- Console.WriteLine("New user \'{0}\' registered!", name);
- reader.Close();
- return "regAccepted";
- }
- }
- public static uint[] PullStats(string username)
- {
- string getStatsQuery = string.Format("SELECT * FROM users WHERE username='{0}';", username);
- MySqlCommand command = new MySqlCommand(getStatsQuery, conn);
- reader = command.ExecuteReader();
- if (reader.Read())
- {
- uint[] ret = new uint[3];
- ret[0] = (uint)reader["level"];
- ret[1] = (uint)reader["health"];
- ret[2] = (uint)reader["strength"];
- return ret;
- }
- else
- {
- // logged in user doesn't exist in DB?
- reader.Close();
- command.Dispose();
- uint[] ret = new uint[] { 0 };
- return ret;
- }
- }
- }
- }
- namespace MUD_Crypto
- {
- class MUD_MD5
- {
- public static string Encrypt(string pw)
- {
- MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
- byte[] bs = System.Text.Encoding.UTF8.GetBytes(pw);
- bs = md5.ComputeHash(bs);
- StringBuilder sb = new StringBuilder();
- foreach (byte b in bs)
- {
- sb.Append(b.ToString("x2").ToLower());
- }
- return sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement