对战细节调整

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 {
status,
roomId,
userId,
name,
routeId,

View File

@ -100,7 +100,7 @@ namespace OnlineUserPool.Hander
// targetData._Cadence.ToString()
//};
var weightKg = 0.0D;
if(targetData._Power >= 0)
if (targetData._Power >= 0)
{
weightKg = Math.Round(targetData._Power / item.Weight, 2);
}
@ -143,11 +143,11 @@ namespace OnlineUserPool.Hander
PreDistance = item.GetPreDistance()
};
msgModels.Add(info);
}
}
catch (Exception e)
{
Log.Error("加载虚拟人物错误:" + e.Message + "\r\n" + e.StackTrace);
}
{
Log.Error("加载虚拟人物错误:" + e.Message + "\r\n" + e.StackTrace);
}
}
}
return msgModels;

View File

@ -12,15 +12,59 @@ namespace OnlineUserPool.Unility
public class GameRoomHelper
{
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()
{
GameRoomFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\GameRoom.txt";
GameRoomIdPath = AppDomain.CurrentDomain.BaseDirectory + "\\GameRoomId.txt";
if (!File.Exists(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()
{
var list = new List<RoomModel>();
@ -48,13 +92,13 @@ namespace OnlineUserPool.Unility
{
try
{
var str = JsonConvert.SerializeObject(list);
File.WriteAllText(GameRoomFilePath,str);
}
catch (Exception e)
{
Log.Error($"本地存储对战信息出错:{e}");
lock (_lockobj)
{
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>();
private static ConcurrentBag<MsgModel> receiveMes = new ConcurrentBag<MsgModel>();
private static CustomList<RoomModel> RoomList = new CustomList<RoomModel>();
private static int RoomMaxId = 0;
private static int Ticks = 0;
private static object locker = new object();
public static System.Timers.Timer timer;
private static IHandle mapRecordRankingHander;
@ -88,6 +90,7 @@ namespace OnlineUserPool.ViewModels
{
RoomList.Add(item);
}
RoomMaxId = GameRoomHelper.GetMaxId();
//初测程序关闭事件
Application.Current.MainWindow.Closing += MainWindow_Closing;
Title = $"{ IPAddress.Any }:{ ConfigHelp.UdpPort }";
@ -120,6 +123,7 @@ namespace OnlineUserPool.ViewModels
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
GameRoomHelper.Set(RoomList.ToList());
GameRoomHelper.SetMaxId(RoomMaxId);
}
private void ReceivedData(IPEndPoint remoteIpEndPoint, ReceiveModel msg, IService service)
@ -161,37 +165,16 @@ namespace OnlineUserPool.ViewModels
var msg1 = (msg as MsgModel);
client.Competitionid = msg1.Competitionid;
client.Model = msg1.Model;
client.RoomId = msg1.RoomId;
if (client.Model == "")
{
client.RoomId = msg1.RoomId;
}
if (msg1.V > 0)
{
client.V = msg1.V;
}
receiveMes.Add(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);
}
}
}
HandleGameRoomSaved(msg1);
}
break;
//设置客户端
@ -232,7 +215,47 @@ namespace OnlineUserPool.ViewModels
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Ticks++;
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)
@ -274,7 +297,8 @@ namespace OnlineUserPool.ViewModels
{
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 strV21 = $"*l{{{ temp }}};g{{{ G}}};#";
var ddd = new Data1(strV21);
@ -290,7 +314,7 @@ namespace OnlineUserPool.ViewModels
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>();
list.Add(new MsgModel()
@ -302,7 +326,7 @@ namespace OnlineUserPool.ViewModels
foreach (var item in needlist)
{
var G = GameRoomMessageHandler(item);
var strV21 = $"*l{{{ temp }}};g{{{ G}}};#";
var strV21 = $"*l{{{ temp }}};g{{{G}}};#";
var ddd = new Data1(strV21);
bool isZip = item.Encoding == "gzip";
item.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, item.IPEndPoint);
@ -329,13 +353,21 @@ namespace OnlineUserPool.ViewModels
List<RoomModel> rooms;
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
{
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)
{
string ss = "";
@ -343,23 +375,19 @@ namespace OnlineUserPool.ViewModels
{
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);
bool isZip = needSend.Encoding == "gzip";
needSend.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, needSend.IPEndPoint);
}
//查询房间明细
private void HandleQueryGameRoomDetail(ReceiveModel msg)
{
}
//创建房间
private void HandleCreateGameRoom(ReceiveModel msg)
{
var createMsg = msg as CreateGameRoomCommand;
RemoveOtherRoomInfo(createMsg.UserId);
var list = new List<RoomDetailModel>();
list.Add(new RoomDetailModel()
{
@ -367,14 +395,10 @@ namespace OnlineUserPool.ViewModels
JoinAt = DateTime.Now.ToUniversalTime(),
IsOwner = true,
});
var maxId = 0;
if (RoomList.Count() > 0)
{
maxId = RoomList.ToList().Max(c => c.RoomId);
}
var roomId = RoomMaxId + 1;
var room = new RoomModel
{
RoomId = maxId + 1,
RoomId = roomId,
Name = createMsg.Name,
Password = createMsg.Password,
CloseTime = createMsg.CloseTime,
@ -400,12 +424,29 @@ namespace OnlineUserPool.ViewModels
mine.RoomId = room.RoomId;
}
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)
{
var msg1 = msg as JoinGameRoomCommand;
RemoveOtherRoomInfo(msg1.UserId);
var room = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault();
var client = Clients.FirstOrDefault(n => n.MemberId.Equals(msg1.UserId));
//更新房间信息
@ -419,17 +460,22 @@ namespace OnlineUserPool.ViewModels
}
else
{
list.Add(new RoomDetailModel
//如果房间人数已经满了就拒绝加入
var currentCount = room.List.Count;
if (currentCount + 1 <= room.MaxMembers)
{
UserId = msg1.UserId,
JoinAt = msg1.JoinAt,
RoomId = msg1.RoomId
});
}
if (client != null)
{
client.RoomId = room.RoomId;
client.Request = "";
list.Add(new RoomDetailModel
{
UserId = msg1.UserId,
JoinAt = msg1.JoinAt,
RoomId = msg1.RoomId
});
if (client != null)
{
client.RoomId = room.RoomId;
client.Request = "";
}
}
}
}
//发送当前房间的信息给当前房间内的人
@ -456,6 +502,11 @@ namespace OnlineUserPool.ViewModels
private void HandleGameRoomStart(ReceiveModel msg)
{
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();
if (room != null)
{
@ -486,7 +537,7 @@ namespace OnlineUserPool.ViewModels
}
}
//人数为0删除房间
if (list.Count() == 0)
if (list.Count() == 0 && room.Status == 0)
{
RoomList.Remove(room);
}
@ -530,7 +581,6 @@ namespace OnlineUserPool.ViewModels
}
SendGameRoomMessage(room);
}
}
private string GameRoomMessageHandler(HostModel item)
@ -539,8 +589,6 @@ namespace OnlineUserPool.ViewModels
var G = "";
if (!string.IsNullOrEmpty(item.Request))
{
//var resultList = WebService.GetGameRoomList(0, 1000);
//List / 0 / 6 /
var param = item.Request.Split('/');
var pageInfo = param[0];
if (pageInfo.Equals("List"))
@ -549,14 +597,25 @@ namespace OnlineUserPool.ViewModels
var pageSize = Convert.ToInt32(param[2]);
var querName = param[3].ToString();
List<RoomModel> rooms;
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
{
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)
{
string ss = "";
@ -568,17 +627,7 @@ namespace OnlineUserPool.ViewModels
}
}
}
////如果当前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}";
return G;
}
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 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}"));
//TODO:去重
var e = string.Join("|", list.Where(c => c.RoomId > 0).Select(c => $"{c.MemberId},{c.RoomId},{c.FrameRate},{c.TotalTicks}"));
var strV21 = $"*l{{{ temp }}};w{{{ watchList1 }}};e{{{ e }}};#";
ddd = new Data1(strV21);
}
@ -798,7 +846,7 @@ namespace OnlineUserPool.ViewModels
private void GameRoomDisConnectHandler(HostModel client)
{
//如果是在对战房间或者对战列表掉线执行,骑行阶段断开连接不移除房间信息
if ( !string.IsNullOrEmpty(client.Model) && !client.Model.Equals("GameRoom"))
if (client.Model != null && !client.Model.Equals("GameRoom"))
return;
RoomList.ToList().ForEach(o => {
@ -808,7 +856,7 @@ namespace OnlineUserPool.ViewModels
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)
{
RoomList.Remove(item);