// POST: api/Account public RetValue Post([FromBody] string value) { RetValue ret = new RetValue(); JsonData jsonData = JsonMapper.ToObject(value); //1.验证时间戳 //2.验证签名 int type = Convert.ToInt32(jsonData["Type"].ToString()); string userName = jsonData["UserName"].ToString(); string pwd = jsonData["Pwd"].ToString(); //客户端时间戳 long t = Convert.ToInt64(jsonData["t"].ToString()); //签名 string sign = jsonData["sign"].ToString(); //客户端驱动id string deviceIdentifier = jsonData["deviceIdentifier"].ToString(); string deviceModel = jsonData["DeviceModel"].ToString(); long st = MFDSAUtil.GetTimestamp(); //第一重检验 //if (st - t > 300) //{ // ret.IsError = true; // ret.ErrorMsg = "请求失败"; // return ret; //} //第二重检验 if (string.Format("{0},{1}", deviceIdentifier, t) != sign) { ret.IsError = true; ret.ErrorMsg = "请求失败"; return(ret); } if (type == 0) { string ChannelId = JsonMapper.ToObject(value)["Channelld"].ToString(); MFReturnValue <int> ret2 = AccountCacheModel.Instance.Register(userName, pwd, ChannelId, deviceIdentifier, deviceModel); ret.IsError = ret2.HasError; ret.ErrorMsg = ret2.Message; AccountEntity entity = AccountCacheModel.Instance.GetEntity(ret2.Value); ret.RetData = JsonMapper.ToJson(new RetAccountEntity(entity)); } else { AccountEntity entity = AccountCacheModel.Instance.LogOn(userName, pwd, deviceIdentifier, deviceModel); if (entity != null) { ret.IsError = false; ret.RetData = entity.Id; } else { ret.IsError = true; ret.ErrorMsg = "账号或者密码错误"; } RetAccountEntity retEnitity = new RetAccountEntity(entity); ret.RetData = JsonMapper.ToJson(retEnitity); } return(ret); }
public List <GameServerOnePageResponseProto.GameServerOnePageItem> GetGameServerOnePageList(int page) { List <GameServerOnePageResponseProto.GameServerOnePageItem> retList = new List <GameServerOnePageResponseProto.GameServerOnePageItem>(); //List<GameServerEntity> gameServerOnePageList = new List<GameServerEntity>(); MFReturnValue <List <GameServerEntity> > retValue = GetPageList(isDesc: false, pageSize: 10, pageIndex: page, isAutoStatus: false); if (!retValue.HasError) { List <GameServerEntity> list = retValue.Value; for (int i = 0; i < list.Count; i++) { GameServerOnePageResponseProto.GameServerOnePageItem item = new GameServerOnePageResponseProto.GameServerOnePageItem(); item.ServerId = (int)list[i].Id; item.Status = (int)list[i].Status; item.RunState = list[i].RunStatus; item.IsCommand = list[i].IsCommand; item.IsNew = list[i].IsNew; item.Name = list[i].Name; item.Ip = list[i].Ip; item.Port = list[i].Port; retList.Add(item); } } return(retList); }
/// <summary> /// 更新对象 存在事务中 命令自己写 /// </summary> /// <param name="transaction"></param> /// <param name="setStr"></param> /// <param name="conditionStr"></param> /// <param name="parameterDic"></param> /// <returns></returns> public virtual MFReturnValue <object> Update(SqlTransaction transaction, string setStr, string conditionStr, IDictionary <string, object> parameterDic) { MFReturnValue <object> retValue = new MFReturnValue <object>(); try { SqlParameter[] paramArray = new SqlParameter[parameterDic.Count]; int index = 0; foreach (var item in parameterDic) { paramArray[index] = new SqlParameter(item.Key, item.Value); index++; } string commandText = string.Format("UPDATE {0} SET {1} WHERE {2}", this.TableName, setStr, conditionStr); if (transaction != null) { MFSqlHelper.ExecuteNonQuery(transaction, CommandType.Text, commandText, paramArray); } else { MFSqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.Text, commandText, paramArray); } } catch (Exception e) { retValue.HasError = true; retValue.Message = e.Message; } return(retValue); }
/// <summary> /// 新增对象 /// </summary> /// <param name="trans">事物</param> /// <returns></returns> public MFReturnValue <object> Create(SqlTransaction trans, T entity) { SqlParameter[] paramArray = ValueParas(entity); paramArray[0].Direction = ParameterDirection.Output; paramArray[paramArray.Length - 2].Direction = ParameterDirection.Output; paramArray[paramArray.Length - 1].Direction = ParameterDirection.ReturnValue; if (trans.IsNullOrEmpty()) { MFSqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "{0}_Create".FormatWith(this.TableName), paramArray); } else { MFSqlHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, "{0}_Create".FormatWith(this.TableName), paramArray); } int nReturnCode = paramArray[paramArray.Length - 1].Value.ToInt(); MFReturnValue <object> val = new MFReturnValue <object>(); if (nReturnCode < 0) { val.HasError = true; } else { val.HasError = false; val.OutputValues["Id"] = paramArray[0].Value.ToInt(); } val.Message = paramArray[paramArray.Length - 2].Value.ObjectToString(); val.ReturnCode = nReturnCode; return(val); }
public RetAccountEntity(AccountEntity entity) { Id = entity.Id.Value; UserName = entity.UserName; YuanBao = entity.Money; LastServerId = entity.LastLogOnServerId; LastServerName = entity.LastLogOnServerName; if (LastServerId == 0) { MFReturnValue <List <GameServerEntity> > ret = GameServerCacheModel.Instance.GetPageList(isDesc: true, pageSize: 1); if (!ret.HasError) { List <GameServerEntity> lst = ret.Value; if (lst != null && lst.Count > 0) { LastServerId = lst[0].Id.Value; LastServerName = lst[0].Name; LastServerIP = lst[0].Ip; LastServerPort = lst[0].Port; LastServerRunStatus = lst[0].RunStatus; } } } else { GameServerEntity entityGameServer = GameServerCacheModel.Instance.GetEntity(LastServerId); if (entityGameServer != null) { LastServerIP = entityGameServer.Ip; LastServerPort = entityGameServer.Port; LastServerRunStatus = entityGameServer.RunStatus; } } }
public List <RetGameserverEntity> GetGameServerList(int pageIndex) { MFReturnValue <List <GameServerEntity> > list = null; List <RetGameserverEntity> retList = new List <RetGameserverEntity>(); if (pageIndex == 0) { //推荐服务器 玩家所在服务器 list = this.GetPageList(pageSize: 3, isDesc: false); } else { list = this.GetPageList(pageSize: 10, pageIndex: pageIndex, isDesc: false); } if (list != null) { for (int i = 0; i < list.Value.Count; i++) { retList.Add(new RetGameserverEntity() { Id = list.Value[i].Id.Value, RunSatus = list.Value[i].RunSatus, IsCommand = list.Value[i].IsCommand, IsNew = list.Value[i].IsNew, Name = list.Value[i].Name, Ip = list.Value[i].Ip, Port = list.Value[i].Port }); } } return(retList); }
/// <summary> /// 更新对象 /// </summary> /// <param name="trans">事务</param> /// <param name="setStr"></param> /// <param name="conditionStr"></param> /// <param name="parameters"></param> /// <returns></returns> public virtual MFReturnValue <object> Update(SqlTransaction trans, string setStr, string conditionStr, IDictionary <string, object> parameters) { MFReturnValue <object> retValue = new MFReturnValue <object>(); try { SqlParameter[] paramArray = new SqlParameter[parameters.Count]; int i = 0; foreach (KeyValuePair <string, object> parameter in parameters) { paramArray[i] = new SqlParameter(parameter.Key, parameter.Value); i++; } string sql = string.Format("UPDATE [{0}] SET {1} WHERE {2}", this.TableName, setStr, conditionStr); if (trans != null) { MFSqlHelper.ExecuteNonQuery(trans, CommandType.Text, sql, paramArray); } else { MFSqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.Text, sql, paramArray); } } catch (Exception ex) { retValue.HasError = true; retValue.Message = ex.Message; } return(retValue); }
/// <summary> /// 批量删除 /// </summary> /// <param name="ids">,隔开的编号</param> /// <returns></returns> public MFReturnValue <object> Delete(string ids) { MFReturnValue <object> val = new MFReturnValue <object>(); using (SqlConnection conn = new SqlConnection(this.ConnectionString)) { conn.Open(); SqlTransaction trans = conn.BeginTransaction(); try { IList <string> lst = ids.ToList(); lst.ForEach(id => { Delete(id.ToInt(), trans); }); trans.Commit(); val.HasError = false; val.Message = "批量删除成功"; } catch { trans.Rollback(); val.HasError = false; val.Message = "批量删除失败"; } } return(val); }
/// <summary> /// 批量删除 /// </summary> /// <param name="ids">,隔开编号</param> /// <returns></returns> public virtual MFReturnValue <object> Delete(string ids) { MFReturnValue <object> retValue = new MFReturnValue <object>(); using (SqlConnection conn = new SqlConnection()) { conn.Open(); SqlTransaction trans = conn.BeginTransaction(); try { IList <string> list = ids.ToList(','); foreach (var item in list) { Delete(item.ToInt(), trans); } trans.Commit(); retValue.HasError = false; retValue.Message = "批量上传成功"; } catch (Exception e) { trans.Rollback(); retValue.HasError = true; retValue.Message = e.Message; } } return(retValue); }
/// <summary> 服务器返回创建角色 </summary> /// <param name="role"></param> /// <param name="retValue"></param> private void OnRoleCreateResponse(Role role, MFReturnValue <object> retValue) { RoleCreateResponseProto proto = new RoleCreateResponseProto(); if (!retValue.HasError) { proto.IsSuccess = true; //初始化角色技能 RoleSkillEntity entity1 = new RoleSkillEntity(); entity1.Status = EnumEntityStatus.Released; entity1.RoleId = (int)retValue.OutputValues["Id"]; entity1.SkillId = 106; entity1.SkillLevel = 1; entity1.SlotsNode = 1; entity1.CreateTime = DateTime.Now; entity1.UpdateTime = DateTime.Now; RoleSkillDBModel.Instance.Create(entity1); RoleSkillEntity entity2 = new RoleSkillEntity(); entity2.Status = EnumEntityStatus.Released; entity2.RoleId = (int)retValue.OutputValues["Id"]; entity2.SkillId = 107; entity2.SkillLevel = 1; entity2.SlotsNode = 2; entity2.CreateTime = DateTime.Now; entity2.UpdateTime = DateTime.Now; RoleSkillDBModel.Instance.Create(entity2); } else { proto.IsSuccess = false; proto.MsgCode = 1000305; } role.ClientSocket.SendMsg(proto.ToArray()); }
public List <RetGameServerEntity> GetGameServerList(int pageIndex, string condition) { List <RetGameServerEntity> retList = new List <RetGameServerEntity>(); if (pageIndex == 0) { //取推荐服务器 //1、新区 2、玩家有账户的区 //临时的方案 返回最新的前三个 MFReturnValue <List <GameServerEntity> > retValue = this.GetPageList(condition: condition, pageSize: 3, pageIndex: 1); if (!retValue.HasError) { List <GameServerEntity> lst = retValue.Value; for (int i = 0; i < lst.Count; i++) { retList.Add(new RetGameServerEntity() { Id = lst[i].Id.Value, RunStatus = lst[i].RunStatus, IsCommand = lst[i].IsCommand, IsNew = lst[i].IsNew, Name = lst[i].Name, Ip = lst[i].Ip, Port = lst[i].Port, }); } } } else { MFReturnValue <List <GameServerEntity> > retValue = this.GetPageList(condition: condition, pageSize: 10, pageIndex: pageIndex, isDesc: false); if (!retValue.HasError) { List <GameServerEntity> lst = retValue.Value; for (int i = 0; i < lst.Count; i++) { retList.Add(new RetGameServerEntity() { Id = lst[i].Id.Value, RunStatus = lst[i].RunStatus, IsCommand = lst[i].IsCommand, IsNew = lst[i].IsNew, Name = lst[i].Name, Ip = lst[i].Ip, Port = lst[i].Port, }); } } } return(retList.OrderByDescending(p => p.Id).ToList()); }
/// <summary> /// 玩家注册 /// </summary> /// <param name="userName"></param> /// <param name="pwd"></param> /// <param name="channelId">渠道号</param> /// <returns></returns> public MFReturnValue <int> Register(string userName, string pwd, string channelId, string deviceIdentifier, string deviceModel) { MFReturnValue <int> retValue = new MFReturnValue <int>(); //1、验证用户名是否存在 //2、如果不存在添加数据 using (SqlConnection conn = new SqlConnection(DBConn.DBAccount)) { conn.Open(); //开始事物的目的,只打开一次数据库 SqlTransaction trans = conn.BeginTransaction(); List <AccountEntity> lst = GetListWithTran(this.TableName, "Id", "UserName='******'", trans: trans, isAutoStatus: false); if (lst == null || lst.Count == 0) { //说明用户不存在 AccountEntity entity = new AccountEntity(); entity.Status = EnumEntityStatus.Released; entity.UserName = userName; entity.Pwd = MFEncryptUtil.Md5(pwd); entity.ChannelId = channelId; entity.LastLogOnServerTime = DateTime.Now; entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.DeviceIdentifier = deviceIdentifier; entity.DeviceModel = deviceModel; MFReturnValue <object> ret = this.Create(trans, entity); if (!ret.HasError) { retValue.Value = (int)ret.OutputValues["Id"]; trans.Commit(); } else { retValue.HasError = true; retValue.Message = "用户名已经存在"; trans.Rollback(); } } else { //说明存在 retValue.HasError = true; retValue.Message = "用户名已经存在"; } }; return(retValue); }
/// <summary> /// 返回分页集合 /// </summary> /// <param name="columns">列名</param> /// <param name="condition">查询条件</param> /// <param name="orderby">排序条件</param> /// <param name="isDesc">是否倒序</param> /// <param name="pageSize">每页数量</param> /// <param name="pageIndex">当前页码</param> /// <param name="isAutoStatus">是否自动状态</param> /// <param name="isEfficient">是否使用高效存储过程(高效存储过程要求排序字段只有一个 值类型 数据唯一)</param> /// <returns></returns> public MFReturnValue <List <T> > GetPageList(string columns = "*", string condition = "", string orderby = "Id", bool isDesc = true, int?pageSize = 20, int?pageIndex = 1, bool isAutoStatus = true, bool isEfficient = false) { if (isAutoStatus && ColumnList.Contains("Status") && condition.IndexOf("Status") == -1) { var statuString = " Status = 1 "; condition = statuString + (condition.IsNullOrEmpty() ? string.Empty : " And ") + condition; } //非高效存储过程 并且 不包含排序关键字的时候 自动加排序关键字 if (orderby.IndexOf("asc") == -1 && orderby.IndexOf("desc") == -1 && orderby.IndexOf(",") == -1 && isEfficient == false) { orderby += isDesc ? " desc" : " asc"; } //表名 var tableParam = new SqlParameter("@TableName", this.TableName); //查询列 var colParam = new SqlParameter("@Fields", columns); //查询条件 var whereParam = new SqlParameter("@OrderField", orderby); //排序条件 var orderbyParam = new SqlParameter("@sqlWhere", condition); //分页条数 var pageSizeParam = new SqlParameter("@PageSize", pageSize); //当前页码 var pageIndexParam = new SqlParameter("@PageIndex", pageIndex); //总记录数 var pagesParam = new SqlParameter("@TotalCount", SqlDbType.Int); pagesParam.Direction = ParameterDirection.Output; //排序方式 var orderTypeParam = new SqlParameter("@OrderType", isDesc); SqlParameter[] paramArray = null; //高效存储过程多一个参数 if (isEfficient) { paramArray = new SqlParameter[] { tableParam, colParam, whereParam, orderbyParam, pageSizeParam, pageIndexParam, orderTypeParam, pagesParam }; } else { paramArray = new SqlParameter[] { tableParam, colParam, whereParam, orderbyParam, pageSizeParam, pageIndexParam, pagesParam }; } IDataReader reader = MFSqlHelper.ExecuteReader(this.ConnectionString, CommandType.StoredProcedure, isEfficient ? "GetPageList_Efficient" : "GetPageList", paramArray); var backVal = new MFReturnValue <List <T> >(); backVal.Value = GetEntityList(reader); backVal.OutputValues["TotalCount"] = Convert.ToInt32(pagesParam.Value); return(backVal); }
private void DeleteRoleCallBack(Role role, byte[] content) { RoleOperation_DeleteRoleProto proto = RoleOperation_DeleteRoleProto.ToProto(content); IDictionary <string, object> dic = new Dictionary <string, object>(); dic["Id"] = proto.RoleID; dic["Status"] = (byte)EnumEntityStatus.Deleted; dic["UpdateTime"] = DateTime.Now;; MFReturnValue <object> ret = RoleCacheModel.Instance.Update("[Status]=@Status,[UpdateTime]=@UpdateTime", "Id=@Id", dic); DeleteRoleReturn(role, ret); }
private void CreateRoleProtoCallBack(Role role, byte[] content) { RoleOperation_CreateRoleProto proto = RoleOperation_CreateRoleProto.ToProto(content); //查询是否又相同昵称的 int count = RoleCacheModel.Instance.GetCount(string.Format("[NickName] = '{0}'", proto.NickName)); MFReturnValue <object> ret = null; if (count == 0) { //创建 RoleEntity entity = new RoleEntity(); entity.JobId = proto.JobID; entity.NickName = proto.NickName; entity.Sex = (byte)EnumSex.Female; entity.Status = EnumEntityStatus.Released; entity.Level = 1; entity.AccountId = role.AccountId; entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.LastWorldMapSceneId = 1; //暂定 JobEntity jobEntity = JobDBModel.Instance.Get(proto.JobID); JobLevelEntity jobLevelEntity = JobLevelDBModel.Instance.Get(entity.Level); entity.MaxHP = jobLevelEntity.HP; entity.CurrentHP = entity.MaxHP; entity.MaxMP = jobLevelEntity.MP; entity.CurrentMP = entity.MaxMP; entity.Attack = (int)Math.Round(jobEntity.Attack * jobLevelEntity.Attack * 0.01f); entity.Defense = (int)Math.Round(jobEntity.Defense * jobLevelEntity.Defense * 0.01f); entity.Hit = (int)Math.Round(jobEntity.Hit * jobLevelEntity.Hit * 0.01f); entity.Dodge = (int)Math.Round(jobEntity.Dodge * jobLevelEntity.Dodge * 0.01f); entity.Cri = (int)Math.Round(jobEntity.Cri * jobLevelEntity.Cri * 0.01f); entity.Res = (int)Math.Round(jobEntity.Res * jobLevelEntity.Res * 0.01f); entity.Gold = 0; entity.Money = 0; entity.Exp = 0; entity.Fighting = entity.Attack * 4 + entity.Defense * 4 + entity.Hit * 2 + entity.Dodge * 2 + entity.Res + entity.Cri; ret = RoleCacheModel.Instance.Create(entity); role.CurRoleId = (int)ret.OutputValues["Id"]; } else { ret = new MFReturnValue <object>(); ret.HasError = true; ret.ReturnCode = 1000; } SelectRoleInfoReturn(role); CreateRoleProtoReturn(ret, role); SelectSkillReturn(role); }
public AccountRegisterResponseProto Register(string userName, string pwd, string chennelId, string deviceIdentifier, string deviceModel) { AccountRegisterResponseProto retProto = new AccountRegisterResponseProto(); //1.判断用户名是否存在 //2.如果存在添加数据 using (SqlConnection conn = new SqlConnection(DBConn.DBAccount)) { conn.Open(); //开启事务 只打开一次数据库 SqlTransaction trans = conn.BeginTransaction(); List <AccountEntity> list = GetListWithTran(this.TableName, "Id", "UserName='******'", trans: trans, isAutoStatus: false); Console.WriteLine(list.Count); //Console.WriteLine(list[0].Mobile + " " + list[0].Mail+" "+ list[0].Status); if (list == null || list.Count == 0) { //说明用户名不存在 可以添加数据 AccountEntity entity = new AccountEntity(); entity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; entity.UserName = userName; entity.Pwd = pwd; entity.ChannelId = chennelId; entity.LastLogOnServerTime = DateTime.Now; entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.DeviceIdentifier = deviceIdentifier; entity.DeviceModel = deviceModel; MFReturnValue <object> ret = this.Create(trans, entity); if (!ret.HasError) { retProto.IsSuccess = true; retProto.UserId = (int)ret.OutputValues["Id"]; trans.Commit(); } else { retProto.IsSuccess = false; retProto.MsgCode = 102; trans.Rollback(); } } else { retProto.IsSuccess = false; retProto.MsgCode = 102; } } return(retProto); }
private void DeleteRoleReturn(Role role, MFReturnValue <object> ret) { RoleOperation_DeleteRoleReturnProto proto = new RoleOperation_DeleteRoleReturnProto(); if (ret.HasError) { proto.IsSucess = false; } else { proto.IsSucess = true; } role.Client_Socket.SendMsg(proto.ToArray()); }
/// <summary> 客户端请求创建角色 </summary> /// <param name="role"></param> /// <param name="buffer"></param> private void OnRoleCreate(Role role, byte[] buffer) { RoleCreateRequestProto proto = RoleCreateRequestProto.GetProto(buffer); //查询昵称是否存在 int count = RoleCacheModel.Instance.GetCount(string.Format("[NickName]='{0}'", proto.RoleName)); MFReturnValue <object> retValue = null; if (count > 0) { //角色名已经存 retValue = new MFReturnValue <object>(); retValue.HasError = true; } else { RoleEntity entity = new RoleEntity(); entity.JobId = proto.JobId; entity.NickName = proto.RoleName; entity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; entity.AccountId = role.AccountId; entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.Level = 1; //给角色战斗相关的属性赋值(根据 职业 等级) JobEntity jobEntity = JobDBModel.Instance.Get(entity.JobId); JobLevelEntity jobLevelEntity = JobLevelDBModel.Instance.Get(entity.Level); entity.Attack = (int)Math.Round(jobLevelEntity.Attack * (jobEntity.AttackCoefficient * 0.01f)); entity.AttackAddition = 0; entity.Defense = (int)Math.Round(jobLevelEntity.Defense * jobEntity.DefenseCoefficient * 0.01f); entity.DefenseAddition = 0; entity.Res = (int)Math.Round(jobEntity.ResCoefficient * jobLevelEntity.Res * 0.01f); entity.ResAddition = 0; entity.Hit = (int)Math.Round(jobEntity.HitCoefficient * jobLevelEntity.Hit * 0.01f); entity.HitAddition = 0; entity.Cri = (int)Math.Round(jobEntity.CriCoefficient * jobLevelEntity.Cri * 0.01f); entity.CriAddition = 0; entity.Dodge = (int)Math.Round(jobEntity.DodgeCoefficient * jobLevelEntity.Dodge * 0.01f); entity.DodgeAddition = 0; entity.CurrHP = entity.MaxHP = jobLevelEntity.HP; entity.CurrMP = entity.MaxMP = jobLevelEntity.MP; entity.Fighting = FightingUtil.GetRoleFighting(entity.MaxHP, entity.Attack, 0, entity.Defense, 0, entity.Res, 0, entity.Hit, 0, entity.Dodge, 0, entity.Cri, 0); entity.FightingAddition = 0; entity.LastInWorldMapId = 1; retValue = RoleCacheModel.Instance.Create(entity); Console.WriteLine(retValue.ReturnCode + " " + retValue.OutputValues["Id"] + " " + retValue.Message); } OnRoleCreateResponse(role, retValue); }
/// <summary> /// 删除指定编号的对象 /// </summary> /// <param name="id">编号</param> /// <param name="trans">事物</param> /// <returns></returns> public MFReturnValue <object> Delete(int?id, SqlTransaction trans) { MFReturnValue <object> val = new MFReturnValue <object>(); int nId; if (id.HasValue) { nId = id.Value; } else { val.HasError = true; val.Message = "无效主键"; return(val); } SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter("@Id", nId), new SqlParameter("@RetMsg", SqlDbType.NVarChar, 255), new SqlParameter("@ReturnValue", SqlDbType.Int) }; paramArray[paramArray.Length - 2].Direction = ParameterDirection.Output; paramArray[paramArray.Length - 1].Direction = ParameterDirection.ReturnValue; if (trans.IsNullOrEmpty()) { MFSqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "{0}_Delete".FormatWith(this.TableName), paramArray); } else { MFSqlHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, "{0}_Delete".FormatWith(this.TableName), paramArray); } int nReturnCode = paramArray[paramArray.Length - 1].Value.ToInt(); if (nReturnCode < 0) { val.HasError = true; } else { val.HasError = false; } val.Message = paramArray[paramArray.Length - 2].Value.ObjectToString(); val.ReturnCode = nReturnCode; return(val); }
private void CreateRoleProtoReturn(MFReturnValue <object> ret, Role role) { RoleOperation_CreateRoleReturnProto proto = new RoleOperation_CreateRoleReturnProto(); if (ret.HasError) { proto.IsSucess = false; proto.MessageID = 100; } else { proto.IsSucess = true; } role.Client_Socket.SendMsg(proto.ToArray()); }
/// <summary> /// 删除 在事务中 /// </summary> /// <param name="id"></param> /// <param name="transaction"></param> /// <returns></returns> public virtual MFReturnValue <object> Delete(int?id, SqlTransaction transaction) { MFReturnValue <object> retValue = new MFReturnValue <object>(); int nId; if (id.HasValue) { nId = id.Value; } else { retValue.HasError = true; retValue.Message = "无效的主键"; return(retValue); } SqlParameter[] parameterArr = new SqlParameter[] { new SqlParameter("@Id", nId), new SqlParameter("@RetMsg", SqlDbType.NVarChar, 255), new SqlParameter("@ReturnValue", SqlDbType.Int) }; parameterArr[parameterArr.Length - 2].Direction = ParameterDirection.Output; parameterArr[parameterArr.Length - 1].Direction = ParameterDirection.ReturnValue; if (transaction.IsNullOrEmpty()) { MFSqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, string.Format("{0}_Delete", this.TableName), parameterArr); } else { MFSqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, String.Format("{0}_Delete", this.TableName), parameterArr); } int returnCode = parameterArr[parameterArr.Length - 1].Value.ToInt(); if (returnCode < 0) { retValue.HasError = true; } else { retValue.HasError = false; } return(retValue); }
public MFReturnValue <int> Register(string userName, string pwd, string channelID, string deviceIdentifier, string deviceModel) { MFReturnValue <int> ret1 = new MFReturnValue <int>(); using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=DBAccount;Integrated Security=True")) { conn.Open(); //1.查询是否存在相同的账号 //2.没有则注册 SqlTransaction trans = conn.BeginTransaction(); List <AccountEntity> list = this.GetListWithTran(this.TableName, "Id", "UserName='******'", trans: trans, isAutoStatus: false); if (list == null || list.Count == 0) { AccountEntity entity = new AccountEntity(); entity.UserName = userName; entity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; entity.Pwd = pwd; entity.Channelld = channelID; entity.LastLogOnServerTime = DateTime.Now; entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.DeviceIdentifier = deviceIdentifier; entity.DeviceModel = deviceModel; MFReturnValue <object> ret2 = this.Create(trans, entity); if (ret2.HasError) { ret1.HasError = true; ret1.Message = ret1.Message; trans.Rollback(); } else { ret1.HasError = false; ret1.Value = (int)ret2.OutputValues["Id"]; trans.Commit(); } } else { ret1.HasError = true; ret1.Message = "账号已经存在"; } return(ret1); } }
/// <summary> /// 获取用户信息 /// </summary> /// <param name="channelId"></param> /// <param name="access_token"></param> /// <param name="openid"></param> /// <param name="parameter"></param> /// <returns></returns> private string GetUserInfo(short channelId, string access_token, string openid, object parameter) { string url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}", access_token, openid); string content = NetWorkHttp.Instance.HttpGet(url); JsonData data = JsonMapper.ToObject(content); string nickname = data["nickname"].ToString(); //昵称 string sex = data["sex"].ToString(); string language = data["language"].ToString(); string city = data["city"].ToString(); string province = data["province"].ToString(); string country = data["country"].ToString(); string headimgurl = data["headimgurl"].ToString(); //头像图片 string privilege = data["privilege"].ToString(); string unionid = data["unionid"].ToString(); //微信返回的唯一id string[] arr = parameter as string[]; AccountEntity entity = AccountCacheModel.Instance.LogOn(unionid, unionid, arr[0], arr[1]); if (entity != null) { //如果账户存在 返回账户信息 RetAccountEntity retAccountEntity = new RetAccountEntity(entity); retAccountEntity.SDKNickName = nickname; return(JsonMapper.ToJson(retAccountEntity)); } else { //账户不存在 进行注册 MFReturnValue <int> retValue = AccountCacheModel.Instance.Register(unionid, unionid, channelId, arr[0], arr[1]); int userID = retValue.Value; AccountEntity accountentity = AccountCacheModel.Instance.GetEntity(userID); RetAccountEntity retAccountEntity = new RetAccountEntity(accountentity); retAccountEntity.SDKNickName = nickname; return(JsonMapper.ToJson(retAccountEntity)); } }
static void Main() { //把角色信息添加到数据库 RoleEntity entity = new RoleEntity(); entity.JobId = 1; entity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; entity.AccountId = 222; entity.NickName = "sdsada"; entity.Level = 1; entity.LastInWorldMapId = 1; entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.CurrHP = entity.MaxHP = 100; entity.CurrMP = entity.MaxMP = 100; entity.ToSpeed = 10; entity.WeaponDamageMin = 0; entity.WeaponDamageMax = 0; entity.AttackNumber = 0; entity.StrikePower = 0; entity.PiercingPower = 0; entity.MagicPower = 0; entity.ChoppingDefense = 0; entity.PuncturDefense = 0; entity.MagicDefense = 0; Console.Write("创建角色" + entity.JobId + "昵称:" + entity.NickName); int count = RoleCacheModel.Instance.GetCount(string.Format("[NickName]='{0}'", entity.NickName)); MFReturnValue <object> retValue = null; if (count == 0) { retValue = RoleCacheModel.Instance.Create(entity); } else { retValue = new MFReturnValue <object>(); retValue.HasError = true; retValue.ReturnCode = 1000; } }
/// <summary> /// 创建对象,存在事务中 /// </summary> /// <param name="transaction"></param> /// <param name="entity"></param> /// <returns></returns> public virtual MFReturnValue <object> Create(SqlTransaction transaction, T entity) { //设置参数 SqlParameter[] parameterArray = ValueParas(entity); //第一个参数是id,所以传入的参数是无效的,类似out parameterArray[0].Direction = ParameterDirection.Output; //倒数第二个参数(RetMsg),闯入的参数是无效的,类似 out parameterArray[parameterArray.Length - 2].Direction = ParameterDirection.Output; //最后一个参数是存储过程的返回值 parameterArray[parameterArray.Length - 1].Direction = ParameterDirection.ReturnValue; //执行没有事务的查询 if (transaction.IsNullOrEmpty()) { MFSqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, string.Format("{0}_Create", this.TableName), parameterArray); } //执行有事务的查询 else { MFSqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, string.Format("{0}_Create", this.TableName), parameterArray); } //获取存储过程返回值 int returnCode = parameterArray[parameterArray.Length - 1].Value.ToInt(); //设置返回值 MFReturnValue <object> val = new MFReturnValue <object>(); if (returnCode < 0) { val.HasError = true; } else { val.HasError = false; val.OutputValues["Id"] = parameterArray[0].Value.ToInt(); } val.Message = parameterArray[parameterArray.Length - 2].Value.ObjectToString(); val.ReturnCode = returnCode; return(val); }
/// <summary> /// 物品加入背包 /// </summary> /// <param name="trans">事务</param> /// <param name="roleId">角色编号</param> /// <param name="goodsInType">物品入库类型</param> /// <param name="goodsType">物品类型</param> /// <param name="goodsId">物品编号</param> /// <param name="goodsCount">本次物品入库的数量</param> private bool Add(SqlTransaction trans, int roleId, GoodsInType goodsInType, GoodsType goodsType, int goodsId, int goodsCount, ref List <Role_BackpackItemChangeEntity> changeList) { Queue <int> goodsServerIdQueue = new Queue <int>(); //服务器端物品编号的队列 MFReturnValue <object> retValue = null; changeList = new List <Role_BackpackItemChangeEntity>(); try { int goodsServerId = 0; int speed = DateTime.Now.ToString("hhMMssfff").ToInt(); //种子 for (int i = 0; i < goodsCount; i++) { //1.加入物品库 装备 道具 材料(根据物品类型 加入对应的物品表 如果是装备 要进行随机属性和随机属性值处理) switch (goodsType) { case GoodsType.Equip: #region 装备入库 Role_EquipEntity equipEntity = new Role_EquipEntity(); equipEntity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; equipEntity.RoleId = roleId; equipEntity.EquipId = goodsId; //拿到基础装备信息 EquipEntity baseEntity = EquipDBModel.Instance.Get(goodsId); //基础信息 equipEntity.BaseAttr1Type = (byte)baseEntity.BackAttrOneType; equipEntity.BaseAttr1Value = baseEntity.BackAttrOneValue; equipEntity.BaseAttr2Type = (byte)baseEntity.BackAttrTwoType; equipEntity.BaseAttr2Value = baseEntity.BackAttrTwoValue; //随机属性 //1.计算出随机属性的数量 一共是8个属性 我们随机取2-6个 也可以根据装备等级不同 随机数不同 int attrCount = new Random(speed).Next(2, 7); speed += 100; //0=HP 1=MP 2=攻击 3=防御 4=命中 5=闪避 6=暴击 7=抗性 int[] arr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }; List <int> lst = new List <int>(); lst.AddRange(arr); #region 随机属性 for (int j = 0; j < attrCount; j++) { //随机获取一个索引 对他进行随机值 //随机算法 要根据具体的项目 结合数值策划提供的算法公式来做 比如根据角色的等级 VIP等级 运气值等等来计算 //这里是进行最基础的随机 int index = new Random(speed).Next(0, lst.Count); speed += 100; switch (lst[index]) { case 0: equipEntity.HP = new Random(speed).Next((int)(baseEntity.HP * 0.5f), (int)(baseEntity.HP * 1.5f)); speed += 100; break; case 1: equipEntity.MP = new Random(speed).Next((int)(baseEntity.MP * 0.5f), (int)(baseEntity.MP * 1.5f)); speed += 100; break; case 2: equipEntity.Attack = new Random(speed).Next((int)(baseEntity.Attack * 0.5f), (int)(baseEntity.Attack * 1.5f)); speed += 100; break; case 3: equipEntity.Res = new Random(speed).Next((int)(baseEntity.Res * 0.5f), (int)(baseEntity.Res * 1.5f)); speed += 100; break; case 4: equipEntity.Hit = new Random(speed).Next((int)(baseEntity.Hit * 0.5f), (int)(baseEntity.Hit * 1.5f)); speed += 100; break; case 5: equipEntity.Defense = new Random(speed).Next((int)(baseEntity.Defense * 0.5f), (int)(baseEntity.Defense * 1.5f)); speed += 100; break; case 6: equipEntity.Dodge = new Random(speed).Next((int)(baseEntity.Dodge * 0.5f), (int)(baseEntity.Dodge * 1.5f)); speed += 100; break; case 7: equipEntity.Cri = new Random(speed).Next((int)(baseEntity.Cri * 0.5f), (int)(baseEntity.Cri * 1.5f)); speed += 100; break; } //这个索引 也就是属性用过了 就移除 lst.RemoveAt(index); } #endregion equipEntity.CreateTime = DateTime.Now; equipEntity.UpdateTime = DateTime.Now; retValue = Role_EquipDBModel.Instance.Create(trans, equipEntity); #endregion break; case GoodsType.Item: #region 道具入库 Role_ItemEntity itemEntity = new Role_ItemEntity(); itemEntity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; itemEntity.RoleId = roleId; itemEntity.ItemId = goodsId; itemEntity.CreateTime = DateTime.Now; itemEntity.UpdateTime = DateTime.Now; retValue = Role_ItemDBModel.Instance.Create(trans, itemEntity); #endregion break; case GoodsType.Material: #region 材料入库 Role_MaterialEntity materialEntity = new Role_MaterialEntity(); materialEntity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; materialEntity.RoleId = roleId; materialEntity.MaterialId = goodsId; materialEntity.CreateTime = DateTime.Now; materialEntity.UpdateTime = DateTime.Now; retValue = Role_MaterialDBModel.Instance.Create(trans, materialEntity); #endregion break; } if (retValue.HasError) { return(false); } goodsServerId = retValue.OutputValues["Id"].ToInt(); //物品的服务器端编号 goodsServerIdQueue.Enqueue(goodsServerId); //2.添加物品入库日志(要先从 步骤1 拿到物品的服务器端编号) Log_GoodsInEntity goodsInEntity = new Log_GoodsInEntity(); goodsInEntity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; goodsInEntity.RoleId = roleId; goodsInEntity.Type = (byte)goodsInType; goodsInEntity.GoodsType = (byte)goodsType; goodsInEntity.GoodsId = goodsId; goodsInEntity.GoodsServerId = goodsServerId; goodsInEntity.CreateTime = DateTime.Now; goodsInEntity.UpdateTime = DateTime.Now; retValue = Log_GoodsInDBModel.Instance.Create(trans, goodsInEntity); //加入入库日志 if (retValue.HasError) { return(false); } } //3.加入背包(要进行物品的叠加处理) //(1).拿到物品基础表的叠加数量 装备是1 道具材料是20 //(2).如果叠加数量是1 跳过此步骤 查询库中 该物品叠加数量最低的背包项 比如回血药 有两个背包项 20 5,那就按物品叠加数量正顺序排序 取出第一个5 //(3).如果叠加数量是1 跳过此步骤 对这个背包项进行填充 5 -> 20 //(4).进行剩余物品的处理 根据剩余物品的数量 该物品的叠加数量上限 动态创建背包项目 直到剩余物品全部添加完毕 #region (1).拿到物品基础表的叠加数量 装备是1 道具材料是20 int goodsOverlayCount = 1; //物品叠加数量 switch (goodsType) { case GoodsType.Item: ItemEntity itemEntity = ItemDBModel.Instance.Get(goodsId); if (itemEntity != null) { goodsOverlayCount = itemEntity.maxAmount; } break; case GoodsType.Material: MaterialEntity materialEntity = MaterialDBModel.Instance.Get(goodsId); if (materialEntity != null) { goodsOverlayCount = materialEntity.maxAmount; } break; } #endregion #region (2).如果叠加数量是1 跳过此步骤 查询库中 该物品叠加数量最低的背包项 比如回血药 有两个背包项 20 5,那就按物品叠加数量正顺序排序 取出第一个5 Role_BackpackEntity backpackEntity = null; if (goodsOverlayCount > 1) { MFReturnValue <List <Role_BackpackEntity> > retList = Role_BackpackDBModel.Instance.GetPageListWithTran(condition: string.Format("[RoleId]={0} AND [GoodsType]={1} AND [GoodsId]={2}", roleId, (byte)goodsType, goodsId), orderby: "[GoodsOverlayCount]", isDesc: false, pageSize: 1, pageIndex: 1, trans: trans); if (retList.HasError) { return(false); } if (retList.Value != null && retList.Value.Count > 0) { backpackEntity = retList.Value[0]; } } #endregion #region (3).如果叠加数量是1 跳过此步骤 如果能查到背包项 对这个背包项进行填充 5 -> 20 if (goodsOverlayCount > 1) { if (backpackEntity != null) { //需要填充的数量 = 物品的叠加数量 - 当前背包项的叠加数量 int needCount = goodsOverlayCount - backpackEntity.GoodsOverlayCount; if (needCount > 0) { int currOverlayCount = 0; if (goodsCount > needCount) { //当前叠加数量 = 物品叠加数量 currOverlayCount = goodsOverlayCount; goodsCount -= needCount; //本次添加的物品数量减少 } else { //当前叠加数量 = 原来的叠加数量 + 本次添加的物品数量 currOverlayCount = backpackEntity.GoodsOverlayCount + goodsCount; goodsCount = 0; } backpackEntity.GoodsOverlayCount = currOverlayCount; Role_BackpackDBModel.Instance.Update(trans, backpackEntity); changeList.Add(new Role_BackpackItemChangeEntity() { BackpackId = backpackEntity.Id.Value, Type = BackpackItemChangeType.Update, GoodsType = (GoodsType)backpackEntity.GoodsType, GoodsId = backpackEntity.GoodsId, GoodsCount = backpackEntity.GoodsOverlayCount, GoodsServerId = backpackEntity.GoodsServerId }); Console.WriteLine("背包项修改了==>" + backpackEntity.Id.Value); } } } #endregion #region (4).进行剩余物品的处理 根据剩余物品的数量 该物品的叠加数量上限 动态创建背包项目 直到剩余物品全部添加完毕 while (goodsCount > 0) { int currOverlayCount = 0; if (goodsCount > goodsOverlayCount) { currOverlayCount = goodsOverlayCount; goodsCount -= goodsOverlayCount; } else { currOverlayCount = goodsCount; goodsCount = 0; } Role_BackpackEntity entity = new Role_BackpackEntity(); entity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; entity.RoleId = roleId; entity.GoodsType = (byte)goodsType; entity.GoodsId = goodsId; entity.GoodsOverlayCount = currOverlayCount; entity.GoodsServerId = goodsServerIdQueue.Dequeue(); entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; retValue = Role_BackpackDBModel.Instance.Create(trans, entity); if (retValue.HasError) { return(false); } else { entity.Id = retValue.GetOutputValue <int>("Id"); Console.WriteLine("背包项添加了==>" + entity.Id.Value); changeList.Add(new Role_BackpackItemChangeEntity() { BackpackId = entity.Id.Value, Type = BackpackItemChangeType.Add, GoodsType = (GoodsType)entity.GoodsType, GoodsId = entity.GoodsId, GoodsCount = entity.GoodsOverlayCount, GoodsServerId = entity.GoodsServerId }); } } #endregion return(true); } catch (Exception ex) { Console.WriteLine("报错了==>" + ex.Message); return(false); } finally { goodsServerIdQueue.Clear(); goodsServerIdQueue = null; retValue = null; } }
//100 武器 //200 护腕 //300 衣服 //400 护腿 //500 鞋 //600 戒指 //700 项链 //800 腰带 public void EquipPutOn(int roleId, int goodsId, int goodsServerId, ref List <Role_BackpackItemChangeEntity> changeList) { //1.检查原来的装备槽上是否有装备 有的话先卸下(卸下的装备加入背包更新项 新增) //2.把当前装备穿戴到身上(穿戴的装备加入背包更新项目 删除) //3.重新计算角色战斗力(发送角色战斗里更新消息) MFReturnValue <object> retValue = null; //excel表格中的数据 EquipEntity entity = EquipDBModel.Instance.Get(goodsId); //1.检查原来的装备槽上是否有装备 有的话先卸下(卸下的装备加入背包更新项 新增) bool isHasPutOffEquip = false; int putOffEquipServerId = 0; //脱下的装备服务器端编号 RoleEntity roleEntity = this.GetEntity(roleId); switch (entity.Type) { case 100: if (roleEntity.Equip_Weapon > 0) { isHasPutOffEquip = true; putOffEquipServerId = roleEntity.Equip_Weapon; } roleEntity.Equip_Weapon = goodsServerId; //设置新装备 break; case 200: if (roleEntity.Equip_Cuff > 0) { isHasPutOffEquip = true; putOffEquipServerId = roleEntity.Equip_Cuff; } roleEntity.Equip_Cuff = goodsServerId; //设置新装备 break; case 300: if (roleEntity.Equip_Clothes > 0) { isHasPutOffEquip = true; putOffEquipServerId = roleEntity.Equip_Clothes; } roleEntity.Equip_Clothes = goodsServerId; //设置新装备 break; case 400: if (roleEntity.Equip_Pants > 0) { isHasPutOffEquip = true; putOffEquipServerId = roleEntity.Equip_Pants; } roleEntity.Equip_Pants = goodsServerId; //设置新装备 break; case 500: if (roleEntity.Equip_Shoe > 0) { isHasPutOffEquip = true; putOffEquipServerId = roleEntity.Equip_Shoe; } roleEntity.Equip_Shoe = goodsServerId; //设置新装备 break; case 600: if (roleEntity.Equip_Ring > 0) { isHasPutOffEquip = true; putOffEquipServerId = roleEntity.Equip_Ring; } roleEntity.Equip_Ring = goodsServerId; //设置新装备 break; case 700: if (roleEntity.Equip_Necklace > 0) { isHasPutOffEquip = true; putOffEquipServerId = roleEntity.Equip_Necklace; } roleEntity.Equip_Necklace = goodsServerId; //设置新装备 break; case 800: if (roleEntity.Equip_Belt > 0) { isHasPutOffEquip = true; putOffEquipServerId = roleEntity.Equip_Belt; } roleEntity.Equip_Belt = goodsServerId; //设置新装备 break; } if (isHasPutOffEquip) { //找到当前身上穿的武器 设置状态为脱下 Role_EquipEntity equipPutOffEntity = Role_EquipDBModel.Instance.GetEntity(putOffEquipServerId); equipPutOffEntity.IsPutOn = false; Role_EquipDBModel.Instance.Update(equipPutOffEntity); //把脱下的武器加入背包 Role_BackpackEntity backpackEntity = new Role_BackpackEntity(); backpackEntity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; backpackEntity.RoleId = roleId; backpackEntity.GoodsType = (byte)0; backpackEntity.GoodsId = equipPutOffEntity.EquipId; backpackEntity.GoodsOverlayCount = 1; backpackEntity.GoodsServerId = putOffEquipServerId; backpackEntity.CreateTime = DateTime.Now; backpackEntity.UpdateTime = DateTime.Now; retValue = Role_BackpackDBModel.Instance.Create(backpackEntity); backpackEntity.Id = retValue.GetOutputValue <int>("Id"); changeList.Add(new Role_BackpackItemChangeEntity() { BackpackId = backpackEntity.Id.Value, Type = BackpackItemChangeType.Add, GoodsType = GoodsType.Equip, GoodsId = equipPutOffEntity.EquipId, GoodsCount = 1, GoodsServerId = putOffEquipServerId }); } //2.把当前装备穿戴到身上(穿戴的装备加入背包更新项目 删除) Role_EquipEntity equipPutOnEntity = Role_EquipDBModel.Instance.GetEntity(goodsServerId); equipPutOnEntity.IsPutOn = true; Role_EquipDBModel.Instance.Update(equipPutOnEntity); //查询当前装备的背包项 Role_BackpackEntity currEquipBackpackEntity = Role_BackpackDBModel.Instance.GetEntity(string.Format(" [RoleId]={0} and [GoodsId]={1} and [GoodsType]=0 and [GoodsServerId]={2}", roleId, goodsId, goodsServerId)); Role_BackpackDBModel.Instance.Delete(currEquipBackpackEntity.Id); //从背包中移除 changeList.Add(new Role_BackpackItemChangeEntity() { BackpackId = currEquipBackpackEntity.Id.Value, Type = BackpackItemChangeType.Delete, GoodsType = GoodsType.Equip, GoodsId = goodsId, GoodsCount = 1, GoodsServerId = goodsServerId }); //3.重新计算角色战斗力 UpdateFighting(roleEntity); }
/// <summary> /// 道具使用 /// </summary> /// <param name="roleId"></param> /// <param name="roleBackpackId"></param> /// <param name="goodsType"></param> /// <param name="goodsId"></param> /// <param name="goodsServerId"></param> /// <returns></returns> public MFReturnValue <bool> UseItem(int roleId, int roleBackpackId, int goodsId) { //该物品减少 //根据道具的类型 增加不同的内容 如宝箱会增加物品 经验书会增加经验 bool isSuccess = false; //是否使用成功 int oldValue = -1; int currValue = -1; //1.读取Excel 拿到道具信息 ItemEntity item = ItemDBModel.Instance.Get(goodsId); List <Role_BackpackItemChangeEntity> totalList = new List <Role_BackpackItemChangeEntity>(); List <VirtualItemUpdateEntity> virtualItemUpdateList = new List <VirtualItemUpdateEntity>(); //虚拟资产更新列表 if (!string.IsNullOrEmpty(item.UsedItems)) { string[] arr = item.UsedItems.Split('|'); for (int i = 0; i < arr.Length; i++) { string[] arr1 = arr[i].Split('_'); ItemUsedAcquisitionType aType = (ItemUsedAcquisitionType)int.Parse(arr1[0]); //获得物品类型 int value = int.Parse(arr1[1]); List <Role_BackpackItemChangeEntity> changeList = null; RoleEntity roleEntity = RoleCacheModel.Instance.GetEntity(roleId); Dictionary <string, object> param = new Dictionary <string, object>(); param["@Id"] = roleId; switch (aType) { case ItemUsedAcquisitionType.Equip: //增加装备 { this.Add(roleId, GoodsInType.ItemUsed, new Role_BackpackAddItemEntity() { GoodsId = value, GoodsType = GoodsType.Equip, GoodsCount = 1 } , ref changeList); } break; case ItemUsedAcquisitionType.Item: //增加道具 { this.Add(roleId, GoodsInType.ItemUsed, new Role_BackpackAddItemEntity() { GoodsId = value, GoodsType = GoodsType.Item, GoodsCount = 1 } , ref changeList); } break; case ItemUsedAcquisitionType.Material: //增加材料 { this.Add(roleId, GoodsInType.ItemUsed, new Role_BackpackAddItemEntity() { GoodsId = value, GoodsType = GoodsType.Material, GoodsCount = 1 } , ref changeList); } break; case ItemUsedAcquisitionType.Money: //增加元宝 { oldValue = roleEntity.Money; currValue = oldValue + value; param["@Money"] = currValue; RoleCacheModel.Instance.Update("[Money]= @Money", "Id=@Id", param); //更新数据库 virtualItemUpdateList.Add(new VirtualItemUpdateEntity() { Type = aType, ChangeType = (byte)(currValue >= oldValue ? 0 : 1), OldValue = oldValue, CurrValue = currValue }); } break; case ItemUsedAcquisitionType.Gold: //增加金币 { oldValue = roleEntity.Gold; currValue = oldValue + value; param["@Gold"] = currValue; RoleCacheModel.Instance.Update("[Gold]= @Gold", "Id=@Id", param); //更新数据库 virtualItemUpdateList.Add(new VirtualItemUpdateEntity() { Type = aType, ChangeType = (byte)(currValue >= oldValue ? 0 : 1), OldValue = oldValue, CurrValue = currValue }); } break; case ItemUsedAcquisitionType.Exp: //增加经验 { oldValue = roleEntity.Exp; currValue = oldValue + value; param["@Exp"] = currValue; RoleCacheModel.Instance.Update("[Exp]= @Exp", "Id=@Id", param); //更新数据库 virtualItemUpdateList.Add(new VirtualItemUpdateEntity() { Type = aType, ChangeType = (byte)(currValue >= oldValue ? 0 : 1), OldValue = oldValue, CurrValue = currValue }); } break; case ItemUsedAcquisitionType.Power: //增加体力 { //如果超过体力上限 不能增加 } break; case ItemUsedAcquisitionType.HP: //增加血量 { oldValue = roleEntity.CurrHP; currValue = oldValue + value; virtualItemUpdateList.Add(new VirtualItemUpdateEntity() { Type = aType, ChangeType = (byte)(currValue >= oldValue ? 0 : 1), OldValue = oldValue, CurrValue = currValue }); } break; } if (changeList != null) { totalList.AddRange(changeList); } } } //该物品减少 MFReturnValue <bool> retValue = Reduce(roleId, roleBackpackId, 1, goodsId, 0, 1, GoodsOutType.ItemUse); //背包项改变列表 这次改变 指的是使用了该道具 该道具消失 List <Role_BackpackItemChangeEntity> lst = retValue.GetOutputValue <List <Role_BackpackItemChangeEntity> >("BackpackItemChange"); totalList.AddRange(lst); //物品增加 或者 虚拟资产增加 retValue.SetOutputValue("IsSuccess", isSuccess); retValue.SetOutputValue("VirtualItemUpdateList", virtualItemUpdateList); retValue.SetOutputValue("BackpackItemChange", totalList); return(retValue); }
public void EquipPutOff(int roleId, int goodsId, int goodsServerId, ref List <Role_BackpackItemChangeEntity> changeList) { MFReturnValue <object> retValue = null; //1.把当前的装备卸下(卸下的装备加入背包更新项 新增) //excel表格中的数据 EquipEntity entity = EquipDBModel.Instance.Get(goodsId); RoleEntity roleEntity = this.GetEntity(roleId); switch (entity.Type) { case 100: roleEntity.Equip_Weapon = -1; // break; case 200: roleEntity.Equip_Cuff = -1; // break; case 300: roleEntity.Equip_Clothes = -1; // break; case 400: roleEntity.Equip_Pants = -1; // break; case 500: roleEntity.Equip_Shoe = -1; // break; case 600: roleEntity.Equip_Ring = -1; // break; case 700: roleEntity.Equip_Necklace = -1; // break; case 800: roleEntity.Equip_Belt = -1; // break; } //找到当前身上穿的武器 设置状态为脱下 Role_EquipEntity equipPutOffEntity = Role_EquipDBModel.Instance.GetEntity(goodsServerId); equipPutOffEntity.IsPutOn = false; Role_EquipDBModel.Instance.Update(equipPutOffEntity); //把脱下的武器加入背包 Role_BackpackEntity backpackEntity = new Role_BackpackEntity(); backpackEntity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; backpackEntity.RoleId = roleId; backpackEntity.GoodsType = (byte)0; backpackEntity.GoodsId = goodsId; backpackEntity.GoodsOverlayCount = 1; backpackEntity.GoodsServerId = goodsServerId; backpackEntity.CreateTime = DateTime.Now; backpackEntity.UpdateTime = DateTime.Now; retValue = Role_BackpackDBModel.Instance.Create(backpackEntity); backpackEntity.Id = retValue.GetOutputValue <int>("Id"); changeList.Add(new Role_BackpackItemChangeEntity() { BackpackId = backpackEntity.Id.Value, Type = BackpackItemChangeType.Add, GoodsType = GoodsType.Equip, GoodsId = goodsId, GoodsCount = 1, GoodsServerId = goodsServerId }); //2.重新计算角色战斗力(发送角色战斗里更新消息) UpdateFighting(roleEntity); }
private MFReturnValue <bool> Reduce(int roleId, int roleBackpackId, int goodsType, int goodsId, int goodsServerId, int count, GoodsOutType outType) { MFReturnValue <bool> retValue = new MFReturnValue <bool>(); //1.查询物品的售价 物品的已有数量 int sellPrice = 0; //售价 int maxAmount = 0; //叠加数量 int hasCount = 0; //已有数量 bool isCanSell = false; //是否可以销售 StringBuilder sbrSql = new StringBuilder(); StringBuilder sbrSqlLog = new StringBuilder(); #region 查询 售价 叠加数量 已有数量 switch ((GoodsType)goodsType) { case GoodsType.Equip: EquipEntity equipEntity = EquipDBModel.Instance.Get(goodsId); if (equipEntity != null) { sellPrice = equipEntity.SellMoney; maxAmount = 1; } sbrSql.AppendFormat("[Status]=1 and [EquipId]={0}", goodsId); if (goodsServerId > 0) { sbrSql.AppendFormat(" and [Id]={0}", goodsServerId); } //已有数量 hasCount = Role_EquipDBModel.Instance.GetCount(sbrSql.ToString()); if (hasCount >= count) { isCanSell = true; } break; case GoodsType.Item: ItemEntity itemEntity = ItemDBModel.Instance.Get(goodsId); if (itemEntity != null) { sellPrice = itemEntity.SellMoney; maxAmount = itemEntity.maxAmount; } sbrSql.AppendFormat("[Status]=1 and [ItemId]={0}", goodsId); if (goodsServerId > 0 && maxAmount == 1) { sbrSql.AppendFormat(" and [Id]={0}", goodsServerId); } hasCount = Role_ItemDBModel.Instance.GetCount(sbrSql.ToString()); if (hasCount >= count) { isCanSell = true; } break; case GoodsType.Material: MaterialEntity materialEntity = MaterialDBModel.Instance.Get(goodsId); if (materialEntity != null) { sellPrice = materialEntity.SellMoney; maxAmount = materialEntity.maxAmount; } sbrSql.AppendFormat("[Status]=1 and [MaterialId]={0}", goodsId); if (goodsServerId > 0 && maxAmount == 1) { sbrSql.AppendFormat(" and [Id]={0}", goodsServerId); } hasCount = Role_MaterialDBModel.Instance.GetCount(sbrSql.ToString()); if (hasCount >= count) { isCanSell = true; } break; } #endregion //2.核对玩家拥有的物品数量是否满足出售数量 if (!isCanSell) { retValue.HasError = true; retValue.ReturnCode = -1; //数量不足 return(retValue); } Log_GoodsInEntity logInEntity; Log_GoodsOutEntity logOutEntity; int currGoodsServerId = 0; int currSellCount = count; //3.从对应的物品表中移除 #region 从对应的物品表中移除 while (currSellCount > 0) { sbrSql.Clear(); sbrSqlLog.Clear(); //从对应的物品表中删除 switch ((GoodsType)goodsType) { case GoodsType.Equip: sbrSql.AppendFormat("[Status]=1 and [EquipId]={0}", goodsId); if (goodsServerId > 0) { sbrSql.AppendFormat(" and [Id]={0}", goodsServerId); } Role_EquipEntity equipEntity = Role_EquipDBModel.Instance.GetEntity(sbrSql.ToString()); Role_EquipDBModel.Instance.Delete(equipEntity.Id); //删除物品 currGoodsServerId = equipEntity.Id.Value; break; case GoodsType.Item: sbrSql.AppendFormat("[Status]=1 and [ItemId]={0}", goodsId); if (goodsServerId > 0 && maxAmount == 1) { sbrSql.AppendFormat(" and [Id]={0}", goodsServerId); } Role_ItemEntity itemEntity = Role_ItemDBModel.Instance.GetEntity(sbrSql.ToString()); Role_ItemDBModel.Instance.Delete(itemEntity.Id); //删除物品 currGoodsServerId = itemEntity.Id.Value; break; case GoodsType.Material: sbrSql.AppendFormat("[Status]=1 and [MaterialId]={0}", goodsId); if (goodsServerId > 0 && maxAmount == 1) { sbrSql.AppendFormat(" and [Id]={0}", goodsServerId); } Role_MaterialEntity materialEntity = Role_MaterialDBModel.Instance.GetEntity(sbrSql.ToString()); Role_MaterialDBModel.Instance.Delete(materialEntity.Id); //删除物品 currGoodsServerId = materialEntity.Id.Value; break; } sbrSqlLog.AppendFormat("[Status]=1 and [RoleId]={0} and [GoodsType]={1} and [GoodsId]={2} and [GoodsServerId]={3}", roleId, goodsType, goodsId, currGoodsServerId); //添加出库日志 logInEntity = Log_GoodsInDBModel.Instance.GetEntity(sbrSqlLog.ToString()); //获取对应的入库日志 logOutEntity = new Log_GoodsOutEntity(); logOutEntity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released; logOutEntity.RoleId = roleId; logOutEntity.Type = (byte)outType; logOutEntity.GoodsType = (byte)goodsType; logOutEntity.GoodsId = goodsId; logOutEntity.GoodsServerId = currGoodsServerId; logOutEntity.LogGoodsInId = logInEntity.Id.Value; logOutEntity.CreateTime = DateTime.Now; logOutEntity.UpdateTime = DateTime.Now; Log_GoodsOutDBModel.Instance.Create(logOutEntity); currSellCount--; } #endregion //4.修改或者删除对应的背包项 currSellCount = count; //背包项改变列表 List <Role_BackpackItemChangeEntity> lst = new List <Role_BackpackItemChangeEntity>(); //roleBackpackId 更新背包项的时候,优先从这个背包项更新(修改或者删除) while (currSellCount > 0) { //roleBackpackId 第一次减的时候,这个背包项是肯定存在的 //出售50个 叠加是20 当前背包项13个 //1.先从传递过来的背包项中删除物品数量 Role_BackpackEntity backpackEntity = null; if (roleBackpackId > 1) { //获取传递过来的背包项 backpackEntity = this.GetEntity(roleBackpackId); roleBackpackId = 0; } else { //获取其他背包项 backpackEntity = this.GetEntity(string.Format("[Status]=1 and [RoleId]={0} and [GoodsType]={1} and [GoodsId]={2}", roleId, goodsType, goodsId)); } if (currSellCount >= backpackEntity.GoodsOverlayCount) { //如果出售的数量 大于等于当前的背包项数量 则把这个背包项删除 currSellCount -= backpackEntity.GoodsOverlayCount; //删除背包项目 this.Delete(backpackEntity.Id); //添加到背包项改变列表 lst.Add(new Role_BackpackItemChangeEntity() { BackpackId = backpackEntity.Id.Value, Type = BackpackItemChangeType.Delete, GoodsType = (GoodsType)goodsType, GoodsId = goodsId, GoodsServerId = backpackEntity.GoodsServerId, GoodsCount = 0 }); } else { //如果出售的数量小于 当前背包项的数量 则从当前背包项中 减去出售的数量 backpackEntity.GoodsOverlayCount -= currSellCount; this.Update(backpackEntity); //更新背包项目 currSellCount = 0; //添加到背包项改变列表 lst.Add(new Role_BackpackItemChangeEntity() { BackpackId = backpackEntity.Id.Value, Type = BackpackItemChangeType.Update, GoodsType = (GoodsType)goodsType, GoodsId = goodsId, GoodsServerId = backpackEntity.GoodsServerId, GoodsCount = backpackEntity.GoodsOverlayCount }); } } retValue.SetOutputValue("BackpackItemChange", lst); retValue.SetOutputValue("TotalSellPrice", sellPrice * count); //总售价 = 单价 * 数量 return(retValue); }