PROGRAMMATOR Написал:Ну то что запись в БД есть - ни о чем не говорит. Что там в AccountManager?
p.s. Выборку нужно делать не в серверном пакете, а в клиентском PROTOCOL_LOGIN_REQ на моменте авторизации.
[SRC="csharp"]using System;
using System.Collections.Generic;
using System.Text;
using Login.src.model.accounts;
using MySql.Data.MySqlClient;
using Login.db;
using System.Data;
using Login;
using Login.model.players;
namespace Login.src.managers
{
public class AccountManager
{
private static AccountManager acm = new AccountManager();
private int id = 1;
public int dbstatus = 0;
public static AccountManager getInstance()
{
return acm;
}
protected List<Account> _accounts = new List<Account>();
public bool accountExists(string user)
{
return true;
}
public bool isCookie(string cookie)
{
foreach (Account acc in AccountManager.getInstance().getAccounts())
{
if (acc.cookie == cookie)
return true;
}
return false;
}
public void setCookie(string cookie, string login)
{
foreach (Account acc in AccountManager.getInstance().getAccounts())
{
if (acc.name == login)
acc.cookie = cookie;
}
}
public void deleteCookie(string cookie)
{
foreach (Account acc in AccountManager.getInstance().getAccounts())
{
if (acc.cookie == cookie)
acc.cookie = "";
}
}
public void updatePlayer(Account p)
{
try
{
using (MySqlConnection connection = SQLjec.getInstance().conn())
{
MySqlCommand cmd = connection.CreateCommand();
connection.Open();
CLogger.getInstance().warning("[AccountManager]: " + "Update player info: " + "player_id=" + p.player_id.ToString() + " rank=" + p.getRank().ToString() + " exp=" + p.getExp().ToString() + " gp=" + p.getGP().ToString());
cmd.CommandText = "UPDATE accounts SET rank='" + p.getRank() + "' WHERE player_id='" + p.player_id.ToString() + "';";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE accounts SET exp='" + p.getExp() + "' WHERE player_id='" + p.player_id.ToString() + "';";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE accounts SET gp='" + p.getGP() + "' WHERE player_id='" + p.player_id.ToString() + "';";
cmd.ExecuteNonQuery();
}
this.get(p.name).setRank(p.getRank());
this.get(p.name).setExp(p.getExp());
this.get(p.name).setGP(p.getGP());
}
catch (Exception e)
{
CLogger.getInstance().warning("[AccountManager]: ERROR UPDATE player: " + e.ToString());
}
}
public Account get(string username)
{
foreach (Account acc in AccountManager.getInstance().getAccounts())
{
if (acc.name == username)
return acc;
}
return null;
}
public Account getAccountInName(string player_name)
{
foreach (Account account in getInstance().getAccounts())
{
if (account.player_name == player_name)
{
return account;
}
}
return null;
}
public Account getAccountInObjectId(int player_id)
{
foreach (Account acc in AccountManager.getInstance().getAccounts())
{
if (acc.player_id == player_id)
return acc;
}
return null;
}
public AccountManager()
{
try
{
using (MySqlConnection connection = SQLjec.getInstance().conn())
{
MySqlCommand cmd = connection.CreateCommand();
connection.Open();
cmd.CommandText = "SELECT * FROM accounts";
cmd.CommandType = CommandType.Text;
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Account acc = new Account();
acc.id = id;
acc.name = reader.GetString("login");
acc.player_name = reader.GetString("player_name");
acc.setPlayerId(reader.GetInt32("player_id"));
acc.password = reader.GetString("password");
acc.access_level = reader.GetInt32("access_level");
acc.gp = reader.GetInt32("gp");
acc.setRank(reader.GetInt32("rank"));
acc.money = reader.GetInt32("money");
acc.exp = reader.GetInt32("exp");
_accounts.Add(acc);
id++;
}
CLogger.getInstance().extra_info("[AccountManager]: loaded " + _accounts.Count + " accounts");
}
}
catch(Exception e)
{
dbstatus = -100;
CLogger.getInstance().error("[AccountManager]: " + "Not loaded accounts");
CLogger.getInstance().error("[AccountManager]: " + e.ToString());
}
}
public int getPlayerId(string name)
{
using (MySqlConnection connection = SQLjec.getInstance().conn())
{
MySqlCommand cmd = connection.CreateCommand();
connection.Open();
cmd.CommandText = "SELECT player_id FROM accounts WHERE player_name='" + name + "' LIMIT 1;";
cmd.CommandType = CommandType.Text;
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
int id = reader.GetInt32("player_id");
return id;
}
}
return -1;
}
public bool isPlayerNameExist(string name)
{
try
{
using (MySqlConnection connection = SQLjec.getInstance().conn())
{
MySqlCommand cmd = connection.CreateCommand();
//узнать количество строк в бд
connection.Open();
//cmd.CommandText = "UPDATE account (account, player_name, rank, exp, gp) VALUES ('" + account.ToLower() + "', '" + p.getPlayerName() + "', '" + p.getRank().ToString() + "', '" + p.getExp().ToString() + "', '" + p.getGP().ToString() + "');";
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT COUNT(*) FROM accounts WHERE player_name='" + name + "'";
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count == 0)
{
return false;
}
else
{
return true;
}
}
}
catch
{
return true;
}
}
public int CreatePlayer(string account, Account p)
{
try
{
using (MySqlConnection connection = SQLjec.getInstance().conn())
{
MySqlCommand cmd = connection.CreateCommand();
//узнать количество строк в бд
connection.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT COUNT(*) FROM accounts WHERE player_name='" + p.getPlayerName() + "'";
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count == 0)
{
cmd.CommandText = "UPDATE accounts SET player_name='" + p.getPlayerName() + "' WHERE player_id='" + p.player_id.ToString() + "';";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE accounts SET rank='" + p.getRank().ToString() + "' WHERE player_id='" + p.player_id.ToString() + "';";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE accounts SET exp='" + p.getExp().ToString() + "' WHERE player_id='" + p.player_id.ToString() + "';";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE accounts SET gp='" + p.getGP().ToString() + "' WHERE player_id='" + p.player_id.ToString() + "';";
cmd.ExecuteNonQuery();
}
else
{
return -1;
}
return 0;
}
}
catch (MySqlException e)
{
return -1;
}
}
public void AddInitialItems(int player_id, ItemsModel item, int equip)
{
try
{
using (MySqlConnection connection = SQLjec.getInstance().conn())
{
MySqlCommand cmd = connection.CreateCommand();
//CLogger.getInstance().info("Added started item=" + item.id.ToString());
connection.Open();
cmd.CommandText = "INSERT INTO items (owner_id, item_id, item_type, count, loc_slot) VALUES ('" + player_id + "', '" + item.id.ToString() + "', '" + equip + "', '" + "1" + "', '" + item.slot + "');";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
catch
{
}
}
public List<Account> getAccounts()
{
return _accounts;
}
public List<Account> getOnlineAccounts()
{
List<Account> _temp = new List<Account>();
foreach (Account acc in AccountManager.getInstance().getAccounts())
{
if (acc.getClient() != null)
{
_temp.Add(acc);
}
}
return _temp;
}
public void AddAccount(Account acc)
{
_accounts.Add(acc);
}
public bool CreateAccount(string login, string password)
{
try
{
using (MySqlConnection connection = SQLjec.getInstance().conn())
{
MySqlCommand cmd = connection.CreateCommand();
connection.Open();
cmd.CommandText = "SELECT COUNT(*) FROM accounts WHERE login='" + login + "';";
int count = Convert.ToInt32(cmd.ExecuteScalar());
if(count == 0)
{
cmd.CommandText = "INSERT INTO accounts (login, password) VALUES ('" + login.ToLower() + "', '" + password + "');";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
else
{
return false;
}
}
Account ac = new Account();
ac.name = login;
ac.password = password;
ac.id = _accounts.Count;
this.AddAccount(ac);
return true;
}
catch(Exception e)
{
CLogger.getInstance().error("[AccountManager]: " + "Cannot create account: " + login);
CLogger.getInstance().error("[AccountManager]: " + e.ToString());
return false;
}
}
}
}[/SRC]