475 lines
16 KiB
C#
475 lines
16 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.AspNetCore.Identity;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using static asg_form.blog;
|
||
using System.Security.Claims;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using NPOI.OpenXmlFormats.Spreadsheet;
|
||
using Castle.Components.DictionaryAdapter;
|
||
using Flandre.Core.Messaging;
|
||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
|
||
using Flandre.Core.Common;
|
||
using Flandre.Core.Messaging.Segments;
|
||
using static asg_form.Controllers.InviteReferee;
|
||
using Mirai.Net.Data.Shared;
|
||
|
||
namespace asg_form.Controllers
|
||
{
|
||
public class schedule:ControllerBase
|
||
{
|
||
private readonly RoleManager<Role> roleManager;
|
||
private readonly UserManager<User> userManager;
|
||
public schedule(
|
||
RoleManager<Role> roleManager, UserManager<User> userManager)
|
||
{
|
||
|
||
this.roleManager = roleManager;
|
||
this.userManager = userManager;
|
||
}
|
||
|
||
[Authorize]
|
||
[Route("api/v1/admin/game")]
|
||
[HttpPut]
|
||
public async Task<ActionResult<object>> gameput(long gameid,[FromBody] req_team_game req)
|
||
{
|
||
|
||
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||
var user = await userManager.FindByIdAsync(id);
|
||
|
||
bool a = await userManager.IsInRoleAsync(user, "admin");
|
||
if (a)
|
||
{
|
||
int wp = -1;
|
||
try
|
||
{
|
||
using (TestDbContext testDb = new TestDbContext())
|
||
{
|
||
var game = await testDb.team_Games.FirstOrDefaultAsync(a => a.id == gameid);
|
||
game.team1_name = req.team1_name;
|
||
game.judge_Id = req.judge_Id;
|
||
game.referee_Id = req.referee_Id;
|
||
game.Remarks = req.Remarks;
|
||
game.team2_name = req.team2_name;
|
||
game.opentime = req.opentime;
|
||
game.bilibiliuri = req.bilibiliuri;
|
||
game.referee = req.referee;
|
||
game.belong = req.belong;
|
||
game.tag=req.tag;
|
||
game.judge = req.judge;
|
||
wp = 1;
|
||
game.commentary = req.commentary;
|
||
if(req.comLimit != null)
|
||
game.com_limit = req.comLimit;
|
||
else game.com_limit = 2;
|
||
wp = 2;
|
||
game.person_type = req.personType;
|
||
wp = 3;
|
||
await testDb.SaveChangesAsync();
|
||
return Ok(new { code = 200, message = "加入成功" });
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
var innerException = ex.InnerException ?? ex; // 如果没有 InnerException,使用 ex 本身
|
||
// 记录错误信息
|
||
return Ok(new { code = 500, message = "服务器错误", details = innerException.Message });
|
||
}
|
||
|
||
}
|
||
return Ok(new error_mb { code = 401, message = "无权访问" });
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 发布一个竞猜比赛
|
||
/// </summary>
|
||
/// <param name="req"></param>
|
||
/// <returns></returns>
|
||
[Authorize]
|
||
[Route("api/v1/admin/game")]
|
||
[HttpPost]
|
||
public async Task<ActionResult<object>> gamepost([FromBody] req_team_game req)
|
||
{
|
||
|
||
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||
var user = await userManager.FindByIdAsync(id);
|
||
|
||
bool a = await userManager.IsInRoleAsync(user, "admin");
|
||
if (a)
|
||
{
|
||
int wp = -1;
|
||
try
|
||
{
|
||
|
||
using (TestDbContext testDb = new TestDbContext())
|
||
{ wp = 0;
|
||
var ins = new team_game
|
||
{
|
||
team1_name = req.team1_name,
|
||
team2_name = req.team2_name,
|
||
opentime = req.opentime,
|
||
team1_piaoshu = 0,
|
||
team2_piaoshu = 0,
|
||
commentary = req.commentary,
|
||
referee = req.referee,
|
||
belong = req.belong,
|
||
tag = req.tag,
|
||
judge = req.judge,
|
||
judge_Id= req.judge_Id,
|
||
referee_Id= req.referee_Id,
|
||
Remarks= req.Remarks,
|
||
com_limit = req.comLimit,
|
||
person_type = req.personType,
|
||
};
|
||
|
||
wp = 1;
|
||
await testDb.team_Games.AddAsync(ins);
|
||
await testDb.SaveChangesAsync();
|
||
}
|
||
return Ok(new { code = 200, message = "加入成功" });
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
var innerException = ex.InnerException ?? ex; // 如果没有 InnerException,使用 ex 本身
|
||
// 记录错误信息
|
||
return Ok(new { code = 500, message = "服务器错误", details = innerException.Message, wp });
|
||
}
|
||
|
||
|
||
}
|
||
return BadRequest(new error_mb { code = 401, message = "无权访问" });
|
||
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 发布胜利者
|
||
/// </summary>
|
||
/// <param name="teamid"></param>
|
||
/// <param name="winteam"></param>
|
||
/// <returns></returns>
|
||
[Authorize]
|
||
[Route("api/v1/admin/game/win")]
|
||
[HttpPost]
|
||
public async Task<ActionResult<string>> gamepost(long teamid,string winteam)
|
||
{
|
||
|
||
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||
var user = await userManager.FindByIdAsync(id);
|
||
|
||
bool a = await userManager.IsInRoleAsync(user, "admin");
|
||
if (a)
|
||
{
|
||
TestDbContext testDb = new TestDbContext();
|
||
team_game game=testDb.team_Games.Include(a=>a.logs).First(a=>a.id==teamid);
|
||
game.winteam = winteam;
|
||
foreach(var log in game.logs)
|
||
{
|
||
if (log.chickteam == winteam)
|
||
{
|
||
log.win = true;
|
||
}else
|
||
{
|
||
log.win = false;
|
||
}
|
||
}
|
||
await testDb.SaveChangesAsync();
|
||
|
||
try
|
||
{
|
||
|
||
var message1 = new MessageBuilder().Image(GetPictureData($@"{AppDomain.CurrentDomain.BaseDirectory}loge\{game.belong}\{winteam}.png")).Text($"恭喜战队{winteam}获胜!").Build();
|
||
await runbot.runbotr.SendMessageAsync(MessageEnvironment.Channel, "456414070", null, message1, "456414070");
|
||
|
||
}
|
||
catch
|
||
{
|
||
var message1 = new MessageBuilder().Text($"恭喜战队{winteam}获胜!").Build();
|
||
await runbot.runbotr.SendMessageAsync(MessageEnvironment.Channel, "456414070", null, message1, "456414070");
|
||
|
||
}
|
||
|
||
return "ok";
|
||
}
|
||
else
|
||
{
|
||
return Ok(new error_mb { code = 401, message = "无权访问" });
|
||
|
||
}
|
||
|
||
}
|
||
public byte[] GetPictureData(string imagePath)
|
||
{
|
||
FileStream fs = new FileStream(imagePath, FileMode.Open);
|
||
byte[] byteData = new byte[fs.Length];
|
||
fs.Read(byteData, 0, byteData.Length);
|
||
fs.Close();
|
||
return byteData;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除竞猜比赛
|
||
/// </summary>
|
||
/// <param name="gameid"></param>
|
||
/// <returns></returns>
|
||
[Authorize]
|
||
[Route("api/v1/admin/game")]
|
||
[HttpDelete]
|
||
public async Task<ActionResult<string>> gamepush(int gameid)
|
||
{
|
||
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||
var user = await userManager.FindByIdAsync(id);
|
||
|
||
bool a = await userManager.IsInRoleAsync(user, "admin");
|
||
if (a)
|
||
{
|
||
TestDbContext testDb = new TestDbContext();
|
||
team_game game = testDb.team_Games.Include(a => a.logs).First(a => a.id == gameid);
|
||
testDb.team_Games.Remove(game);
|
||
var query = testDb.T_Invitation.Where(n => n.match_id == gameid).ToList();
|
||
if (query.Any())
|
||
{
|
||
testDb.T_Invitation.RemoveRange(query);
|
||
}
|
||
await testDb.SaveChangesAsync();
|
||
return Ok(new error_mb { code = 200, message = "成功删除" });
|
||
}
|
||
else
|
||
{
|
||
return Ok(new error_mb { code = 401, message = "无权访问" });
|
||
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 投票
|
||
/// </summary>
|
||
/// <param name="gameid"></param>
|
||
/// <param name="teamid"></param>
|
||
/// <returns></returns>
|
||
[Authorize]
|
||
[Route("api/v1/game/pushgame")]
|
||
[HttpPut]
|
||
public async Task<ActionResult<string>> gamepush(int gameid,int teamid)
|
||
{
|
||
int wp = -1;
|
||
try
|
||
{
|
||
wp = 0;
|
||
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||
TestDbContext test =new TestDbContext();
|
||
var team = test.team_Games.Include(a=>a.logs).Single(a=>a.id==gameid);
|
||
bool isckick = team.logs.Any(a => a.userid == id);
|
||
if (isckick)
|
||
{
|
||
return BadRequest(new error_mb { code = 400, message = "不要重复投票" });
|
||
|
||
}
|
||
else
|
||
{
|
||
if (teamid == 1)
|
||
{
|
||
team.team1_piaoshu++;
|
||
team.logs.Add(new schedule_log { userid= id ,chickteam=team.team1_name,team=team,win= null });
|
||
}
|
||
else if(teamid == 2)
|
||
{
|
||
team.team2_piaoshu++;
|
||
team.logs.Add(new schedule_log { userid = id, chickteam = team.team2_name, team = team,win=null });
|
||
|
||
}
|
||
else
|
||
{
|
||
return BadRequest(new error_mb { code = 400, message = "队伍id不合法" });
|
||
|
||
}
|
||
wp = 1;
|
||
await test.SaveChangesAsync();
|
||
wp = 2;
|
||
return Ok(new { code = 200, message = "投票成功" });
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
var innerException = ex.InnerException ?? ex; // 如果没有 InnerException,使用 ex 本身
|
||
// 记录错误信息
|
||
return Ok(new { code = 500, message = "服务器错误", details = innerException.Message, wp });
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获得所有比赛
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[Route("api/v1/game/")]
|
||
[HttpGet]
|
||
public async Task<ActionResult<List<team_game>>> gameall(int page,int page_long,string belong="all")
|
||
{
|
||
|
||
TestDbContext test = new TestDbContext();
|
||
|
||
int c = test.Forms.Count();
|
||
int b = page_long * page;
|
||
if (page_long * page > c)
|
||
{
|
||
b = c;
|
||
}
|
||
List<team_game> team = new List<team_game>();
|
||
if(belong=="all")
|
||
{
|
||
team = test.team_Games.OrderByDescending(a => a.opentime).Skip(page_long * page - page_long).Take(page_long).ToList();
|
||
|
||
}
|
||
else
|
||
{
|
||
team = test.team_Games.Where(a => a.belong == belong).OrderByDescending(a => a.opentime).Skip(page_long * page - page_long).Take(page_long).ToList();
|
||
|
||
}
|
||
|
||
return team;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取我的竞猜
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[Authorize]
|
||
[Route("api/v1/game/mylog")]
|
||
[HttpGet]
|
||
public async Task<ActionResult<List<schedule_log>>> mylog()
|
||
{
|
||
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||
|
||
TestDbContext test = new TestDbContext();
|
||
var team = test.schlogs.Include(a=>a.team).Where(a=>a.userid==id).ToList();
|
||
foreach(var team_game in team)
|
||
{
|
||
team_game.team.logs = null;
|
||
}
|
||
return team;
|
||
|
||
|
||
}
|
||
|
||
[Authorize]
|
||
[Route("api/v1/game/final")]
|
||
[HttpPost]
|
||
public async Task<ActionResult<object>> finSc(long gameid,FinalScore msg)
|
||
{
|
||
using(var db = new TestDbContext())
|
||
{
|
||
try
|
||
{
|
||
var query = db.team_Games.FirstOrDefault(n => n.id == gameid);
|
||
if (query == null) return Ok(new { code = 404, message = "未找到比赛" });
|
||
query.final_score = msg.finalScore;
|
||
await db.SaveChangesAsync();
|
||
return Ok(new { code = 200, message = "结果已存入" });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
var innerException = ex.InnerException ?? ex; // 如果没有 InnerException,使用 ex 本身
|
||
// 记录错误信息
|
||
return Ok(new { code = 500, message = "服务器错误", details = innerException.Message});
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public class schedule_log
|
||
{
|
||
public long Id { get; set; }
|
||
public string userid { get; set; }
|
||
public team_game team { get; set; }
|
||
public string chickteam { get; set; }
|
||
public bool? win { get; set; }
|
||
}
|
||
|
||
|
||
public class team_game
|
||
{
|
||
public long id { get; set; }
|
||
|
||
public string team1_name { get; set; }
|
||
public int team1_piaoshu { get; set; }
|
||
public string team2_name { get; set; }
|
||
public int team2_piaoshu { get; set; }
|
||
public string opentime { get; set; }
|
||
/// <summary>
|
||
/// 解说的名字,用逗号隔开!!!!!!!
|
||
/// </summary>
|
||
public string commentary { get; set; }
|
||
/// <summary>
|
||
/// 裁判的名字
|
||
/// </summary>
|
||
public string? referee { get; set; }
|
||
public int? referee_Id { get; set; }
|
||
/// <summary>
|
||
/// bilibili录屏路径
|
||
/// </summary>
|
||
public Uri? bilibiliuri { get; set; }
|
||
public string? winteam { get; set; }
|
||
public string? tag { get; set; }
|
||
public string? belong { get; set; }
|
||
public List<schedule_log> logs { get; set; } = new List<schedule_log>();
|
||
|
||
public string? judge { get; set; }
|
||
|
||
public int? judge_Id { get; set; }
|
||
public int com_limit { get; set; } = 2;
|
||
public string? person_type { get; set; }
|
||
public string? final_score { get; set; }
|
||
public string? Remarks { get; set; }
|
||
}
|
||
|
||
|
||
|
||
public class req_team_game
|
||
{
|
||
public int? referee_Id { get; set; }
|
||
|
||
public int? judge_Id { get; set; }
|
||
|
||
public string? Remarks { get; set; }
|
||
public string team1_name { get; set; }
|
||
public string team2_name { get; set; }
|
||
public string opentime { get; set; }
|
||
|
||
/// <summary>
|
||
/// 解说的名字,用逗号隔开!!!!!!!
|
||
/// </summary>
|
||
public string commentary { get; set; }
|
||
/// <summary>
|
||
/// 裁判的名字
|
||
/// </summary>
|
||
public string referee { get; set; }
|
||
public string? belong { get; set; }
|
||
public Uri? bilibiliuri { get; set;}
|
||
public string tag { get; set; }
|
||
public string? judge { get; set; }
|
||
public int comLimit { get; set; } = 2;
|
||
public string? personType { get; set; }
|
||
}
|
||
|
||
public class FinalScore
|
||
{
|
||
public string finalScore { get; set; }
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|