|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using MySql.Data.MySqlClient; |
| 3 | +using Rosalind.Core.Models; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Data; |
| 7 | + |
| 8 | +namespace Rosalind.Core.Services |
| 9 | +{ |
| 10 | + public class SqlService |
| 11 | + { |
| 12 | + private readonly Setting _setting; |
| 13 | + |
| 14 | + public SqlService(IServiceProvider service) |
| 15 | + { |
| 16 | + _setting = service.GetRequiredService<Setting>(); |
| 17 | + } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// 길드 테이블에 새로운 유저 추가 |
| 21 | + /// </summary> |
| 22 | + /// <param name="guildid">길드 아이디</param> |
| 23 | + /// <param name="userid">추가할 유저 아이디</param> |
| 24 | + public User AddNewUser(ulong guildId, ulong userId) |
| 25 | + { |
| 26 | + User user = null; |
| 27 | + |
| 28 | + try |
| 29 | + { |
| 30 | + long insertedId = 0; |
| 31 | + |
| 32 | + using (MySqlConnection _connection = new MySqlConnection(_setting.Config.ConnectionString)) |
| 33 | + { |
| 34 | + _connection.Open(); |
| 35 | + |
| 36 | + using (MySqlCommand sqlCom = new MySqlCommand()) |
| 37 | + { |
| 38 | + sqlCom.Connection = _connection; |
| 39 | + sqlCom.CommandText = $"INSERT INTO TABLE_{guildId} (USERID, MONEY) VALUES(@USERID, 0)"; |
| 40 | + sqlCom.Parameters.AddWithValue("@USERID", userId); |
| 41 | + sqlCom.CommandType = CommandType.Text; |
| 42 | + sqlCom.ExecuteNonQuery(); |
| 43 | + |
| 44 | + insertedId = sqlCom.LastInsertedId; |
| 45 | + } |
| 46 | + |
| 47 | + _connection.Close(); |
| 48 | + } |
| 49 | + |
| 50 | + user = new User( |
| 51 | + id: Convert.ToUInt64(insertedId), |
| 52 | + guildId: guildId, |
| 53 | + userId: userId, |
| 54 | + coin: 0 |
| 55 | + ); |
| 56 | + } |
| 57 | + catch (MySqlException ex) |
| 58 | + { |
| 59 | + switch ((MySqlErrorCode)ex.Number) |
| 60 | + { |
| 61 | + case MySqlErrorCode.NoSuchTable: |
| 62 | + AddNewGuild(guildId); |
| 63 | + user = AddNewUser(guildId, userId); |
| 64 | + break; |
| 65 | + |
| 66 | + default: |
| 67 | + throw new Exception($"처리되지 못한 예외가 발생하였습니다. ({ex.Number}, {ex.Message})"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return user; |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// 길드를 데이터베이스에 새로 추가합니다. |
| 76 | + /// </summary> |
| 77 | + /// <param name="guildid">길드 아이디</param> |
| 78 | + public void AddNewGuild(ulong guildid) |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + using (MySqlConnection _connection = new MySqlConnection(_setting.Config.ConnectionString)) |
| 83 | + { |
| 84 | + _connection.Open(); |
| 85 | + |
| 86 | + using (MySqlCommand sqlCom = new MySqlCommand()) |
| 87 | + { |
| 88 | + sqlCom.Connection = _connection; |
| 89 | + sqlCom.CommandText = $"CREATE TABLE TABLE_{guildid} (_ID INT PRIMARY KEY AUTO_INCREMENT, USERID BIGINT NOT NULL, MONEY BIGINT NOT NULL) ENGINE = INNODB;"; |
| 90 | + sqlCom.CommandType = CommandType.Text; |
| 91 | + sqlCom.ExecuteNonQuery(); |
| 92 | + } |
| 93 | + |
| 94 | + _connection.Close(); |
| 95 | + } |
| 96 | + } |
| 97 | + catch (MySqlException ex) |
| 98 | + { |
| 99 | + switch ((MySqlErrorCode)ex.Number) |
| 100 | + { |
| 101 | + case MySqlErrorCode.TableExists: |
| 102 | + throw new Exception("길드가 이미 존재합니다."); |
| 103 | + |
| 104 | + default: |
| 105 | + throw new Exception($"처리되지 못한 예외가 발생하였습니다. ({ex.Number}, {ex.Message})"); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// 랭킹을 데이터베이스로 부터 가져옵니다. |
| 112 | + /// </summary> |
| 113 | + /// <param name="guildid">길드 아이디</param> |
| 114 | + /// <param name="limit">랭킹 유저의 수</param> |
| 115 | + /// <returns></returns> |
| 116 | + public List<User> GetRanking(ulong guildId, int limit) |
| 117 | + { |
| 118 | + List<User> users = new List<User>(); |
| 119 | + |
| 120 | + try |
| 121 | + { |
| 122 | + using (MySqlConnection _connection = new MySqlConnection(_setting.Config.ConnectionString)) |
| 123 | + { |
| 124 | + _connection.Open(); |
| 125 | + |
| 126 | + using (MySqlCommand sqlCom = new MySqlCommand()) |
| 127 | + { |
| 128 | + sqlCom.Connection = _connection; |
| 129 | + sqlCom.CommandText = $"SELECT * FROM TABLE_{guildId} WHERE NOT MONEY=0 ORDER BY MONEY DESC LIMIT @LIMIT;"; |
| 130 | + sqlCom.Parameters.AddWithValue("@LIMIT", limit); |
| 131 | + sqlCom.CommandType = CommandType.Text; |
| 132 | + |
| 133 | + using (MySqlDataReader reader = sqlCom.ExecuteReader()) |
| 134 | + { |
| 135 | + while (reader.Read()) |
| 136 | + { |
| 137 | + users.Add(new User( |
| 138 | + id: Convert.ToUInt64(reader["_ID"]), |
| 139 | + guildId: guildId, |
| 140 | + userId: Convert.ToUInt64(reader["USERID"]), |
| 141 | + coin: Convert.ToUInt64(reader["MONEY"]) |
| 142 | + )); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + _connection.Close(); |
| 148 | + } |
| 149 | + } |
| 150 | + catch (MySqlException ex) |
| 151 | + { |
| 152 | + switch ((MySqlErrorCode)ex.Number) |
| 153 | + { |
| 154 | + case MySqlErrorCode.NoSuchTable: |
| 155 | + AddNewGuild(guildId); |
| 156 | + users = GetRanking(guildId, limit); |
| 157 | + break; |
| 158 | + |
| 159 | + default: |
| 160 | + throw new Exception($"처리되지 못한 예외가 발생하였습니다. ({ex.Number}, {ex.Message})"); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return users; |
| 165 | + } |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// 유저를 데이터베이스에서 가져와 객체로 반환합니다. |
| 169 | + /// </summary> |
| 170 | + /// <param name="guildId">길드의 아이디</param> |
| 171 | + /// <param name="userId">유저의 아이디</param> |
| 172 | + /// <returns></returns> |
| 173 | + public User GetUser(ulong guildId, ulong userId) |
| 174 | + { |
| 175 | + User user = null; |
| 176 | + |
| 177 | + try |
| 178 | + { |
| 179 | + using (MySqlConnection _connection = new MySqlConnection(_setting.Config.ConnectionString)) |
| 180 | + { |
| 181 | + _connection.Open(); |
| 182 | + |
| 183 | + using (MySqlCommand sqlCom = new MySqlCommand()) |
| 184 | + { |
| 185 | + sqlCom.Connection = _connection; |
| 186 | + sqlCom.CommandText = $"SELECT * FROM TABLE_{guildId} WHERE USERID=@ID LIMIT 1;"; |
| 187 | + sqlCom.Parameters.AddWithValue("@ID", userId); |
| 188 | + sqlCom.CommandType = CommandType.Text; |
| 189 | + |
| 190 | + using (MySqlDataReader reader = sqlCom.ExecuteReader()) |
| 191 | + { |
| 192 | + if (reader.HasRows) |
| 193 | + { |
| 194 | + while (reader.Read()) |
| 195 | + { |
| 196 | + user = new User( |
| 197 | + id: Convert.ToUInt64(reader["_ID"]), |
| 198 | + guildId: guildId, |
| 199 | + userId: Convert.ToUInt64(reader["USERID"]), |
| 200 | + coin: Convert.ToUInt64(reader["MONEY"]) |
| 201 | + ); |
| 202 | + } |
| 203 | + } |
| 204 | + else |
| 205 | + { |
| 206 | + user = AddNewUser(guildId, userId); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + _connection.Close(); |
| 212 | + } |
| 213 | + } |
| 214 | + catch (MySqlException ex) |
| 215 | + { |
| 216 | + switch ((MySqlErrorCode)ex.Number) |
| 217 | + { |
| 218 | + case MySqlErrorCode.NoSuchTable: |
| 219 | + AddNewGuild(guildId); |
| 220 | + user = GetUser(guildId, userId); |
| 221 | + break; |
| 222 | + |
| 223 | + default: |
| 224 | + throw new Exception($"처리되지 못한 예외가 발생하였습니다. ({ex.Number}, {ex.Message})"); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + return user; |
| 229 | + } |
| 230 | + |
| 231 | + /// <summary> |
| 232 | + /// 유저에게 코인을 추가합니다. |
| 233 | + /// </summary> |
| 234 | + /// <param name="guildId">길드 아이디</param> |
| 235 | + /// <param name="userId">유저 아이디</param> |
| 236 | + /// <param name="coin">추가할 코인의 양</param> |
| 237 | + public void AddUserCoin(ulong guildId, ulong userId, ulong coin) |
| 238 | + { |
| 239 | + try |
| 240 | + { |
| 241 | + using (MySqlConnection _connection = new MySqlConnection(_setting.Config.ConnectionString)) |
| 242 | + { |
| 243 | + _connection.Open(); |
| 244 | + |
| 245 | + using (MySqlCommand sqlCom = new MySqlCommand()) |
| 246 | + { |
| 247 | + sqlCom.Connection = _connection; |
| 248 | + sqlCom.CommandText = $"UPDATE TABLE_{guildId} SET MONEY=MONEY+@MONEY WHERE USERID=@ID"; |
| 249 | + sqlCom.Parameters.AddWithValue("@MONEY", coin); |
| 250 | + sqlCom.Parameters.AddWithValue("@ID", userId); |
| 251 | + sqlCom.CommandType = CommandType.Text; |
| 252 | + |
| 253 | + int numberOfRecords = sqlCom.ExecuteNonQuery(); |
| 254 | + |
| 255 | + if (numberOfRecords == 0) |
| 256 | + { |
| 257 | + AddNewUser(guildId, userId); |
| 258 | + AddUserCoin(guildId, userId, coin); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + _connection.Close(); |
| 263 | + } |
| 264 | + } |
| 265 | + catch (MySqlException ex) |
| 266 | + { |
| 267 | + switch ((MySqlErrorCode)ex.Number) |
| 268 | + { |
| 269 | + case MySqlErrorCode.NoSuchTable: //ER_NO_SUCH_TABLE |
| 270 | + AddNewGuild(guildId); |
| 271 | + break; |
| 272 | + |
| 273 | + default: |
| 274 | + throw new Exception($"처리되지 못한 예외가 발생하였습니다. ({ex.Number}, {ex.Message})"); |
| 275 | + } |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + /// <summary> |
| 280 | + /// 유저에게서 코인을 가져갑니다. |
| 281 | + /// </summary> |
| 282 | + /// <param name="guildId">길드 아이디</param> |
| 283 | + /// <param name="userId">유저 아이디</param> |
| 284 | + /// <param name="coin">추가할 코인의 량</param> |
| 285 | + public void SubUserCoin(ulong guildId, ulong userId, ulong coin) |
| 286 | + { |
| 287 | + try |
| 288 | + { |
| 289 | + using (MySqlConnection _connection = new MySqlConnection(_setting.Config.ConnectionString)) |
| 290 | + { |
| 291 | + _connection.Open(); |
| 292 | + |
| 293 | + using (MySqlCommand sqlCom = new MySqlCommand()) |
| 294 | + { |
| 295 | + sqlCom.Connection = _connection; |
| 296 | + sqlCom.CommandText = $"UPDATE TABLE_{guildId} SET MONEY=MONEY-@MONEY WHERE USERID=@ID"; |
| 297 | + sqlCom.Parameters.AddWithValue("@MONEY", coin); |
| 298 | + sqlCom.Parameters.AddWithValue("@ID", userId); |
| 299 | + sqlCom.CommandType = CommandType.Text; |
| 300 | + |
| 301 | + int numberOfRecords = sqlCom.ExecuteNonQuery(); |
| 302 | + |
| 303 | + if (numberOfRecords == 0) |
| 304 | + { |
| 305 | + AddNewUser(guildId, userId); |
| 306 | + AddUserCoin(guildId, userId, coin); |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + _connection.Close(); |
| 311 | + } |
| 312 | + } |
| 313 | + catch (MySqlException ex) |
| 314 | + { |
| 315 | + switch ((MySqlErrorCode)ex.Number) |
| 316 | + { |
| 317 | + case MySqlErrorCode.NoSuchTable: |
| 318 | + AddNewGuild(guildId); |
| 319 | + break; |
| 320 | + |
| 321 | + default: |
| 322 | + throw new Exception($"처리되지 못한 예외가 발생하였습니다. ({ex.Number}, {ex.Message})"); |
| 323 | + } |
| 324 | + } |
| 325 | + } |
| 326 | + } |
| 327 | +} |
0 commit comments