public static void AddAttr(this RoleDbInfo self, AttrType t, string v) { self.AttrStrs.Add(new AttrStr() { K = (int)t, V = v }); }
public static void AddAttr(this RoleDbInfo self, AttrType t, long v) { self.AttrInts.Add(new AttrInt() { K = (int)t, V = v }); }
public static void GetRoleDbInfo(this AttributeComponent self, RoleDbInfo data) { data.AddAttr(AttrType.Level, self.Level); data.AddAttr(AttrType.Name, self.Name); data.AddAttr(AttrType.Exp, self.Exp); data.AddAttr(AttrType.Gold, self.Gold); data.AddAttr(AttrType.Atk, self.Atk); }
public static async Task <bool> InitDataSync(this PlayerDbComponent self) { RoleDbInfo dbInfo = await Game.Scene.GetComponent <SqlComponent>().GetUserDbInfo(self.AccountId); var unit = self.GetParent <Model.Fishs.Entitys.Unit>(); if (unit != null) { unit.AddComponent <AttributeComponent, RoleDbInfo>(dbInfo); } else { Log.Error("InitDataAsync Parent 不存在"); return(false); } self.UpdateFrameAsync(); return(true); }
public async static Task <RoleDbInfo> GetUserDbInfo(this SqlComponent self, int UserId) { string sql = "select * from User_Info where UserId = @UserId"; using (var db = self.GetDBConnection()) { var ret = await db.QueryFirstOrDefaultAsync <UserInfo>(sql, new { UserId }); if (ret == null) { return(new RoleDbInfo()); } else { RoleDbInfo role = RoleDbInfo.Parser.ParseFrom(ret.UserData); return(role); } } }
public static void Awake(this AttributeComponent self, RoleDbInfo data) { if (data.AttrInts.Count == 0) { self.Name = "刘杰"; } else { //初始化数据 foreach (var item in data.AttrInts) { self.InitAttrInt(item); } foreach (var item in data.AttrStrs) { self.InitAttrStr(item); } } self.UpdateAttrAsync(); }
public static async void UpdateFrameAsync(this PlayerDbComponent self) { TimerComponent timerComponent = Game.Scene.GetComponent <TimerComponent>(); long instanceId = self.InstanceId; while (true) { await timerComponent.WaitAsync(10000); if (self.InstanceId != instanceId) { return; } //Log.Debug("保存"); RoleDbInfo roledb = new RoleDbInfo(); self.GetParent <Model.Fishs.Entitys.Unit>().GetComponent <AttributeComponent>().GetRoleDbInfo(roledb); // await Game.Scene.GetComponent <SqlComponent>().SaveUserDbInfo(self.AccountId, roledb); } }
public async static Task <bool> SaveUserDbInfo(this SqlComponent self, int UserId, RoleDbInfo data) { string sql = "Update User_Info Set UserData=@UserData where UserId = @UserId"; using (var db = self.GetDBConnection()) { var param = new { UserData = data.ToByteArray(), UserId = UserId }; var row = await db.ExecuteAsync(sql, param); if (row == 0) { sql = "Insert User_Info (UserId,UserData)values(@UserId,@UserData)"; row = await db.ExecuteAsync(sql, param); return(row == 1); } else { return(true); } } }