赞
踩
1.客户端添加点击按钮所触发的事件,在selectMenu界面中增加myDelete函数,当点击“删除角色”按钮时触发该函数的内容。
- public void myDelete()
- {
- string message = nowPlayer.id;
- //string m = Coding<StringDTO>.encode(message);
- NetWorkScript.getInstance().sendMessage(Protocol.USER, 0, 6, message);
- Debug.Log(message);
- }
2.服务器也成功接受到来自客户端的内容,这里有瑕疵,此时是明文通信。
3.完善remove函数中的内容,这个大改了,从最终的测试结果来看是成功的:
- public PlayerModel[] remove(string accId, string playerId)
- {
- //确实到这里的,但是逻辑需要大改
- Console.WriteLine("remove:");
- Console.WriteLine(accId);
- Console.WriteLine(playerId);
- if (this.userPlayerIds.ContainsKey(accId))//当前账号是否注册
- {
- List<string> stringList1;
- this.userPlayerIds.TryGetValue(accId, out stringList1);//得到当前账号下所有的角色信息
- if (stringList1 != null && stringList1.Contains(playerId))
- {
- stringList1.Remove(playerId);//删除或者怎加以后还要写入,类似的操作不久前刚做过的
- //借鉴之前put中的相关操作,其实可以很快完成的
- //写userPlayerIds.txt
- //List<string> stringList;
- List<string> stringList_out;
- this.userPlayerIds.TryRemove(accId, out stringList_out);//删旧的
- bool ur=this.userPlayerIds.TryAdd(accId, stringList1);//写新的--
- //原来账号下三个角色,此时应该就剩下2个了
- if (ur)//这次应该没问题了
- {
- StreamWriter file = new StreamWriter("userPlayerIds.txt");
- string json = JsonConvert.SerializeObject(this.userPlayerIds);
- Console.WriteLine("userPlayerIds.TryAdd:" + json);
- file.Write(json);
- file.Close();
- Console.WriteLine("userPlayerIds删除完成");
- }
- //到此位置删除 并 文件写入 userPlayerIds
-
- //下面删除players.txt中的内容
-
- bool ur1=this.players.TryRemove(playerId, out PlayerModel _);//这里直接删就行,不用删完再加了
- if (ur1)//这次应该没问题了
- {
- StreamWriter file = new StreamWriter("players.txt");
- string json = JsonConvert.SerializeObject(this.players);
- Console.WriteLine("userPlayerIds.TryAdd:" + json);
- file.Write(json);
- file.Close();
- Console.WriteLine("players删除完成");
- }
- }
- }
- //返回playermodel数组--自己改的
- List<string> stringList;
- this.userPlayerIds.TryGetValue(accId, out stringList);//创建第一个角色,这里是空的--之前注册角色的id(PlayerIds)--专属名词调试几遍就会了
- if (stringList == null)
- return (PlayerModel[])null;
- PlayerModel[] playerModelArray = new PlayerModel[stringList.Count];
- for (int index = 0; index < stringList.Count; ++index)
- this.players.TryGetValue(stringList[index], out playerModelArray[index]);
- return playerModelArray;
-
- }

4.返回值的部分也需要做对应的修改:command用1,直接更新列表!
5.最后成功删除角色:
6.补充
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。