milimoe c1bfd1e106
Add forget password 第一部分 (#12)
* 添加找回密码,首次尝试使用DataRequest

* 跟随Core修改

* 完成ForgetPassword 第一部分

* Remove Newtonsoft.Json

* 跟随Core工厂方法的改动
2023-06-14 09:20:29 +08:00

345 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Common.Architecture;
using Milimoe.FunGame.Core.Library.Common.Network;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.Exception;
using Milimoe.FunGame.Desktop.Library;
using Milimoe.FunGame.Desktop.Library.Component;
using Milimoe.FunGame.Desktop.UI;
namespace Milimoe.FunGame.Desktop.Model
{
public class MainModel : BaseModel
{
private readonly Main Main;
private readonly Core.Model.Session Usercfg = RunTime.Session;
public MainModel(Main main) : base(RunTime.Socket)
{
Main = main;
}
#region
public async Task<bool> LogOut()
{
try
{
// 需要当时登录给的Key发回去确定是账号本人在操作才允许登出
if (Usercfg.LoginKey != Guid.Empty)
{
SetWorking();
if (RunTime.Socket?.Send(SocketMessageType.Logout, Usercfg.LoginKey) == SocketResult.Success)
{
string msg = "";
Guid key = Guid.Empty;
(msg, key) = await Task.Factory.StartNew(SocketHandler_LogOut);
if (key == Usercfg.LoginKey)
{
Usercfg.LoginKey = Guid.Empty;
Main.UpdateUI(MainInvokeType.LogOut, msg ?? "");
return true;
}
}
}
throw new CanNotLogOutException();
}
catch (Exception e)
{
ShowMessage.ErrorMessage("无法登出您的账号,请联系服务器管理员。", "登出失败", 5);
Main.GetMessage(e.GetErrorInfo());
}
return false;
}
public async Task<bool> IntoRoom(Room room)
{
try
{
SetWorking();
if (RunTime.Socket?.Send(SocketMessageType.IntoRoom, room.Roomid) == SocketResult.Success)
{
string roomid = await Task.Factory.StartNew(SocketHandler_IntoRoom);
if (roomid.Trim() != "" && roomid == "-1")
{
Main.GetMessage($"已连接至公共聊天室。");
}
else
{
Usercfg.InRoom = room;
}
return true;
}
throw new CanNotIntoRoomException();
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
return false;
}
}
public async Task<bool> UpdateRoom()
{
try
{
SetWorking();
if (RunTime.Socket?.Send(SocketMessageType.UpdateRoom) == SocketResult.Success)
{
List<Room> list = await Task.Factory.StartNew(SocketHandler_UpdateRoom);
Main.UpdateUI(MainInvokeType.UpdateRoom, list);
return true;
}
throw new GetRoomListException();
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
return false;
}
}
public async Task<int> GetRoomPlayerCount(string roomid)
{
try
{
SetWorking();
if (RunTime.Socket?.Send(SocketMessageType.GetRoomPlayerCount, roomid) == SocketResult.Success)
{
return await Task.Factory.StartNew(SocketHandler_GetRoomPlayerCount);
}
return 0;
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
return 0;
}
}
public async Task<bool> QuitRoom(string roomid, bool isMaster)
{
bool result = false;
try
{
SetWorking();
if (RunTime.Socket?.Send(SocketMessageType.QuitRoom, roomid, isMaster) == SocketResult.Success)
{
result = await Task.Factory.StartNew(SocketHandler_QuitRoom);
if (result)
{
Usercfg.InRoom = General.HallInstance;
return result;
}
}
throw new QuitRoomException();
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
return result;
}
}
public async Task<string> CreateRoom(string RoomType, string Password = "")
{
try
{
SetWorking();
if (RunTime.Socket?.Send(SocketMessageType.CreateRoom, RoomType, Usercfg.LoginUser.Id, Password) == SocketResult.Success)
{
string roomid = await Task.Factory.StartNew(SocketHandler_CreateRoom);
if (roomid.Trim() != "")
{
return roomid;
}
}
throw new CreateRoomException();
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
return "";
}
}
public bool Chat(string msg)
{
try
{
if (RunTime.Socket?.Send(SocketMessageType.Chat, msg) == SocketResult.Success)
{
return true;
}
throw new CanNotSendTalkException();
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
return false;
}
}
public override void SocketHandler(SocketObject SocketObject)
{
try
{
// 定义接收的通信类型
SocketMessageType[] SocketMessageTypes = new SocketMessageType[] { SocketMessageType.GetNotice, SocketMessageType.Logout, SocketMessageType.IntoRoom, SocketMessageType.QuitRoom,
SocketMessageType.Chat, SocketMessageType.UpdateRoom, SocketMessageType.CreateRoom };
if (SocketObject.SocketType == SocketMessageType.HeartBeat)
{
// 心跳包单独处理
if ((RunTime.Socket?.Connected ?? false) && Usercfg.LoginUser.Id != 0)
Main.UpdateUI(MainInvokeType.SetGreenAndPing);
}
else if (SocketObject.SocketType == SocketMessageType.ForceLogout)
{
// 服务器强制下线登录
Guid key = Guid.Empty;
string? msg = "";
if (SocketObject.Length > 0) key = SocketObject.GetParam<Guid>(0);
if (SocketObject.Length > 1) msg = SocketObject.GetParam<string>(1);
msg ??= "";
if (key == Usercfg.LoginKey)
{
Usercfg.LoginKey = Guid.Empty;
Main.UpdateUI(MainInvokeType.LogOut, msg ?? "");
}
}
else if (SocketObject.SocketType == SocketMessageType.Chat)
{
// 收到房间聊天信息
string? user = "", msg = "";
if (SocketObject.Length > 0) user = SocketObject.GetParam<string>(0);
if (SocketObject.Length > 1) msg = SocketObject.GetParam<string>(1);
if (user != Usercfg.LoginUserName)
{
Main.GetMessage(msg, TimeType.None);
}
}
else if (SocketObject.SocketType == SocketMessageType.UpdateRoomMaster)
{
// 收到房间更换房主的信息
User user = General.UnknownUserInstance;
Room room = General.HallInstance;
if (SocketObject.Length > 0) user = SocketObject.GetParam<User>(0) ?? General.UnknownUserInstance;
if (SocketObject.Length > 1) room = SocketObject.GetParam<Room>(1) ?? General.HallInstance;
if (room.Roomid != "-1" && room.Roomid == Usercfg.InRoom.Roomid) Main.UpdateUI(MainInvokeType.UpdateRoomMaster, room);
}
else if (SocketMessageTypes.Contains(SocketObject.SocketType))
{
Work = SocketObject;
Working = false;
}
}
catch (Exception e)
{
RunTime.Controller?.Error(e);
}
}
#endregion
#region SocketHandler
private (string, Guid) SocketHandler_LogOut()
{
string? msg = "";
Guid key = Guid.Empty;
try
{
WaitForWorkDone();
// 返回一个Key如果这个Key是空的登出失败
if (Work.Length > 0) key = Work.GetParam<Guid>(0);
if (Work.Length > 1) msg = Work.GetParam<string>(1);
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
}
msg ??= "";
return (msg, key);
}
private string SocketHandler_IntoRoom()
{
string? roomid = "";
try
{
WaitForWorkDone();
if (Work.Length > 0) roomid = Work.GetParam<string>(0);
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
}
roomid ??= "";
return roomid;
}
private string SocketHandler_CreateRoom()
{
string? roomid = "";
try
{
WaitForWorkDone();
if (Work.Length > 0) roomid = Work.GetParam<string>(0);
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
}
roomid ??= "";
return roomid;
}
private bool SocketHandler_QuitRoom()
{
bool result = false;
try
{
WaitForWorkDone();
if (Work.Length > 0) result = Work.GetParam<bool>(0);
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
}
return result;
}
private int SocketHandler_GetRoomPlayerCount()
{
int count = 0;
try
{
WaitForWorkDone();
if (Work.Length > 0) count = Work.GetParam<int>(0);
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
}
return count;
}
private List<Room> SocketHandler_UpdateRoom()
{
List<Room> list = new();
try
{
WaitForWorkDone();
if (Work.Length > 0) list = Work.GetParam<List<Room>>(0) ?? new();
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo());
}
return list;
}
#endregion
}
}