예제 #1
0
        public static void AddExtraDemand(int PlanetId, int NetId, int NodeId, int PowerAmount)
        {
            if (Energy.TryGetValue(PlanetId, out var mapping))
            {
                for (int i = 0; i < mapping.Count; i++)
                {
                    if (mapping[i].NetId == NetId)
                    {
                        bool foundNodeId = false;
                        for (int j = 0; j < mapping[i].NodeId.Count; j++)
                        {
                            if (mapping[i].NodeId[j] == NodeId)
                            {
                                foundNodeId             = true;
                                mapping[i].Activated[j] = true;
                                mapping[i].ExtraPower  += PowerAmount;
                                break;
                            }
                        }

                        if (!foundNodeId)
                        {
                            mapping[i].NodeId.Add(NodeId);
                            mapping[i].Activated.Add(true);
                            mapping[i].ExtraPower += PowerAmount;
                        }

                        return;
                    }
                }

                EnergyMapping map = new PowerTowerManager.EnergyMapping();
                map.NetId = NetId;
                map.NodeId.Add(NodeId);
                map.Activated.Add(true);
                map.ExtraPower = PowerAmount;

                mapping.Add(map);
            }
            else
            {
                List <EnergyMapping> mapping2 = new List <EnergyMapping>();

                EnergyMapping map = new PowerTowerManager.EnergyMapping();
                map.NetId = NetId;
                map.NodeId.Add(NodeId);
                map.Activated.Add(true);
                map.ExtraPower = PowerAmount;

                mapping2.Add(map);
                Energy.Add(PlanetId, mapping2);
            }
        }
예제 #2
0
        public static void AddExtraDemand(int PlanetId, int NetId, int NodeId, int PowerAmount)
        {
            if (Energy.TryGetValue(PlanetId, out var mapping))
            {
                for (int i = 0; i < mapping.Count; i++)
                {
                    if (Monitor.TryEnter(mapping, 100))
                    {
                        try
                        {
                            if (mapping[i].NetId == NetId)
                            {
                                bool foundNodeId = false;
                                for (int j = 0; j < mapping[i].NodeId.Count; j++)
                                {
                                    if (mapping[i].NodeId[j] == NodeId)
                                    {
                                        foundNodeId = true;
                                        mapping[i].Activated[j]++;
                                        mapping[i].ExtraPower += PowerAmount;
                                        break;
                                    }
                                }

                                if (!foundNodeId)
                                {
                                    mapping[i].NodeId.Add(NodeId);
                                    mapping[i].Activated.Add(1);
                                    mapping[i].ExtraPower += PowerAmount;
                                }

                                return;
                            }
                        }
                        finally
                        {
                            Monitor.Exit(mapping);
                        }
                    }
                    else
                    {
                        Log.Warn($"PowerTower: cant wait longer for threading lock, PowerTowers will be desynced!");
                    }
                }

                if (Monitor.TryEnter(mapping, 100))
                {
                    try
                    {
                        EnergyMapping map = new PowerTowerManager.EnergyMapping();
                        map.NetId = NetId;
                        map.NodeId.Add(NodeId);
                        map.Activated.Add(1);
                        map.ExtraPower = PowerAmount;

                        mapping.Add(map);
                    }
                    finally
                    {
                        Monitor.Exit(mapping);
                    }
                }
                else
                {
                    Log.Warn($"PowerTower: cant wait longer for threading lock, PowerTowers will be desynced!");
                }
            }
            else
            {
                List <EnergyMapping> mapping2 = new List <EnergyMapping>();

                EnergyMapping map = new PowerTowerManager.EnergyMapping();
                map.NetId = NetId;
                map.NodeId.Add(NodeId);
                map.Activated.Add(1);
                map.ExtraPower = PowerAmount;

                mapping2.Add(map);
                if (!Energy.TryAdd(PlanetId, mapping2))
                {
                    // if we failed to add then most likely because another thread was faster, so call this again to run the above part of the method.
                    AddExtraDemand(PlanetId, NetId, NodeId, PowerAmount);
                }
            }
        }