-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ea9382
commit a9fb7cd
Showing
3 changed files
with
174 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using AnyBaseLib; | ||
using CounterStrikeSharp.API.Core; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace PlayerSettings | ||
{ | ||
internal static class Migrate | ||
{ | ||
static IAnyBase sqlite, mysql; | ||
|
||
internal static void Init(IAnyBase mysql) | ||
{ | ||
Migrate.mysql = mysql; | ||
sqlite = CAnyBase.Base("sqlite"); | ||
sqlite.Set(AnyBaseLib.Bases.CommitMode.AutoCommit, Path.Combine(PlayerSettingsCore.plugin.ModuleDirectory, "settings")); | ||
sqlite.Init(); | ||
|
||
mysql.QueryAsync("SELECT COUNT(*) FROM `settings_users`", [], StartMigrate); | ||
} | ||
|
||
|
||
private static void StartMigrate(List<List<string>> res) | ||
{ | ||
if (res[0][0] == "0") | ||
Task.Run(MigrateUsers); | ||
else | ||
Close(); | ||
} | ||
|
||
private static void MigrateUsers() | ||
{ | ||
var res = sqlite.Query("SELECT `id`,`steam` FROM `settings_users`", []); | ||
if (res.Count == 0) | ||
{ | ||
Console.WriteLine("Nothing to migrate [users]"); | ||
Close(); | ||
} | ||
else | ||
{ | ||
PlayerSettingsCore.plugin.Logger.LogInformation("Migrating users..."); | ||
var sql = ""; | ||
var args = new List<string>(); | ||
int count = 0; | ||
foreach (var row in res) | ||
{ | ||
sql += "INSERT INTO `settings_users` (`id`,`steam`) VALUES ({ARG}, '{ARG}'); "; | ||
args.Add(row[0]); | ||
args.Add(row[1]); | ||
count++; | ||
if (count % (res.Count / 10) == 0) PlayerSettingsCore.plugin.Logger.LogInformation($"Migrating... [{Math.Round((float)count / ((float)res.Count) * 100, MidpointRounding.ToPositiveInfinity)}%]"); | ||
} | ||
|
||
mysql.QueryAsync(sql, args, (_) => PlayerSettingsCore.plugin.Logger.LogInformation("Migrated users!"), true); | ||
|
||
MigrateSettings(); | ||
} | ||
} | ||
|
||
private static void MigrateSettings() | ||
{ | ||
var res = sqlite.Query("SELECT `user_id`,`param`,`value` FROM `settings_values`", []); | ||
if (res.Count > 0) | ||
{ | ||
PlayerSettingsCore.plugin.Logger.LogInformation("Migrating settings..."); | ||
var sql = ""; | ||
var args = new List<string>(); | ||
int count = 0; | ||
foreach (var row in res) | ||
{ | ||
sql += "INSERT INTO `settings_values` (`user_id`,`param`, `value`) VALUES ({ARG}, '{ARG}', '{ARG}'); "; | ||
args.Add(row[0]); | ||
args.Add(row[1]); | ||
args.Add(row[2]); | ||
count++; | ||
if(count % (res.Count/10) == 0) PlayerSettingsCore.plugin.Logger.LogInformation($"Migrating... [{Math.Round((float)count / ((float)res.Count) * 100, MidpointRounding.ToPositiveInfinity)}%]"); | ||
} | ||
|
||
mysql.QueryAsync(sql, args, (_) => | ||
{ | ||
PlayerSettingsCore.plugin.Logger.LogInformation("Migrated settings!"); | ||
Close(); | ||
}, true); | ||
|
||
} | ||
else | ||
{ | ||
Console.WriteLine("Nothing to migrate [values]"); | ||
Close(); | ||
} | ||
} | ||
|
||
|
||
private static void Close() | ||
{ | ||
sqlite.Close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters