对战细节调整

This commit is contained in:
lishuo 2022-05-18 10:39:35 +08:00
parent d77ed1bad1
commit aa9a4080fe
4 changed files with 182 additions and 89 deletions

View File

@ -49,6 +49,7 @@ namespace OnlineUserPool.Api
{ {
var result = PostAsync<JsonResult>($"Admin/GameRoom/Add", new { var result = PostAsync<JsonResult>($"Admin/GameRoom/Add", new {
status, status,
roomId,
userId, userId,
name, name,
routeId, routeId,

View File

@ -100,7 +100,7 @@ namespace OnlineUserPool.Hander
// targetData._Cadence.ToString() // targetData._Cadence.ToString()
//}; //};
var weightKg = 0.0D; var weightKg = 0.0D;
if(targetData._Power >= 0) if (targetData._Power >= 0)
{ {
weightKg = Math.Round(targetData._Power / item.Weight, 2); weightKg = Math.Round(targetData._Power / item.Weight, 2);
} }

View File

@ -12,15 +12,59 @@ namespace OnlineUserPool.Unility
public class GameRoomHelper public class GameRoomHelper
{ {
public static string GameRoomFilePath { get; set; } public static string GameRoomFilePath { get; set; }
public static string GameRoomIdPath { get; set; }
public static object _lockobj = new object();
public static object _lockMaxId = new object();
static GameRoomHelper() static GameRoomHelper()
{ {
GameRoomFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\GameRoom.txt"; GameRoomFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\GameRoom.txt";
GameRoomIdPath = AppDomain.CurrentDomain.BaseDirectory + "\\GameRoomId.txt";
if (!File.Exists(GameRoomFilePath)) if (!File.Exists(GameRoomFilePath))
{ {
File.Create(GameRoomFilePath); File.Create(GameRoomFilePath);
} }
if (!File.Exists(GameRoomIdPath))
{
File.Create(GameRoomIdPath);
}
}
public static int GetMaxId()
{
try
{
if (!File.Exists(GameRoomIdPath))
{
return 0;
}
var content = File.ReadAllText(GameRoomIdPath);
if (string.IsNullOrEmpty(content))
{
return 0;
}
return Convert.ToInt32(content);
}
catch (Exception e)
{
Log.Error($"本地读取对战信息房间号出错:{e}");
return 0;
}
}
public static void SetMaxId(int id)
{
try
{
lock (_lockMaxId)
{
File.WriteAllText(GameRoomIdPath, $"{id}");
}
}
catch (Exception e)
{
Log.Error($"本地存储对战信息房间号出错:{e}");
}
} }
public static List<RoomModel> Get() public static List<RoomModel> Get()
{ {
var list = new List<RoomModel>(); var list = new List<RoomModel>();
@ -48,13 +92,13 @@ namespace OnlineUserPool.Unility
{ {
try try
{ {
var str = JsonConvert.SerializeObject(list); lock (_lockobj)
File.WriteAllText(GameRoomFilePath,str);
}
catch (Exception e)
{ {
Log.Error($"本地存储对战信息出错:{e}"); var str = JsonConvert.SerializeObject(list);
File.WriteAllText(GameRoomFilePath, str);
} }
} }
catch (Exception e) { Log.Error($"本地存储对战信息出错:{e}"); }
}
} }
} }

View File

@ -31,6 +31,8 @@ namespace OnlineUserPool.ViewModels
public ObservableCollection<HostModel> Clients { get; private set; } = new ObservableCollection<HostModel>(); public ObservableCollection<HostModel> Clients { get; private set; } = new ObservableCollection<HostModel>();
private static ConcurrentBag<MsgModel> receiveMes = new ConcurrentBag<MsgModel>(); private static ConcurrentBag<MsgModel> receiveMes = new ConcurrentBag<MsgModel>();
private static CustomList<RoomModel> RoomList = new CustomList<RoomModel>(); private static CustomList<RoomModel> RoomList = new CustomList<RoomModel>();
private static int RoomMaxId = 0;
private static int Ticks = 0;
private static object locker = new object(); private static object locker = new object();
public static System.Timers.Timer timer; public static System.Timers.Timer timer;
private static IHandle mapRecordRankingHander; private static IHandle mapRecordRankingHander;
@ -88,6 +90,7 @@ namespace OnlineUserPool.ViewModels
{ {
RoomList.Add(item); RoomList.Add(item);
} }
RoomMaxId = GameRoomHelper.GetMaxId();
//初测程序关闭事件 //初测程序关闭事件
Application.Current.MainWindow.Closing += MainWindow_Closing; Application.Current.MainWindow.Closing += MainWindow_Closing;
Title = $"{ IPAddress.Any }:{ ConfigHelp.UdpPort }"; Title = $"{ IPAddress.Any }:{ ConfigHelp.UdpPort }";
@ -120,6 +123,7 @@ namespace OnlineUserPool.ViewModels
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{ {
GameRoomHelper.Set(RoomList.ToList()); GameRoomHelper.Set(RoomList.ToList());
GameRoomHelper.SetMaxId(RoomMaxId);
} }
private void ReceivedData(IPEndPoint remoteIpEndPoint, ReceiveModel msg, IService service) private void ReceivedData(IPEndPoint remoteIpEndPoint, ReceiveModel msg, IService service)
@ -161,37 +165,16 @@ namespace OnlineUserPool.ViewModels
var msg1 = (msg as MsgModel); var msg1 = (msg as MsgModel);
client.Competitionid = msg1.Competitionid; client.Competitionid = msg1.Competitionid;
client.Model = msg1.Model; client.Model = msg1.Model;
if (client.Model == "")
{
client.RoomId = msg1.RoomId; client.RoomId = msg1.RoomId;
}
if (msg1.V > 0) if (msg1.V > 0)
{ {
client.V = msg1.V; client.V = msg1.V;
} }
receiveMes.Add(msg1); receiveMes.Add(msg1);
//对战房间内的人保存处理 HandleGameRoomSaved(msg1);
if (msg1.RoomId > 0 && msg1.Saved)
{
var current = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault();
if (current != null)
{
//更新房间内的人的保存状态
var currentPlayer = current.List.Where(c => c.UserId == msg1.MemberId).FirstOrDefault();
if (currentPlayer != null)
{
currentPlayer.Saved = true;
}
var notAllSaved = current.List.Where(c => !c.Saved).Any();
if (!notAllSaved && current.Status != 2)
{
current.Status = 2;
WebService.UpdateGameRoom(current.RoomId, current.Status);
}
if (!current.Saved)
{
current.Saved = true;
WebService.AddGameRoom(current.RoomId,current.Status,current.UserId, current.Name, current.MapRouteId, current.Password, current.StartTime.Value, current.CloseTime, current.MaxMembers);
}
}
}
} }
break; break;
//设置客户端 //设置客户端
@ -232,7 +215,47 @@ namespace OnlineUserPool.ViewModels
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{ {
Ticks++;
NotifyClient(); NotifyClient();
//定时保存当前房间信息
if (Ticks % 5 == 0)
{
GameRoomHelper.SetMaxId(RoomMaxId);
GameRoomHelper.Set(RoomList.ToList());
}
}
private void HandleGameRoomSaved(MsgModel msg1)
{
//对战房间内的人保存处理
if (msg1.RoomId > 0 && msg1.Saved)
{
var current = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault();
if (current != null)
{
//更新房间内的人的保存状态
var currentPlayer = current.List.Where(c => c.UserId == msg1.MemberId).FirstOrDefault();
if (currentPlayer != null)
{
currentPlayer.Saved = true;
}
var notAllSaved = current.List.Where(c => !c.Saved).Any();
if (!notAllSaved && current.Status != 2)
{
current.Status = 2;
WebService.UpdateGameRoom(current.RoomId, current.Status);
}
if (!current.Saved)
{
notAllSaved = current.List.Where(c => !c.Saved).Any();
if (!notAllSaved && current.Status != 2)
{
current.Status = 2;
}
current.Saved = true;
WebService.AddGameRoom(current.RoomId, current.Status, current.UserId, current.Name, current.MapRouteId, current.Password, current.StartTime.Value, current.CloseTime, current.MaxMembers);
}
}
}
} }
private void HandleGameRoom(ReceiveModel msg) private void HandleGameRoom(ReceiveModel msg)
@ -274,7 +297,8 @@ namespace OnlineUserPool.ViewModels
{ {
ss = string.Join("|", gameRoom.List.Select(c => c.ToString())); ss = string.Join("|", gameRoom.List.Select(c => c.ToString()));
} }
var G = $"[{gameRoom},detail{{{ss}}}]"; int total = 0;
var G = $"{total}l[{gameRoom},detail{{{ss}}}]";
var temp = string.Join("|", list.Select(m => m.ToString(2))) + "|"; var temp = string.Join("|", list.Select(m => m.ToString(2))) + "|";
var strV21 = $"*l{{{ temp }}};g{{{ G}}};#"; var strV21 = $"*l{{{ temp }}};g{{{ G}}};#";
var ddd = new Data1(strV21); var ddd = new Data1(strV21);
@ -290,7 +314,7 @@ namespace OnlineUserPool.ViewModels
private void BroadCastGameRoomList() private void BroadCastGameRoomList()
{ {
//广播 //广播
var needlist = Clients.Where(c => !string.IsNullOrEmpty(c.Model) && c.Model.Equals("GameRoom") && !string.IsNullOrEmpty(c.Request)); var needlist = Clients.Where(c => c.Model != null && c.Model.Equals("GameRoom") && !string.IsNullOrEmpty(c.Request));
var list = new List<MsgModel>(); var list = new List<MsgModel>();
list.Add(new MsgModel() list.Add(new MsgModel()
@ -302,7 +326,7 @@ namespace OnlineUserPool.ViewModels
foreach (var item in needlist) foreach (var item in needlist)
{ {
var G = GameRoomMessageHandler(item); var G = GameRoomMessageHandler(item);
var strV21 = $"*l{{{ temp }}};g{{{ G}}};#"; var strV21 = $"*l{{{ temp }}};g{{{G}}};#";
var ddd = new Data1(strV21); var ddd = new Data1(strV21);
bool isZip = item.Encoding == "gzip"; bool isZip = item.Encoding == "gzip";
item.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, item.IPEndPoint); item.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, item.IPEndPoint);
@ -329,13 +353,21 @@ namespace OnlineUserPool.ViewModels
List<RoomModel> rooms; List<RoomModel> rooms;
if (!string.IsNullOrEmpty(query.Name)) if (!string.IsNullOrEmpty(query.Name))
{ {
rooms = RoomList.ToList().Where(c => c.Name.Contains(query.Name) || c.RoomId.ToString() == query.Name).OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).Skip(query.PageIndex * query.PageSize).Take(query.PageSize).ToList(); rooms = RoomList.ToList().Where(c => c.Name.Contains(query.Name) || c.RoomId.ToString() == query.Name).OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).ToList();
} }
else else
{ {
rooms = RoomList.ToList().OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).Skip(query.PageIndex * query.PageSize).Take(query.PageSize).ToList(); rooms = RoomList.ToList().OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).ToList();
} }
string G = ""; //计算总页数
int total = 0;
if (query.PageSize != 0)
{
total = rooms.Count() / query.PageSize;
total = rooms.Count() % query.PageSize == 0 ? total : total + 1;
}
rooms = rooms.Skip(query.PageIndex * query.PageSize).Take(query.PageSize).ToList();
string roomInfo = $"{total}l";
foreach (var r in rooms) foreach (var r in rooms)
{ {
string ss = ""; string ss = "";
@ -343,23 +375,19 @@ namespace OnlineUserPool.ViewModels
{ {
ss = string.Join("|", r.List.Select(c => c.ToString())); ss = string.Join("|", r.List.Select(c => c.ToString()));
} }
G += $"[{r.ToString()},detail{{{ss}}}]"; roomInfo += $"[{r.ToString()},detail{{{ss}}}]";
} }
var strV21 = $"*l{{{ temp }}};g{{{ G}}};#"; var strV21 = $"*l{{{ temp }}};g{{{roomInfo}}};#";
var ddd = new Data1(strV21); var ddd = new Data1(strV21);
bool isZip = needSend.Encoding == "gzip"; bool isZip = needSend.Encoding == "gzip";
needSend.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, needSend.IPEndPoint); needSend.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, needSend.IPEndPoint);
}
//查询房间明细
private void HandleQueryGameRoomDetail(ReceiveModel msg)
{
} }
//创建房间 //创建房间
private void HandleCreateGameRoom(ReceiveModel msg) private void HandleCreateGameRoom(ReceiveModel msg)
{ {
var createMsg = msg as CreateGameRoomCommand; var createMsg = msg as CreateGameRoomCommand;
RemoveOtherRoomInfo(createMsg.UserId);
var list = new List<RoomDetailModel>(); var list = new List<RoomDetailModel>();
list.Add(new RoomDetailModel() list.Add(new RoomDetailModel()
{ {
@ -367,14 +395,10 @@ namespace OnlineUserPool.ViewModels
JoinAt = DateTime.Now.ToUniversalTime(), JoinAt = DateTime.Now.ToUniversalTime(),
IsOwner = true, IsOwner = true,
}); });
var maxId = 0; var roomId = RoomMaxId + 1;
if (RoomList.Count() > 0)
{
maxId = RoomList.ToList().Max(c => c.RoomId);
}
var room = new RoomModel var room = new RoomModel
{ {
RoomId = maxId + 1, RoomId = roomId,
Name = createMsg.Name, Name = createMsg.Name,
Password = createMsg.Password, Password = createMsg.Password,
CloseTime = createMsg.CloseTime, CloseTime = createMsg.CloseTime,
@ -400,12 +424,29 @@ namespace OnlineUserPool.ViewModels
mine.RoomId = room.RoomId; mine.RoomId = room.RoomId;
} }
RoomList.Add(room); RoomList.Add(room);
//创建房间 RoomMaxId = room.RoomId;
}
//先移除当前用户在其他房间的信息(考虑服务器重启的时候没有响应客户端退出命令)
private void RemoveOtherRoomInfo(int userId)
{
var preRoom = RoomList.ToList().Where(c => c.Status == 0 && c.List.Where(c => c.UserId == userId).Any()).FirstOrDefault();
if (preRoom != null)
{
var needRemove = preRoom.List.Where(c => c.UserId == userId).FirstOrDefault();
preRoom.List.Remove(needRemove);
if (preRoom.List.Count == 0)
{
RoomList.Remove(preRoom);
}
//发送给这个房间的人
SendGameRoomMessage(preRoom);
}
} }
//处理当前用户加入房间 //处理当前用户加入房间
private void HandleJoinGameRoom(ReceiveModel msg) private void HandleJoinGameRoom(ReceiveModel msg)
{ {
var msg1 = msg as JoinGameRoomCommand; var msg1 = msg as JoinGameRoomCommand;
RemoveOtherRoomInfo(msg1.UserId);
var room = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault(); var room = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault();
var client = Clients.FirstOrDefault(n => n.MemberId.Equals(msg1.UserId)); var client = Clients.FirstOrDefault(n => n.MemberId.Equals(msg1.UserId));
//更新房间信息 //更新房间信息
@ -418,6 +459,10 @@ namespace OnlineUserPool.ViewModels
member.JoinAt = msg1.JoinAt; member.JoinAt = msg1.JoinAt;
} }
else else
{
//如果房间人数已经满了就拒绝加入
var currentCount = room.List.Count;
if (currentCount + 1 <= room.MaxMembers)
{ {
list.Add(new RoomDetailModel list.Add(new RoomDetailModel
{ {
@ -425,13 +470,14 @@ namespace OnlineUserPool.ViewModels
JoinAt = msg1.JoinAt, JoinAt = msg1.JoinAt,
RoomId = msg1.RoomId RoomId = msg1.RoomId
}); });
}
if (client != null) if (client != null)
{ {
client.RoomId = room.RoomId; client.RoomId = room.RoomId;
client.Request = ""; client.Request = "";
} }
} }
}
}
//发送当前房间的信息给当前房间内的人 //发送当前房间的信息给当前房间内的人
SendGameRoomMessage(room); SendGameRoomMessage(room);
} }
@ -456,6 +502,11 @@ namespace OnlineUserPool.ViewModels
private void HandleGameRoomStart(ReceiveModel msg) private void HandleGameRoomStart(ReceiveModel msg)
{ {
var msg1 = msg as GameRoomStartCommand; var msg1 = msg as GameRoomStartCommand;
var client = Clients.Where(c => c.MemberId == msg1.UserId).FirstOrDefault();
if (client != null)
{
client.RoomId = msg1.RoomId;
}
var room = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault(); var room = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault();
if (room != null) if (room != null)
{ {
@ -486,7 +537,7 @@ namespace OnlineUserPool.ViewModels
} }
} }
//人数为0删除房间 //人数为0删除房间
if (list.Count() == 0) if (list.Count() == 0 && room.Status == 0)
{ {
RoomList.Remove(room); RoomList.Remove(room);
} }
@ -530,7 +581,6 @@ namespace OnlineUserPool.ViewModels
} }
SendGameRoomMessage(room); SendGameRoomMessage(room);
} }
} }
private string GameRoomMessageHandler(HostModel item) private string GameRoomMessageHandler(HostModel item)
@ -539,8 +589,6 @@ namespace OnlineUserPool.ViewModels
var G = ""; var G = "";
if (!string.IsNullOrEmpty(item.Request)) if (!string.IsNullOrEmpty(item.Request))
{ {
//var resultList = WebService.GetGameRoomList(0, 1000);
//List / 0 / 6 /
var param = item.Request.Split('/'); var param = item.Request.Split('/');
var pageInfo = param[0]; var pageInfo = param[0];
if (pageInfo.Equals("List")) if (pageInfo.Equals("List"))
@ -549,14 +597,25 @@ namespace OnlineUserPool.ViewModels
var pageSize = Convert.ToInt32(param[2]); var pageSize = Convert.ToInt32(param[2]);
var querName = param[3].ToString(); var querName = param[3].ToString();
List<RoomModel> rooms; List<RoomModel> rooms;
if (!string.IsNullOrEmpty(querName)) if (!string.IsNullOrEmpty(querName))
{ {
rooms = RoomList.ToList().Where(c => c.Name.Contains(querName) || c.RoomId.ToString() == querName).OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).Skip(pageIndex * pageSize).Take(pageSize).ToList(); rooms = RoomList.ToList().Where(c => c.Name.Contains(querName) || c.RoomId.ToString() == querName).OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).ToList();
} }
else else
{ {
rooms = RoomList.ToList().OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).Skip(pageIndex * pageSize).Take(pageSize).ToList(); rooms = RoomList.ToList().OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).ToList();
} }
//计算总页数
int total = 0;
if (pageSize != 0)
{
total = rooms.Count() / pageSize;
total = rooms.Count() % pageSize == 0 ? total : total + 1;
}
G = $"{total}l";
rooms = rooms.Skip(pageIndex * pageSize).Take(pageSize).ToList();
foreach (var r in rooms) foreach (var r in rooms)
{ {
string ss = ""; string ss = "";
@ -568,17 +627,7 @@ namespace OnlineUserPool.ViewModels
} }
} }
} }
return G;
////如果当前Client有房间信息将当前房间信息发送当前客户端
//var currentRoom = RoomList.Where(c => c.Code == item.RoomCode).FirstOrDefault();
//var gameList = "";
//var room = "";
//if (currentRoom != null)
//{
// room = currentRoom.ToString();
// gameList = string.Join('|', currentRoom.List.Select(c => c.ToString()));
//}
return $"list{G}";
} }
private void NotifyClient() private void NotifyClient()
@ -765,8 +814,7 @@ namespace OnlineUserPool.ViewModels
{ {
var temp = string.Join("|", list.Where(m => m.Competitionid == item.Competitionid).Select(m => m.ToString(2))) + "|"; var temp = string.Join("|", list.Where(m => m.Competitionid == item.Competitionid).Select(m => m.ToString(2))) + "|";
var watchList1 = string.Join('|', clients1.Where(c => c.IsWatch && c.Competitionid == item.Competitionid).Select(c => c.MemberId)); var watchList1 = string.Join('|', clients1.Where(c => c.IsWatch && c.Competitionid == item.Competitionid).Select(c => c.MemberId));
var e = string.Join("|", list.Where(c => c.RoomId > 0).Distinct().Select(c => $"{c.MemberId},{c.RoomId}")); var e = string.Join("|", list.Where(c => c.RoomId > 0).Select(c => $"{c.MemberId},{c.RoomId},{c.FrameRate},{c.TotalTicks}"));
//TODO:去重
var strV21 = $"*l{{{ temp }}};w{{{ watchList1 }}};e{{{ e }}};#"; var strV21 = $"*l{{{ temp }}};w{{{ watchList1 }}};e{{{ e }}};#";
ddd = new Data1(strV21); ddd = new Data1(strV21);
} }
@ -798,7 +846,7 @@ namespace OnlineUserPool.ViewModels
private void GameRoomDisConnectHandler(HostModel client) private void GameRoomDisConnectHandler(HostModel client)
{ {
//如果是在对战房间或者对战列表掉线执行,骑行阶段断开连接不移除房间信息 //如果是在对战房间或者对战列表掉线执行,骑行阶段断开连接不移除房间信息
if ( !string.IsNullOrEmpty(client.Model) && !client.Model.Equals("GameRoom")) if (client.Model != null && !client.Model.Equals("GameRoom"))
return; return;
RoomList.ToList().ForEach(o => { RoomList.ToList().ForEach(o => {
@ -808,7 +856,7 @@ namespace OnlineUserPool.ViewModels
o.List.Remove(needRemove); o.List.Remove(needRemove);
} }
}); });
var needRemoveList = RoomList.ToList().Where(c => c.List.Count == 0).ToList(); var needRemoveList = RoomList.ToList().Where(c => c.List.Count == 0 && c.Status == 0).ToList();
foreach (var item in needRemoveList) foreach (var item in needRemoveList)
{ {
RoomList.Remove(item); RoomList.Remove(item);