366 lines
14 KiB
C#
366 lines
14 KiB
C#
using Flandre.Core.Common;
|
||
using Flandre.Core.Messaging.Segments;
|
||
using Flandre.Core.Messaging;
|
||
using Manganese.Text;
|
||
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;
|
||
using static asg_form.Controllers.InviteReferee;
|
||
using Manganese.Array;
|
||
using static asg_form.Controllers.schedule;
|
||
|
||
namespace asg_form.Controllers
|
||
{
|
||
|
||
|
||
[ApiController]
|
||
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();
|
||
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();
|
||
|
||
return JsonConvert.SerializeObject(teamgame);
|
||
}
|
||
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });
|
||
}
|
||
/// <summary>
|
||
/// 选定场次
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[Route("api/v1/com/")]
|
||
[HttpPost]
|
||
[Authorize]
|
||
public async Task<ActionResult<object>> com_post(long gameid)
|
||
{
|
||
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||
var user = await userManager.FindByIdAsync(id);
|
||
if (user.officium == "Commentator")
|
||
{
|
||
if (user.Integral == null)
|
||
{
|
||
user.Integral = 0;
|
||
await userManager.UpdateAsync(user);
|
||
}
|
||
TestDbContext testDb = new TestDbContext();
|
||
string chinaname = user.chinaname;
|
||
var teamgame = await testDb.team_Games.FirstAsync(a => a.id == gameid);
|
||
if (teamgame.isAllowChoose == 0) return Ok(new error_mb { code = 401, message = "无法选择该比赛" });
|
||
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();
|
||
user.Integral = user.Integral+5;
|
||
await userManager.UpdateAsync(user);
|
||
try
|
||
{
|
||
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");
|
||
|
||
}
|
||
catch
|
||
{
|
||
|
||
}
|
||
return "成功";
|
||
}
|
||
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });
|
||
}
|
||
public static long cut_value(long value)
|
||
{
|
||
long _value = value;
|
||
value = value - 5;
|
||
if (value < 0)
|
||
{
|
||
throw new ArgumentException("你已经没钱啦!");
|
||
|
||
}
|
||
return value;
|
||
}
|
||
public static int[] ConvertStringToIntArray(string input)
|
||
{
|
||
var cleanedInput = input.Trim('[', ']').Replace("'", "");
|
||
int[] result = cleanedInput.Split(',')
|
||
.Select(int.Parse)
|
||
.ToArray();
|
||
|
||
return result;
|
||
}
|
||
|
||
//写一个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!);
|
||
|
||
//对testDb.team_Games.commentary中出现最多的十个解说名称进行排名
|
||
var ranking = testDb.team_Games.ToList();
|
||
var ranking1= ranking.SelectMany(g => JsonConvert.DeserializeObject<List<com_json>>(g.commentary).Select(c => c.chinaname))
|
||
.GroupBy(c => c)
|
||
.OrderByDescending(g => g.Count())
|
||
.Take(10)
|
||
.Select(g => new { Name = g.Key, Count = g.Count() })
|
||
.ToList();
|
||
|
||
return ranking1;
|
||
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 导播选定场次
|
||
/// </summary>
|
||
[Route("api/v1/anchor")]
|
||
[HttpPost]
|
||
[Authorize]
|
||
public async Task<ActionResult<object>> anchorPost(string gameIds)
|
||
{
|
||
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||
var user = await userManager.FindByIdAsync(id);
|
||
|
||
if (user.officium != "Anchor")
|
||
{
|
||
return Ok(new error_mb { code = 401, message = $"你是{user.officium},你不是导播,无法操作" });
|
||
}
|
||
|
||
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);
|
||
if (teamgame.isAllowChoose == 0) return Ok(new error_mb { code = 401, message = "存在无法选择的比赛" });
|
||
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]
|
||
public async Task<ActionResult<object>> DeleteSelectedGame(int gameId,string reason)
|
||
{
|
||
string userId = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||
var user = await userManager.FindByIdAsync(userId);
|
||
|
||
if (user.officium != "Anchor")
|
||
{
|
||
return Ok(new error_mb { code = 401, message = $"你是{user.officium},你不是导播,无法操作" });
|
||
}
|
||
|
||
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 });
|
||
}
|
||
return Ok(new { code = 200, message = "删除成功" });
|
||
}
|
||
|
||
|
||
public class com_json
|
||
{
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public int id { get; set; }
|
||
/// <summary>
|
||
/// 老恐龙
|
||
/// </summary>
|
||
public string chinaname { get; set; }
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 取消选班
|
||
/// </summary>
|
||
/// <param name="gameid"></param>
|
||
/// <returns></returns>
|
||
[Route("api/v1/com/")]
|
||
[HttpDelete]
|
||
[Authorize]
|
||
public async Task<ActionResult<string>> com_del(long gameid,string Cancellation_Reason )
|
||
{
|
||
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||
var user = await userManager.FindByIdAsync(id);
|
||
if (user.Integral == null)
|
||
{
|
||
user.Integral = 0;
|
||
await userManager.UpdateAsync(user);
|
||
}
|
||
if (user.officium == "Commentator")
|
||
{
|
||
TestDbContext testDb = new TestDbContext();
|
||
string chinaname = user.chinaname;
|
||
var teamgame = await testDb.team_Games.FirstAsync(a => a.id == gameid);
|
||
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 = $"你的金钱无法满足你完成以下操作" });
|
||
|
||
}
|
||
teamgame.commentary = JsonConvert.SerializeObject(com);
|
||
await testDb.SaveChangesAsync();
|
||
try
|
||
{
|
||
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();
|
||
|
||
await runbot.runbotr.SendMessageAsync(MessageEnvironment.Channel, "925510646", null, message, "925510646");
|
||
|
||
}
|
||
catch
|
||
{
|
||
|
||
}
|
||
return "成功";
|
||
}
|
||
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });
|
||
}
|
||
|
||
[Route("api/v1/com/search_ok_regist")]
|
||
[HttpPost]
|
||
[Authorize]
|
||
[ResponseCache(Duration = 60)]
|
||
public async Task<ActionResult<string>> Search()
|
||
{
|
||
TestDbContext testDb = new TestDbContext();
|
||
var team = await testDb.team_Games.Select(a => new { a.id, a.commentary, a.opentime, a.team1_name, a.team2_name, a.belong }).ToListAsync();
|
||
var team1 = team.Where(a => a.commentary.Split(",").Length <= 1);
|
||
return JsonConvert.SerializeObject(team1);
|
||
}
|
||
|
||
|
||
[Route("api/v1/com/Integral/ranking1")]
|
||
[HttpPost]
|
||
[Authorize]
|
||
[ResponseCache(Duration = 60)]
|
||
public async Task<ActionResult<object>> ranking1()
|
||
{
|
||
object user= await userManager.Users.OrderByDescending(a => a.Integral).Select(a=>new{a.Id,a.chinaname,a.Integral}).Take(10).ToListAsync();
|
||
return user;
|
||
}
|
||
|
||
|
||
|
||
[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;
|
||
TestDbContext testDb = new TestDbContext();
|
||
int a = await testDb.team_Games.CountAsync(a => a.commentary.IndexOf(id) >= 0);
|
||
return a;
|
||
}
|
||
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|