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 MainViewModel() { timer = ServiceLocator.Current.GetInstance <ITimerService>(); timer.Tick += Timer_Tick; timer.Start(new TimeSpan(0, 0, 0, 0, 001)); initializeSimulation(); X = 400; Y = 400; isRandomPlacement = false; this.setXAndY(); Add = new AddBoidCommand(this); Exit = new ExitCommand(this); Pause = new PauseCommand(this); Sliders = new SliderHandler(this); ChangePlacement = new PlacementCommand(this); }
//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(); }