private void moveUnit(GameCommand cmdReceived, PlayerGame playerGame, Game game, PlayerUnit myUnit, int targetYpos, int targetXpos) { MapTile targetMapTile = game.Map.MapTiles.Where(c => c.Xpos == targetXpos && c.Ypos == targetYpos).FirstOrDefault(); if (targetMapTile == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Map position not found (out of map bounds)").Save(this.FAppPrivate); } else { if (!targetMapTile.IsAccessible) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Map position type is inaccessible").Save(this.FAppPrivate); } else { PlayerUnit unitsOnPosition = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Unit.Position.Xpos == targetXpos && c.Unit.Position.Ypos == targetYpos).FirstOrDefault(); if (unitsOnPosition != null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Map position is occupied").Save(this.FAppPrivate); } else { //yes can move there myUnit.Unit.Position.Ypos = targetYpos; myUnit.Unit.Position.Xpos = targetXpos; UpdateRadarRange(playerGame, game, myUnit.Unit); myUnit.Save(this.FAppPrivate); //List<PlayerMapTile> pmtOthers = this.flPlayerTiles.TypedItems<PlayerMapTile>().Where(c => c.Tile.Xpos == targetXpos && c.Tile.Ypos == targetYpos && c.UserId != playerGame.UserId && c.GameId == game.Id).ToList(); //foreach (PlayerMapTile pmt in pmtOthers) //{ // PlayerUnit pu = CnC.Helpers.DeepClone.CloneJson<PlayerUnit>(myUnit); // pu.Key = null; // pu.UserId = pmt.UserId; // pu.Unit.UserId = playerGame.UserId; // pu.Save(this.FAppPrivate); //} // PlayerUnit pu = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Unit.Id == myUnit.Unit.Id && c.UserId == pmt.UserId).FirstOrDefault(); // if(pu == null) // { // pu = CnC.Helpers.DeepClone.CloneJson<PlayerUnit>(myUnit); // pu.Unit.UserId = pmt.UserId; // pu.UserId = playerGame.UserId; // } // else // { // pu.Unit.Position.Xpos = targetXpos; // pu.Unit.Position.Ypos = targetYpos; // } // pu.Save(this.FAppPrivate); new CommandFeedback(cmdReceived, enCommandStatus.Accepted, myUnit.Unit.Name + " moved").Save(this.FAppPrivate); } } } }
private void AddUnit(GameCommand cmdReceived, PlayerGame playerGame, Game game) { Unit newUnitType = game.AvailableUnits.Where(c => c.Id == cmdReceived.ObjectId).FirstOrDefault(); if (newUnitType == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, newUnitType.Name + " not found in the game").Save(this.FAppPrivate); } else { if (playerGame == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "PlayerGame not found").Save(this.FAppPrivate); } else { if (this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.UserId == cmdReceived.UserId && c.Unit.UnitTypeId == newUnitType.UnitTypeId && c.Unit.UserId == playerGame.UserId).Count() >= newUnitType.MaxNumberOf) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Already too many " + newUnitType.Name + "s in the game").Save(this.FAppPrivate); } else { // Unit unitsOnPosition = game.UnitsInGame.Where(c => c.Position.Xpos == playerGame.Spawn.Xpos && c.Position.Ypos == playerGame.Spawn.Ypos).FirstOrDefault(); PlayerUnit unitsOnPosition = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Unit.Position.Xpos == playerGame.Spawn.Xpos && c.Unit.Position.Ypos == playerGame.Spawn.Ypos).FirstOrDefault(); if (unitsOnPosition != null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Could not spawn, spawn is occupied").Save(this.FAppPrivate); } else { Unit newUnit = DeepClone.CloneJson<Unit>(newUnitType); newUnit.Id = Guid.NewGuid().ToString(); newUnit.CreatedAt = DateTime.Now; newUnit.UpdatedAt = DateTime.Now; newUnit.Position.Xpos = playerGame.Spawn.Xpos; newUnit.Position.Ypos = playerGame.Spawn.Ypos; newUnit.UserId = cmdReceived.UserId; game.UnitsInGame.Add(newUnit); //TEST game.Save(this.FAppPublic); PlayerUnit newPlayerUnit = new PlayerUnit(game.Id, playerGame.UserId, newUnit); newPlayerUnit.Save(this.FAppPrivate); UpdateRadarRange(playerGame, game, newUnit); //update map for player //playerGame.Units.Add(newUnit); //playerGame.Save(this.FAppPrivate); //subscribe unit to game ticker this.gameUpdateList.Add(new GameSubscriber() { FireObject = playerGame, GameObject = newUnit, Game = game, Unit = newUnit }); new CommandFeedback(cmdReceived, enCommandStatus.Accepted, newUnitType.Name + " added to the game").Save(this.FAppPrivate); } } } } }