unity学习(21)——客户端与服务器合力完成注册功能(3)用字典存储账号密码

cs 复制代码
public void reg(Session session, SocketModel model)
{
  LoginDTO loginDto = Coding<LoginDTO>.decode(model.Message);
  if(loginDto.userName.Length<=17&& loginDto.passWord.Length<=17)
  {
       bool v = BizUtil.account.create(loginDto.userName, loginDto.passWord);//bool代表注册成功或者失败
       session.write(0, 0, 3, (object)new BoolDTO(v));//这个session还需要单独拿时间来学
  }
}

0.最简单的一句 session.write(0, 0, 3, (object)new BoolDTO(v));

这句往session中的List队列中写入了一个消息,就是最早的SocketModel格式的,最后的message就一个bool值,代表最终的注册结果成功与否。

1.首先知道注册信息写到什么地方去了,先进create函数

cs 复制代码
public bool create(string userName, string password)
{
    if (this.accounts.ContainsKey(userName))
    {
        Console.WriteLine("注册失败");
        return false;
    }
  AccountModel accountModel = new AccountModel(Guid.NewGuid().ToString(), userName, password);
  return this.accounts.TryAdd(userName, accountModel);
}

2.然后,this.accounts.ContainsKey(userName)的结果代表用户名是否重复,所以其中自然有如何读取,进而判定用户名重复的内容。

先从accounts的定义入手,private ConcurrentDictionary<string, AccountModel> accounts = new ConcurrentDictionary<string, AccountModel>();

accounts是个字典,键是string代表用户名,值是AccountModel结构体。

ContainsKey函数是C#中的Dictionary方法,用于检查Dictionary中是否存在键。

如果键(用户名)已经存在,则注册失败,不存在,则通过以下代码新建一个值

cs 复制代码
AccountModel accountModel = new AccountModel(Guid.NewGuid().ToString(), userName, password);

再把用户名和这个值加入字典

cs 复制代码
 return this.accounts.TryAdd(userName, accountModel);
相关推荐
SlowFeather16 分钟前
Unity TMP可控角度多色渐变文字
unity·游戏引擎
菜鸟‍1 小时前
【论文学习】大语言模型(LLM)论文
论文阅读·人工智能·学习
游戏开发爱好者82 小时前
FTP 抓包分析实战,命令、被动主动模式要点、FTPS 与 SFTP 区别及真机取证流程
运维·服务器·网络·ios·小程序·uni-app·iphone
我先去打把游戏先2 小时前
ESP32学习笔记(基于IDF):IOT应用——WIFI连接
笔记·单片机·嵌入式硬件·mcu·物联网·学习·esp32
Brookty2 小时前
【算法】前缀和
java·学习·算法·前缀和·动态规划
。TAT。2 小时前
C++ - List
数据结构·c++·学习
纸带3 小时前
USB --SETUP --STATUS阶段
linux·服务器·网络
张永清-老清4 小时前
每周读书与学习->JMeter主要元件详细介绍(一)配置元件
学习·jmeter·性能调优·jmeter性能测试·性能分析·干货分享
言之。4 小时前
ClickHouse 数据更新策略深度解析:突变操作与最佳实践
服务器·数据库·clickhouse
鼾声鼾语4 小时前
grootN1 grootN1.5 gr00t安装方法以及使用(学习)
学习·angular.js·simulink·isaacsim·isaaclab