コード例 #1
0
        static void ProccessGenerateResourceOps()
        {
            while (generateResourceRequestOps.TryDequeue(out var op))
            {
                var asteroidEntityId = op.EntityId.Id;

                var response = new ResourceGenerationReponse();

                if (ResourcesInventorySystem.TryGetResourceInfo(asteroidEntityId, 0, out var resourceInfo))
                {
                    response.databaseId = resourceInfo.databaseId;
                    response.type       = resourceInfo.type;
                    response.quantity   = resourceInfo.quantity;
                }
                else
                {
                    var position = PositionsSystem.GetComponent(asteroidEntityId).coords;

                    var time = (long)(DateTime.Now.ToUniversalTime() - centuryBegin).TotalSeconds;

                    var lowSample  = ProbabilityMap.EvaluateLowSample(position.x, position.y, position.z, time);
                    var medSample  = ProbabilityMap.EvaluateMedSample(position.x, position.y, position.z, time);
                    var highSample = ProbabilityMap.EvaluateHighSample(position.x, position.y, position.z, time);

                    var sample = ProbabilityMap.LayeringSample(lowSample, medSample, highSample);

                    var      seed = asteroidEntityId ^ BitConverter.ToInt64(Encoding.UTF8.GetBytes(op.Request.userDatabaseId), 0);
                    IRandomU prng = new XoShiRo128starstar(seed);

                    var scanner = op.Request.scanner;

                    var quantity = QuantityGenerator.Sample(sample, prng, scanner);

                    if (quantity > 0)
                    {
                        response.quantity = quantity;

                        var scannerResource = scanner.speciality;
                        if (scannerResource == ResourceType.Random)
                        {
                            scannerResource = (ResourceType)prng.Next(1, (int)ResourceType.Count);
                        }

                        response.type = scannerResource;

                        var resourceDBId = Helpers.GenerateCloudFireStoreRandomDocumentId(prng);
                        var asteroidDBId = Helpers.GenerateCloudFireStoreRandomDocumentId(prng);

                        response.databaseId = resourceDBId;

                        SpatialOSConnectionSystem.addPersistenceOps.Enqueue(
                            new AddComponentOp <PersistenceData>
                        {
                            EntityId = op.EntityId,
                            Data     = new PersistenceData()
                        });

                        var addComponentOp = new AddComponentOp <IdentificationData>
                        {
                            EntityId = op.EntityId,
                            Data     = new IdentificationData
                            {
                                entityDatabaseId = asteroidDBId,
                            }
                        };

                        IdentificationsSystem.OnComponentAdded(addComponentOp);
                        SpatialOSConnectionSystem.addIdentificationOps.Enqueue(addComponentOp);

                        ResourcesInventorySystem.QueueAddResourceOp(asteroidEntityId, resourceInfo.databaseId, new Resource(resourceInfo.type, resourceInfo.quantity));

                        var asteroidRef = CloudFirestoreInfo.Database.Collection(CloudFirestoreInfo.AsteroidsCollection).Document(asteroidDBId);
                        asteroidRef.CreateAsync(new Dictionary <string, object> {
                            { CloudFirestoreInfo.CoordinatesField, new double[3] {
                                  position.x, position.y, position.z
                              } }
                        });
                    }
                }

                SpatialOSConnectionSystem.responseGenerateResourceOps.Enqueue(
                    new CommandResponseOp <Harvestable.Commands.GenerateResource, ResourceGenerationReponse>//just using CommandResponseOp as container for request raw id and response
                {
                    EntityId  = new EntityId(),
                    RequestId = new RequestId <OutgoingCommandRequest <Harvestable.Commands.GenerateResource> >(op.RequestId.Id),
                    Response  = response,
                });
            }
        }
コード例 #2
0
        static void ProcessExtractResourceOps()
        {
            while (extractResourceRequestOps.TryDequeue(out var op))
            {
                var commandRequest   = op.Request;
                var asteroidEntityId = op.EntityId.Id;

                var response = new ResourceExtractionReponse();

                if (ResourcesInventorySystem.TryGetResourceInfo(asteroidEntityId, 0, out var resourceInfo))
                {
                    int extractionRate = commandRequest.extractRate; //flat amount
                    if (extractionRate < 0)                          //percent amount
                    {
                        extractionRate = (int)(-1.0d / extractionRate * resourceInfo.quantity);
                    }

                    int actualAmountExtracted = extractionRate;

                    ResourcesInventorySystem.QueueResourceQuantityDeltaOp(asteroidEntityId, 0, -actualAmountExtracted);

                    if (resourceInfo.quantity <= extractionRate)
                    {
                        actualAmountExtracted = resourceInfo.quantity;

                        SpatialOSConnectionSystem.deleteEntityOps.Enqueue(new EntityId(asteroidEntityId));

                        var asteroidRef = CloudFirestoreInfo.Database.Collection(CloudFirestoreInfo.AsteroidsCollection).Document(IdentificationsSystem.GetEntityDBId(asteroidEntityId));
                        asteroidRef.DeleteAsync();
                    }

                    response.databaseId = resourceInfo.databaseId;
                    response.quantity   = actualAmountExtracted;
                    response.type       = resourceInfo.type;
                }

                SpatialOSConnectionSystem.responseExtractResourceOps.Enqueue(
                    new CommandResponseOp <Harvestable.Commands.ExtractResource, ResourceExtractionReponse>
                {
                    EntityId  = new EntityId(asteroidEntityId),
                    RequestId = new RequestId <OutgoingCommandRequest <Harvestable.Commands.ExtractResource> >(op.RequestId.Id),
                    Response  = response,
                });
            }
        }