Exemplo n.º 1
0
 private void InstallViruses(string[] viruses)
 {
     if (viruses.Any(s => !SusadminData.VirusNameExists(s)))
     {
         WriteLine(PrintError("ERROR") + ": virus not found");
     }
     else if (viruses.Any(s => installedViruses.Contains(SusadminData.GetVirusId(s))))
     {
         WriteLine(PrintError("ERROR") + ": virus already installed");
     }
     else if (new HashSet <string>(viruses.Select(v => v.ToUpper())).Count != viruses.Length)
     {
         WriteLine(PrintError("ERROR") + ": duplicates");
     }
     else
     {
         installedViruses.AddRange(viruses.Select(s => SusadminData.GetVirusId(s)));
     }
 }
Exemplo n.º 2
0
 private void DeleteViruses(string[] viruses)
 {
     if (viruses.Any(s => !SusadminData.VirusNameExists(s)))
     {
         WriteLine(PrintError("ERROR") + ": virus not found");
     }
     else if (viruses.Any(s => !installedViruses.Contains(SusadminData.GetVirusId(s))))
     {
         WriteLine(PrintError("ERROR") + ": virus not installed");
     }
     else if (new HashSet <string>(viruses.Select(v => v.ToUpper())).Count != viruses.Length)
     {
         WriteLine(PrintError("ERROR") + ": duplicates");
     }
     else
     {
         foreach (Vector2Int id in viruses.Select(s => SusadminData.GetVirusId(s)))
         {
             installedViruses.Remove(id);
         }
     }
 }
Exemplo n.º 3
0
    private IEnumerator ProcessCommand()
    {
        Action EndCommandProcessing = () => {
            this.command     = "";
            shouldUpdateText = true;
            readyToWrite     = true;
        };
        string command = this.command.ToLower().Trim();

        if (command == "")
        {
            EndCommandProcessing();
            yield break;
        }
        if (Regex.IsMatch(command, "^status( |$)"))
        {
            if (command.Split(' ').Where(s => s != "").Count() > 1)
            {
                WriteLine(PrintError("ERROR") + ": invalid arguments count");
            }
            else
            {
                WriteLine(PrintOSVersion());
                WriteLine("Installed security protocols:");
                foreach (int sp in securityProtocols)
                {
                    WriteLine("    " + PrintSecurityProtocolName(sp));
                }
                WriteLine("Safety Level: " + (safetyLevelIsPublic ? PrintNumber(safetyLevel.ToString()) : PrintError("UNKNOWN")));
                if (installedViruses.Count > 0)
                {
                    WriteLine("Installed viruses:");
                    int           i    = 0;
                    List <string> line = new List <string>();
                    foreach (Vector2Int virusId in installedViruses)
                    {
                        if (i++ % 4 == 0 && i != 1)
                        {
                            WriteLine(PrintError("  " + line.Join("   ")));
                            line.Clear();
                        }
                        line.Add(SusadminData.GetVirusName(virusId));
                    }
                    WriteLine(PrintError("  " + line.Join("   ")));
                }
            }
            EndCommandProcessing();
            yield break;
        }
        if (Regex.IsMatch(command, "^info( |$)"))
        {
            string[] args = command.Split(' ').Skip(1).Where(s => s != "").ToArray();
            if (args.Length == 0)
            {
                WriteLine(PrintError("ERROR") + ": invalid arguments count");
                EndCommandProcessing();
                yield break;
            }
            foreach (string virusName in args)
            {
                if (!SusadminData.VirusNameExists(virusName))
                {
                    WriteLine(PrintError("ERROR") + ": virus not found");
                    break;
                }
                Vector2Int id = SusadminData.GetVirusId(virusName);
                WriteLine(string.Format(" {0}: ci={1}; p={2}", virusName.ToUpper(), compatibilityIndices[id.x][id.y], virusesPower[id.x][id.y]));
            }
            EndCommandProcessing();
            yield break;
        }
        if (Regex.IsMatch(command, @"^(install|add)( |$)"))
        {
            string[] args = command.Split(' ').Where(s => s != "").Skip(1).ToArray();
            if (args.Length == 0)
            {
                WriteLine(PrintError("ERROR") + ": invalid arguments count");
            }
            else
            {
                yield return(Loader("Installing", 4 * args.Length));

                InstallViruses(args);
                WriteLine("Installed");
            }
            EndCommandProcessing();
            yield break;
        }
        if (Regex.IsMatch(command, @"^(delete|del|remove|rm)( | $)"))
        {
            string[] args = command.Split(' ').Where(s => s != "").Skip(1).ToArray();
            if (args.Length == 0)
            {
                WriteLine(PrintError("ERROR") + ": invalid arguments count");
            }
            else
            {
                yield return(Loader("Deletion", 4 * args.Length));

                DeleteViruses(args);
                WriteLine("Deleted");
            }
            EndCommandProcessing();
            yield break;
        }
        if (Regex.IsMatch(command, @"^clear( |$)"))
        {
            if (command.Split(' ').Where(s => s != "").Count() > 1)
            {
                WriteLine(PrintError("ERROR") + ": invalid arguments count");
            }
            else
            {
                yield return(Loader("Deletion all viruses", 4 * installedViruses.Count));

                installedViruses = new List <Vector2Int>();
                Debug.LogFormat("[SUSadmin #{0}] All viruses deleted", moduleId);
                WriteLine("All viruses deleted");
            }
            EndCommandProcessing();
            yield break;
        }
        if (Regex.IsMatch(command, @"^activate( |$)"))
        {
            if (command.Split(' ').Where(s => s != "").Count() > 1)
            {
                WriteLine(PrintError("ERROR") + ": invalid arguments count");
                EndCommandProcessing();
                yield break;
            }
            if (installedViruses.Count == 0)
            {
                WriteLine(PrintError("ERROR") + ": no viruses to activate");
                EndCommandProcessing();
                yield break;
            }
            yield return(Loader("Activation", 4 * installedViruses.Count));

            HashSet <int> compatibilityIndices = new HashSet <int>(installedViruses.Select(id => this.compatibilityIndices[id.x][id.y]));
            HashSet <int> sp = new HashSet <int>(securityProtocols);
            int           minCompatibilityIndex = compatibilityIndices.Min();
            int           maxCompatibilityIndex = compatibilityIndices.Max();
            int           totalPower            = installedViruses.Select(id => virusesPower[id.x][id.y]).Sum();
            Debug.LogFormat("[SUSadmin #{0}] Submitted viruses: {1}", moduleId, InstalledVirusesName.Join(", "));
            Debug.LogFormat("[SUSadmin #{0}] Submitted total power: {1}", moduleId, totalPower);
            if (maxCompatibilityIndex - minCompatibilityIndex > vulnerability)
            {
                Debug.LogFormat("[SUSadmin #{0}] Viruses conflict. Min: {1}. Max: {2}", moduleId, minCompatibilityIndex, maxCompatibilityIndex);
                WriteLine(PrintError("ERROR" + ": Viruses conflict"));
                yield return(Loader(PrintError("STRIKE")));

                WriteLine("STRIKE");
                Module.HandleStrike();
            }
            else if (installedViruses.Any(id => !SusadminData.VirusIsInvisible(id, sp)))
            {
                string detectedVirusName = SusadminData.GetVirusName(installedViruses.First(id => !SusadminData.VirusIsInvisible(id, sp)));
                Debug.LogFormat("[SUSadmin #{0}] Virus detected: {1}", moduleId, detectedVirusName);
                WriteLine(string.Format("{0}: Virus {1} detected", PrintError("ERROR"), PrintError(detectedVirusName)));
                yield return(Loader(PrintError("STRIKE")));

                WriteLine("STRIKE");
                Module.HandleStrike();
            }
            else if (totalPower < safetyLevel)
            {
                Debug.LogFormat("[SUSadmin #{0}] Expected total power: {1}", moduleId, safetyLevel);
                WriteLine(string.Format("{0}: OS detected viruses", PrintError("ERROR")));
                WriteLine("OS safety level: " + PrintNumber(safetyLevel.ToString()));
                safetyLevelIsPublic = true;
                yield return(Loader(PrintError("STRIKE")));

                WriteLine("STRIKE");
                Module.HandleStrike();
            }
            else
            {
                forceSolved = false;
                Debug.LogFormat("[SUSadmin #{0}] Module solved", moduleId);
                WriteLine("Network damaged!");
                yield return(Loader("Solving module"));

                WriteLine("Module solved");
                linePointer = linePointer == 0 ? LINES_COUNT : linePointer - 1;
                solved      = true;
                Module.HandlePass();
                yield return(SelfDestruct());

                yield break;
            }
            EndCommandProcessing();
            yield break;
        }
        WriteLine(PrintError("ERROR") + ": Unknown command");
        EndCommandProcessing();
    }