public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { Dictionary <byte, object> responseParameter = new Dictionary <byte, object>(); try { //获取并查询用户 Dictionary <byte, object> parameters = operationRequest.Parameters; String userName = (String)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.UserName); int cardsSetId = (int)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.CardsSetId); //用户卡组变化 User user = DictionaryUtils.getValue <String, User>(Application.loginUserDict, userName); user.CardSets.Remove(cardsSetId); //将变化保存到文件数据库 UserManager.disassembleCardSetsInfo(user.UserName, user.CardSets); //保存用户 Application.loginUserDict.Remove(user.UserName); Application.loginUserDict.Add(user.UserName, user); //封装信息 responseParameter.Add((byte)ParameterCode.CardsSetOperationResult, 1); } catch (Exception e) { Application.logger.Info(e.ToString()); responseParameter.Add((byte)ParameterCode.CardsSetOperationResult, 0); } OperationResponse response = new OperationResponse((byte)OPCode.DeleteCardsSet, responseParameter); peer.SendOperationResponse(response, sendParameters); }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { //信息提取 Dictionary <byte, object> parameter = operationRequest.Parameters; String userName = (String)DictionaryUtils.getValue <byte, object>(parameter, (byte)ParameterCode.UserName); String password = (String)DictionaryUtils.getValue <byte, object>(parameter, (byte)ParameterCode.Password); Application.logger.Info("===================" + userName + " " + password + "尝试登录==========================="); //检查注册结果 Dictionary <byte, object> responseParameter = new Dictionary <byte, object>(); int result = UserInfoServices.register(userName, password); if (result == 0) { responseParameter.Add((byte)ParameterCode.RegisterResult, 0); Application.logger.Info("===================" + userName + " " + password + "注册失败==========================="); } else if (result == 1) { responseParameter.Add((byte)ParameterCode.RegisterResult, 1); Application.logger.Info("===================" + userName + " " + password + "注册成功==========================="); } else { responseParameter.Add((byte)ParameterCode.RegisterResult, 2); Application.logger.Info("===================" + userName + " " + password + "注册失败==========================="); } //响应客户端 OperationResponse operationResponse = new OperationResponse((byte)OPCode.Register, responseParameter); peer.SendOperationResponse(operationResponse, sendParameters); }
//重新封装卡组 public static bool disassembleCardSetsInfo(String userName, SerializableDictionary <int, CardsSet> dict) { String path = Path.Combine(DataRootPath, "UserInfo\\" + userName + "\\CardSetsInfo.txt"); try { File.WriteAllText(path, String.Empty); using (StreamWriter writer = new StreamWriter(new FileStream(path, FileMode.Open), Encoding.GetEncoding("gb2312"))) { foreach (int cardSetNumber in dict.Keys) { CardsSet cardsSet = DictionaryUtils.getValue <int, CardsSet>(dict, cardSetNumber); writer.WriteLine("CardsSet" + " " + cardsSet.Name + " " + (byte)cardsSet.profession + " " + (byte)cardsSet.gameMode); for (int i = 0; i < cardsSet.CardCapable; i++) { if (cardsSet.cards[i] != null && cardsSet.cards[i] != "") { writer.WriteLine(cardsSet.cards[i] + " " + cardsSet.isGolden[i]); } else { break; } } writer.WriteLine("*CardsSet"); } } return(true); } catch (Exception e) { Application.logger.Info(e.ToString()); return(false); } }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { Dictionary <byte, object> responseParameter = new Dictionary <byte, object>(); try { //获取并查询用户 Dictionary <byte, object> parameters = operationRequest.Parameters; String userName = (String)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.UserName); String cardString = (String)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.CardName); String[] info = cardString.Split(' '); Card card = new Card((Series)int.Parse(info[0]), (Profession)int.Parse(info[1]), (Rarity)int.Parse(info[2]), (CardType)int.Parse(info[3]) , int.Parse(info[4]), info[5], info[6], int.Parse(info[7]), int.Parse(info[8]), (Species)int.Parse(info[9])); bool isgolden = (bool)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.IsColden); //用户卡牌变化 User user = DictionaryUtils.getValue <String, User>(Application.loginUserDict, userName); int cardNumber = DictionaryUtils.getValue <String, int>(user.MyCards, card.Name); user.MyCards.Remove(card.Name); if (isgolden) { cardNumber -= 1000; } else { cardNumber -= 1; } if (cardNumber != 0) { user.MyCards.Add(card.Name, cardNumber); } //将变化保存到文件数据库 UserManager.disassembleCardInfo(user.UserName, user.MyCards); //用户奥术之尘变化 user.ArcaneDust += DataUtils.getNumberOfArcaneDustWhenDecompose(card.Rarity, isgolden); //将变化保存到数据库 UserManager.Update(user); //保存用户 Application.loginUserDict.Remove(user.UserName); Application.loginUserDict.Add(user.UserName, user); //封装信息 responseParameter.Add((byte)ParameterCode.CardOperationResult, 1); } catch (Exception e) { Application.logger.Info(e.ToString()); responseParameter.Add((byte)ParameterCode.CardOperationResult, 0); } OperationResponse response = new OperationResponse((byte)OPCode.DecomposeCard, responseParameter); peer.SendOperationResponse(response, sendParameters); }
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { BaseHandler handler = DictionaryUtils.getValue <OPCode, BaseHandler>(Application.handlerDict, (OPCode)operationRequest.OperationCode); if (handler != null) { handler.doResponse(operationRequest, sendParameters, this); } }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { Dictionary <byte, object> responseParameter = new Dictionary <byte, object>(); try { //获取并查询用户 Dictionary <byte, object> parameters = operationRequest.Parameters; String userName = (String)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.UserName); String newCardsSet = (String)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.CardsSet); int cardsSetId = (int)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.CardsSetId); String[] info = newCardsSet.Split(' '); CardsSet set = new CardsSet(); set.Name = info[0]; set.profession = (Profession)int.Parse(info[1]); set.gameMode = (GameMode)int.Parse(info[2]); //用户卡组变化 User user = DictionaryUtils.getValue <String, User>(Application.loginUserDict, userName); List <int> list = new List <int>(); foreach (int i in user.CardSets.Keys) { if (DictionaryUtils.getValue <int, CardsSet>(user.CardSets, i) == null) { list.Add(i); } } foreach (int i in list) { user.CardSets.Remove(i); } user.CardSets.Add(cardsSetId, set); //将变化保存到文件数据库 UserManager.disassembleCardSetsInfo(user.UserName, user.CardSets); //保存用户 Application.loginUserDict.Remove(user.UserName); Application.loginUserDict.Add(user.UserName, user); //封装信息 responseParameter.Add((byte)ParameterCode.CardsSetOperationResult, 1); } catch (Exception e) { Application.logger.Info(e.ToString()); responseParameter.Add((byte)ParameterCode.CardsSetOperationResult, 0); } OperationResponse response = new OperationResponse((byte)OPCode.NewCardsSet, responseParameter); peer.SendOperationResponse(response, sendParameters); }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { Dictionary <byte, object> responseParameter = new Dictionary <byte, object>(); try { //获取并查询用户 Dictionary <byte, object> parameters = operationRequest.Parameters; String userName = (String)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.UserName); String cardsSet = (String)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.CardsSet); int cardsSetId = (int)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.CardsSetId); String[] info = cardsSet.Split(' '); CardsSet set = new CardsSet(); set.Name = info[0]; set.CardCapable = int.Parse(info[1]); set.Changeable = bool.Parse(info[2]); set.Number = int.Parse(info[3]); set.profession = (Profession)int.Parse(info[4]); set.gameMode = (GameMode)int.Parse(info[5]); for (int i = 0; i < set.Number; i++) { set.cards[i] = info[6 + 2 * i]; set.isGolden[i] = bool.Parse(info[7 + 2 * i]); } Application.logger.Info(cardsSetId + set.ToString()); //用户卡组变化 User user = DictionaryUtils.getValue <String, User>(Application.loginUserDict, userName); user.CardSets.Remove(cardsSetId); user.CardSets.Add(cardsSetId, set); //将变化保存到文件数据库 UserManager.disassembleCardSetsInfo(user.UserName, user.CardSets); //保存用户 Application.loginUserDict.Remove(user.UserName); Application.loginUserDict.Add(user.UserName, user); //封装信息 responseParameter.Add((byte)ParameterCode.CardsSetOperationResult, 1); } catch (Exception e) { Application.logger.Info(e.ToString()); responseParameter.Add((byte)ParameterCode.CardsSetOperationResult, 0); } OperationResponse response = new OperationResponse((byte)OPCode.ChangeCardsSet, responseParameter); peer.SendOperationResponse(response, sendParameters); }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { //信息提取 Dictionary <byte, object> parameter = operationRequest.Parameters; String userName = (String)DictionaryUtils.getValue <byte, object>(parameter, (byte)ParameterCode.UserName); bool has = false; foreach (String s in Application.readyList) { if (s.Equals(userName)) { has = true; } } if (!has) { //将用户名添加到List之中 Application.readyList.Add(userName); } //转化准备好的userNameList字符串 String all = ""; foreach (String s in Application.readyList) { all += s + " "; } Dictionary <byte, object> responseParameter = new Dictionary <byte, object>(); responseParameter.Add((byte)ParameterCode.ReadyUserNameList, all); //通知其他所有准备好的客户端有新的客户端加入 foreach (String s in Application.readyList) { if (!s.Equals(userName)) { Dictionary <byte, object> dict = new Dictionary <byte, object>(); dict.Add((byte)ParameterCode.ReadyUserNameList, all); EventData data = new EventData((byte)EventCode.Ready, dict); DictionaryUtils.getValue <String, MyClientPeer>(Application.clientPeerDict, s).SendEvent(data, sendParameters); } } //响应客户端 OperationResponse operationResponse = new OperationResponse((byte)OPCode.Ready, responseParameter); peer.SendOperationResponse(operationResponse, sendParameters); }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { Dictionary <byte, object> requestParameter = operationRequest.Parameters; //拿到用户名,卡包类型,卡包数量 String userName = (String)DictionaryUtils.getValue <byte, object>(requestParameter, (byte)ParameterCode.UserName); Series packageSeries = (Series)DictionaryUtils.getValue <byte, object>(requestParameter, (byte)ParameterCode.Series); int packageNumber = (int)DictionaryUtils.getValue <byte, object>(requestParameter, (byte)ParameterCode.PackageNumber); //操作用户的卡包字典 User user = DictionaryUtils.getValue <String, User>(Application.loginUserDict, userName); SerializableDictionary <Series, int> packageInfoDict = user.MyCardsPackage; int allPackageNumber = packageNumber + DictionaryUtils.getValue <Series, int>(packageInfoDict, packageSeries); packageInfoDict.Remove(packageSeries); packageInfoDict.Add(packageSeries, allPackageNumber); //重新封装包信息 bool success = UserManager.disassembleCardPackageInfo(userName, packageInfoDict); Dictionary <byte, object> responseParameter = new Dictionary <byte, object>(); if (success) { DictionaryUtils.getValue <String, User>(Application.loginUserDict, userName).MyCardsPackage = packageInfoDict; //减少钱 user.Money -= packageNumber * 100; UserManager.Update(user); //改变user user.MyCardsPackage = packageInfoDict; Application.loginUserDict.Remove(user.UserName); Application.loginUserDict.Add(user.UserName, user); //封装返回信息 responseParameter.Add((byte)ParameterCode.PurchaseCardPackageResult, 1); } else { responseParameter.Add((byte)ParameterCode.PurchaseCardPackageResult, 0); } Application.logger.Info("===================" + user.UserName + "买了" + packageNumber + "包" + packageSeries + "系列的卡包,还剩" + user.Money + "====================="); //响应客户端 OperationResponse operationResponse = new OperationResponse((byte)OPCode.PurchaseCardPackage, responseParameter); peer.SendOperationResponse(operationResponse, sendParameters); }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { //信息提取 Dictionary <byte, object> parameter = operationRequest.Parameters; String userName = (String)DictionaryUtils.getValue <byte, object>(parameter, (byte)ParameterCode.UserName); String anotherUserName = (String)DictionaryUtils.getValue <byte, object>(parameter, (byte)ParameterCode.AnotherUserName); //发送消息 Dictionary <byte, object> dict = new Dictionary <byte, object>(); dict.Add((byte)ParameterCode.UserName, userName); EventData data = new EventData((byte)EventCode.FightInvit, dict); DictionaryUtils.getValue <String, MyClientPeer>(Application.clientPeerDict, anotherUserName).SendEvent(data, sendParameters); //添加信息 Application.Waiting.Add(userName, anotherUserName); }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { //信息提取 Dictionary <byte, object> parameter = operationRequest.Parameters; String userName = (String)DictionaryUtils.getValue <byte, object>(parameter, (byte)ParameterCode.UserName); String anotherUserName = (String)DictionaryUtils.getValue <byte, object>(parameter, (byte)ParameterCode.AnotherUserName); bool fightInvitResponseResult = (bool)DictionaryUtils.getValue <byte, object>(parameter, (byte)ParameterCode.FightInvitResponseResult); //waiting应当删除键值对 if (DictionaryUtils.getValue <String, String>(Application.Waiting, anotherUserName) != null) { Application.Waiting.Remove(anotherUserName); } if (fightInvitResponseResult) { //Fighting应当添加,List应当去除 try { Application.Fighting.Add(anotherUserName, userName); Application.Fighting.Add(userName, anotherUserName); Application.readyList.Remove(anotherUserName); Application.readyList.Remove(userName); } catch (Exception e) { e.ToString(); } } else { } //发送消息 Dictionary <byte, object> dict = new Dictionary <byte, object>(); dict.Add((byte)ParameterCode.FightInvitResponseResult, fightInvitResponseResult); EventData data = new EventData((byte)EventCode.FightInvitResponse, dict); DictionaryUtils.getValue <String, MyClientPeer>(Application.clientPeerDict, anotherUserName).SendEvent(data, sendParameters); }
//拆解卡包信息 public static bool disassembleCardPackageInfo(String userName, SerializableDictionary <Series, int> dict) { String path = Path.Combine(DataRootPath, "UserInfo\\" + userName + "\\CardPackageInfo.txt"); try { File.WriteAllText(path, String.Empty); using (StreamWriter writer = new StreamWriter(new FileStream(path, FileMode.Open), Encoding.GetEncoding("gb2312"))) { foreach (Series series in dict.Keys) { String data = (byte)series + " " + DictionaryUtils.getValue <Series, int>(dict, series); writer.WriteLine(data); } } return(true); } catch (Exception e) { Application.logger.Info(e.ToString()); return(false); } }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { //信息提取 Dictionary <byte, object> parameter = operationRequest.Parameters; String userName = (String)DictionaryUtils.getValue <byte, object>(parameter, (byte)ParameterCode.UserName); //将用户名在List之中删除 bool i = false; foreach (string s in Application.readyList) { if (s.Equals(userName)) { i = true; break; } } if (i) { Application.readyList.Remove(userName); //转化准备好的userNameList字符串 String all = ""; foreach (String s in Application.readyList) { all += s + " "; } //通知其他所有准备好的客户端有新的客户端加入 foreach (String s in Application.readyList) { Dictionary <byte, object> dict = new Dictionary <byte, object>(); dict.Add((byte)ParameterCode.ReadyUserNameList, all); EventData data = new EventData((byte)EventCode.Left, dict); DictionaryUtils.getValue <String, MyClientPeer>(Application.clientPeerDict, s).SendEvent(data, sendParameters); } } }
//User字符串化 public static String praseUserToUserString(User user) { String userString = user.UserName + ";" + user.Password + ";" + user.Money + ";" + user.ArcaneDust + ";"; foreach (Series s in user.MyCardsPackage.Keys) { userString += (byte)s + ":" + DictionaryUtils.getValue <Series, int>(user.MyCardsPackage, s) + ","; } userString += ";"; foreach (String s in user.MyCards.Keys) { userString += s + ":" + DictionaryUtils.getValue <String, int>(user.MyCards, s) + ","; } userString += ";"; foreach (int s in user.CardSets.Keys) { CardsSet cardsSet = DictionaryUtils.getValue <int, CardsSet>(user.CardSets, s); if (cardsSet != null) { userString += s + ":" + cardsSet.ToString() + ","; } } return(userString); }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { Dictionary <byte, object> dict = operationRequest.Parameters; Dictionary <byte, object> responseParameter = new Dictionary <byte, object>(); try { //获取参数 string userName = (string)DictionaryUtils.getValue <byte, object>(dict, (byte)ParameterCode.UserName); string packageString = (string)DictionaryUtils.getValue <byte, object>(dict, (byte)ParameterCode.CardPackage); Series series = (Series)DictionaryUtils.getValue <byte, object>(dict, (byte)ParameterCode.Series); Application.logger.Info("====================" + userName + packageString + "========================"); //反序列化 CardPackage package = null; using (StringReader reader = new StringReader(packageString)) { XmlSerializer serializer = new XmlSerializer(typeof(CardPackage)); package = (CardPackage)serializer.Deserialize(reader); } //修改user字典的信息 User user = DictionaryUtils.getValue <String, User>(Application.loginUserDict, userName); int number = DictionaryUtils.getValue <Series, int>(user.MyCardsPackage, series); number--; if (number < 0) { responseParameter.Add((byte)ParameterCode.UseCardPackageResult, 0); goto End; } user.MyCardsPackage.Remove(series); if (number != 0) { user.MyCardsPackage.Add(series, number); } for (int i = 0; i < 5; i++) { if (package.isGolden[i]) { int cardNumber = DictionaryUtils.getValue <String, int>(user.MyCards, package.cards[i]); user.MyCards.Remove(package.cards[i]); user.MyCards.Add(package.cards[i], cardNumber + 1000); } else { int cardNumber = DictionaryUtils.getValue <String, int>(user.MyCards, package.cards[i]); user.MyCards.Remove(package.cards[i]); user.MyCards.Add(package.cards[i], cardNumber + 1); } } Application.loginUserDict.Remove(userName); Application.loginUserDict.Add(userName, user); //修改文件数据库的信息 UserManager.disassembleCardInfo(userName, user.MyCards); UserManager.disassembleCardPackageInfo(userName, user.MyCardsPackage); //响应信息 responseParameter.Add((byte)ParameterCode.UseCardPackageResult, 1); } catch (Exception e) { Application.logger.Info("=====================" + e.ToString() + "=========================="); responseParameter.Add((byte)ParameterCode.UseCardPackageResult, 0); } //响应客户端 End : OperationResponse operationResponse = new OperationResponse((byte)OPCode.UseCardPackage, responseParameter); peer.SendOperationResponse(operationResponse, sendParameters); }
public override void doResponse(OperationRequest operationRequest, SendParameters sendParameters, MyClientPeer peer) { //获取并查询用户 Dictionary <byte, object> parameters = operationRequest.Parameters; String userName = (String)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.UserName); String password = (String)DictionaryUtils.getValue <byte, object>(parameters, (byte)ParameterCode.Password); Application.logger.Info("===================" + userName + " " + password + "尝试登录==========================="); //检查登录结果 int result = UserInfoServices.verify(userName, password); if (result == 2) { Application.logger.Info("===================" + userName + " " + password + "登录失败(账号不存在)==========================="); } else if (result == 1) { Application.logger.Info("===================" + userName + " " + password + "登录成功==========================="); } else { Application.logger.Info("===================" + userName + " " + password + "登录失败(密码错误)==========================="); } //封装所有信息 User user = null; String userString = null; if (result == 1) { user = UserInfoServices.encapsulateAllUserInfo(userName); //存放在loginUser字典中 if (DictionaryUtils.getValue <String, User>(Application.loginUserDict, userName) != null) { Application.loginUserDict.Remove(userName); } Application.loginUserDict.Add(user.UserName, user); //尝试序列化 userString = UserManager.praseUserToUserString(user); } //开始封装返回参数 Dictionary <byte, object> responseParameters = new Dictionary <byte, object>(); responseParameters.Add((byte)ParameterCode.LoginResult, result); responseParameters.Add((byte)ParameterCode.User, userString); //将用户名保存到客户端对象之中 if (user != null) { peer.userName = user.UserName; if (DictionaryUtils.getValue <String, MyClientPeer>(Application.clientPeerDict, user.UserName) != null) { Application.clientPeerDict.Remove(userName); } Application.clientPeerDict.Add(userName, peer); } //发送响应 OperationResponse operationResponse = new OperationResponse((byte)this.opCode, responseParameters); peer.SendOperationResponse(operationResponse, sendParameters); }