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

news/2025/2/15 15:59:02/

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

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

internal class UserCache
{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();//所有账号的角色信息,虽然角色信息中不包含账号idpublic static ConcurrentDictionary<string, List<string>> dataUserPlayerIds()//这样好像就行了{StreamReader file = new StreamReader("userPlayerIds.txt");string all = file.ReadToEnd();file.Close();ConcurrentDictionary<string, List<string>> results = new ConcurrentDictionary<string, List<string>>();try{//ConcurrentDictionary<string, List<string>> results = JsonConvert.DeserializeObject<ConcurrentDictionary<string, List<string>>>(JsonConvert.DeserializeObject(all).ToString());results = JsonConvert.DeserializeObject<ConcurrentDictionary<string, List<string>>>(JsonConvert.DeserializeObject(all).ToString());}catch {Console.WriteLine("dataUserPlayerIds()中出现问题");}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 = new ConcurrentDictionary<string, PlayerModel>();try{results = JsonConvert.DeserializeObject<ConcurrentDictionary<string, PlayerModel>>(JsonConvert.DeserializeObject(all).ToString());}catch {Console.WriteLine("dataPlayers()中出现问题");}return results;//return JsonConvert.DeserializeObject(all);}public void put(string accId, PlayerModel model)//逻辑刚开始有些小问题,问题不大{bool ur = false;if (this.userPlayerIds.ContainsKey(accId))//这里出了问题了,同在一个put函数中{List<string> stringList;List<string> stringList_out;this.userPlayerIds.TryGetValue(accId, out stringList);//得到已有的角色id(player)stringList.Add(model.id);//给账号新加一个角色//如果已存账号信息--//重复的键,TryAdd是家不进去的ur = this.userPlayerIds.TryRemove(accId, out stringList_out);ur = this.userPlayerIds.TryAdd(accId, stringList);//同名 键 应该是可以覆盖的--不是第一次先用字符串修改值,再增加到对应键值对中}else{//重来没有,自然不会重复ur = this.userPlayerIds.TryAdd(accId, new List<string>() { model.id });//第一次只加1个}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();}}


http://www.ppmy.cn/news/1380317.html

相关文章

Midjourney绘图欣赏系列(十)

Midjourney介绍 Midjourney 是生成式人工智能的一个很好的例子&#xff0c;它根据文本提示创建图像。它与 Dall-E 和 Stable Diffusion 一起成为最流行的 AI 艺术创作工具之一。与竞争对手不同&#xff0c;Midjourney 是自筹资金且闭源的&#xff0c;因此确切了解其幕后内容尚不…

完美十进制数——去年天梯校赛

7-4 完美十进制数 对于一个十进制正整数 x&#xff0c;其包含 k 个位&#xff0c;每个数位的数字累加和为 sum 。如果 sum%k0&#xff0c;那么这个十 进制数就是完美的。 例&#xff1a;对于数 12340&#xff0c;包含 5 位&#xff0c;数位累加和为 10&#xff0c;10%50&#x…

python爬虫(7)之pandas模块

1、pandas模块里的两个对象 1、Series 对象 series很类似于一维数组但是它相较于数组多了行标签 示例代码&#xff1a; import pandas as pd ac pd.Series([[1,2,8,9],[3,5,5,8]]) print(ac) 结果如下&#xff1a; 当然也可以更改行标签 示例如下&#xff1a; import …

理解文件系统

个人主页&#xff1a;Lei宝啊 愿所有美好如期而遇 磁盘 磁盘的机械构成 磁盘的物理存储 每个磁盘的盘面&#xff0c;磁头&#xff0c;扇面&#xff0c;扇区都有唯一的编号&#xff0c;同时&#xff0c;一个扇区的大小是512字节&#xff08;扇区的大小也有4KB的&#xff0c;或…

Java SE String类(一):常用方法(上)

1. 常用方法 1.1 字符串构造 String类的常用构造方法只有以下三种 public class Main {public static void main(String[] args) {String s1 "hello";//使用常量串进行构造String s2 new String("hello");//创建String对象char[] array {h,e,l,l,o};…

python的数据容器--字符串

字符串的取值 my_str "my name is zhou jie lun" valuemy_str[2] value2my_str[-10]print(f"从字符串{my_str}取下标为2的元素{value} 倒数16的值是{value2}")字符串的index查找方法 valmy_str.index("is") print(f"{val}")字符串的…

MonkeyRunner在自动化测试里的应用场景!

MonkeyRunner是Android提供的一个自动化测试工具&#xff0c;主要用于对Android设备或模拟器进行功能和压力测试。以下是一些MonkeyRunner在自动化测试中的应用场景及实例代码&#xff1a; 基本操作测试 点击屏幕上的特定位置或元素。 模拟滑动和手势操作。 发送按键事件。 …

2023年第三届中国高校大数据挑战赛第二场赛题D题赛题:行业职业技术培训能力评价(成品论文 代码与思路 视频讲解)

赛题 中国是制造业大国&#xff0c;产业门类齐全&#xff0c;每年需要培养大量的技能娴熟的技术工人进入工厂。某行业在全国有多所不同类型&#xff08;如国家级、省级等&#xff09;的职业技术培训学校&#xff0c;进行 5 种技能培训。学员入校时需要进行统一的技能考核&…