366 lines
14 KiB
C#
Raw Normal View History

2024-10-20 13:51:44 +08:00
using Flandre.Core.Common;
using Flandre.Core.Messaging.Segments;
using Flandre.Core.Messaging;
using Manganese.Text;
2024-08-03 20:40:34 +08:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Mirai.Net.Sessions.Http.Managers;
using Newtonsoft.Json;
using System.Security.Claims;
2024-10-20 13:51:44 +08:00
using static asg_form.Controllers.InviteReferee;
2024-11-06 22:51:29 +08:00
using Manganese.Array;
using static asg_form.Controllers.schedule;
2024-08-03 20:40:34 +08:00
namespace asg_form.Controllers
{
2025-01-27 20:32:23 +08:00
2025-02-09 19:17:34 +08:00
2024-08-03 20:40:34 +08:00
public class Com : ControllerBase
{
private readonly RoleManager<Role> roleManager;
private readonly UserManager<User> userManager;
public Com(
RoleManager<Role> roleManager, UserManager<User> userManager)
{
this.roleManager = roleManager;
this.userManager = userManager;
}
/// <summary>
/// 获取我的场次
/// </summary>
/// <returns></returns>
[Route("api/v1/com/my")]
[HttpPost]
[Authorize]
public async Task<ActionResult<string>> com_my()
{
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
var user = await userManager.FindByIdAsync(id);
if (user.officium == "Commentator")
{
TestDbContext testDb = new TestDbContext();
2024-12-21 21:57:24 +08:00
var teamgame = testDb.team_Games.Where(a => a.commentary.IndexOf(id) >= 0).Select(a => new { a.id, a.team1_name, a.team2_name,a.belong,a.tag, a.bilibiliuri, a.commentary, a.referee,a.judge,a.person_type ,a.opentime,a.team1_piaoshu,a.team2_piaoshu}).OrderByDescending(a => a.opentime).ToList();
2024-08-03 20:40:34 +08:00
return JsonConvert.SerializeObject(teamgame);
}
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });
}
/// <summary>
/// 选定场次
/// </summary>
/// <returns></returns>
[Route("api/v1/com/")]
[HttpPost]
[Authorize]
2025-01-22 11:40:30 +08:00
public async Task<ActionResult<object>> com_post(long gameid)
2024-08-03 20:40:34 +08:00
{
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
var user = await userManager.FindByIdAsync(id);
if (user.officium == "Commentator")
{
2024-08-17 17:37:47 +08:00
if (user.Integral == null)
{
user.Integral = 0;
await userManager.UpdateAsync(user);
}
2024-08-03 20:40:34 +08:00
TestDbContext testDb = new TestDbContext();
string chinaname = user.chinaname;
var teamgame = await testDb.team_Games.FirstAsync(a => a.id == gameid);
2025-01-22 11:40:30 +08:00
if (teamgame.isAllowChoose == 0) return Ok(new error_mb { code = 401, message = "无法选择该比赛" });
2024-08-10 19:01:49 +08:00
var com = JsonConvert.DeserializeObject<List<com_json>>(teamgame.commentary);
com.Add(new com_json { id = id.ToInt32(), chinaname = chinaname });
teamgame.commentary = JsonConvert.SerializeObject(com);
await testDb.SaveChangesAsync();
2024-11-06 22:51:29 +08:00
user.Integral = user.Integral+5;
2024-08-10 19:01:49 +08:00
await userManager.UpdateAsync(user);
2024-08-03 20:40:34 +08:00
try
{
2024-10-20 13:51:44 +08:00
var message = new MessageBuilder().Add(new AtSegment(user.qqnumber??"2667210109")).Text($"解说:\r\n{chinaname}\r\n选择了比赛:\r\n{teamgame.team1_name} VS {teamgame.team2_name}").Build();
await runbot.runbotr.SendMessageAsync(MessageEnvironment.Channel, "925510646", null, message, "925510646");
2024-08-03 20:40:34 +08:00
}
catch
{
}
return "成功";
}
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });
}
2025-01-27 20:32:23 +08:00
public static long cut_value(long value)
{
long _value = value;
value = value - 5;
if (value < 0)
{
throw new ArgumentException("你已经没钱啦!");
2024-08-03 20:40:34 +08:00
2025-01-27 20:32:23 +08:00
}
return value;
}
2024-11-06 22:51:29 +08:00
public static int[] ConvertStringToIntArray(string input)
{
var cleanedInput = input.Trim('[', ']').Replace("'", "");
int[] result = cleanedInput.Split(',')
.Select(int.Parse)
.ToArray();
return result;
}
2025-01-27 20:32:23 +08:00
//写一个api获取选班数前十的名单
[Route("api/v1/com/Integral/ranking")]
[HttpPost]
[Authorize]
[ResponseCache(Duration = 500)]
public async Task<ActionResult<object>> ranking()
{
using(TestDbContext testDb = new TestDbContext()) {
var user = await userManager.FindByNameAsync(this.User.Identity!.Name!);
2025-02-02 16:26:04 +08:00
2025-01-27 20:32:23 +08:00
//对testDb.team_Games.commentary中出现最多的十个解说名称进行排名
2025-02-02 16:26:04 +08:00
var ranking = testDb.team_Games.ToList();
var ranking1= ranking.SelectMany(g => JsonConvert.DeserializeObject<List<com_json>>(g.commentary).Select(c => c.chinaname))
2025-01-27 20:32:23 +08:00
.GroupBy(c => c)
.OrderByDescending(g => g.Count())
.Take(10)
.Select(g => new { Name = g.Key, Count = g.Count() })
.ToList();
2025-02-02 16:26:04 +08:00
return ranking1;
2025-01-27 20:32:23 +08:00
2025-02-02 16:26:04 +08:00
2025-01-27 20:32:23 +08:00
}
}
2024-11-06 22:51:29 +08:00
/// <summary>
/// 导播选定场次
/// </summary>
[Route("api/v1/anchor")]
[HttpPost]
[Authorize]
2024-11-07 14:33:50 +08:00
public async Task<ActionResult<object>> anchorPost(string gameIds)
2024-11-06 22:51:29 +08:00
{
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
var user = await userManager.FindByIdAsync(id);
if (user.officium != "Anchor")
{
2024-11-07 14:33:50 +08:00
return Ok(new error_mb { code = 401, message = $"你是{user.officium},你不是导播,无法操作" });
2024-11-06 22:51:29 +08:00
}
else
{
if (user.Integral == null)
{
user.Integral = 0;
await userManager.UpdateAsync(user);
}
TestDbContext testDb = new TestDbContext();
string chinaname = user.chinaname;
int[] gameIdArray = ConvertStringToIntArray(gameIds);
var message = $"导播:\r\n{chinaname}\r\n选择了比赛:";
foreach (var gameId in gameIdArray)
{
var teamgame = await testDb.team_Games.FirstOrDefaultAsync(a => a.id == gameId);
2025-01-22 11:40:30 +08:00
if (teamgame.isAllowChoose == 0) return Ok(new error_mb { code = 401, message = "存在无法选择的比赛" });
2024-11-06 22:51:29 +08:00
if (teamgame != null)
{
teamgame.referee = chinaname;
teamgame.referee_Id = (int)(user.Id);
await testDb.SaveChangesAsync();
user.Integral += 5;
await userManager.UpdateAsync(user);
message += $"\r\n{teamgame.team1_name} VS {teamgame.team2_name}";
}
}
try
{
var msg = new MessageBuilder().Add(new AtSegment(user.qqnumber ?? "2667210109")).Text(message).Build();
await runbot.runbotr.SendMessageAsync(MessageEnvironment.Channel, "925510646", null, msg, "925510646");
}
catch (Exception ex)
{
return Ok(new { code = 500, message = "服务器错误", ex });
}
return Ok(new { code = 200, message = "报名成功"}); ;
}
}
/// <summary>
/// 删除导播选择的比赛
/// </summary>
[Route("api/v1/anchor")]
[HttpDelete]
[Authorize]
2024-11-07 14:33:50 +08:00
public async Task<ActionResult<object>> DeleteSelectedGame(int gameId,string reason)
2024-11-06 22:51:29 +08:00
{
string userId = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
var user = await userManager.FindByIdAsync(userId);
if (user.officium != "Anchor")
{
2024-11-07 14:33:50 +08:00
return Ok(new error_mb { code = 401, message = $"你是{user.officium},你不是导播,无法操作" });
2024-11-06 22:51:29 +08:00
}
TestDbContext testDb = new TestDbContext();
var teamGame = await testDb.team_Games.FirstOrDefaultAsync(a => a.id == gameId);
if (teamGame == null)
{
return NotFound(new { code = 404, message = "比赛未找到" });
}
teamGame.referee = null;
teamGame.referee_Id = null;
await testDb.SaveChangesAsync();
user.Integral = user.Integral - 5 > 0 ? user.Integral - 5 : 0; // 确保积分不会减少到负数
await userManager.UpdateAsync(user);
try
{
var message = new MessageBuilder()
.Add(new AtSegment(user.qqnumber ?? "2667210109"))
.Text($"导播 {user.chinaname} 已取消比赛 {teamGame.team1_name} VS {teamGame.team2_name} 的选择,\n原因是{reason}")
.Build();
await runbot.runbotr.SendMessageAsync(MessageEnvironment.Channel, "925510646", null, message, "925510646");
}
catch (Exception ex)
{
return StatusCode(500, new { code = 500, message = "服务器错误", ex });
}
2024-11-07 14:33:50 +08:00
return Ok(new { code = 200, message = "删除成功" });
2024-11-06 22:51:29 +08:00
}
2024-08-10 19:01:49 +08:00
public class com_json
{
/// <summary>
///
/// </summary>
public int id { get; set; }
/// <summary>
/// 老恐龙
/// </summary>
public string chinaname { get; set; }
}
2024-08-03 20:40:34 +08:00
/// <summary>
/// 取消选班
/// </summary>
/// <param name="gameid"></param>
/// <returns></returns>
[Route("api/v1/com/")]
[HttpDelete]
[Authorize]
2024-11-03 00:08:34 +08:00
public async Task<ActionResult<string>> com_del(long gameid,string Cancellation_Reason )
2024-08-03 20:40:34 +08:00
{
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
var user = await userManager.FindByIdAsync(id);
2024-08-17 17:37:47 +08:00
if (user.Integral == null)
{
user.Integral = 0;
await userManager.UpdateAsync(user);
}
2024-08-03 20:40:34 +08:00
if (user.officium == "Commentator")
{
TestDbContext testDb = new TestDbContext();
string chinaname = user.chinaname;
var teamgame = await testDb.team_Games.FirstAsync(a => a.id == gameid);
2024-08-10 19:01:49 +08:00
var com = JsonConvert.DeserializeObject<List<com_json>>(teamgame.commentary);
com.Remove(com.First(a => a.id == id.ToInt32()));
try{
user.Integral = cut_value((long)user.Integral);
await userManager.UpdateAsync(user);
}
catch{
return BadRequest(new error_mb { code = 400, message = $"你的金钱无法满足你完成以下操作" });
2024-08-03 20:40:34 +08:00
}
2024-08-10 19:01:49 +08:00
teamgame.commentary = JsonConvert.SerializeObject(com);
2024-08-03 20:40:34 +08:00
await testDb.SaveChangesAsync();
2024-10-20 13:51:44 +08:00
try
{
2024-11-03 00:08:34 +08:00
var message = new MessageBuilder().Add(new AtSegment(user.qqnumber ?? "2667210109")).Text($"解说:\r\n{chinaname}\r\n取消了比赛:\r\n{teamgame.team1_name} VS {teamgame.team2_name}\r\n原因:{Cancellation_Reason}").Build();
2024-10-20 13:51:44 +08:00
await runbot.runbotr.SendMessageAsync(MessageEnvironment.Channel, "925510646", null, message, "925510646");
}
catch
{
}
2024-08-03 20:40:34 +08:00
return "成功";
}
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });
}
2024-11-06 22:51:29 +08:00
2024-08-03 20:40:34 +08:00
[Route("api/v1/com/search_ok_regist")]
[HttpPost]
[Authorize]
[ResponseCache(Duration = 60)]
public async Task<ActionResult<string>> Search()
{
TestDbContext testDb = new TestDbContext();
2024-08-10 19:01:49 +08:00
var team = await testDb.team_Games.Select(a => new { a.id, a.commentary, a.opentime, a.team1_name, a.team2_name, a.belong }).ToListAsync();
2024-08-03 20:40:34 +08:00
var team1 = team.Where(a => a.commentary.Split(",").Length <= 1);
2024-08-10 19:01:49 +08:00
return JsonConvert.SerializeObject(team1);
2024-08-03 20:40:34 +08:00
}
2025-01-27 20:32:23 +08:00
[Route("api/v1/com/Integral/ranking1")]
2024-08-10 19:01:49 +08:00
[HttpPost]
[Authorize]
[ResponseCache(Duration = 60)]
2025-01-27 20:32:23 +08:00
public async Task<ActionResult<object>> ranking1()
2024-08-10 19:01:49 +08:00
{
object user= await userManager.Users.OrderByDescending(a => a.Integral).Select(a=>new{a.Id,a.chinaname,a.Integral}).Take(10).ToListAsync();
return user;
}
2025-01-27 20:32:23 +08:00
2024-08-10 19:01:49 +08:00
2024-08-03 20:40:34 +08:00
[Route("api/v1/com/")]
[HttpGet]
[Authorize]
public async Task<ActionResult<int>> com_cout(long gameid)
{
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
var user = await userManager.FindByIdAsync(id);
if (user.officium == "Commentator")
{
var chinaname = user.chinaname;
2024-08-10 19:01:49 +08:00
TestDbContext testDb = new TestDbContext();
int a = await testDb.team_Games.CountAsync(a => a.commentary.IndexOf(id) >= 0);
2024-08-03 20:40:34 +08:00
return a;
}
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });
}
}
2025-01-27 20:32:23 +08:00
2024-08-03 20:40:34 +08:00
}
2024-08-10 19:01:49 +08:00
2024-08-03 20:40:34 +08:00