public CommandFeedback(GameCommand command, enCommandStatus status, string feedback) { this.Path = this.GetType().Name; this.CommandId = command.Id; this.CommandStatus = status; this.CommandType = command.Command; this.UserId = command.UserId; this.Feedback = feedback; }
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 MoveUnit(GameCommand cmdReceived, PlayerGame playerGame, Game game) { if (playerGame == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "User did not join game").Save(this.FAppPrivate); } else { if (game == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Game not found").Save(this.FAppPrivate); } else { PlayerGame myPlayerGame = this.flPlayerGames.TypedItems<PlayerGame>().Where(c => c.UserId == cmdReceived.UserId).FirstOrDefault(); if (myPlayerGame == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Player not found in game").Save(this.FAppPrivate); } else { // Unit myUnit = myPlayerGame.Units.Where(c => c.Id == cmdReceived.ObjectId).FirstOrDefault(); PlayerUnit playerUnit = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Id == cmdReceived.ObjectId && c.Unit.UserId == playerGame.UserId).FirstOrDefault(); if (playerUnit == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Unit not found in game").Save(this.FAppPrivate); } else { Unit myUnit = playerUnit.Unit; //can the unit move? if (cmdReceived.Distance > myUnit.MovingRange) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, playerUnit.Unit.Name + " can not move that distance in one move").Save(this.FAppPrivate); } else { int targetYpos = -1; int targetXpos = -1; switch (playerUnit.Unit.Heading) { case enHeading.North: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos); break; case enHeading.NorthWest: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; case enHeading.South: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos); break; case enHeading.SouthWest: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; case enHeading.SouthEast: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.NorthEast: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.East: targetYpos = (int)(myUnit.Position.Ypos); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.West: targetYpos = (int)(myUnit.Position.Ypos); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; default: break; } if (targetXpos > -1 && targetYpos > -1) { moveUnit(cmdReceived, playerGame, game, playerUnit, targetYpos, targetXpos); } } } } } } }
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); } } } } }
private void LeaveGame(GameCommand cmdReceived, PlayerGame playerGame, Game game) { if (playerGame == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "User did not join this game").Save(this.FAppPrivate); } else { if (game == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Could not find this game").Save(this.FAppPrivate); } else { PlayerGame playerToRemove = game.PlayersInGame.Where(c => c.UserId == cmdReceived.UserId).FirstOrDefault(); if (playerToRemove == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Player not found in the game").Save(this.FAppPrivate); } else { game.PlayersInGame.Remove(playerToRemove); game.Save(this.FAppPublic); playerToRemove.Delete(this.FAppPrivate); new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Left the Game").Save(this.FAppPrivate); } } } }
private void JoinGame(GameCommand cmdReceived, Player player, PlayerGame playerGame, Game game) { if (playerGame != null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "User already joined game").Save(this.FAppPrivate); } else { if (game == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Game not found").Save(this.FAppPrivate); } else { if (game.PlayersInGame.Count >= game.MaxNumberOfPlayers) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Already too many players in the game").Save(this.FAppPrivate); } else { PlayerGame newPlayerGame = new PlayerGame(game, cmdReceived.UserId, player.NickName); foreach (MapTile tile in game.Map.MapTiles) { MapTile playerTile = new MapTile(); playerTile.TerrainType = -1;// tile.TerrainType; playerTile.UserId = newPlayerGame.UserId; playerTile.Xpos = tile.Xpos; playerTile.Ypos = tile.Ypos; newPlayerGame.Map.MapTiles.Add(playerTile); //set spawn, check if not occupied first if (newPlayerGame.Spawn == null && tile.IsSpawningPoint) { if (game.PlayersInGame.Where(c => c.Spawn.Xpos == tile.Xpos && c.Spawn.Ypos == tile.Ypos).Count() == 0) { //not used yet newPlayerGame.Spawn = playerTile; PlayerMapTile playerMapTile = new PlayerMapTile(game.Id, cmdReceived.UserId, playerTile); playerTile.TerrainType = 100; playerTile.IsSpawningPoint = true; playerTile.IsAccessible = true; playerMapTile.Save(this.FAppPrivate); } } } newPlayerGame.Save(this.FAppPrivate); game.PlayersInGame.Add(newPlayerGame); game.Save(this.FAppPublic); new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Joined the game").Save(this.FAppPrivate); } } } }
private void RotateUnit(GameCommand cmdReceived, PlayerGame playerGame, Game game) { PlayerUnit playerUnit = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Id == cmdReceived.ObjectId && c.Unit.UserId == playerGame.UserId).FirstOrDefault(); if (playerUnit == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "PlayerUnit not found").Save(this.FAppPrivate); } else { Unit myUnit = playerUnit.Unit; if (myUnit == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Unit not found").Save(this.FAppPrivate); } else { switch (myUnit.Heading) { case enHeading.South: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.SouthEast; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South-East").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.SouthWest; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South-West").Save(this.FAppPrivate); } break; case enHeading.SouthWest: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.South; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.West; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading West").Save(this.FAppPrivate); } break; case enHeading.West: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.SouthWest; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South-West").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.NorthWest; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North-East").Save(this.FAppPrivate); } break; case enHeading.NorthWest: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.West; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading West").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.North; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North").Save(this.FAppPrivate); } break; case enHeading.North: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.NorthWest; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North-West").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.NorthEast; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North-East").Save(this.FAppPrivate); } break; case enHeading.NorthEast: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.North; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.East; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading East").Save(this.FAppPrivate); } break; case enHeading.East: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.NorthEast; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North-East").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.SouthEast; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South-East").Save(this.FAppPrivate); } break; case enHeading.SouthEast: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.East; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading East").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.South; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South").Save(this.FAppPrivate); } break; default: break; } playerUnit.Save(this.FAppPrivate); // playerGame.Save(this.FAppPrivate); // game.UnitsInGame.Where(c => c.Id == myUnit.Id).FirstOrDefault().Heading = myUnit.Heading; //Test game.Save(this.FAppPublic); } } }
private void FireFixedGun(GameCommand cmdReceived, PlayerGame playerGame, Game game) { PlayerUnit playerUnit = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Id == cmdReceived.ObjectId && c.Unit.UserId == playerGame.UserId).FirstOrDefault(); if (playerUnit == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "PlayerUnit not found").Save(this.FAppPrivate); } else { Unit myUnit = playerUnit.Unit; if (myUnit == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Unit not found").Save(this.FAppPrivate); } else { if (!myUnit.HasFixedGun || myUnit.FixedGunFiringRange < cmdReceived.Distance) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Unit doesn't have a fixed gun or distance out of range").Save(this.FAppPrivate); } else { int targetYpos = -1; int targetXpos = -1; switch (myUnit.Heading) { case enHeading.North: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos); break; case enHeading.NorthWest: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; case enHeading.South: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos); break; case enHeading.SouthWest: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; case enHeading.SouthEast: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.NorthEast: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.East: targetYpos = (int)(myUnit.Position.Ypos); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.West: targetYpos = (int)(myUnit.Position.Ypos); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; default: break; } if (targetXpos > -1 && targetYpos > -1) { PlayerUnit pu = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Unit.Position.Xpos == targetXpos && c.Unit.Position.Ypos == targetYpos).FirstOrDefault(); if (pu == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Target missed").Save(this.FAppPrivate); } else { Unit attackedUnit = pu.Unit; attackedUnit.Armor -= myUnit.FixedGunArmorDamage; attackedUnit.Shields -= myUnit.FixedGunShieldDamage; pu.Save(this.FAppPrivate); if (attackedUnit.Armor < 1 || attackedUnit.Shields < 1) { attackedUnit.Status = enUnitStatus.Destroyed; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, attackedUnit.Name + " destroyed!").Save(this.FAppPrivate); pu.Delete(this.FAppPrivate); } else { new CommandFeedback(cmdReceived, enCommandStatus.Accepted, attackedUnit.Name + " hit!, enemy shields: " + attackedUnit.Shields + ", enemy armor: " + attackedUnit.Armor).Save(this.FAppPrivate); } } } } } } }
private void btnFire_Click(object sender, EventArgs e) { if (this.txtPlayerUnitSelected.Text != "") { GameCommand myCommand = new GameCommand(this.me.Id, this.txtPlayerUnitSelected.Text, enCommand.FireFixedGun, distance: 1); myCommand.Save(this.FAppPrivate); } }
private void btnRotateVehicleLeft_Click(object sender, EventArgs e) { if (this.txtPlayerUnitSelected.Text != "") { GameCommand myCommand = new GameCommand(this.me.Id, this.txtPlayerUnitSelected.Text, enCommand.RotateCW, rotation: 45); myCommand.Save(this.FAppPrivate); } }
private void btnAddVehicle_Click(object sender, EventArgs e) { if (this.lbxUnits.SelectedIndex > -1) { GameCommand myCommand = new GameCommand(this.me.Id, this.lbxUnits.SelectedItem.ToString(), enCommand.AddUnit); myCommand.Save(this.FAppPrivate); } }
private void btnLeaveGame_Click(object sender, EventArgs e) { if (this.lbxGames.SelectedIndex > -1) { GameCommand myCommand = new GameCommand(this.me.Id, this.lbxGames.SelectedItem.ToString(), enCommand.LeaveGame); myCommand.Save(this.FAppPrivate); } }