unity学习(48)——服务器三次注册限制以及数据库化角色信息3--数据流程

1.经过一系列调试,定位到UserCache.cs中的put函数,内容如下:

cs 复制代码
public void put(string accId, PlayerModel model)
{
  if (this.userPlayerIds.ContainsKey(accId))
  {
    List<string> stringList;
    this.userPlayerIds.TryGetValue(accId, out stringList);//得到已有的角色id(player)
    stringList.Add(model.id);//新加一个
  }
  else
    this.userPlayerIds.TryAdd(accId, new List<string>()
    {
      model.id
    });
  this.players.TryAdd(model.id, model);//最后真正怎加角色信息 player=角色 account=账号
}

2.TryAdd是系统函数,所以最终的结果就存在this.player中!跳转到其定义位置

3.把之前账号密码服务器化的技巧应用过来:

ctrf+f可以进行查找,快速找到之前账号密码的数据库化部分:

4.账号密码写入部分的代码如下:

cs 复制代码
      bool r=this.accounts.TryAdd(userName, accountModel);
      if (r)
      {
          Console.WriteLine("成功注册时,写入文件");
          StreamWriter file = new StreamWriter("login.txt");
          string json = JsonConvert.SerializeObject(this.accounts);
          Console.WriteLine("字典转json之后的内容:"+ json);
          file.Write(json);
          file.Close();
      }

字典的TryAdd操作之后进行文件写入

5.账号密码初始化部分的代码如下:自建data初始化函数

cs 复制代码
    public static ConcurrentDictionary<string, AccountModel> data()//这样好像就行了
{
        StreamReader file = new StreamReader("login.txt");
        string all = file.ReadToEnd();
        file.Close();

        ConcurrentDictionary<string, AccountModel> results = JsonConvert.DeserializeObject<ConcurrentDictionary<string, AccountModel>>(JsonConvert.DeserializeObject(all).ToString());
       
        return results;
    }

data函数的调用位置

6.角色信息的读取部分的代码如下:

cs 复制代码
private ConcurrentDictionary<string, List<string>> userPlayerIds = dataUserPlayerIds();
//private ConcurrentDictionary<string, List<string>> userPlayerIds = new ConcurrentDictionary<string, List<string>>();
//private ConcurrentDictionary<string, PlayerModel> players = new ConcurrentDictionary<string, PlayerModel>();
private ConcurrentDictionary<string, PlayerModel> players = dataPlayers();

public static ConcurrentDictionary<string, List<string>> dataUserPlayerIds()//这样好像就行了
{
    StreamReader file = new StreamReader("userPlayerIds.txt");
    string all = file.ReadToEnd();
    file.Close();
    ConcurrentDictionary<string, List<string>> results = JsonConvert.DeserializeObject<ConcurrentDictionary<string, List<string>>>(JsonConvert.DeserializeObject(all).ToString());
    return results;
    //return JsonConvert.DeserializeObject(all);
}
public static ConcurrentDictionary<string, PlayerModel> dataPlayers()//这样好像就行了
{
    StreamReader file = new StreamReader("players.txt");
    string all = file.ReadToEnd();
    file.Close();
    ConcurrentDictionary<string, PlayerModel> results = JsonConvert.DeserializeObject<ConcurrentDictionary<string, PlayerModel>>(JsonConvert.DeserializeObject(all).ToString());
    return results;
    //return JsonConvert.DeserializeObject(all);
}

7.角色信息的写入部分的代码如下:

cs 复制代码
    public void put(string accId, PlayerModel model)
{
        if (this.userPlayerIds.ContainsKey(accId))
        {
            List<string> stringList;
            this.userPlayerIds.TryGetValue(accId, out stringList);//得到已有的角色id(player)
            stringList.Add(model.id);//新加一个
        }
        else
        {
            bool ur=this.userPlayerIds.TryAdd(accId, new List<string>()
    {
      model.id
    });
            if (ur)
            {
                StreamWriter file = new StreamWriter("userPlayerIds.txt");
                string json = JsonConvert.SerializeObject(this.userPlayerIds);
                Console.WriteLine("userPlayerIds.TryAdd:" + json);
                file.Write(json);
                file.Close();
            }
        }
  bool pr=this.players.TryAdd(model.id, model);//最后真正怎加角色信息 player=角色 account=账号
        //将角色详细信息写入文件
        if (pr)
        {
            StreamWriter file = new StreamWriter("players.txt");
            string json = JsonConvert.SerializeObject(this.players);
            Console.WriteLine("players.TryAdd:" + json);
            file.Write(json);
            file.Close();
        }
    }

8.调试出现异常!

9.调试解决异常:提前手动创建2个txt文件即可。

10.出现新的异常:应该是第一次读取文件时存在问题。

相关推荐
AI视觉网奇2 小时前
ue 角色驱动衣服 绑定衣服
笔记·学习·ue5
wdfk_prog3 小时前
[Linux]学习笔记系列 -- [drivers][input]serio
linux·笔记·学习
ZH15455891315 小时前
Flutter for OpenHarmony Python学习助手实战:GUI桌面应用开发的实现
python·学习·flutter
编程小白20265 小时前
从 C++ 基础到效率翻倍:Qt 开发环境搭建与Windows 神级快捷键指南
开发语言·c++·windows·qt·学习
学历真的很重要5 小时前
【系统架构师】第二章 操作系统知识 - 第二部分:进程与线程(补充版)
学习·职场和发展·系统架构·系统架构师
深蓝海拓6 小时前
PySide6,QCoreApplication::aboutToQuit与QtQore.qAddPostRoutine:退出前后的清理工作
笔记·python·qt·学习·pyqt
酒鼎6 小时前
学习笔记(3)HTML5新特性(第2章)
笔记·学习·html5
L***一6 小时前
2026届大专跨境电商专业毕业生就业能力提升路径探析
学习
.小墨迹6 小时前
apollo学习之借道超车的速度规划
linux·c++·学习·算法·ubuntu
ZH15455891316 小时前
Flutter for OpenHarmony Python学习助手实战:模块与包管理的实现
python·学习·flutter