ItemInfo 为游戏物品数据,背包中的信息应该是角色自身的物品,之前ItemInfo只是用于测试背包效果。
表结构
相对于ItemInfo就简单多了,关联了userId 和 itemId,实现user 和 item的多对多关系。
一个User 可以拥有一个以上的相同Item
一个Item也可以被多个User拥有
Lv 用来计算当前Item的属性
count 表示每一组Item的现有数量, itemInfo只规定了最大数量
whereType 表示item所在位置,角色身上,背包里,仓库
UserItemEntity
public enum WhereType
{
Knapsack,
Body,
Warehouse,
}
[Serializable]
public class UserItemEntity : IEntityIntKey
{
public virtual int ID { get; set; }
public virtual UserEntity UserInfo { get; set; }
public virtual ItemEntity ItemInfo { get; set; }
public virtual int Count { get; set; }
public virtual int Lv { get; set; }
public virtual WhereType WhereType { get; set; }
}
UserItemMap
public class UserItemMap : ClassMap
{
public UserItemMap()
{
Table("useritem");
Id(x => x.ID).Column("id");
Map(x => x.Count).Column("count");
Map(x => x.Lv).Column("lv");
Map(x => x.WhereType).Column("WhereType").CustomType();
References(x => x.UserInfo).Column("userid");
References(x => x.ItemInfo).Column("itemid");
}
}
UserItemRepository
public class UserItemRepository : Repository
{
public UserItemRepository(ISession session) : base(session)
{
}
}
GetUserItemCommand
用UserID去查找角色所有物品
public class GetUserItemCommand : ICommand
{
public int UserID { get; set; }
}
GetUserItemHandler
public class GetUserItemHandler : ICommandHandler
{
private readonly UserItemRepository userItemRepository;
private UnitOfWork unitOfWork;
public GetUserItemHandler()
{
unitOfWork = new UnitOfWork();
userItemRepository = new UserItemRepository(unitOfWork.Session);
}
public ICommandResult Excute(GetUserItemCommand command)
{
IEnumerable userItems = userItemRepository.FilterBy(x => x.UserInfo.ID == command.UserID);
unitOfWork.Commit();
return new CommandResult(true, userItems);
}
}
OperationCode
public enum OperationCode : byte
{
Register,
Login,
GetHeroInfo,
CreateHero,
GetItemInfo,
GetUserItem,
}
AliPeer
protected virtual void HandleGetUserItemOperation(OperationRequest operationRequest, SendParameters sendParameters)
{
try
{
object json;
operationRequest.Parameters.TryGetValue(operationRequest.OperationCode, out json);
UserEntity user = JsonMapper.ToObject(json.ToString());
var command = Mapper.Map(user);
OperationResponse response = new OperationResponse((byte)OperationCode.GetUserItem);
CommandResult result = commandBus.Submit(command) as CommandResult;
if (result.Success)
{
response.ReturnCode = (short)ReturnCode.Sucess;
Dictionary<byte, object> dict = new Dictionary<byte, object>();
string strJson = JsonMapper.ToJson(result.Result);
dict.Add((byte)ReturnCode.Sucess, strJson);
response.Parameters = dict;
}
else
{
response.ReturnCode = (short)ReturnCode.Faild;
}
SendOperationResponse(response, sendParameters);
}
catch (Exception ex)
{
if (AliPeer.log.IsDebugEnabled)
{
AliPeer.log.DebugFormat("Error: {0}", ex.Message);
}
}
}
同样是需要缓存的数据,但是不是在客户端启动的时候进行缓存,而是在角色进入游戏后进行缓存。
private void BtnEnterOnClick(PointerEventData obj)
{
if (m_SelectHero.SelectedUser != null)
{
PhotonDataCache.AddOrUpdateDataCache(PhotonCacheType.SelectedUser, m_SelectHero.SelectedUser);
//初始化角色物品
string json = JsonMapper.ToJson(m_SelectHero.SelectedUser);
Dictionary<byte, object> parameter = new Dictionary<byte, object>();
parameter.Add((byte)OperationCode.GetUserItem, json);
PhotonManager.Instance.Peer.OpCustom((byte)OperationCode.GetUserItem, parameter, true);
//切换场景
StartCoroutine(GameMain.Instance.LoadScene(SceneName.Battle));
}
else
{
Debug.Log("Error");
}
}
同样在PhotonManager的回调函数中进行缓存
public void OnOperationResponse(OperationResponse operationResponse)
{
switch ((OperationCode)operationResponse.OperationCode)
{
case OperationCode.Login:
HandleLoginResponse(operationResponse);
break;
case OperationCode.Register:
HandleRegisterResponse(operationResponse);
break;
case OperationCode.GetHeroInfo:
HandleGetHeroInfoResponse(operationResponse);
break;
case OperationCode.CreateHero:
HandleCreateHeroResponse(operationResponse);
break;
case OperationCode.GetItemInfo:
HandleGetItemInfoResponse(operationResponse);
break;
case OperationCode.GetUserItem:
HandleGetUserItemResponse(operationResponse);
break;
}
}
private void HandleGetUserItemResponse(OperationResponse operationResponse)
{
switch ((ReturnCode)operationResponse.ReturnCode)
{
case ReturnCode.Sucess:
object json;
operationResponse.Parameters.TryGetValue((byte)ReturnCode.Sucess, out json);
if (json != null)
{
JsonData data = JsonMapper.ToObject(json.ToString());
new JsonToEntity(PhotonCacheType.UserItem, data).BuildEntityToCache();
}
var cache = PhotonDataCache.GetAll(PhotonCacheType.UserItem);
if (cache != null && cache.Count > 0)
{
Debug.Log("角色物品数据初始化成功");
}
else
{
Debug.Log("角色物品数据初始化失败");
}
break;
}
}
最早UserItem的命名是 ItemEntity Item{get;set;},然后服务端调用JsonMapper.ToJson弄死不会把ItemEntity序列化出来,从吃饭前查到吃饭后,写了一堆测试代码,最后把名字换成ItemInfo,终于问题解决了。
看了LitJson的源代码也没看出问题。