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-08-03 20:40:34 +08:00
namespace asg_form.Controllers
{
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 ( ) ;
string chinaname = user . chinaname ;
2024-10-19 14:15:49 +08:00
var teamgame = testDb . team_Games . Where ( a = > a . commentary . IndexOf ( chinaname ) > = 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]
public async Task < ActionResult < string > > com_post ( long gameid )
{
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 ) ;
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 ( ) ;
user . Integral = user . Integral + 10 ;
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},你不是解说,无法操作" } ) ;
}
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},你不是解说,无法操作" } ) ;
}
[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
}
2024-08-10 19:01:49 +08:00
[Route("api/v1/com/Integral/ranking")]
[HttpPost]
[Authorize]
[ResponseCache(Duration = 60)]
public async Task < ActionResult < object > > ranking ( )
{
object user = await userManager . Users . OrderByDescending ( a = > a . Integral ) . Select ( a = > new { a . Id , a . chinaname , a . Integral } ) . Take ( 10 ) . ToListAsync ( ) ;
return user ;
}
public long cut_value ( long value )
{
long _value = value ;
value = value - 10 ;
if ( value < 0 )
{
throw new ArgumentException ( "你已经没钱啦!" ) ;
}
return value ;
}
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},你不是解说,无法操作" } ) ;
}
}
}
2024-08-10 19:01:49 +08:00
2024-08-03 20:40:34 +08:00