-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPlayerCreator.cs
123 lines (107 loc) · 3.99 KB
/
PlayerCreator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Security.Cryptography;
using System.Text;
using DOL.Database;
using MySql.Data.MySqlClient;
using AtlasSimulator;
using AtlasSimulator.playerclasses;
using log4net;
namespace AtlasSimulator
{
class PlayerCreator
{
public class Position
{
public int x, y, z;
public int zone;
};
public enum Class
{
Paladin,
Wizard
}
MySqlConnection _dbConnection;
public PlayerCreator()
{
_dbConnection = new MySqlConnection("server=localhost;user=atlassimulator;database=atlas;port=3306;password=atlassimulator");
}
public void Create(IPlayerClass pc){
CreateAccount(pc);
CreateChar(pc);
}
public void CreateChar(IPlayerClass pc)
{
_dbConnection.Open();
string sql = String.Format("SELECT Name FROM dolcharacters WHERE Name='{0}'", pc.charname);
MySqlCommand cmd = new MySqlCommand(sql, _dbConnection);
MySqlDataReader rdr = cmd.ExecuteReader();
bool accountExists = rdr.Read();
_dbConnection.Close();
if (accountExists)
{
// Char exists
Console.WriteLine(String.Format("Char {0} already exists!", pc.charname));
}
else
{
_dbConnection.Open();
Console.WriteLine(String.Format("Creating char named {0}!", pc.charname));
cmd = new MySqlCommand(pc.sql, _dbConnection);
cmd.ExecuteNonQuery();
_dbConnection.Close();
}
}
public void CreateAccount(IPlayerClass pc)
{
Account account = new Account
{
Name = pc.accountName,
Password = CryptPassword(pc.password),
PrivLevel = (uint)3,
Realm = (int)1,
CreationDate = DateTime.Now,
Language = "EN"
};
_dbConnection.Open();
string sql = String.Format("SELECT Name FROM account WHERE Name='{0}'",pc.accountName);
MySqlCommand cmd = new MySqlCommand(sql, _dbConnection);
MySqlDataReader rdr = cmd.ExecuteReader();
bool accountExists = rdr.Read();
_dbConnection.Close();
if (accountExists) {
// Account exists
Console.WriteLine(String.Format("Account {0} already exists!", pc.accountName));
} else
{
// Write Data
_dbConnection.Open();
Console.WriteLine(String.Format("Creating account named {0}!", pc.accountName));
string dateString = account.CreationDate.ToString("yyyy-MM-dd HH:mm:ss.fff");
sql = String.Format("INSERT INTO account(Name,password,CreationDate,Realm,PrivLevel,Language,Account_ID) " +
"VALUES('{0}','{1}','{2}',{3},{4},'{5}','{6}');", account.Name, account.Password, dateString, account.Realm, account.PrivLevel, account.Language, account.Name);
cmd = new MySqlCommand(sql, _dbConnection);
cmd.ExecuteNonQuery();
_dbConnection.Close();
}
}
private string CryptPassword(string password)
{
MD5 md5 = new MD5CryptoServiceProvider();
char[] pw = password.ToCharArray();
var res = new byte[pw.Length * 2];
for (int i = 0; i < pw.Length; i++)
{
res[i * 2] = (byte)(pw[i] >> 8);
res[i * 2 + 1] = (byte)(pw[i]);
}
byte[] bytes = md5.ComputeHash(res);
var crypted = new StringBuilder();
crypted.Append("##");
for (int i = 0; i < bytes.Length; i++)
{
crypted.Append(bytes[i].ToString("X"));
}
return crypted.ToString();
}
}
}