From 67e907e6c8e2ce65fa44f58fc17776e43f961a24 Mon Sep 17 00:00:00 2001 From: milimoe Date: Mon, 29 Dec 2025 01:43:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E7=89=88=20sc=20=E6=8E=92?= =?UTF-8?q?=E8=A1=8C=E6=A6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MilimoeWebAPI/Controllers/APIController.cs | 198 ++++++++++++++++++--- MilimoeWebAPI/MilimoeWebAPI.cs | 3 +- 2 files changed, 177 insertions(+), 24 deletions(-) diff --git a/MilimoeWebAPI/Controllers/APIController.cs b/MilimoeWebAPI/Controllers/APIController.cs index 105a90f..c77de34 100644 --- a/MilimoeWebAPI/Controllers/APIController.cs +++ b/MilimoeWebAPI/Controllers/APIController.cs @@ -1,42 +1,194 @@ -using Microsoft.AspNetCore.Authorization; +using System.Data; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using Milimoe.FunGame.Core.Api.Transmittal; -using Milimoe.FunGame.Core.Library.Exception; -using Milimoe.FunGame.WebAPI.Constant; namespace Milimoe.FunGame.WebAPI.Controllers { [Authorize(AuthenticationSchemes = "CustomBearer")] [ApiController] [Route("[controller]")] - public class APIController : ControllerBase + public class APIController(SQLHelper sql, ILogger logger) : ControllerBase { - [HttpGet("getuser")] - public async Task GetUser(long id) + [HttpPost("scadd")] + public string SCAdd(long uid, long group, double sc, int year = 0, int month = 0, string content = "") { - if (Statics.RunningPlugin != null) + string result = ""; + if (year == 0) year = DateTime.Today.Year; + if (month == 0) month = DateTime.Today.Month; + + try { - try + sql.NewTransaction(); + sql.Script = "select * from saints where uid = @uid and `group` = @group and `year` = @year and `month` = @month"; + sql.Parameters.Add("uid", uid); + sql.Parameters.Add("group", group); + sql.Parameters.Add("year", year); + sql.Parameters.Add("month", month); + sql.ExecuteDataSet(); + string record = ""; + if (sql.Success) { - SQLHelper? sql = Statics.RunningPlugin.Controller.SQLHelper; - if (sql != null) - { - sql.Script = "select * from users where id = @id"; - sql.Parameters.Add("id", id); - await sql.ExecuteDataSetAsync(); - if (sql.Success) - { - return Ok(sql.DataSet); - } - } + record = Convert.ToString(sql.DataSet.Tables[0].Rows[0]["record"]) ?? ""; } - catch (Exception e) + record = $"{DateTime.Now:MM/dd HH:mm}:{content}({(sc < 0 ? "-" : "+") + Math.Abs(sc)})\r\n{record}"; + record = string.Join("\r\n", record.Split("\r\n", StringSplitOptions.RemoveEmptyEntries).Take(10)); + if (sql.Success) { - Statics.RunningPlugin.Controller.Error(e); - return NotFound("无法调用此接口。原因:\r\n" + e.GetErrorInfo()); + sql.Script = "update saints set sc = sc + @sc, times = times + 1, record = @record where uid = @uid and `group` = @group and `year` = @year and `month` = @month"; + } + else + { + sql.Script = "insert into saints(uid, sc, `group`, `year`, `month`, record) values(@uid, @sc, @group, @year, @month, @record)"; + } + sql.Parameters.Add("sc", sc); + sql.Parameters.Add("uid", uid); + sql.Parameters.Add("group", group); + sql.Parameters.Add("year", year); + sql.Parameters.Add("month", month); + sql.Parameters.Add("record", record); + sql.Execute(); + if (sql.Success) + { + logger.LogDebug("用户 {uid} 的圣人点数增加了 {sc}", uid, sc); + sql.Commit(); + } + else + { + sql.Rollback(); } } - return StatusCode(500, "无法调用此接口。原因:与 SQL 服务器通信失败。"); + catch (Exception e) + { + result = e.ToString(); + sql.Rollback(); + } + + return result; + } + + [HttpGet("sclist")] + public string SCList(long id, long group, int year = 0, int month = 0, bool reverse = false) + { + string result; + if (year == 0) year = DateTime.Today.Year; + if (month == 0) month = DateTime.Today.Month; + string season = $"{year} 年 {month} 月赛季"; + + (bool userHas, double userSC, int userTimes, int userTop, string userRemark) = (false, 0, 0, 0, ""); + if (!reverse) + { + result = $"☆--- OSMTV 圣人排行榜 TOP10 ---☆\r\n{season}\r\n"; + } + else + { + result = $"☆--- OSMTV 出生排行榜 TOP10 ---☆\r\n{season}\r\n"; + } + sql.Script = "select * from saints_backup where `group` = @group and `year` = @year and `month` = @month order by sc" + (!reverse ? " desc" : ""); + sql.Parameters.Add("group", group); + sql.Parameters.Add("year", year); + sql.Parameters.Add("month", month); + sql.ExecuteDataSet(); + if (sql.Success && sql.DataSet.Tables.Count > 0) + { + int count = 0; + foreach (DataRow dr in sql.DataSet.Tables[0].Rows) + { + count++; + long uid = Convert.ToInt64(dr["uid"]); + double sc = Convert.ToDouble(dr["sc"]); + int times = Convert.ToInt32(dr["times"]); + string remark = Convert.ToString(dr["remark"]) ?? ""; + if (reverse) + { + sc = -sc; + remark = remark.Replace("+", "-"); + } + if (uid == id) + { + userHas = true; + userSC = sc; + userTimes = times; + userTop = count; + userRemark = remark; + } + if (count > 10) continue; + if (!reverse) + { + result += $"{count}. 用户:{uid},圣人点数:{sc} 分{(remark.Trim() != "" ? $" ({remark})" : "")}\r\n"; + } + else + { + result += $"{count}. 用户:{uid},出生点数:{sc} 分{(remark.Trim() != "" ? $" ({remark})" : "")}\r\n"; + } + } + if (!reverse && userHas) + { + result += $"你在的圣人点数为:{userSC} 分{(userRemark.Trim() != "" ? $"({userRemark})" : "")}(累计有效:{userTimes} 次),排在 {season}第 {userTop} / {sql.DataSet.Tables[0].Rows.Count} 名。\r\n" + + $"本排行榜仅供娱乐,不代表任何官方立场或真实情况。"; + } + if (reverse && userHas) + { + result += $"你在的出生点数为:{userSC} 分{(userRemark.Trim() != "" ? $"({userRemark})" : "")}(累计有效:{userTimes} 次),排在 {season}的出生榜第 {userTop} / {sql.DataSet.Tables[0].Rows.Count} 名。\r\n" + + $"本排行榜仅供娱乐,不代表任何官方立场或真实情况。"; + } + } + else + { + if (reverse) + { + result = "出生榜目前没有任何数据。"; + } + else + { + result = "圣人榜目前没有任何数据。"; + } + } + + return result.Trim(); + } + + [HttpGet("screcord")] + public string SCRecord(long id, long group, int year = 0, int month = 0) + { + string result = ""; + if (year == 0) year = DateTime.Today.Year; + if (month == 0) month = DateTime.Today.Month; + string season = $"{year} 年 {month} 月赛季"; + + result = $"☆--- 圣人点数信息 ---☆\r\n{season}\r\n"; + sql.Script = "select * from saints where `group` = @group and `year` = @year and `month` = @month order by sc desc"; + sql.Parameters.Add("uid", id); + sql.Parameters.Add("group", group); + sql.Parameters.Add("year", year); + sql.Parameters.Add("month", month); + sql.ExecuteDataSet(); + if (sql.Success && sql.DataSet.Tables.Count > 0) + { + Dictionary dict = sql.DataSet.Tables[0].AsEnumerable().Select((r, i) => new { Index = i + 1, Row = r }).ToDictionary(c => c.Index, c => c.Row); + int index = dict.Where(kv => Convert.ToInt64(kv.Value["uid"]) == id).Select(r => r.Key).FirstOrDefault(); + if (index != 0 && dict.TryGetValue(index, out DataRow? dr) && dr != null) + { + long uid = Convert.ToInt64(dr["uid"]); + double sc = Convert.ToDouble(dr["sc"]); + int times = Convert.ToInt32(dr["times"]); + string remark = Convert.ToString(dr["remark"]) ?? ""; + string record = Convert.ToString(dr["record"]) ?? ""; + result += $"用户:{uid},圣人点数:{sc} 分{(remark.Trim() != "" ? $" ({remark})" : "")}(累计有效:{times} 次),排在 {season}圣人榜第 {index} / {sql.DataSet.Tables[0].Rows.Count} 名。\r\n" + + $"{(record != "" ? "显示近期点数变动信息:\r\n" + record + "\r\n" : "")}本系统仅供娱乐,不代表任何官方立场或真实情况。"; + } + else + { + result = $"你在这个群没有任何 {season}的历史记录。"; + } + } + else + { + result = $"你在这个群没有任何 {season}的历史记录。"; + } + + return result.Trim(); } } } diff --git a/MilimoeWebAPI/MilimoeWebAPI.cs b/MilimoeWebAPI/MilimoeWebAPI.cs index 240381d..71c33b3 100644 --- a/MilimoeWebAPI/MilimoeWebAPI.cs +++ b/MilimoeWebAPI/MilimoeWebAPI.cs @@ -44,8 +44,9 @@ namespace Milimoe.FunGame.WebAPI { SQLHelper? sql = Factory.OpenFactory.GetSQLHelper(); if (sql != null) return sql; - throw new Milimoe.FunGame.SQLServiceException(); + throw new SQLServiceException(); }); + builder.Services.AddTransient(); } WebAPIAuthenticator.WebAPICustomBearerTokenAuthenticator += CustomBearerTokenAuthenticator; }