• unity学习(51)——服务器三次注册限制以及数据库化角色信息6--完结


    同一账号只写第一次,不同账号第一次爆炸 ,就因为下面部分得到逻辑有问题

    修改后的代码如下:1.成功完成角色注册信息的数据库化记录。2.每个账号上限3个角色。3.角色是可以重名的,但是角色的id不会重名。

    1. internal class UserCache
    2. {
    3. private ConcurrentDictionary<string, List<string>> userPlayerIds = dataUserPlayerIds();
    4. //private ConcurrentDictionary> userPlayerIds = new ConcurrentDictionary>();
    5. //private ConcurrentDictionary players = new ConcurrentDictionary();
    6. private ConcurrentDictionary<string, PlayerModel> players = dataPlayers();//所有账号的角色信息,虽然角色信息中不包含账号id
    7. public static ConcurrentDictionary<string, List<string>> dataUserPlayerIds()//这样好像就行了
    8. {
    9. StreamReader file = new StreamReader("userPlayerIds.txt");
    10. string all = file.ReadToEnd();
    11. file.Close();
    12. ConcurrentDictionary<string, List<string>> results = new ConcurrentDictionary<string, List<string>>();
    13. try
    14. {
    15. //ConcurrentDictionary> results = JsonConvert.DeserializeObject>>(JsonConvert.DeserializeObject(all).ToString());
    16. results = JsonConvert.DeserializeObjectstring, List<string>>>(JsonConvert.DeserializeObject(all).ToString());
    17. }
    18. catch {
    19. Console.WriteLine("dataUserPlayerIds()中出现问题");
    20. }
    21. return results;
    22. //return JsonConvert.DeserializeObject(all);
    23. }
    24. public static ConcurrentDictionary<string, PlayerModel> dataPlayers()//这样好像就行了
    25. {
    26. StreamReader file = new StreamReader("players.txt");
    27. string all = file.ReadToEnd();
    28. file.Close();
    29. ConcurrentDictionary<string, PlayerModel> results = new ConcurrentDictionary<string, PlayerModel>();
    30. try
    31. {
    32. results = JsonConvert.DeserializeObjectstring, PlayerModel>>(JsonConvert.DeserializeObject(all).ToString());
    33. }
    34. catch {
    35. Console.WriteLine("dataPlayers()中出现问题");
    36. }
    37. return results;
    38. //return JsonConvert.DeserializeObject(all);
    39. }
    40. public void put(string accId, PlayerModel model)//逻辑刚开始有些小问题,问题不大
    41. {
    42. bool ur = false;
    43. if (this.userPlayerIds.ContainsKey(accId))//这里出了问题了,同在一个put函数中
    44. {
    45. List<string> stringList;
    46. List<string> stringList_out;
    47. this.userPlayerIds.TryGetValue(accId, out stringList);//得到已有的角色id(player)
    48. stringList.Add(model.id);//给账号新加一个角色
    49. //如果已存账号信息--
    50. //重复的键,TryAdd是家不进去的
    51. ur = this.userPlayerIds.TryRemove(accId, out stringList_out);
    52. ur = this.userPlayerIds.TryAdd(accId, stringList);//同名 键 应该是可以覆盖的--不是第一次先用字符串修改值,再增加到对应键值对中
    53. }
    54. else
    55. {//重来没有,自然不会重复
    56. ur = this.userPlayerIds.TryAdd(accId, new List<string>() { model.id });//第一次只加1个
    57. }
    58. if (ur)//这次应该没问题了
    59. {
    60. StreamWriter file = new StreamWriter("userPlayerIds.txt");
    61. string json = JsonConvert.SerializeObject(this.userPlayerIds);
    62. Console.WriteLine("userPlayerIds.TryAdd:" + json);
    63. file.Write(json);
    64. file.Close();
    65. }
    66. bool pr=this.players.TryAdd(model.id, model);//最后真正怎加角色信息 player=角色 account=账号
    67. //将角色详细信息写入文件
    68. if (pr)
    69. {
    70. StreamWriter file = new StreamWriter("players.txt");
    71. string json = JsonConvert.SerializeObject(this.players);
    72. Console.WriteLine("players.TryAdd:" + json);
    73. file.Write(json);
    74. file.Close();
    75. }
    76. }

  • 相关阅读:
    微服务中间件
    整数智能·迪拜GITEX 2023 |探索未来科技,感受创新脉搏
    Python编程从入门到实践(第2版)个人学习笔记
    手把手教你通过PaddleHub快速实现输入中/英文本生成图像(Stable Diffusion)
    ssh服务攻防与加固
    水果店圈子:小型水果店用什么保鲜,经营水果店的保鲜方法
    通过反射获取Class对象的四种方式
    IO流的讲解(3)
    .net core 3.0 NLog 日志的使用
    vue3+ts做echarts做一个简单的折线渐变图
  • 原文地址:https://blog.csdn.net/qq_27863281/article/details/136553086