Example #1
0
        public HackOrder CreateHackOrder(int instituteId, int computerId, string to, int type,
                                         int variable, int conditional, int iterator, int json, int _class, int breakpoint,
                                         int knowledge, int ingenyous, int coffee)
        {
            Computer  computer  = repository.GetComputer(computerId).Result;
            Institute institute = repository.GetInstitute(instituteId).Result;

            //Comprobaciones de naves
            if (variable > computer.Script.Variable ||
                conditional > computer.Script.Conditional ||
                iterator > computer.Script.Iterator ||
                json > computer.Script.Json ||
                _class > computer.Script.Class ||
                breakpoint > computer.Script.BreakPoint)
            {
                return(null);
            }

            /*//Comprobaciones de ip
             * if (!CheckIpHasComputer(instituteId, to))
             *  return null;*/

            //Comprobaciones de carga
            if (knowledge > computer.Resource.Knowledge ||
                ingenyous > computer.Resource.Ingenyous ||
                coffee > computer.Resource.Coffee)
            {
                return(null);
            }

            //Cálculo distancia
            int distance = Math.Abs(parseIpToNumber(computer.IpDirection) - parseIpToNumber(to));

            //Comprobaciones de café
            int totalScripts = variable + conditional + iterator + json + _class + breakpoint;
            int needCoffe    = totalScripts * distance;

            if (needCoffe > computer.Resource.Coffee)
            {
                return(null);
            }

            // Los gastos de recursos y naves
            computer.Resource.Knowledge -= knowledge;
            computer.Resource.Ingenyous -= ingenyous;
            computer.Resource.Coffee    -= coffee + needCoffe;

            computer.Script.Variable    -= variable;
            computer.Script.Conditional -= conditional;
            computer.Script.Iterator    -= iterator;
            computer.Script.Json        -= json;
            computer.Script.Class       -= _class;
            computer.Script.BreakPoint  -= breakpoint;

            //repository.NotAsyncUpdateResource(computer.Resource);
            Computer c = repository.UpdateComputer(computer).Result;

            Computer computerTo = repository.GetComputer(instituteId, to);

            HackOrder hackOrder = new HackOrder(
                computerId,
                type == 4 ? -1 : computerTo.Id,
                (int)(distance * 60 / institute.RateTime),
                variable,
                conditional,
                iterator,
                json,
                _class,
                breakpoint,
                type,
                knowledge,
                ingenyous,
                coffee,
                to
                );

            return(repository.SaveHackOrder(hackOrder).Result);
        }