/// <summary> /// 创建房间处理 /// </summary> /// <param name="token"></param> /// <param name="model"></param> /// <returns> /// 1 创建成功 /// -1 连接已在房间中 /// -2 连接未登录 /// -3 获取房间出错 /// </returns> int OnCreateRoom(BaseToken token, TransModel model) { if (TokenHasRoom(token)) { return(-1); } if (!UserCache.Ins.IsOnline(token)) { return(-2); } MatchRoom room = GetEmptyRoom(); if (room == null) { return(-3); } room.masterName = UserCache.Ins.GetDALByToken(token).name; room.allTokens.Add(room.masterName, token); if (!string.IsNullOrEmpty(model.GetMsg <string>())) { room.passwd = model.GetMsg <string>(); } roomCache.TryAdd(room.id, room); tokenToRoomID.TryAdd(token, room.id); return(1); }
/// <summary> /// 子弹伤害请求 /// </summary> /// <param name="token"></param> /// <param name="model"></param> internal void BulletDamageCREQ(BaseToken token, TransModel model) { if (state != FightRoomState.Fight) { return; } //throw new NotImplementedException(); if (IsVaildUserModel(token)) { try { DamageDTO damageDTO = model.GetMsg <DamageDTO>(); if (tokenToModelID[token] == damageDTO.SrcID) { if (tokenToModelID.ContainsValue(damageDTO.DstID)) { lock (DamageDic) { DamageMutex.WaitOne(); if (!DamageDic.ContainsKey(damageDTO.SrcID)) { //DamageDic.Add(damageDTO.SrcID, 0); baseModelDic[damageDTO.DstID].hp -= 20; //伤害广播 Broadcast(tokenToModelID.Keys.ToList(), 1003010, 0, damageDTO); if (baseModelDic[damageDTO.DstID].hp <= 0) { //击杀 广播 && 失败反馈 BaseToken dstToken = tokenToModelID.Where((t, id) => t.Value == damageDTO.DstID).ToList()[0].Key; Broadcast(tokenToModelID.Keys.ToList(), 1003011, 0, damageDTO); Send(dstToken, 10039998, damageDTO.DstID); // 清除失败 token tokenToModelID.Remove(dstToken); tokenToUserDTO.Remove(dstToken); long rid; FightManager.Ins.tokenToRoomID.Remove(dstToken, out rid); if (tokenToModelID.Count == 1) { //成功广播 并销毁房间 Broadcast(tokenToModelID.Keys.ToList(), 10039999, damageDTO.SrcID); Clear(); } } } DamageMutex.ReleaseMutex(); } } } } catch (Exception ex) { Debugger.Error(ex.ToString()); //throw; } } }
/// <summary> /// /// </summary> /// <param name="token"></param> /// <param name="model"></param> /// <returns> /// 1 可以移除 /// -1 连接未登录 /// -2 不是有效连接 //连接不在房间内 /// -3 连接对应房间不存在 /// -4 不是房主,不能踢人 /// -5 dto出错或踢的人是自己 /// -6 踢的人不存在 /// </returns> int OnRemove(BaseToken token, TransModel model) { if (!UserCache.Ins.IsOnline(token)) { return(-1); } //if (!tokenToRoomID.ContainsKey(token)) { // return -2; //} //long id= tokenToRoomID[token]; //if (!roomCache.ContainsKey(id)) { // tokenToRoomID.Remove(token, out id); // return -3; //} if (!IsVaildToken(token)) { return(-2); } long id = tokenToRoomID[token]; string tName = UserCache.Ins.GetDALByToken(token).name; if (!roomCache[id].masterName.Equals(tName)) { return(-4); } if (string.IsNullOrEmpty(model.GetMsg <string>()) || model.GetMsg <string>().Equals(tName)) { return(-5); } if (!roomCache[id].allTokens.ContainsKey(model.GetMsg <string>())) { return(-6); } roomCache[id].Remove(model.GetMsg <string>()); return(1); }
public void ChangeUserDataCREQ(BaseToken token, TransModel model) { if (!string.IsNullOrEmpty(model.GetMsg <UserDTO>().name)) { UserDTO d = model.GetMsg <UserDTO>(); UserDAL dal = new UserDAL(); dal.clothData = d.clothData; dal.hairData = d.hairData; dal.headID = d.headID; if (UserCache.Ins.SaveUserDAL(token, dal)) { Send(token, 1002802, 1);//更换成功 } else { Send(token, 1002802, 0);//更换失败 } } }
/// <summary> /// 射击相关请求(瞄准 开火) 广播是 1003008(在对应房间广播) /// </summary> /// <param name="token"></param> /// <param name="model"></param> internal void ShootCREQ(BaseToken token, TransModel model) { if (state != FightRoomState.Fight) { return; } //throw new NotImplementedException(); if (IsVaildUserModel(token)) { try { if (tokenToModelID[token] == model.GetMsg <ShootDataDTO>().modelID) { Broadcast(tokenToModelID.Keys.ToList(), 1003008, 0, model.GetMsg <ShootDataDTO>()); } } catch (Exception ex) { Debugger.Error(ex.ToString()); //throw; } } }
/// <summary> /// 移动请求 /// </summary> /// <param name="token"></param> /// <param name="model"></param> internal void MoveCREQ(BaseToken token, TransModel model) { if (state != FightRoomState.Fight) { return; } //throw new NotImplementedException(); if (IsVaildUserModel(token)) { try { if (model.area == 0) { if (tokenToModelID[token] == model.GetMsg <MoveDataDTO>().modelID) { Broadcast(tokenToModelID.Keys.ToList(), 1003006, 0, model.GetMsg <MoveDataDTO>()); } } else if (model.area == 3 || model.area == 4) { if (tokenToModelID[token] == model.GetMsg <RotateYDTO>().modelID) { Broadcast(tokenToModelID.Keys.ToList(), 1003006, model.area, model.GetMsg <RotateYDTO>()); } } else { if (tokenToModelID[token] == model.GetMsg <MouseBtnDTO>().modelID) { Broadcast(tokenToModelID.Keys.ToList(), 1003006, model.area, model.GetMsg <MouseBtnDTO>()); } } } catch (Exception ex) { Debugger.Error(ex.ToString()); //throw; } } }
/// <summary> /// 用户注册 /// </summary> /// <param name="model"></param> /// <param name="token"></param> /// <returns> /// 1 注册成功 /// -1 dto错误 /// -2 用户名以及密码出错 /// -3 用户已存在 /// </returns> public int OnUserRegister(BaseToken token, TransModel model) { if (model.GetMsg <UserDTO>() == null) { return(-1); } UserDTO dto = model.GetMsg <UserDTO>(); if (string.IsNullOrEmpty(dto.name) || string.IsNullOrEmpty(dto.password)) { return(-2); } if (cache.HasUser(dto.name)) { return(-3); } cache.AddUser(dto.name, dto.password); return(1); }
/// <summary> /// 登录处理 /// </summary> /// <param name="token"></param> /// <param name="model"></param> /// <returns> /// 1 登录成功 /// -1 dto错误 /// -2 dto信息错误 /// -3 用户不存在 /// -4 用户或连接已登录 /// -5 缓存出错 /// -6 密码错误 /// </returns> public int OnLogin(BaseToken token, TransModel model) { if (model.GetMsg <UserDTO>() == null) { return(-1); } UserDTO dto = model.GetMsg <UserDTO>(); if (string.IsNullOrEmpty(dto.name) || string.IsNullOrEmpty(dto.password)) { return(-2); } if (!cache.HasUser(dto.name)) { return(-3); } if (!cache.GetDALByName(dto.name).passwd.Equals(dto.password)) { return(-6); } if (cache.IsOnline(dto.name) || cache.IsOnline(token)) { return(-4); } if (!cache.Online(dto.name, token)) { return(-5); } return(1); }
/// <summary> /// 客户端的状态信息 /// </summary> /// <param name="token"></param> /// <param name="model"></param> internal void StatusCREQ(BaseToken token, TransModel model) { //throw new NotImplementedException(); if (IsVaildUserModel(token)) { //同步客户端模型状态(每个客户端5秒发一次过来) try { BaseModel m = model.GetMsg <BaseModel>(); if (GetModelByToken(token) != null && GetModelByToken(token).id == m.id) { Broadcast(tokenToModelID.Keys.ToList(), 1003202, 0, m); } } catch (Exception ex) { Debugger.Error(ex.ToString()); //throw; } } }
/// <summary> /// 进入房间请求 /// </summary> /// <param name="token"></param> /// <returns> /// -4 密码错误 /// -5 人数已满 /// -6 进入房间出错 /// </returns> public int EnterCREQ(BaseToken token, TransModel model) { if (allTokens.Count >= maxNum) { return(-5); } if (string.IsNullOrEmpty(passwd)) { if (!manager.tokenToRoomID.TryAdd(token, id)) { return(-6); } string name = UserCache.Ins.GetDALByToken(token).name; allTokens.Add(name, token); //发送进入广播,(房间信息) Broadcast(allTokens.Values.ToList(), 1002006, 1, GetDTO()); return(1); } else { string pwd = model.GetMsg <string>(); if (string.IsNullOrEmpty(pwd) || !pwd.Equals(passwd)) { return(-4); } if (!manager.tokenToRoomID.TryAdd(token, id)) { return(-6); } string name = UserCache.Ins.GetDALByToken(token).name; allTokens.Add(name, token); //发送进入广播,(房间信息) Broadcast(allTokens.Values.ToList(), 1002006, 1, GetDTO()); return(1); } }
private void OnMsgReceive(TransModel transModel) { tx.text = transModel.GetMsg <string>(); }