上一篇 [7] ET框架初養成 mac OS 實現[登錄並註冊] DB存儲 - 安裝MongoDB數據庫
下一篇 還沒寫
上一篇安裝好MongoDB了 我們來使用可視化工具創建數據庫
創建數據庫連接
創建名字爲ETdata的數據庫
我們回到Unity 點擊菜單 Tools -> 命令行配置
配置如下:
Connection:
mongodb://127.0.0.1:27017/
DBName:
ETdata
好了 這就配置好了 服務器 接下來我們去創建我們要保存的實體類
在這個目錄下創建兩個實體 /ET/Server/Hotfix/Module/Demo/Entity
AccountInfo 賬號信息實體 保存賬號密碼
using ETModel;
using MongoDB.Bson.Serialization.Attributes;
namespace ETHotfix
{
[BsonIgnoreExtraElements]
public class AccountInfo : Entity
{
public string Account { get; set; }
public string Password { get; set; }
}
}
UserInfo 用戶信息實體 保存玩家暱稱 金幣數量
using ETModel;
using MongoDB.Bson.Serialization.Attributes;
namespace ETHotfix
{
[BsonIgnoreExtraElements]
public class UserInfo : Entity
{
public string Nickname { get; set; }
public int Gold { get; set; }
}
}
我們來實現 C2R_RegisterAndLoginHandler 的數據庫查詢和保存
using System;
using System.Collections.Generic;
using System.Net;
using ETModel;
namespace ETHotfix
{
[MessageHandler(AppType.Realm)]
public class C2R_RegisterAndLoginHandler : AMRpcHandler<C2R_RegisterAndLogin, R2C_RegisterAndLogin>
{
protected override void Run(Session session, C2R_RegisterAndLogin message, Action<R2C_RegisterAndLogin> reply)
{
RunAsync(session, message, reply).Coroutine();
}
private async ETVoid RunAsync(Session session, C2R_RegisterAndLogin message, Action<R2C_RegisterAndLogin> reply)
{
Console.WriteLine(" --> C2R_RegisterAndLoginHandler");
R2C_RegisterAndLogin response = new R2C_RegisterAndLogin();
try
{
Console.WriteLine("account : "+ message.Account+" password : "+ message.Password);
//數據庫操作對象
{
DBProxyComponent dbProxy = Game.Scene.GetComponent<DBProxyComponent>();
Console.WriteLine("dbProxy : " + dbProxy);
//查詢賬號是否存在 使用LINQ和Lambda表達式根據特定條件來查詢
List<ComponentWithId> result = await dbProxy.Query<AccountInfo>(_account => _account.Account == message.Account);
Console.WriteLine("result : " + result);
if (result.Count > 0)
{
Console.WriteLine("result.Count : " + result.Count);
response.Error = ETHotfixErrorCode.ERR_AccountAlreadyRegister;
reply(response);
return;
}
Console.WriteLine("新建賬號 : ");
//新建賬號
AccountInfo newAccount = ComponentFactory.CreateWithId<AccountInfo>(IdGenerater.GenerateId());
newAccount.Account = message.Account;
newAccount.Password = message.Password;
Log.Info($"註冊新賬號:{MongoHelper.ToJson(newAccount)}");
//新建用戶信息
UserInfo newUser = ComponentFactory.CreateWithId<UserInfo>(newAccount.Id);
newUser.Nickname = $"用戶{message.Account}";
newUser.Gold = 10000;
//保存到數據庫
await dbProxy.Save(newAccount);
await dbProxy.Save(newUser);
//釋放數據庫連接
dbProxy.Dispose();
}
// 隨機分配一個Gate
StartConfig config = Game.Scene.GetComponent<RealmGateAddressComponent>().GetAddress();
//Log.Debug($"gate address: {MongoHelper.ToJson(config)}");
IPEndPoint innerAddress = config.GetComponent<InnerConfig>().IPEndPoint;
Session gateSession = Game.Scene.GetComponent<NetInnerComponent>().Get(innerAddress);
// 向gate請求一個key,客戶端可以拿着這個key連接gate
G2R_GetLoginKey g2RGetLoginKey = (G2R_GetLoginKey)await gateSession.Call(new R2G_GetLoginKey() {Account = message.Account});
string outerAddress = config.GetComponent<OuterConfig>().Address2;
response.Address = outerAddress;
response.Key = g2RGetLoginKey.Key;
reply(response);
}
catch (Exception e)
{
ReplyError(response, e, reply);
}
}
}
}
我們單獨把DB操作拿出來講
數據庫操作組件:
DBProxyComponent dbProxy = Game.Scene.GetComponent<DBProxyComponent>();
向數據庫查詢數據:(使用LINQ和Lambda表達式根據特定條件來查詢)
List<ComponentWithId> result = await dbProxy.Query<AccountInfo>(_account => _account.Account == message.Account);
如果 result.Count > 0 表示數據庫中存在這個賬號 ,返回錯誤碼 賬號已存在(錯誤碼自己去ErrorCode添加)
response.Error = ETHotfixErrorCode.ERR_AccountAlreadyRegister;
reply(response);
創建賬號信息實體 通過ID生成器創建的ID生成賬戶唯一ID ,然後設置賬號密碼
AccountInfo newAccount = ComponentFactory.CreateWithId<AccountInfo>(IdGenerater.GenerateId());
newAccount.Account = message.Account;
newAccount.Password = message.Password;
創建用戶信息 ID使用我們的賬號唯一ID,初始贈送10000金幣
UserInfo newUser = ComponentFactory.CreateWithId<UserInfo>(newAccount.Id);
newUser.Nickname = $"用戶{message.Account}";
newUser.Gold = 10000;
最後保存到數據庫
await dbProxy.Save(newAccount);
await dbProxy.Save(newUser);
最後的最後 釋放數據庫連接 ! 必做
dbProxy.Dispose();
持久化的代碼寫到這裏就結束了 各位快去Unity跑跑看結果吧