Beispiel #1
0
        public void TestLockable()
        {
            SpacegameServer.Core.Core Instance = SpacegameServer.Core.Core.Instance;


            SpacegameServer.Core.User x = Mock.MockUserAndAdd(Instance);

            int shipId1 = (int)Instance.identities.shipLock.getNext();
            int shipId2 = (int)Instance.identities.shipLock.getNext();

            SpacegameServer.Core.Ship Ship1, Ship2;
            Ship1 = new SpacegameServer.Core.Ship(shipId1);
            Ship2 = new SpacegameServer.Core.Ship(shipId2);

            SpacegameServer.Core.Colony Colony1;
            Colony1 = new SpacegameServer.Core.Colony(1);

            //Object can only be locked once
            Assert.IsTrue(Ship1.setLock());
            Assert.IsFalse(Ship1.setLock());

            //LockAll of Ships can only be set once
            Assert.IsTrue(SpacegameServer.Core.Ship.setLockAll());
            Assert.IsFalse(SpacegameServer.Core.Ship.setLockAll());

            //ships can't be lcoked anymore, colonies can
            Assert.IsFalse(Ship1.setLock());
            Assert.IsFalse(Ship2.setLock());
            Assert.IsTrue(Colony1.setLock());
            Ship1.removeLock();
            Colony1.removeLock();

            //after removing LockAll, all is lockable again
            SpacegameServer.Core.Ship.removeLockAll();
            Assert.IsTrue(Ship1.setLock());
            Assert.IsTrue(Ship2.setLock());
            Assert.IsTrue(Colony1.setLock());
        }
Beispiel #2
0
        private static bool TransferChecks(int userId, UserSpaceObject sender, UserSpaceObject receiver, Transfer transfer, Ship senderShip, Ship targetShip, Colony senderColony, Colony targetColony)
        {
            //check that sender is ownder by user
            if (sender.GetUserId() != userId) return false;

            var SenderUser = Core.Instance.users[sender.GetUserId()];
            var ReceiverUser = Core.Instance.users[receiver.GetUserId()];

            //check that one does not move cargo from a neutral (0) space station (hullId 201)
            if (sender is Ship)
            {
                var SendShip = (Ship)sender;
                if (SendShip.hullid == 201 && SendShip.userid == 0) return false;
            }
            if (receiver is Ship)
            {
                var RecShip = (Ship)receiver;
                if (RecShip.hullid == 201 && RecShip.userid == 0) return false;
            }


            //test if goods are sent or received:
            if (transfer.Goods.Count == 0) return false;

            var send = false;
            var receive = false;

            receive = (transfer.Goods.Any(e => e.Qty < 0));
            send = (transfer.Goods.Any(e => e.Qty > 0));

            if (!send && !receive) return false;


            //if trade is between users, and goods are received, check that they are at war           
            if (receive && sender.GetUserId() != receiver.GetUserId() && receiver.GetUserId() != 0)
            {
                Relation CurrentRelation = Core.Instance.userRelations.getRelation(SenderUser, ReceiverUser);
                if (SenderUser.allianceId == ReceiverUser.allianceId) CurrentRelation = Relation.AllianceMember;


                //check that both are enemies (or player 0  is involved)
                if (receive && CurrentRelation != Relation.War && CurrentRelation != Relation.AllianceMember)
                {
                    return false;
                }
            }

            //test that both are on the same field  (if it is not scrap or recycle=
            if (transfer.Target > 0)
            {
                Field SenderField = null;
                Tuple<byte, byte> SenderSystemXY = null;

                if (senderShip != null)
                {
                    SenderField = senderShip.field;
                    SenderSystemXY = senderShip.getSystemCoords();
                }

                if (senderColony != null)
                {
                    SenderField = senderColony.field;
                    SenderSystemXY = senderColony.systemXY();
                }

                Field TargetField = null;
                Tuple<byte, byte> TargetSystemXY = null;

                if (targetShip != null)
                {
                    TargetField = targetShip.field;
                    TargetSystemXY = targetShip.getSystemCoords();
                }

                if (targetColony != null)
                {
                    TargetField = targetColony.field;
                    TargetSystemXY = targetColony.systemXY();
                }

                if (SenderField != TargetField) return false;
                if (SenderSystemXY != null && TargetSystemXY == null) return false;
                if (SenderSystemXY == null && TargetSystemXY != null) return false;
                if (SenderSystemXY != null && (SenderSystemXY.Item1 != TargetSystemXY.Item1 || SenderSystemXY.Item2 != TargetSystemXY.Item2)) return false;
            }

            //tests that all goods are in store on both sides of the transfer
            List<SpaceObjectStock> SenderStock = null;
            List<SpaceObjectStock> TargetStock = null;
            if (senderShip != null) SenderStock = senderShip.goods.ToList<SpaceObjectStock>();
            if (targetShip != null) TargetStock = targetShip.goods.ToList<SpaceObjectStock>();
            if (senderColony != null) SenderStock = senderColony.goods.ToList<SpaceObjectStock>();
            if (targetColony != null) TargetStock = targetColony.goods.ToList<SpaceObjectStock>();

            int TransferCapacity = 0;
            foreach (var transferLine in transfer.Goods){
                var Good = Core.Instance.Goods[transferLine.Id];

                TransferCapacity += Good.Weight() * transferLine.Qty;

                if (transferLine.Qty > 0)
                {
                    //check that sender has it on store
                    if (!SenderStock.Any(e => e.goodsId == transferLine.Id && e.amount >= transferLine.Qty)) return false;
                }
                else
                {
                    //check that target has it on store
                    if (!TargetStock.Any(e => e.goodsId == transferLine.Id && e.amount >= -transferLine.Qty)) return false;
                }

            }

            //test that scrap or recycle does not demand something in return. transfer can be sent to user 0  -> that will always be a scrap or recycle command
            if (receiver.GetUserId() == 0 && ( receiver.Id == 0 || receiver.Id == -1) )
            {
                if (receive) return false;
            }
 
            //sender + receiver : sufficient cargoSpace
            if (TransferCapacity < 0)
            {
                int RemainigFreeSpace = sender.CalcStorage() - sender.AmountOnStock();
                //check SenderStock:
                if (RemainigFreeSpace < -TransferCapacity) return false;
            }
            else
            {
                if (receiver.GetUserId() != 0)
                {
                    int RemainigFreeSpace = receiver.CalcStorage() - receiver.AmountOnStock();
                    //check ReceiverStock:
                    if (RemainigFreeSpace < TransferCapacity) return false;
                }
            }



            return true;
        }
Beispiel #3
0
        public static bool BuildModules(int userId, SpacegameServer.Core.Transfer transfer, ref string xml)
        {
            SpacegameServer.Core.Core core = SpacegameServer.Core.Core.Instance;

            if (!core.colonies.ContainsKey(transfer.Sender))
            {
                return(false);
            }

            Colony colony = core.colonies[transfer.Sender];

            if (colony.userId != userId)
            {
                return(false);
            }

            User user = Core.Instance.users[userId];

            //check if modules are researched:
            foreach (var moduleLine in transfer.Goods)
            {
                if (moduleLine.Qty < 0)
                {
                    return(false);
                }

                //check that the Module exists
                if (!Core.Instance.Modules.Any(e => e != null && e.goodsId == moduleLine.Id))
                {
                    return(false);
                }

                Module toBuild = Core.Instance.Modules.First(e => e != null && e.goodsId == moduleLine.Id);

                if (!user.hasModuleResearch(toBuild))
                {
                    return(false);
                }
            }

            if (!Modules.checkGoodsAvailability(colony, transfer))
            {
                return(false);
            }

            List <Lockable> elementsToLock = new List <Lockable>(3);

            elementsToLock.Add(colony);

            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return(false);
            }

            try
            {
                if (!Modules.checkGoodsAvailability(colony, transfer))
                {
                    return(false);
                }

                //remove TemplateModules
                foreach (var moduleLine in transfer.Goods)
                {
                    Module toBuild = core.Modules.First(e => e != null && e.goodsId == moduleLine.Id);

                    foreach (var costs in toBuild.ModulesCosts)
                    {
                        colony.addGood(costs.goodsId, -costs.amount * moduleLine.Qty, false);
                    }

                    colony.addGood(toBuild.goodsId, moduleLine.Qty, false);
                }

                Core.Instance.dataConnection.saveColonyGoods(colony);
            }
            catch (Exception ex)
            {
                SpacegameServer.Core.Core.Instance.writeExceptionToLog(ex);
            }
            finally
            {
                //release the ressources and return true
                LockingManager.unlockAll(elementsToLock);
            }

            return(true);
        }
Beispiel #4
0
 public void saveColonyGoods(SpacegameServer.Core.Colony colony)
 {
 }
Beispiel #5
0
 public Task saveColony(SpacegameServer.Core.Colony colony, object command)
 {
     return(Task.FromResult(0));
 }
Beispiel #6
0
 public void saveSingleColony(SpacegameServer.Core.Colony colony)
 {
 }
Beispiel #7
0
 public void saveColonyFull(SpacegameServer.Core.SolarSystemInstance planet, SpacegameServer.Core.Colony colony, bool createSurfaceFiels = true)
 {
 }
Beispiel #8
0
        //similar to Ship.createSpaceStation(), but not easily generalizable
        public bool build2(int shipTemplateId, int _userId, int _colonyId, bool fastBuild, ref int newShipId, ref ShipBuildErrorCode errorCode, ref int errorValue)
        {
            Core      core      = Core.Instance;
            GalaxyMap galaxyMap = null;

            // lock colony and field
            Colony colony = core.colonies[_colonyId];

            if (colony == null)
            {
                return(false);
            }
            if (colony.userId != _userId)
            {
                return(false);
            }

            Field        field    = colony.field;
            ShipTemplate template = core.shipTemplate[shipTemplateId];

            if (!(this.checkFreeOrbit(field, _userId, colony.systemXY(), ref errorCode, ref errorValue) &&
                  this.checkGoodsAvailability(colony, template, fastBuild, ref errorCode, ref errorValue) &&
                  this.checkSpaceport(colony, ref errorCode, ref errorValue) &&
                  this.checkTechnology(template, _userId, fastBuild, ref errorCode, ref errorValue) &&
                  this.checkUniqueness(field, template, colony.systemXY(), ref errorCode, ref errorValue)))
            {
                return(false);
            }

            //Lock
            List <Lockable> elementsToLock = new List <Lockable>(3);

            elementsToLock.Add(colony);
            elementsToLock.Add(field);

            //check for transcendence //ToDo: replace 220 with a sql data field
            if (template.hullid == 220 && core.GalaxyMap.transcendenceRequirement == 0)
            {
                galaxyMap = core.GalaxyMap;
                elementsToLock.Add(galaxyMap);
            }

            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return(false);
            }

            newShipId = (int)Core.Instance.identities.shipLock.getNext();
            try
            {
                //all checks again inside of lock
                if (!(this.checkFreeOrbit(field, _userId, colony.systemXY(), ref errorCode, ref errorValue) &&
                      this.checkGoodsAvailability(colony, template, fastBuild, ref errorCode, ref errorValue) &&
                      this.checkSpaceport(colony, ref errorCode, ref errorValue) &&
                      this.checkTechnology(template, _userId, fastBuild, ref errorCode, ref errorValue) &&
                      this.checkUniqueness(field, template, colony.systemXY(), ref errorCode, ref errorValue)))
                {
                    return(false);
                }


                //everything checked and locked
                //now do the work
                Ship newShip = buildShip(newShipId, template, field, _userId, colony, fastBuild);

                newShip.SetTranscension(galaxyMap);

                //write SQL
                Core.Instance.dataConnection.insertShip(newShip);
                Core.Instance.dataConnection.saveColonyGoods(colony);
            }
            catch (Exception ex)
            {
                SpacegameServer.Core.Core.Instance.writeExceptionToLog(ex);
            }
            finally
            {
                //release the ressources
                LockingManager.unlockAll(elementsToLock);
            }

            return(true);
        }
Beispiel #9
0
 public void getColonyBuildings(SpacegameServer.Core.Core _core, SpacegameServer.Core.Colony colony)
 {
 }
Beispiel #10
0
 public void getColonyStock(SpacegameServer.Core.Core _core, SpacegameServer.Core.Colony _colony, bool _refresh = false)
 {
 }
Beispiel #11
0
        public static bool build(int userId, int colonyId, int tileNr, short buildingId, ref int newBuildingId)
        {
            SpacegameServer.Core.Core core = SpacegameServer.Core.Core.Instance;

            //check against research
            if (!Core.Instance.users.ContainsKey(userId))
            {
                return(false);
            }
            var User = Core.Instance.users[userId];

            if (!User.hasGameObjectEnabled(3, buildingId))
            {
                return(false);
            }


            Colony colony = core.colonies[colonyId];

            if (colony == null)
            {
                return(false);
            }

            Building template = core.Buildings[buildingId];

            if (template == null)
            {
                return(false);
            }

            //check on building count in case of mines, hydrocarbon, rare chemicals
            if (buildingId == 2 || buildingId == 6)
            {
                if (ColonyBuildingActions.countBuildings(colony, buildingId) >= ColonyBuildingActions.allowedBuildings(colony, buildingId))
                {
                    return(false);
                }
            }

            //check tile position
            if (colony.colonyBuildings.Any(e => e.planetSurfaceId == tileNr))
            {
                return(false);
            }



            // lock colony
            List <Lockable> elementsToLock = new List <Lockable>(1);

            elementsToLock.Add(colony);
            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return(false);
            }

            try
            {
                //userId may have been changed by another thread, so check it again after locking
                if (colony.userId != userId)
                {
                    colony.removeLock();
                    return(false);
                }

                //test ressources on colony
                var costOK = true;
                foreach (var cost in template.BuildingCosts)
                {
                    if (!colony.goods.Any(e => e.goodsId == cost.goodsId))
                    {
                        costOK = false;
                        break;
                    }

                    if (colony.goods.Find(e => e.goodsId == cost.goodsId).amount < cost.amount)
                    {
                        costOK = false;
                        break;
                    }
                }
                if (!costOK)
                {
                    colony.removeLock();
                    return(false);
                }



                if (template.oncePerColony)
                {
                    if (colony.colonyBuildings.Any(e => e.buildingId == template.id))
                    {
                        colony.removeLock();
                        return(false);
                    }
                }

                //Special Ressourcen

                /*
                 * if (template.id > 1029 && template.id < 1035)
                 * {
                 *  var star = Core.Instance.stars[colony.starId];
                 *  if (star.ressourceid != template.id - 1030)
                 *  {
                 *      colony.removeLock();
                 *      return false;
                 *  }
                 * }
                 */

                //Create Building
                var newId = (int)core.identities.colonyBuildingId.getNext();
                newBuildingId = newId;
                var building = new ColonyBuilding(core, newId, colony, template, tileNr, userId);

                /*
                 * var building = new ColonyBuilding(newId);
                 * building.colonyId = colony.id;
                 * building.colony = colony;
                 * building.planetSurfaceId = tileNr;
                 * building.userId = userId;
                 * building.buildingId  = buildingId;
                 * building.isActive = true;
                 * building.underConstruction = false;
                 * building.remainingHitpoint = 100;
                 * building.building = template;
                 * colony.colonyBuildings.Add(building);
                 * Core.Instance.colonyBuildings.Add(newId, building);
                 */


                foreach (var cost in template.BuildingCosts)
                {
                    colony.addGood(cost.goodsId, -cost.amount);
                }


                /*check auf:
                 *          TODO - Feld frei?
                 *          TODO - Forschung
                 */


                //Todo: Scanrange, CommNode
                if (building.buildingId == 51)
                {
                    building.colony.scanRange = Math.Max(building.colony.scanRange, (byte)7);
                    core.dataConnection.saveSingleColony(building.colony);
                }

                if (building.buildingId == 64)
                {
                    building.colony.scanRange = Math.Max(building.colony.scanRange, (byte)9);
                    core.dataConnection.saveSingleColony(building.colony);
                }

                core.dataConnection.saveColonyBuildings(building);
                core.dataConnection.saveColonyGoods(colony);

                CommunicationNode.createCommNodeBuilding(building);

                //Core.Instance.dataConnection.buildBuilding(userId,  colonyId,  tileNr,  buildingId, ref xml);

                // get colony data (goods, later population (colony ships))
                //Core.Instance.dataConnection.getColonyStock(core, colony);
                //Core.Instance.dataConnection.getColonyBuildings(core, colony);
            }
            catch (Exception ex)
            {
                core.writeExceptionToLog(ex);
            }
            finally
            {
                LockingManager.unlockAll(elementsToLock);
            }
            return(true);
        }
Beispiel #12
0
        public void getAreaCreator(byte _direction, int _userId, Ship _ship, ref List <Ship> _ships, ref List <SpacegameServer.Core.SystemMap> _stars, ref List <SpacegameServer.Core.Colony> _colonies, Colony scanningColony = null)
        {
            var starXY = getDestinationField(_ship, _direction);

            if (_userId < 0)
            {
                return;
            }
            SpacegameServer.Core.User user = (SpacegameServer.Core.User)users[_userId];
            if (user == null)
            {
                return;
            }

            //fetch an area of radius 7 around the target field
            //check all colonies inside it. Every colony that has the targetfield inside it's border range is returned...
            //List<Field> neigbouringFields = new List<Field>();
            //GeometryIndex.getFields(_ship.field, 7, neigbouringFields);

            //int targetRegionId = GeometryIndex.calcRegionId(starXY.Key, starXY.Value);
            //var targetField = GeometryIndex.regions[targetRegionId].findOrCreateField(starXY.Key, starXY.Value);

            int targetRegionId = GeometryIndex.calcRegionId(starXY.Key, starXY.Value);
            var targetField    = GeometryIndex.regions[targetRegionId].findOrCreateField(starXY.Key, starXY.Value);

            //fetch all colonies affecting the targetField
            _colonies = Core.Instance.colonies.Where(colony => targetField.InfluencedBy.Any(influencer => influencer == colony.Value)).Select(colKeyValue => colKeyValue.Value).ToList();


            //fetch the star each colony is positioned
            foreach (int fieldId in (_colonies.Select(colony => colony.field.id)))
            {
                Field currentField = ((Field)GeometryIndex.allFields[fieldId]);

                if (currentField.starId != null)
                {
                    int       starId = (int)currentField.starId;
                    SystemMap star   = stars[starId];
                    _stars.Add(star);

                    //if scan is determined for a single ship, check if that user already kwos the systems scanned:
                    if ((_ship != null) && !user.knownStars.Contains(starId))
                    {
                        user.knownStars.Add(starId);
                        UserStarMap newStar = new UserStarMap(user.id, starId);

                        List <AsyncInsertable> newKnownStar = new List <AsyncInsertable>();
                        newKnownStar.Add(newStar);

                        dataConnection.insertAsyncTransaction(newKnownStar);
                    }
                }
            }

            return;
        }
Beispiel #13
0
        public void getUserScans(int _userId, Ship _ship, ref List <Ship> _ships, ref List <SpacegameServer.Core.SystemMap> _stars, ref List <SpacegameServer.Core.Colony> _colonies, Colony scanningColony = null)
        {
            if (_userId < 0)
            {
                return;
            }
            SpacegameServer.Core.User user = (SpacegameServer.Core.User)users[_userId];
            if (user == null)
            {
                return;
            }


            //during startup, add all colonies of stars known to the user
            if (_ship == null && scanningColony == null)
            {
                _colonies = Core.Instance.stars.Where(star => user.knownStars.Any(known => known == star.Value.id))
                            .SelectMany(star => star.Value.planets)
                            .Where(planet => planet.colony != null)
                            .Select(planet => planet.colony).ToList();

                /*foreach (int starId in user.knownStars)
                 * {
                 *  if (Core.Instance.stars[starId].planets.Any(planet => planet.colony != null))
                 *  {
                 *      _colonies = Core.Instance.stars[starId].planets.Where(planet => planet.colony != null).Select(planet => planet.colony).ToList();
                 *  }
                 * }
                 */
            }


            //make a list with unique fields, only the scanner with the greates scanrange is needed
            Dictionary <Int32, KeyValuePair <Field, byte> > fieldScans = new Dictionary <int, KeyValuePair <Field, byte> >();

            if (_ship != null || scanningColony != null)
            {
                if (_ship != null)
                {
                    fieldScans[_ship.field.id] = new KeyValuePair <Field, byte>(_ship.field, _ship.scanRange);
                }

                if (scanningColony != null)
                {
                    fieldScans[scanningColony.field.id] = new KeyValuePair <Field, byte>(scanningColony.field, scanningColony.scanRange);
                }
            }
            else
            {
                for (int i = 0; i < user.ships.Count; i++)
                {
                    Ship currentShip = user.ships[i];
                    KeyValuePair <Field, byte> val;
                    if (fieldScans.TryGetValue(user.ships[i].field.id, out val))
                    {
                        if (val.Value < currentShip.scanRange)
                        {
                            fieldScans[currentShip.field.id] = new KeyValuePair <Field, byte>(currentShip.field, currentShip.scanRange);
                        }
                    }
                    else
                    {
                        fieldScans[currentShip.field.id] = new KeyValuePair <Field, byte>(currentShip.field, currentShip.scanRange);
                    }
                }

                for (int i = 0; i < user.colonies.Count; i++)
                {
                    Colony currentColony = user.colonies[i];
                    KeyValuePair <Field, byte> val;

                    if (fieldScans.TryGetValue(currentColony.field.id, out val))
                    {
                        if (val.Value < currentColony.scanRange)
                        {
                            fieldScans[currentColony.field.id] = new KeyValuePair <Field, byte>(currentColony.field, currentColony.scanRange);
                        }
                    }
                    else
                    {
                        fieldScans[currentColony.field.id] = new KeyValuePair <Field, byte>(currentColony.field, currentColony.scanRange);
                    }
                }
            }
            //find all neighbouring fields of the scanners
            //use the regions to accomplish this
            List <int> scannedFields = new List <int>();

            foreach (KeyValuePair <Int32, KeyValuePair <Field, byte> > scanner in fieldScans)
            {
                scanner.Value.Key.getScanRange(scanner.Value.Value, scannedFields);
            }

            //detect all ships, stars and colonies on the fields scanned
            foreach (int fieldId in (scannedFields.Distinct()))
            {
                Field currentField = ((Field)GeometryIndex.allFields[fieldId]);
                foreach (Ship ship in currentField.ships)
                {
                    _ships.Add(ship);
                }

                _ships = _ships.OrderBy(o => o.id).ToList();

                if (currentField.starId != null)
                {
                    int       starId = (int)currentField.starId;
                    SystemMap star   = stars[starId];
                    _stars.Add(star);

                    foreach (Colony colony in currentField.colonies)
                    {
                        if (!_colonies.Any(c => c == colony))
                        {
                            _colonies.Add(colony);
                        }
                    }

                    //if scan is determined for a single ship, check if that user already kwos the systems scanned:
                    if ((_ship != null) && !user.knownStars.Contains(starId))
                    {
                        user.knownStars.Add(starId);
                        UserStarMap newStar = new UserStarMap(user.id, starId);

                        List <AsyncInsertable> newKnownStar = new List <AsyncInsertable>();
                        newKnownStar.Add(newStar);

                        dataConnection.insertAsyncTransaction(newKnownStar);
                    }
                }
            }


            //add usermap and transcensionStars to the list of known stars:
            if (_ship == null && scanningColony == null)
            {
                foreach (int starId in user.knownStars)
                {
                    if (!_stars.Exists(x => x.id == starId))
                    {
                        _stars.Add(this.stars[starId]);
                    }
                }

                List <Ship> transcensions =
                    (from ship in this.ships
                     where ship.Value.shipTranscension != null
                     select ship.Value).ToList();
                foreach (Ship transc in transcensions)
                {
                    if (transc.systemId == 0)
                    {
                        continue;
                    }
                    if (!_stars.Exists(x => x.id == transc.systemId))
                    {
                        _stars.Add(this.stars[transc.systemId]);
                    }
                }
            }

            return;
        }
Beispiel #14
0
        public List <Field> getUserScannedFields(int _userId, Ship _ship, Colony scanningColony = null)
        {
            List <Field> scanned = new List <Field>();

            if (_userId < 0)
            {
                return(scanned);
            }
            SpacegameServer.Core.User user = (SpacegameServer.Core.User)users[_userId];
            if (user == null)
            {
                return(scanned);
            }

            //make a list with unique fields, only the scanner with the greates scanrange is needed
            Dictionary <Int32, KeyValuePair <Field, byte> > fieldScans = new Dictionary <int, KeyValuePair <Field, byte> >();

            if (_ship != null || scanningColony != null)
            {
                if (_ship != null)
                {
                    fieldScans[_ship.field.id] = new KeyValuePair <Field, byte>(_ship.field, _ship.scanRange);
                }

                if (scanningColony != null)
                {
                    fieldScans[scanningColony.field.id] = new KeyValuePair <Field, byte>(scanningColony.field, scanningColony.scanRange);
                }
            }
            else
            {
                for (int i = 0; i < user.ships.Count; i++)
                {
                    Ship currentShip = user.ships[i];
                    KeyValuePair <Field, byte> val;
                    if (fieldScans.TryGetValue(user.ships[i].field.id, out val))
                    {
                        if (val.Value < currentShip.scanRange)
                        {
                            fieldScans[currentShip.field.id] = new KeyValuePair <Field, byte>(currentShip.field, currentShip.scanRange);
                        }
                    }
                    else
                    {
                        fieldScans[currentShip.field.id] = new KeyValuePair <Field, byte>(currentShip.field, currentShip.scanRange);
                    }
                }

                for (int i = 0; i < user.colonies.Count; i++)
                {
                    Colony currentColony = user.colonies[i];
                    KeyValuePair <Field, byte> val;

                    if (fieldScans.TryGetValue(currentColony.field.id, out val))
                    {
                        if (val.Value < currentColony.scanRange)
                        {
                            fieldScans[currentColony.field.id] = new KeyValuePair <Field, byte>(currentColony.field, currentColony.scanRange);
                        }
                    }
                    else
                    {
                        fieldScans[currentColony.field.id] = new KeyValuePair <Field, byte>(currentColony.field, currentColony.scanRange);
                    }
                }
            }
            //find all neighbouring fields of the scanners
            //use the regions to accomplish this
            List <int> scannedFields = new List <int>();

            foreach (KeyValuePair <Int32, KeyValuePair <Field, byte> > scanner in fieldScans)
            {
                scanner.Value.Key.getScanRange(scanner.Value.Value, scannedFields);
            }


            //detect all ships, stars and colonies on the fields scanned
            foreach (int fieldId in (scannedFields.Distinct()))
            {
                scanned.Add((Field)GeometryIndex.allFields[fieldId]);
            }



            return(scanned);
        }
Beispiel #15
0
        //everything is already checked, locked, checked again
        //write and unlock will also be done by the caller
        public Ship buildShip(int newShipId, ShipTemplate template, Field field, int userId, Colony colony, bool fastBuild)
        {
            Ship newShip = new Ship(newShipId);

            newShip.userid      = userId;
            newShip.FormerOwner = userId;
            newShip.initFromTemplate(template, newShip.id);
            newShip.initFromField(field);
            newShip.initFromColony(colony);

            StatisticsCalculator.calc(newShip, Core.Instance);

            Core.Instance.ships[newShip.id] = newShip;
            Core.Instance.addShipToField(newShip);
            Core.Instance.users[userId].ships.Add(newShip);


            //remove ressources
            removeGoods(colony, template, fastBuild);

            return(newShip);
        }
Beispiel #16
0
        public static bool transfer2(int userId, SpacegameServer.Core.Transfer transfer, ref string xml)
        {
            SpacegameServer.Core.Core core = SpacegameServer.Core.Core.Instance;

            SpacegameServer.Core.UserSpaceObject sender = null;
            SpacegameServer.Core.UserSpaceObject receiver = null;
            Ship SenderShip = null;
            Ship TargetShip = null;
            Colony SenderColony = null;
            Colony TargetColony = null;


            int senderUserId = 0;
            int receiverUserId = 0;
            //for sender and receiver
            if (transfer.Sender < 1 || transfer.SenderType < 1 || transfer.TargetType < 1) return false;

            if (transfer.SenderType == 1)
            {
                if (core.ships.ContainsKey(transfer.Sender)) 
                {
                    SenderShip = core.ships[transfer.Sender]; 
                    sender = core.ships[transfer.Sender];
                    senderUserId = SenderShip.userid;
                }
            }
            if (transfer.SenderType == 2)
            {
                if (core.colonies.ContainsKey(transfer.Sender)) 
                {
                    SenderColony = core.colonies[transfer.Sender];
                    sender = core.colonies[transfer.Sender];
                    senderUserId = SenderColony.userId; 
                }
            }

            if (transfer.TargetType == 1)
            {
                if (core.ships.ContainsKey(transfer.Target)) 
                {
                    TargetShip = core.ships[transfer.Target];
                    receiver = core.ships[transfer.Target]; 
                    receiverUserId = core.ships[transfer.Target].userid;
                }
                else
                {
                    
                }
            }
            if (transfer.TargetType == 2)
            {
                if (core.colonies.ContainsKey(transfer.Target)) 
                {
                    TargetColony = core.colonies[transfer.Target]; 
                    receiver = core.colonies[transfer.Target];
                    receiverUserId = TargetColony.userId; 
                }

                //mock a target for srap and recycling actions
                if (transfer.Target < 1)
                {
                    TargetShip = Ship.createTransferMock();
                    receiver = TargetShip;
                    receiverUserId = 0;
                }
            }

            if (sender == null || receiver == null) return false;


            if (!TransferChecks(userId, sender, receiver, transfer, SenderShip, TargetShip, SenderColony, TargetColony)) return false;

            List<Lockable> toLock = new List<Lockable>();
            toLock.Add(sender);
            if (receiver.GetUserId() != 0)
            { 
                toLock.Add(receiver); 
            }

            if (!LockingManager.lockAllOrSleep(toLock)) return false;

            if (!TransferChecks(userId, sender, receiver, transfer, SenderShip, TargetShip, SenderColony, TargetColony)) 
            {
                LockingManager.unlockAll(toLock);
                return false; 
            }

            try
            {

                if (SenderShip != null) SenderShip.RemoveAllTrades();
                if (TargetShip != null) TargetShip.RemoveAllTrades();

                foreach (var transferLine in transfer.Goods)
                {
                    //var Good = Core.Instance.Goods[transferLine.Id];
                    sender.addGood((short)transferLine.Id, -transferLine.Qty);

                    //recycle:
                    if (transfer.Target == 0)
                    {
                        Core.Instance.Goods[transferLine.Id].Recycle(sender, transferLine.Qty);
                    }

                    //add goods if it is not scrapping or rececling...
                    if (TargetShip == null || TargetShip.id > 0) receiver.addGood((short)transferLine.Id, transferLine.Qty);
                }

                //Core.Instance.dataConnection.saveShipGoods(this);
                //core.dataConnection.saveColonyGoods(colony);

                if (SenderShip != null) Core.Instance.dataConnection.saveShipGoods(SenderShip);
                if (TargetShip != null && transfer.Target > 0) Core.Instance.dataConnection.saveShipGoods(TargetShip);

                if (SenderColony != null) Core.Instance.dataConnection.saveColonyGoods(SenderColony);
                if (TargetColony != null && transfer.Target > 0) Core.Instance.dataConnection.saveColonyGoods(TargetColony);

            }
            catch (Exception e)
            {
                Core.Instance.writeExceptionToLog(e);
                return false;
            }
            finally
            {
                //unlock
                LockingManager.unlockAll(toLock);
            }


            return true;
        }
Beispiel #17
0
 public void saveColonyFull(SpacegameServer.Core.SolarSystemInstance planet, SpacegameServer.Core.Colony colony)
 {
 }
 public bool addColony(Colony _colony)
 {
     this.colonies.Add(_colony);
     _colony.field = this;
     return(true);
 }