Exemple #1
0
        /// <summary>
        /// プレイヤー情報をファイルから読み込む
        /// </summary>
        public void LoadPlayersInfo()
        {
            var    result      = new List <UserOrNpcInfo>();
            var    lines       = System.IO.File.ReadLines($"{Program.serverDataFolder}/{Server.Id}.txt");
            User   nowUser     = null;
            Point  nowPoint    = new Point(0, 0);
            var    nowEmotions = new Dictionary <UserOrNpcInfo, Emotion>();
            var    nowSecrets  = new List <Secret>();
            var    nowStatus   = new Dictionary <string, object>();
            string nowName     = "";

            bool skipToNextUser = false;

            foreach (var line in lines)
            {
                if (line.Trim() == "[User]")
                {
                    nowPoint       = new Point(0, 0);
                    nowEmotions    = new Dictionary <UserOrNpcInfo, Emotion>();
                    nowSecrets     = new List <Secret>();
                    nowStatus      = new Dictionary <string, object>();
                    nowUser        = null;
                    nowName        = "";
                    skipToNextUser = false;
                    continue;
                }
                if (skipToNextUser)
                {
                    continue;
                }
                if (line.Trim() == "[UserEnd]")
                {
                    if (nowUser == null)
                    {
                        result.Add(new NpcInfo(nowName, nowEmotions, nowSecrets, nowStatus));
                    }
                    else
                    {
                        result.Add(new UserInfo(nowUser, nowEmotions, nowSecrets, nowStatus));
                    }
                    continue;
                }

                var splited = line.Split('=').ToArray();
                if (splited.Length < 2)
                {
                    continue;
                }
                string key = splited[0], value = splited[1];
                switch (key)
                {
                case "Id":
                    ulong id = ulong.Parse(value);
                    try
                    {
                        nowUser = Server.Users.First(u => u.Id == id);
                    }
                    catch
                    {
                    }
                    break;

                case "XY":
                    var xy = value.Split(',').Select(a => int.Parse(a)).ToArray();
                    nowPoint = new Point(xy[0], xy[1]);
                    break;

                case "Secret":
                    nowSecrets.Add(Secret.FromCSV(value));
                    break;

                case "Status":
                    nowStatus = UserInfo.StatusFormCSV(line.Substring("Status=".Length));
                    break;

                case "Name":
                    nowName = value;
                    break;

                default:
                    break;
                }
            }
            Players  = result;
            AllUsers = new List <UserOrNpcInfo>(Players);
            // Emotion
            UserOrNpcInfo nowInfo = null;

            foreach (var line in lines)
            {
                if (line.Trim() == "[User]")
                {
                    nowInfo     = null;
                    nowEmotions = new Dictionary <UserOrNpcInfo, Emotion>();
                    continue;
                }
                if (line.Trim() == "[UserEnd]")
                {
                    if (nowInfo != null)
                    {
                        nowInfo.Emotions = nowEmotions;
                    }
                    continue;
                }
                var splited = line.Split('=').ToArray();
                if (splited.Length < 2)
                {
                    continue;
                }
                string key = splited[0], value = splited[1];
                if (key == "Name")
                {
                    nowInfo = AllUsers.First(i => i.Name == value);
                }
                else if (key == "Emotion")
                {
                    var           nickAndEmo = value.Split(',');
                    string        toName     = nickAndEmo[0].Trim();
                    Emotion       emo        = new Emotion(nickAndEmo[1], Emotion.ParseEmotionType(nickAndEmo[2]));
                    UserOrNpcInfo toUser     = null;
                    try { toUser = AllUsers.First(u => u.NickOrName == toName); }
                    catch { continue; }
                    if (toUser == null)
                    {
                        continue;
                    }
                    nowEmotions.Add(toUser, emo);
                }
            }
        }