public bool PlaceShip(string socketId) { ShipSelection selection = GetSelection(socketId); ShipType type = GetShipType(socketId); int count = selection.Count; int size = selection.Size; size--; if (size == 0) { count--; if (count == 0) { selection.IsSelected = false; SaveButton(selection); _context.SaveChanges(); return(false); } size = type.Size; } selection.Size = size; selection.Count = count; _context.SaveChanges(); return(true); }
public void SaveButton(ShipSelection selection) { string buttonId = selection.ButtonId; switch (buttonId) { case "0": selection.Button0IsRemoved = true; break; case "1": selection.Button1IsRemoved = true; break; case "2": selection.Button2IsRemoved = true; break; case "3": selection.Button3IsRemoved = true; break; case "4": selection.Button4IsRemoved = true; break; default: break; } _context.SaveChanges(); }
public void MarkSelection(ShipType type, Player player, string buttonId) { ShipSelection temp = GetSelection(player.Socket); if (temp == null) { temp = new ShipSelection(); temp.Player = player; temp.ShipTypeId = type.ShipTypeId; temp.Count = type.Count; temp.Size = type.Size; temp.ButtonId = buttonId; temp.IsSelected = true; _context.ShipSelections.Add(temp); _context.SaveChanges(); } else { temp.ShipTypeId = type.ShipTypeId; temp.Count = type.Count; temp.Size = type.Size; temp.ButtonId = buttonId; temp.IsSelected = true; _context.SaveChanges(); } }
public void HandleSelection(bool playSound = true) { characterFaceImage.sprite = shipPlatforms[currentShipIndex].GetCharacterFaceSprite(); if (currentShipIndex == 0) { selectedShip = ShipSelection.Victor; } else if (currentShipIndex == 1) { selectedShip = ShipSelection.Oscar; } else if (currentShipIndex == 2) { selectedShip = ShipSelection.Sanic; } else if (currentShipIndex == 3) { selectedShip = ShipSelection.Cristina; } if (playSound) { shipPlatforms[currentShipIndex].GetComponent <AudioSource>().Play(); } }
/* * checking if ShipSelection table is generated for this player * otherwise, it means that the selection button was not selected * also, we check if IsSelected field is equal to true * otherwise, it means that all ships have been placed already * or a new ship type has not been selected */ public bool IsValid(string socketId) { ShipSelection selection = GetSelection(socketId); if (selection == null) { return(false); } else { return(selection.IsSelected); } }
public override UndoResult Undo(string socketId) { PlacementCommand command = _context.Commands.Where(s => s.SocketId.Contains(socketId)).LastOrDefault(); if (command != null) { //restoring ShipSelection to previous iteration ShipSelection selection = shipSelectionController.GetSelection(socketId); selection.Count = command.SelectionCount; selection.IsSelected = command.SelectionIsSelected; selection.Size = command.SelectionSize; selection.ShipTypeId = command.SelectionShipTypeId; selection.ButtonId = command.SelectionButtonId; selection.Button0IsRemoved = command.SelectionButton0IsRemoved; selection.Button1IsRemoved = command.SelectionButton1IsRemoved; selection.Button2IsRemoved = command.SelectionButton2IsRemoved; selection.Button3IsRemoved = command.SelectionButton3IsRemoved; selection.Button4IsRemoved = command.SelectionButton4IsRemoved; _context.SaveChanges(); List <bool> buttons = new List <bool>(); buttons.Add(command.SelectionButton0IsRemoved); buttons.Add(command.SelectionButton1IsRemoved); buttons.Add(command.SelectionButton2IsRemoved); buttons.Add(command.SelectionButton3IsRemoved); buttons.Add(command.SelectionButton4IsRemoved); string activeButton = command.SelectionButtonId; //deleting all cells with the last place ship id List <Coordinate> coordinates = new List <Coordinate>(); Ship shipToRemove = GetShip(GetCell(command.CellId).ShipId); List <Cell> cells = GetCellsByShip(GetCell(command.CellId).ShipId); foreach (var cell in cells) { coordinates.Add(new Coordinate(cell.PosX, cell.PosY)); _context.Cells.Remove(cell); _context.SaveChanges(); } _context.Ships.Remove(shipToRemove); _context.Commands.Remove(command); _context.SaveChanges(); return(new UndoResult(coordinates, activeButton, buttons)); } return(null); }
public bool CorrectPlacement(ShipType type, ShipSelection selection, Ship ship, int arenaId, int posX, int posY) { int shipPartsSelected = type.Size - selection.Size; //check if nearby ship is from the same sequence if (NoShipNearby(posX, posY, arenaId, ship, false)) { List <string> directions = new List <string> { "above", "below", "right", "left" }; for (int i = 0; i < 4; i++) { int sequence = ShipSequence(shipPartsSelected, posX, posY, ship, directions[i], arenaId); if (sequence == shipPartsSelected) { return(true); } } } return(false); }
//creating a record of this command, so it can be undone later public void SaveCommand(Cell cell, ShipSelection selection, string socketId) { PlacementCommand command = new PlacementCommand(); command.CellId = cell.CellId; command.IsArmor = false; command.SocketId = socketId; //saving selection current values command.SelectionShipSelectionId = selection.ShipSelectionId; command.SelectionCount = selection.Count; command.SelectionIsSelected = selection.IsSelected; command.SelectionSize = selection.Size; command.SelectionShipTypeId = selection.ShipTypeId; command.SelectionButtonId = selection.ButtonId; command.SelectionButton0IsRemoved = selection.Button0IsRemoved; command.SelectionButton1IsRemoved = selection.Button1IsRemoved; command.SelectionButton2IsRemoved = selection.Button2IsRemoved; command.SelectionButton3IsRemoved = selection.Button3IsRemoved; command.SelectionButton4IsRemoved = selection.Button4IsRemoved; _context.Commands.Add(command); _context.SaveChanges(); }
public override bool MatchesSelection(ShipSelection selection) { return(selection is EnemyShipSelection); }
public void Reflect(ShipSelection shipSelection) { this.Source = shipSelection; ProcessEnabled(); }
public override bool MatchesSelection(ShipSelection selection) { return(selection is FriendlyShipSelection); }
public ShipType GetShipType(string socketId) { ShipSelection selection = GetSelection(socketId); return(_context.ShipTypes.Where(s => s.ShipTypeId.Equals(selection.ShipTypeId)).FirstOrDefault()); }
public string GetButtonId(string socketId) { ShipSelection selection = GetSelection(socketId); return(selection.ButtonId); }
public List <CommandOutcome> ValidatePlacement(string socketId, int posX, int posY, int arenaId) { ShipSelection selection = GetSelection(socketId); ShipType type = GetShipType(socketId); Cell cell = ReturnCell(posX, posY, arenaId); List <CommandOutcome> commands = new List <CommandOutcome>(); if (cell == null) { if (NoCellsNearby(posX, posY, arenaId, type)) { List <Ship> ships = GetShipsByType(type.ShipTypeId, socketId); int shipId = 0; Console.WriteLine("!!!!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!!!!!!!!!!!!!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!!!!!!!!!~~~~~~~~~~~~~ + COUNT[" + selection.Count + "] SIZE[" + selection.Size + "]"); if (ships.Count < type.Count - selection.Count + 1) { if (NoShipNearby(posX, posY, arenaId, new Ship(), true)) { shipId = AddShip(arenaId, type.ShipTypeId, type.Type, type.Size, socketId, posX, posY); //cell was created when adding a new ship cell = ReturnCell(posX, posY, arenaId); cell.ShipId = shipId; SaveCommand(cell, selection, socketId); if (PlaceShip(socketId)) { commands.Add(new CommandOutcome(PlacementOutcome.Ship, posX, posY)); } else { string id = GetButtonId(socketId); CommandOutcome outcome = new CommandOutcome(PlacementOutcome.LastShip, posX, posY); outcome.idToRemove = id; commands.Add(outcome); } _context.SaveChanges(); } else { commands.Add(new CommandOutcome(PlacementOutcome.Invalid)); } } else { shipId = ships[ships.Count - 1].ShipId; if (!CorrectPlacement(type, selection, ships[ships.Count - 1], arenaId, posX, posY)) { commands.Add(new CommandOutcome(PlacementOutcome.Invalid)); } else { cell = CreateCell(posX, posY, arenaId); cell.ShipId = shipId; if (PlaceShip(socketId)) { commands.Add(new CommandOutcome(PlacementOutcome.Ship, posX, posY)); } else { string id = GetButtonId(socketId); CommandOutcome outcome = new CommandOutcome(PlacementOutcome.LastShip, posX, posY); outcome.idToRemove = id; commands.Add(outcome); } _context.SaveChanges(); Ship ship = GetShip(shipId); Cell preCell = GetCell(ship.CellId); if (preCell.PosX < posX) { //RIGHT for (int i = 1; i < ship.RemainingTiles - 1; i++) { cell = CreateCell(posX + i, posY, arenaId); cell.ShipId = shipId; if (PlaceShip(socketId)) { commands.Add(new CommandOutcome(PlacementOutcome.Ship, posX + i, posY)); } else { string id = GetButtonId(socketId); CommandOutcome outcome = new CommandOutcome(PlacementOutcome.LastShip, posX + i, posY); outcome.idToRemove = id; commands.Add(outcome); } _context.SaveChanges(); } } else if (preCell.PosX > posX) { //LEFT for (int i = 1; i < ship.RemainingTiles - 1; i++) { cell = CreateCell(posX - i, posY, arenaId); cell.ShipId = shipId; if (PlaceShip(socketId)) { commands.Add(new CommandOutcome(PlacementOutcome.Ship, posX - i, posY)); } else { string id = GetButtonId(socketId); CommandOutcome outcome = new CommandOutcome(PlacementOutcome.LastShip, posX - i, posY); outcome.idToRemove = id; commands.Add(outcome); } _context.SaveChanges(); } } else if (preCell.PosY < posY) { //UP for (int i = 1; i < ship.RemainingTiles - 1; i++) { cell = CreateCell(posX, posY + i, arenaId); cell.ShipId = shipId; if (PlaceShip(socketId)) { commands.Add(new CommandOutcome(PlacementOutcome.Ship, posX, posY + i)); } else { string id = GetButtonId(socketId); CommandOutcome outcome = new CommandOutcome(PlacementOutcome.LastShip, posX, posY + i); outcome.idToRemove = id; commands.Add(outcome); } _context.SaveChanges(); } } else { //DOWN for (int i = 1; i < ship.RemainingTiles - 1; i++) { cell = CreateCell(posX, posY - i, arenaId); cell.ShipId = shipId; if (PlaceShip(socketId)) { commands.Add(new CommandOutcome(PlacementOutcome.Ship, posX, posY - i)); } else { string id = GetButtonId(socketId); CommandOutcome outcome = new CommandOutcome(PlacementOutcome.LastShip, posX, posY - i); outcome.idToRemove = id; commands.Add(outcome); } _context.SaveChanges(); } } } } } else { commands.Add(new CommandOutcome(PlacementOutcome.Invalid)); } } else { commands.Add(new CommandOutcome(PlacementOutcome.Invalid)); } return(commands); }
public abstract bool MatchesSelection(ShipSelection selection);