204 lines
9.0 KiB
C#
204 lines
9.0 KiB
C#
using System.Data;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.Extensions.Logging;
|
||
using Milimoe.FunGame.Core.Api.Transmittal;
|
||
using Milimoe.FunGame.WebAPI.Models;
|
||
|
||
namespace Milimoe.FunGame.WebAPI.Controllers
|
||
{
|
||
[Authorize(AuthenticationSchemes = "CustomBearer")]
|
||
[ApiController]
|
||
[Route("[controller]")]
|
||
public class APIController(SQLHelper sql, ILogger<APIController> logger) : ControllerBase
|
||
{
|
||
[HttpPost("scadd")]
|
||
public string SCAdd([FromBody] SCDTO dto)
|
||
{
|
||
long uid = dto.UID;
|
||
long group = dto.Group;
|
||
double sc = dto.SC;
|
||
int year = dto.Year;
|
||
int month = dto.Month;
|
||
string content = dto.Content;
|
||
if (content.Length > 40) content = content[..40];
|
||
string result = "";
|
||
if (year == 0) year = DateTime.Today.Year;
|
||
if (month == 0) month = DateTime.Today.Month;
|
||
|
||
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)
|
||
{
|
||
record = Convert.ToString(sql.DataSet.Tables[0].Rows[0]["record"]) ?? "";
|
||
}
|
||
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)
|
||
{
|
||
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, times, `group`, `year`, `month`, record) values(@uid, @sc, 1, @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();
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
result = e.ToString();
|
||
sql.Rollback();
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[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 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} 分({times} 次){(remark.Trim() != "" ? $" ({remark})" : "")}\r\n";
|
||
}
|
||
else
|
||
{
|
||
result += $"{count}. 用户:{uid},出生点数:{sc} 分({times} 次){(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<int, DataRow> 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();
|
||
}
|
||
}
|
||
}
|