Church.Net.API/Chruch.Net/Controllers/IceBreakController.cs
2022-09-08 08:04:32 -07:00

483 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chruch.Net.Models.IceBreak;
using Church.Net.DAL.EF;
using Church.Net.Entity;
using Church.Net.Utility;
using Microsoft.Ajax.Utilities;
namespace Chruch.Net.Controllers
{
public class IceBreakController : _BaseController
{
private ChurchNetContext db = new ChurchNetContext();
// GET: IceBreak
public ActionResult Index()
{
return View();
}
public ActionResult Host()
{
return View();
}
public ActionResult Hi(string id, GamePlayer model)
{
model = new GamePlayer();
model.TempGameRoomId = PlayerInfo.TempGameRoomId;
return View(model);
}
[HttpPost]
public ActionResult Hi(GamePlayer model)
{
if (ModelState.IsValid)
{
PlayerInfo = model;
PlayerInfo.Id = Church.Net.Utility.Format.Get33BaseGuid();
if (PlayerInfo.TempGameRoomId.IsNullOrWhiteSpace())
{
return RedirectToAction("WhoIsSpyPlay");
}
else
{
return RedirectToAction("WhoIsSpyPlayJoin", new { id = PlayerInfo.TempGameRoomId });
}
}
return View(model);
}
#region WhoIsSpy
#region PublicProperty
public List<Models.IceBreak.GameRoom> GameRooms
{
get
{
if (HttpContext.Application["GameRooms"] == null)
{
HttpContext.Application["GameRooms"] = new List<GameRoom>();
}
return (List<GameRoom>)HttpContext.Application["GameRooms"];
}
set => HttpContext.Application["GameRooms"] = value;
}
public string GameRoomId
{
get
{
if (HttpContext.Session["GameRoomId"] == null)
{
HttpContext.Session["GameRoomId"] = "";
}
return (string)HttpContext.Session["GameRoomId"];
}
set => HttpContext.Session["GameRoomId"] = value;
}
public GamePlayer PlayerInfo
{
get
{
if (HttpContext.Session["PlayerInfo"] == null)
{
HttpContext.Session["PlayerInfo"] = new GamePlayer();
}
return (GamePlayer)HttpContext.Session["PlayerInfo"];
}
set => HttpContext.Session["PlayerInfo"] = value;
}
#endregion
#region Console
// GET: IceBreak
public ActionResult WhoIsSpy(string id)
{
if (id.IsNullOrWhiteSpace() && GameRoomId.IsNullOrWhiteSpace())
{
return View("WhoIsSpy", new Models.IceBreak.WhoIsSpyGameRoom());
}
if (GameRooms.Any(g => g.Id == id))
{
GameRoomId = id;
}
else if(!id.IsNullOrWhiteSpace())
{
Models.IceBreak.WhoIsSpyGameRoom model = new WhoIsSpyGameRoom();
if (model.Id.IsNullOrWhiteSpace())
{
Random r = new Random();
model.Id = r.Next(1, 99).ToString("00");
}
model.Id = GameRoomId;
model.SpyAmount = 2;
model.Status = WhoIsSpyProcess.WaitForPlayer;
GameRooms.Add(model);
GameRoomId = model.Id;
}
var gameRoom = GameRooms.FirstOrDefault(room => room.Id == GameRoomId);
return View("WhoIsSpy", (WhoIsSpyGameRoom)gameRoom);
}
[HttpPost]
public ActionResult WhoIsSpyCreate(string id)
{
if (GameRooms.Any(g => g.Id == id))
{
GameRoomId = id;
}
else
{
Models.IceBreak.WhoIsSpyGameRoom model = new WhoIsSpyGameRoom();
model.Id = id;
if (model.Id.IsNullOrWhiteSpace())
{
Random r = new Random();
model.Id = r.Next(1, 99).ToString("00");
}
model.SpyAmount = 2;
model.Status = WhoIsSpyProcess.WaitForPlayer;
GameRooms.Add(model);
GameRoomId = model.Id;
}
return RedirectToAction("WhoIsSpy");
}
[HttpPost]
public ActionResult WhoIsSpyStart(Models.IceBreak.WhoIsSpyGameRoom model)
{
Random r = new Random();
WhoIsSpyGameRoom gameRoom = (WhoIsSpyGameRoom)GameRooms.FirstOrDefault(room => room.Id == GameRoomId);
if (gameRoom == null)
{
return RedirectToAction("WhoIsSpy");
}
gameRoom.SpyAmount = model.SpyAmount;
gameRoom.EmptyAmount = model.EmptyAmount;
if ((gameRoom.TotalPlayer -1) < gameRoom.SpyAmount*2)
{
ShowScriptMsg("臥底人數太多了!", MessageType.Errors);
return View("WhoIsSpy", gameRoom);
}
if (gameRoom.SpyAmount < gameRoom.EmptyAmount * 2)
{
ShowScriptMsg("空白人數太多了!", MessageType.Errors);
return View("WhoIsSpy", gameRoom);
}
//取題目
int totalAnswer = db.WhoIsSpy.Count(a=> !gameRoom.PlayedAnswerId.Contains(a.Id));
if (totalAnswer==0)
{
gameRoom.PlayedAnswerId=new List<int>();
}
WhoIsSpy answer;
answer = db.WhoIsSpy.Where(a => !gameRoom.PlayedAnswerId.Contains(a.Id)).OrderBy(a => a.Id).Skip(r.Next(0, totalAnswer - 1)).First();
gameRoom.PlayedAnswerId.Add(answer.Id);
bool reverseAnswer = r.Next(0, 100) > 50;
gameRoom.Answer = reverseAnswer
? new WhoIsSpyAnswer(answer.Answer1Cht, answer.Answer1Chs, answer.Answer1En, answer.Answer1Image)
: new WhoIsSpyAnswer(answer.Answer2Cht, answer.Answer2Chs, answer.Answer2En, answer.Answer2Image);
gameRoom.SpysAnswer = !reverseAnswer
? new WhoIsSpyAnswer(answer.Answer1Cht, answer.Answer1Chs, answer.Answer1En, answer.Answer1Image)
: new WhoIsSpyAnswer(answer.Answer2Cht, answer.Answer2Chs, answer.Answer2En, answer.Answer2Image);
//Set Player
foreach (var whoIsSpyPlayer in gameRoom.Players)
{
whoIsSpyPlayer.VoteTo = new List<string>();
whoIsSpyPlayer.IsDead = false;
whoIsSpyPlayer.GameStatus = WhoIsSpyProcess.Started;
whoIsSpyPlayer.Answer = gameRoom.Answer;
whoIsSpyPlayer.IsSpy = false;
}
//Set Spy
WhoIsSpyPlayer spy;
for (int g = 1; g <= gameRoom.SpyAmount; g++)
{
do
{
spy = gameRoom.Players[r.Next(0, gameRoom.TotalPlayer - 1)];
} while (spy.IsSpy);
spy.IsSpy = true;
spy.Answer = g <= gameRoom.EmptyAmount ? new WhoIsSpyAnswer() : gameRoom.SpysAnswer;
}
//Start Order
do
{
gameRoom.StartIndex = r.Next(0, gameRoom.TotalPlayer - 1);
} while (gameRoom.Players[gameRoom.StartIndex].IsDead);
gameRoom.Status = WhoIsSpyProcess.Started;
return RedirectToAction("WhoIsSpy");
}
public ActionResult WhoIsSpyClose()
{
GameRooms.RemoveAll(room => room.Id == GameRoomId);
GameRoomId = "";
return RedirectToAction("WhoIsSpy");
}
[HttpPost]
public ActionResult WhoIsSpyNext()
{
var gameRoom = (WhoIsSpyGameRoom)GameRooms.FirstOrDefault(room => room.Id == GameRoomId);
if (gameRoom==null)
{
return RedirectToAction("WhoIsSpy");
}
switch (gameRoom.Status)
{
case WhoIsSpyProcess.WaitForPlayer:
break;
case WhoIsSpyProcess.Started:
//開始
gameRoom.Status = WhoIsSpyProcess.Votting;
gameRoom.VoteAmount = gameRoom.Players.Count(p => p.IsSpy && !p.IsDead);
gameRoom.Players.Shuffle();
foreach (var whoIsSpyPlayer in gameRoom.Players)
{
whoIsSpyPlayer.VoteTo = new List<string>();
whoIsSpyPlayer.ReceviedVotes = 0;
whoIsSpyPlayer.VoteAmount = gameRoom.Players.Count(p => p.IsSpy && !p.IsDead);
whoIsSpyPlayer.VoteOption=new List<WhoIsSpyVoteOption>(gameRoom.Players.Where(p=>p.Id!=whoIsSpyPlayer.Id&&!p.IsDead).Select(p=>new WhoIsSpyVoteOption(p.Id,p.Name)));
}
break;
case WhoIsSpyProcess.Votting:
foreach (var whoIsSpyPlayer in gameRoom.Players.OrderByDescending(p=>p.ReceviedVotes).Take(gameRoom.Players.Count(p => p.IsSpy && !p.IsDead)))
{
whoIsSpyPlayer.IsDead = true;
}
gameRoom.Status = WhoIsSpyProcess.DisplayResult;
break;
case WhoIsSpyProcess.DisplayResult:
if (gameRoom.Players.Count(p => p.IsSpy && !p.IsDead)>= gameRoom.Players.Count(p => !p.IsSpy && !p.IsDead) || gameRoom.Players.Count(p => p.IsSpy && !p.IsDead)==0)
{
gameRoom.Status = WhoIsSpyProcess.End;
}
else
{
gameRoom.Status = WhoIsSpyProcess.Started;
}
break;
case WhoIsSpyProcess.End:
gameRoom.Status = WhoIsSpyProcess.WaitForPlayer;
break;
default:
throw new ArgumentOutOfRangeException();
}
foreach (var whoIsSpyPlayer in gameRoom.Players)
{
whoIsSpyPlayer.GameStatus = gameRoom.Status;
}
return RedirectToAction("WhoIsSpy");
}
public PartialViewResult WhoIsSpyGetPlayerList()
{
var gameRoom = GameRooms.FirstOrDefault(room => room.Id == GameRoomId);
if (gameRoom == null)
{
return null;
}
return PartialView("~/Views/IceBreak/WhoIsSpyPlayerList.cshtml", gameRoom.Players.Select(p => p.Name).ToList());
}
public PartialViewResult WhoIsSpyGetPlayerVoteList()
{
var gameRoom = GameRooms.FirstOrDefault(room => room.Id == GameRoomId);
if (gameRoom == null)
{
return null;
}
return PartialView("~/Views/IceBreak/WhoIsSpyPlayerList.cshtml", gameRoom?.Players.Where(p => !p.IsDead && p.VoteTo.Count == 0).Select(p => p.Name).ToList());
}
#endregion
#region WhoIsSpyPlayer
public ActionResult WhoIsSpyPlay()
{
if (PlayerInfo.Name.IsNullOrWhiteSpace())
{
return RedirectToAction("Hi");
}
var gameRoom = GameRooms.FirstOrDefault(room => room.Id == GameRoomId);
if (gameRoom != null && gameRoom.Players.Exists(p => p.Id == PlayerInfo.Id))
{
return View(gameRoom.Players.FirstOrDefault(p => p.Id == PlayerInfo.Id));
}
return View();
}
public ActionResult WhoIsSpyPlayJoin(string id)
{
var gameRoom = (WhoIsSpyGameRoom)GameRooms.FirstOrDefault(room => room.Id == id);
if (gameRoom == null)
{
ShowScriptMsg("遊戲室不存在,請重新輸入遊戲室編號", MessageType.Warring);
return RedirectToAction("WhoIsSpyPlay");
}
if (PlayerInfo.Name.IsNullOrWhiteSpace())
{
PlayerInfo.TempGameRoomId = id;
return RedirectToAction("Hi");
}
WhoIsSpyPlayer model;
if (!gameRoom.Players.Exists(p => p.Id == PlayerInfo.Id))
{
model = new WhoIsSpyPlayer()
{
Id = PlayerInfo.Id,
Name = PlayerInfo.Name,
RoomId = id,
GameStatus = gameRoom.Status
};
gameRoom.Players.Add(model);
GameRoomId = gameRoom.Id;
}
return RedirectToAction("WhoIsSpyPlay");
}
public ActionResult WhoIsSpyPlayVote(WhoIsSpyPlayer model)
{
try
{
var gameRoom = GameRooms.FirstOrDefault(room => room.Id == GameRoomId);
var player = gameRoom.Players.Find(p => p.Id == model.Id);
player.GameStatus = WhoIsSpyProcess.DisplayResult;
player.VoteTo = model.VoteTo;
foreach (var s in model.VoteTo)
{
gameRoom.Players.Find(p => p.Id == s).ReceviedVotes += 1;
}
}
catch (Exception e)
{
}
return RedirectToAction("WhoIsSpyPlay");
}
public ActionResult WhoIsSpyExit()
{
var gameRoom = GameRooms.FirstOrDefault(room => room.Id == GameRoomId);
gameRoom.Players.RemoveAll(p => p.Id == PlayerInfo.Id);
PlayerInfo.TempGameRoomId = "";
GameRoomId = "";
return RedirectToAction("WhoIsSpyPlay");
}
public JsonResult CheckGameRoomStatu(string id)
{
var gameRoom = GameRooms.FirstOrDefault(room => room.Id == id);
if (gameRoom==null)
{
return Json(WhoIsSpyProcess.Closed);
}
var player = gameRoom.Players.Find(p => p.Id == PlayerInfo.Id);
return Json(player.GameStatus);
}
#endregion
#endregion
}
}