public ResponseStruct UpgradeLevel(string SeasonID, string LevelName, string BalanceType) { ResponseStruct Result; LauncherClientInformations aClient; if (!OverallInformations.Clients.TryGetValue(SeasonID, out aClient)) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Client không tồn tại." }, Broadcast = new List<string>() { "Khởi động lại Launcher để tiếp tục sử dụng." } }; } else if (String.IsNullOrWhiteSpace(aClient.UserName)) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Chưa đăng nhập." }, Broadcast = new List<string>() { "Tiến hành đăng nhập để tiếp tục sử dụng." } }; } else if (String.IsNullOrWhiteSpace(LevelName)) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Level không hợp lệ." }, Broadcast = new List<string>() { "Sử dụng level được cung cấp trong danh sách." } }; } else { string UserName = aClient.UserName; MySqlConnection DbConnection = new MySqlConnection(OverallInformations.TradingSystemDBConnectionString); DbConnection.Open(); string SqlQuery = "SELECT COUNT(*) FROM shop_players_progress WHERE UserName=@UserName"; MySqlCommand DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@UserName", MySqlDbType.VarChar).Value = UserName; int Count = Convert.ToInt32(DbCommand.ExecuteScalar()); DbCommand.Dispose(); if (Count == 1) { SqlQuery = "SELECT * FROM shop_players_progress WHERE UserName=@UserName"; DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@UserName", MySqlDbType.VarChar).Value = UserName; MySqlDataReader DbReader = DbCommand.ExecuteReader(); string CurrentLevel = null; while (DbReader.Read()) { if (!DbReader.IsDBNull(DbReader.GetOrdinal("CurrentLevel"))) { CurrentLevel = DbReader.GetString("CurrentLevel"); } } DbReader.Dispose(); DbCommand.Dispose(); if (!String.IsNullOrWhiteSpace(CurrentLevel)) { SqlQuery = "SELECT COUNT(*) FROM shop_levels_and_ranks WHERE ParentLevelName=@ParentLevelName AND LevelName=@LevelName"; DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@ParentLevelName", MySqlDbType.VarChar).Value = CurrentLevel; DbCommand.Parameters.Add("@LevelName", MySqlDbType.VarChar).Value = LevelName; Count = Convert.ToInt32(DbCommand.ExecuteScalar()); DbCommand.Dispose(); if (Count == 1) { SqlQuery = "SELECT * FROM shop_levels_and_ranks WHERE ParentLevelName=@ParentLevelName AND LevelName=@LevelName"; DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@ParentLevelName", MySqlDbType.VarChar).Value = CurrentLevel; DbCommand.Parameters.Add("@LevelName", MySqlDbType.VarChar).Value = LevelName; DbReader = DbCommand.ExecuteReader(); ShopLevelInformations NextLevel = new ShopLevelInformations() { LevelName = null, LevelFullName = null, Level = -1, ParentLevelName = null, TShockGroupName = null, StandardPrice = -1, PremiumPrice = -1 }; while (DbReader.Read()) { if (!DbReader.IsDBNull(DbReader.GetOrdinal("LevelName"))) { NextLevel.LevelName = DbReader.GetString("LevelName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("LevelFullName"))) { NextLevel.LevelFullName = DbReader.GetString("LevelFullName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("Level"))) { NextLevel.Level = DbReader.GetInt32("Level"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("ParentLevelName"))) { NextLevel.ParentLevelName = DbReader.GetString("ParentLevelName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("TShockGroupName"))) { NextLevel.TShockGroupName = DbReader.GetString("TShockGroupName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("StandardPrice"))) { NextLevel.StandardPrice = DbReader.GetInt32("StandardPrice"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("PremiumPrice"))) { NextLevel.PremiumPrice = DbReader.GetInt32("PremiumPrice"); } } DbReader.Dispose(); DbCommand.Dispose(); if (NextLevel.StandardPrice == -1 && BalanceType == "Standard Balance") { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Level không sử dụng Standard Balance để nâng cấp." } }; } else if (NextLevel.PremiumPrice == -1 && BalanceType == "Premium Balance") { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Level không sử dụng Premium Balance để nâng cấp." } }; } else if (String.IsNullOrWhiteSpace(NextLevel.TShockGroupName)) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "TShock Group không hợp lệ." }, Broadcast = new List<string>() { "Báo cho Admin level và lỗi này nếu có thể." } }; } else { if (BalanceType == "Standard Balance") { long Balance; if (!Int64.TryParse(Convert.ToString(GetBalance(aClient.SeasonID, "Standard Balance").Content), out Balance)) { Balance = 0; } if (NextLevel.StandardPrice > Balance) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Standard Balance không đủ để có thể nâng cấp." }, Broadcast = new List<string>() { "-_-" } }; } else { string ChangeGroupResonseString; if (LauncherServerSendRequestToTShockInstance.SendRequest(new Request02() { RequestType = "ChangeUserGroup", AuthCode = OverallInformations.AuthCode, Parameters = new List<string>() { UserName, NextLevel.TShockGroupName } }, out ChangeGroupResonseString)) { ResponseStruct ChangeGroupResult = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponseStruct>(ChangeGroupResonseString); if (ChangeGroupResult.Status == ResponseStatusType.Done) { SqlQuery = "UPDATE shop_players_progress SET CurrentLevel=@NextLevel WHERE UserName=@UserName"; DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@NextLevel", MySqlDbType.VarChar).Value = NextLevel.LevelName; DbCommand.Parameters.Add("@UserName", MySqlDbType.VarChar).Value = UserName; DbCommand.ExecuteNonQuery(); DbCommand.Dispose(); string SubtractPlayerResponse; if (LauncherServerSendRequestToTShockInstance.SendRequest(new Request02() { RequestType = "SubtractPlayerBalance", AuthCode = OverallInformations.AuthCode, Parameters = new List<string>() { UserName, Convert.ToString(NextLevel.StandardPrice), String.Format("Upgrade to {0}.", NextLevel.LevelFullName) } }, out SubtractPlayerResponse)) { ResponseStruct SubtractResult = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponseStruct>(SubtractPlayerResponse); if (SubtractResult.Status == ResponseStatusType.Done) { Result = new ResponseStruct() { Status = ResponseStatusType.Done, Error = new List<string>() { }, Broadcast = new List<string>() { String.Format("Tài khoản \"{0}\" vừa nâng cấp lên {1}.\nChi phí: {2} Credits {3}.", UserName, NextLevel.LevelFullName, string.Format(CultureInfo.InvariantCulture, "{0:#,#0}", NextLevel.StandardPrice), BalanceType) } }; LogWriter(UserName: UserName, Task: "Upgrade Level - Standard Balance", Details: String.Join("\n", String.Join("\n", Result.Error), String.Join("\n", Result.Broadcast))); } else if (SubtractResult.Status == ResponseStatusType.Fail) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Không thể trừ tài khoản SEconomy của User." }, Broadcast = new List<string>() { String.Format("Tài khoản \"{0}\" vừa nâng cấp lên {1}.\nChi phí: {2} Credits {3}.", UserName, NextLevel.LevelFullName, string.Format(CultureInfo.InvariantCulture, "{0:#,#0}", NextLevel.StandardPrice), BalanceType) } }; LogWriter(UserName: UserName, Task: "Pending Transaction", Details: String.Join("\n", String.Join("\n", Result.Error), String.Join("\n", Result.Broadcast))); } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Không rõ dữ liệu trả về từ TShock Server." }, Broadcast = new List<string>() { String.Format("Tài khoản \"{0}\" vừa nâng cấp lên {1}.\nChi phí: {2} Credits {3}.", UserName, NextLevel.LevelFullName, string.Format(CultureInfo.InvariantCulture, "{0:#,#0}", NextLevel.StandardPrice), BalanceType) } }; LogWriter(UserName: UserName, Task: "Pending Transaction", Details: String.Join("\n", String.Join("\n", Result.Error), String.Join("\n", Result.Broadcast))); } } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "TShock Server không đáp ứng yêu cầu." }, Broadcast = new List<string>() { String.Format("Tài khoản \"{0}\" vừa nâng cấp lên {1}.\nChi phí: {2} Credits {3}.", UserName, NextLevel.LevelFullName, string.Format(CultureInfo.InvariantCulture, "{0:#,#0}", NextLevel.StandardPrice), BalanceType) } }; LogWriter(UserName: UserName, Task: "Pending Transaction", Details: String.Join("\n", String.Join("\n", Result.Error), String.Join("\n", Result.Broadcast))); } } else if (ChangeGroupResult.Status == ResponseStatusType.Fail) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = ChangeGroupResult.Error, Broadcast = ChangeGroupResult.Broadcast }; } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Không rõ kết quả trả về từ TShock Server." } }; } } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "TShock Server không đáp ứng." } }; } } } else if (BalanceType == "Premium Balance") { long Balance; if (!Int64.TryParse(Convert.ToString(GetBalance(aClient.SeasonID, "Premium Balance").Content), out Balance)) { Balance = 0; } if (NextLevel.PremiumPrice > Balance) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Premium Balance không đủ để có thể nâng cấp." }, Broadcast = new List<string>() { "-_-" } }; } else { string ChangeGroupResonseString; if (LauncherServerSendRequestToTShockInstance.SendRequest(new Request02() { RequestType = "ChangeUserGroup", AuthCode = OverallInformations.AuthCode, Parameters = new List<string>() { UserName, NextLevel.TShockGroupName } }, out ChangeGroupResonseString)) { ResponseStruct ChangeGroupResult = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponseStruct>(ChangeGroupResonseString); if (ChangeGroupResult.Status == ResponseStatusType.Done) { SqlQuery = "UPDATE shop_players_progress SET CurrentLevel=@NextLevel WHERE UserName=@UserName"; DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@NextLevel", MySqlDbType.VarChar).Value = NextLevel.LevelName; DbCommand.Parameters.Add("@UserName", MySqlDbType.VarChar).Value = UserName; DbCommand.ExecuteNonQuery(); DbCommand.Dispose(); SqlQuery = "UPDATE shop_accounts SET Balance=@Balance WHERE UserName=@UserName"; DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@Balance", MySqlDbType.Int32).Value = int.Parse((Balance - NextLevel.PremiumPrice).ToString()); DbCommand.Parameters.Add("@UserName", MySqlDbType.VarChar).Value = UserName; DbCommand.ExecuteNonQuery(); DbCommand.Dispose(); Result = new ResponseStruct() { Status = ResponseStatusType.Done, Broadcast = new List<string>() { String.Format("Tài khoản \"{0}\" vừa nâng cấp lên {1}.\nChi phí: {2} Credits {3}.", UserName, NextLevel.LevelFullName, string.Format(CultureInfo.InvariantCulture, "{0:#,#0}", NextLevel.PremiumPrice), BalanceType) } }; LogWriter(UserName, "Upgrade Level - Premium Balance", String.Join("\n", Result.Broadcast)); } else if (ChangeGroupResult.Status == ResponseStatusType.Fail) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = ChangeGroupResult.Error, Broadcast = ChangeGroupResult.Broadcast }; } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Không rõ kết quả trả về từ TShock Server." } }; } } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "TShock Server không đáp ứng." } }; } } } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Tài khoản thanh toán không hợp lệ." }, Broadcast = new List<string>() { "Chọn tài khoản để thanh toán theo danh sách có sẵn." } }; } } } else if (Count == 0) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Level được yêu cầu không hợp lệ." }, Broadcast = new List<string>() { "Cung cấp level hợp lệ để tiếp tục." } }; } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Lỗi cơ sở dữ liệu." }, Broadcast = new List<string>() { "Thông báo cho Admin nếu có thể." } }; } } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Lỗi cơ sở dữ liệu." }, Broadcast = new List<string>() { "Báo cho Admin nếu có thể." } }; } } else if (Count == 0) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Người chơi phải chọn class để có thể nâng cấp độ theo class đó." } }; } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Lỗi cơ sở dữ liệu, thông báo cho Admin nếu có thể." } }; } DbConnection.Dispose(); } return Result; }
private bool GetInformationsAboutALevel(string LevelName, out ShopLevelInformations aLevel) { bool Result; aLevel = null; MySqlConnection DbConnection = new MySqlConnection(OverallInformations.TradingSystemDBConnectionString); DbConnection.Open(); string SqlQuery = "SELECT COUNT(*) FROM shop_levels_and_ranks WHERE LevelName=@LevelName"; MySqlCommand DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@LevelName", MySqlDbType.VarChar).Value = LevelName; int Count = Convert.ToInt32(DbCommand.ExecuteScalar()); DbCommand.Dispose(); if (Count == 1) { Result = true; aLevel = new ShopLevelInformations() { LevelName = null, LevelFullName = null, Level = -1, ParentLevelName = null, TShockGroupName = null, StandardPrice = -1, PremiumPrice = -1 }; SqlQuery = "SELECT * FROM shop_levels_and_ranks WHERE LevelName=@LevelName"; DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@LevelName", MySqlDbType.VarChar).Value = LevelName; MySqlDataReader DbReader = DbCommand.ExecuteReader(); while (DbReader.Read()) { if (!DbReader.IsDBNull(DbReader.GetOrdinal("LevelName"))) { aLevel.LevelName = DbReader.GetString("LevelName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("LevelFullName"))) { aLevel.LevelFullName = DbReader.GetString("LevelFullName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("Level"))) { aLevel.Level = DbReader.GetInt32("Level"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("ParentLevelName"))) { aLevel.ParentLevelName = DbReader.GetString("ParentLevelName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("TShockGroupName"))) { aLevel.TShockGroupName = DbReader.GetString("TShockGroupName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("StandardPrice"))) { aLevel.StandardPrice = DbReader.GetInt32("StandardPrice"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("PremiumPrice"))) { aLevel.PremiumPrice = DbReader.GetInt32("PremiumPrice"); } } DbReader.Dispose(); DbCommand.Dispose(); } else { Result = false; } DbConnection.Dispose(); return Result; }
public ResponseStruct GetAvailableLevelsForUpgrade(string SeasonID) { ResponseStruct Result; LauncherClientInformations aClient; if (!OverallInformations.Clients.TryGetValue(SeasonID, out aClient)) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Client không tồn tại." }, Broadcast = new List<string>() { "Khởi động lại Launcher để tiếp tục sử dụng." } }; } else if (String.IsNullOrWhiteSpace(aClient.UserName)) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Chưa đăng nhập." }, Broadcast = new List<string>() { "Tiến hành đăng nhập để tiếp tục sử dụng." } }; } else { MySqlConnection DbConnection = new MySqlConnection(OverallInformations.TradingSystemDBConnectionString); DbConnection.Open(); string SqlQuery = "SELECT COUNT(*) FROM shop_players_progress WHERE UserName=@UserName"; MySqlCommand DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@UserName", MySqlDbType.VarChar).Value = aClient.UserName; int Count = Convert.ToInt32(DbCommand.ExecuteScalar()); DbCommand.Dispose(); if (Count == 1) { SqlQuery = "SELECT * FROM shop_players_progress WHERE UserName=@UserName"; DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@UserName", MySqlDbType.VarChar).Value = aClient.UserName; MySqlDataReader DbReader = DbCommand.ExecuteReader(); string CurrentLevel = null; while (DbReader.Read()) { if (!DbReader.IsDBNull(DbReader.GetOrdinal("CurrentLevel"))) { CurrentLevel = DbReader.GetString("CurrentLevel"); } } DbReader.Dispose(); DbCommand.Dispose(); if (!String.IsNullOrWhiteSpace(CurrentLevel)) { SqlQuery = "SELECT * FROM shop_levels_and_ranks WHERE ParentLevelName=@ParentLevelName"; DbCommand = new MySqlCommand(SqlQuery, DbConnection); DbCommand.Prepare(); DbCommand.Parameters.Add("@ParentLevelName", MySqlDbType.VarChar).Value = CurrentLevel; DbReader = DbCommand.ExecuteReader(); List<ShopLevelInformations> ListOfLevels = new List<ShopLevelInformations>(); while (DbReader.Read()) { ShopLevelInformations aLevel = new ShopLevelInformations() { LevelName = null, LevelFullName = null, Level = -1, ParentLevelName = null, TShockGroupName = null, StandardPrice = -1, PremiumPrice = -1 }; if (!DbReader.IsDBNull(DbReader.GetOrdinal("LevelName"))) { aLevel.LevelName = DbReader.GetString("LevelName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("LevelFullName"))) { aLevel.LevelFullName = DbReader.GetString("LevelFullName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("Level"))) { aLevel.Level = DbReader.GetInt32("Level"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("ParentLevelName"))) { aLevel.ParentLevelName = DbReader.GetString("ParentLevelName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("TShockGroupName"))) { aLevel.TShockGroupName = DbReader.GetString("TShockGroupName"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("StandardPrice"))) { aLevel.StandardPrice = DbReader.GetInt32("StandardPrice"); } if (!DbReader.IsDBNull(DbReader.GetOrdinal("PremiumPrice"))) { aLevel.PremiumPrice = DbReader.GetInt32("PremiumPrice"); } ListOfLevels.Add(aLevel); } DbReader.Dispose(); DbCommand.Dispose(); Result = new ResponseStruct() { Status = ResponseStatusType.Done, Content = Newtonsoft.Json.JsonConvert.SerializeObject(ListOfLevels) }; } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Lỗi cơ sở dữ liệu." }, Broadcast = new List<string>() { "Báo cho Admin nếu có thể." } }; } } else if (Count == 0) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Người chơi phải chọn class để có thể nâng cấp độ theo class đó." } }; } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Lỗi cơ sở dữ liệu." }, Broadcast = new List<string>() { "Báo cho Admin nếu có thể." } }; } DbConnection.Dispose(); } return Result; }