添加RoomList

This commit is contained in:
Mili 2023-03-11 21:07:34 +08:00
parent 686c67dc58
commit 59d70fb3a0
6 changed files with 96 additions and 11 deletions

View File

@ -1,28 +1,53 @@
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Interface.Entity;
using Milimoe.FunGame.Core.Library.Constant;
using System.Collections;
namespace Milimoe.FunGame.Core.Entity
{
public class Room
public class Room : BaseEntity
{
public int Id { get; set; }
public string Roomid { get; set; } = "";
public DateTime Time { get; set; } = DateTime.Now;
public Hashtable PlayerList { get; set; } = new Hashtable();
public User? RoomMaster { get; set; }
public RoomType RoomType { get; set; }
public RoomState RoomState { get; set; }
public bool HasPass
{
get
{
if (RoomType == RoomType.MixHasPass || RoomType == RoomType.TeamHasPass)
return true;
else return false;
}
}
public string Password { get; set; } = "";
public GameStatistics? Statistics { get; set; } = null;
internal Room(User? master = null)
{
if (master != null) RoomMaster = master;
}
internal Room(string roomid, User? master = null)
{
Roomid = roomid;
if (master != null) RoomMaster = master;
}
public bool Equals(Room other)
{
return other.Id == Id;
}
public override IEnumerator<Room> GetEnumerator()
{
return GetEnumerator();
}
public override bool Equals(IBaseEntity? other)
{
return Equals(other);
}
}
}

View File

@ -23,12 +23,13 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
private bool _Receiving;
private string _ClientName;
public ClientSocket(System.Net.Sockets.Socket Instance, int ServerPort, string ClientIP, string ClientName)
public ClientSocket(System.Net.Sockets.Socket Instance, int ServerPort, string ClientIP, string ClientName, Guid Token)
{
this.Instance= Instance;
this.ServerPort = ServerPort;
this.ClientIP = ClientIP;
this._ClientName = ClientName;
this.Token = Token;
}
public void Close()

View File

@ -38,14 +38,14 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
else throw new SocketCreateListenException();
}
public ClientSocket Accept()
public ClientSocket Accept(Guid Token)
{
object[] result = SocketManager.Accept();
if (result != null && result.Length == 2)
{
string ClientIP = (string)result[0];
System.Net.Sockets.Socket Client = (System.Net.Sockets.Socket)result[1];
return new ClientSocket(Client, ServerPort, ClientIP, ClientIP);
return new ClientSocket(Client, ServerPort, ClientIP, ClientIP, Token);
}
throw new SocketGetClientException();
}

View File

@ -0,0 +1,45 @@
using System.Collections;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Common.Network;
namespace Milimoe.FunGame.Core.Library.Server
{
public class RoomList
{
public ServerSocket Server => _Server;
public int Count => _List.Count;
private readonly ServerSocket _Server;
private readonly Hashtable _List = new();
public RoomList(ServerSocket Server)
{
_Server = Server;
}
public List<Room> GetList()
{
return _List.Values.Cast<Room>().ToList();
}
public void AddRoom(Room Room)
{
_List.Add(Room.Name, Room);
}
public void RemoveRoom(Room Room)
{
_List.Remove(Room.Name);
}
public Room? GetRoom(string RoomID)
{
Room? room = null;
if (_List.ContainsKey(RoomID))
{
room = (Room?)_List[RoomID];
}
return room;
}
}
}

View File

@ -155,7 +155,7 @@ namespace Milimoe.FunGame.Core.Service
/// <summary>
/// 用于客户端接收服务器信息
/// </summary>
/// <returns>通信类型[0]和参数[1]</returns>
/// <returns>SocketObject</returns>
internal static Library.Common.Network.SocketObject Receive()
{
Library.Common.Network.SocketObject result = default;
@ -184,7 +184,7 @@ namespace Milimoe.FunGame.Core.Service
/// 用于服务器接收客户端信息
/// </summary>
/// <param name="ClientSocket">客户端Socket</param>
/// <returns>通信类型[0]、Token[1]和参数[2]</returns>
/// <returns>SocketObject</returns>
internal static Library.Common.Network.SocketObject Receive(Socket ClientSocket)
{
Library.Common.Network.SocketObject result = default;

View File

@ -137,6 +137,11 @@ namespace Milimoe.FunGame.Desktop.Model
}
}
}
else
{
Config.FunGame_isRetrying = false;
Socket.Close();
}
});
return ConnectResult.Success;
}
@ -238,7 +243,7 @@ namespace Milimoe.FunGame.Desktop.Model
switch (type)
{
case SocketMessageType.Connect:
SocketHandler_Connect(ServerMessage);
if (!SocketHandler_Connect(ServerMessage)) return SocketMessageType.Unknown;
break;
case SocketMessageType.Disconnect:
@ -267,12 +272,20 @@ namespace Milimoe.FunGame.Desktop.Model
#region SocketHandler
private void SocketHandler_Connect(SocketObject ServerMessage)
private bool SocketHandler_Connect(SocketObject ServerMessage)
{
string msg = "";
Guid token = Guid.Empty;
if (ServerMessage.Parameters.Length > 0) msg = ServerMessage.GetParam<string>(0)!;
string[] strings = msg.Split(';');
if (strings.Length != 2)
{
// 服务器拒绝连接
msg = strings[0];
Main.GetMessage(msg);
ShowMessage.ErrorMessage(msg);
return false;
}
string ServerName = strings[0];
string ServerNotice = strings[1];
Config.FunGame_ServerName = ServerName;
@ -283,6 +296,7 @@ namespace Milimoe.FunGame.Desktop.Model
Main.GetMessage($"已连接服务器:{ServerName}。\n\n********** 服务器公告 **********\n\n{ServerNotice}\n\n");
// 设置等待登录的黄灯
Main.UpdateUI(MainInvokeType.WaitLoginAndSetYellow);
return true;
}
private void SocketHandler_Disconnect(SocketObject ServerMessage)