Skip to content

Commit

Permalink
Build 0.12.1alpha: Move animations available in single player mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
michi84o committed Nov 1, 2018
1 parent 48883e1 commit 1befa58
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 58 deletions.
85 changes: 67 additions & 18 deletions AccessBattle/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public enum GamePhase
/// </summary>
public class Game : PropChangeNotifier, IBoardGame // TODO: IDisposeable
{
string _lastExecutedCommand;
public string LastExecutedCommand
{
get => _lastExecutedCommand;
set => SetProp(ref _lastExecutedCommand, value);
}

GamePhase _phase;
/// <summary>
/// Current game phase.
Expand Down Expand Up @@ -266,6 +273,7 @@ public virtual async Task<bool> ExecuteCommand(string command, int player)
if (string.IsNullOrEmpty(command)) return false;

var cmd = command.Trim();
var cmdCpy = cmd;

#region Deploy Command "dp"
// Deployment command is a command that contains 4 'L' and 4 'V' characters.
Expand Down Expand Up @@ -315,15 +323,16 @@ public virtual async Task<bool> ExecuteCommand(string command, int player)
_hasDeployed[player] = true;
if (_hasDeployed[0] && _hasDeployed[1])
BeginTurns();
LastExecutedCommand = CreateLastExecutedCommand(cmdCpy, player);
return true;
}
#endregion

#region Move command "mv"
if (cmd.StartsWith("mv ", StringComparison.InvariantCultureIgnoreCase) && command.Length > 3)
if (cmd.StartsWith("mv ", StringComparison.InvariantCultureIgnoreCase) && cmd.Length > 3)
{
command = command.Substring(3).Trim();
var split = command.Split(new[] { ',' });
cmd = cmd.Substring(3).Trim();
var split = cmd.Split(new[] { ',' });
if (split.Length != 4) return false;
ReplaceLettersWithNumbers(ref split);
uint x1, x2, y1, y2;
Expand Down Expand Up @@ -360,6 +369,7 @@ public virtual async Task<bool> ExecuteCommand(string command, int player)
{
// Could not move card, error!
Phase = GamePhase.Aborted;
LastExecutedCommand = CreateLastExecutedCommand(cmdCpy, player);
return true;
}
if (card2.HasBoost)
Expand All @@ -371,6 +381,7 @@ public virtual async Task<bool> ExecuteCommand(string command, int player)
if (field1.Card != null)
{
// Could not move card, error!
LastExecutedCommand = CreateLastExecutedCommand(cmdCpy, player);
Phase = GamePhase.Aborted;
return true;
}
Expand All @@ -380,15 +391,16 @@ public virtual async Task<bool> ExecuteCommand(string command, int player)
field2.Card = field1.Card;
field1.Card = null;
SwitchPlayerTurnPhase();
LastExecutedCommand = CreateLastExecutedCommand(cmdCpy, player);
return true;
}
#endregion

#region Boost command "bs"
if (command.StartsWith("bs ", StringComparison.InvariantCultureIgnoreCase) && command.Length > 3)
if (cmd.StartsWith("bs ", StringComparison.InvariantCultureIgnoreCase) && cmd.Length > 3)
{
command = command.Substring(3).Trim();
var split = command.Split(new[] { ',' });
cmd = cmd.Substring(3).Trim();
var split = cmd.Split(new[] { ',' });
if (split.Length != 3) return false;

ReplaceLettersWithNumbers(ref split);
Expand Down Expand Up @@ -427,6 +439,7 @@ public virtual async Task<bool> ExecuteCommand(string command, int player)
return false; // Boost already placed
card1.HasBoost = true;
SwitchPlayerTurnPhase();
LastExecutedCommand = CreateLastExecutedCommand(cmdCpy, player);
return true;
}

Expand All @@ -436,15 +449,16 @@ public virtual async Task<bool> ExecuteCommand(string command, int player)

card1.HasBoost = false;
SwitchPlayerTurnPhase();
LastExecutedCommand = CreateLastExecutedCommand(cmdCpy, player);
return true;
}
#endregion

#region Firewall command "fw"
if (cmd.StartsWith("fw ", StringComparison.InvariantCulture) && command.Length > 3)
if (cmd.StartsWith("fw ", StringComparison.InvariantCulture) && cmdCpy.Length > 3)
{
command = command.Substring(3).Trim();
var split = command.Split(new[] { ',' });
cmd = cmd.Substring(3).Trim();
var split = cmd.Split(new[] { ',' });
if (split.Length != 3) return false;

ReplaceLettersWithNumbers(ref split);
Expand Down Expand Up @@ -479,23 +493,25 @@ public virtual async Task<bool> ExecuteCommand(string command, int player)

field1.Card = PlayerFirewallCards[player - 1];
SwitchPlayerTurnPhase();
LastExecutedCommand = CreateLastExecutedCommand(cmdCpy, player);
return true;
}

// enabled == 0
if (!(card1 is FirewallCard) || card1.Owner?.PlayerNumber != player) return false;
field1.Card = null;
SwitchPlayerTurnPhase();
LastExecutedCommand = CreateLastExecutedCommand(cmdCpy, player);
return true;
}
#endregion

#region Virus Check command "vc"

if (command.StartsWith("vc ", StringComparison.InvariantCultureIgnoreCase) && command.Length > 3)
if (cmd.StartsWith("vc ", StringComparison.InvariantCultureIgnoreCase) && cmd.Length > 3)
{
command = command.Substring(3).Trim();
var split = command.Split(new[] { ',' });
cmd = cmd.Substring(3).Trim();
var split = cmd.Split(new[] { ',' });
if (split.Length != 2) return false;

ReplaceLettersWithNumbers(ref split);
Expand Down Expand Up @@ -523,17 +539,18 @@ public virtual async Task<bool> ExecuteCommand(string command, int player)
card1.IsFaceUp = true;
Players[player - 1].DidVirusCheck = true;
SwitchPlayerTurnPhase();
LastExecutedCommand = CreateLastExecutedCommand(cmdCpy, player);
return true;
}

#endregion

#region Error 404 command "er"

if (command.StartsWith("er ", StringComparison.InvariantCultureIgnoreCase) && command.Length > 3)
if (cmd.StartsWith("er ", StringComparison.InvariantCultureIgnoreCase) && cmd.Length > 3)
{
command = command.Substring(3).Trim();
var split = command.Split(new[] { ',' });
cmd = cmd.Substring(3).Trim();
var split = cmd.Split(new[] { ',' });
if (split.Length != 5) return false;

ReplaceLettersWithNumbers(ref split);
Expand Down Expand Up @@ -584,6 +601,7 @@ public virtual async Task<bool> ExecuteCommand(string command, int player)

Players[player - 1].Did404NotFound = true;
SwitchPlayerTurnPhase();
LastExecutedCommand = CreateLastExecutedCommand(cmdCpy, player);
return true;
}

Expand Down Expand Up @@ -821,6 +839,38 @@ public static List<BoardField> GetMoveTargetFields(IBoardGame game, BoardField f
}

#endregion

public static string CreateLastExecutedCommand(string command, int currentPlayer)
{
string lastCommand = null;
// Send make last command visible to opponent of not secret
//var cmd = Game.ReplaceAltSyntax(cmdMsg.Command);
var cmd = command.Trim();
if (cmd.StartsWith("mv", StringComparison.InvariantCultureIgnoreCase) ||
cmd.StartsWith("bs", StringComparison.InvariantCultureIgnoreCase) ||
cmd.StartsWith("fw", StringComparison.InvariantCultureIgnoreCase) ||
cmd.StartsWith("vc", StringComparison.InvariantCultureIgnoreCase))
{
lastCommand = currentPlayer.ToString() + ":" + Game.ReplaceAltSyntax(cmd);
}
else if (cmd.StartsWith("er", StringComparison.InvariantCultureIgnoreCase))
{
// This command contains secret information
cmd = Game.ReplaceAltSyntax(cmd);
cmd = cmd.Substring(3).Trim();
var split = cmd.Split(new[] { ',' });
if (split.Length == 5) // The last split is the secret information
{
cmd = "er ";
for (int i = 0; i < 4; ++i)
cmd += split[i] + ",";
cmd += "?";
lastCommand = currentPlayer.ToString() + ":" + cmd;
}
}
return lastCommand;
}

}

/// <summary>Class for local singleplayer games.</summary>
Expand Down Expand Up @@ -867,6 +917,7 @@ public void SetAi(IArtificialIntelligence ai, int playerNumber = 2)
Players[playerNumber - 1].Name = ai.Name;
}

// TODO: SyncRequired might be invoked too often
/// <summary>
/// Executes the command and automatically tells the AI to make its move.
/// </summary>
Expand Down Expand Up @@ -907,8 +958,6 @@ public override async Task<bool> ExecuteCommand(string command, int player)
Phase = GamePhase.Aborted;
}

// TODO Set LastExecutedCommand in ViewModel of client to enable animation

// Can happen after Deployment
if (Phase == GamePhase.Player2Turn)
{
Expand All @@ -919,8 +968,8 @@ public override async Task<bool> ExecuteCommand(string command, int player)
Phase = GamePhase.Aborted;
}
}
SyncRequired?.Invoke(this, EventArgs.Empty);

SyncRequired?.Invoke(this, EventArgs.Empty);
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions AccessBattle/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static class Log
/// <summary>Log priority.</summary>
public static LogPriority Priority = LogPriority.Verbose;



/// <summary>
/// Set logging mode.
/// </summary>
Expand Down
32 changes: 4 additions & 28 deletions AccessBattle/Networking/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -764,34 +764,10 @@ void ProcessPacket(NetworkPacket packet, NetworkPlayer player)
if (oldPhase == GamePhase.Deployment && game.Phase == GamePhase.Deployment)
return; // The following game sync would reset the cards of the player who hasn't deployed yet

string lastCommand = null;
//string lastCommand = null;
if (result)
{
// Send make last command visible to opponent of not secret
//var cmd = Game.ReplaceAltSyntax(cmdMsg.Command);
var cmd = cmdMsg.Command.Trim();
if (cmd.StartsWith("mv", StringComparison.InvariantCultureIgnoreCase) ||
cmd.StartsWith("bs", StringComparison.InvariantCultureIgnoreCase) ||
cmd.StartsWith("fw", StringComparison.InvariantCultureIgnoreCase) ||
cmd.StartsWith("vc", StringComparison.InvariantCultureIgnoreCase))
{
lastCommand = playerNum.ToString() + ":" + Game.ReplaceAltSyntax(cmd);
}
else if (cmd.StartsWith("er", StringComparison.InvariantCultureIgnoreCase))
{
// This command contains secret information
cmd = Game.ReplaceAltSyntax(cmd);
cmd = cmd.Substring(3).Trim();
var split = cmd.Split(new[] { ',' });
if (split.Length == 5) // The last split is the secret information
{
cmd = "er ";
for (int i = 0; i < 4; ++i)
cmd += split[i] + ",";
cmd += "?";
lastCommand = playerNum.ToString() + ":" + cmd;
}
}
//lastCommand = Game.CreateLastExecutedCommand(cmdMsg.Command, playerNum);

// Apply ELO
if ((oldPhase == GamePhase.Player1Turn || oldPhase == GamePhase.Player2Turn) &&
Expand Down Expand Up @@ -825,8 +801,8 @@ void ProcessPacket(NetworkPacket packet, NetworkPlayer player)
var syncP1 = GameSync.FromGame(game, game.UID, 1);
var syncP2 = GameSync.FromGame(game, game.UID, 2);

syncP1.LastExecutedCommand = lastCommand;
syncP2.LastExecutedCommand = lastCommand;
//syncP1.LastExecutedCommand = lastCommand;
//syncP2.LastExecutedCommand = lastCommand;

Send(JsonConvert.SerializeObject(syncP1, _serializerSettings), NetworkPacketType.GameSync, p1.Connection, p1.ClientCrypto);
Send(JsonConvert.SerializeObject(syncP2, _serializerSettings), NetworkPacketType.GameSync, p2.Connection, p2.ClientCrypto);
Expand Down
1 change: 1 addition & 0 deletions AccessBattle/Networking/Packets/GameSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static GameSync FromGame(Game game, uint id, int player)
Player1 = game.Players[0].GetSync(),
Player2 = game.Players[1].GetSync(),
FieldsWithCards = fieldsWithCard,
LastExecutedCommand = game.LastExecutedCommand
};

if (player != 1 && player != 2) return sync;
Expand Down
4 changes: 2 additions & 2 deletions AccessBattle/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.12.0.0")]
[assembly: AssemblyFileVersion("0.12.0")]
[assembly: AssemblyVersion("0.12.1.0")]
[assembly: AssemblyFileVersion("0.12.1")]
4 changes: 2 additions & 2 deletions AccessBattleAI/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.12.0")]
[assembly: AssemblyFileVersion("0.12.0")]
[assembly: AssemblyVersion("0.12.1")]
[assembly: AssemblyFileVersion("0.12.1")]
4 changes: 2 additions & 2 deletions AccessBattleConsole/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.12.0")]
[assembly: AssemblyFileVersion("0.12.0")]
[assembly: AssemblyVersion("0.12.1")]
[assembly: AssemblyFileVersion("0.12.1")]
4 changes: 2 additions & 2 deletions AccessBattleMySql/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.12.0")]
[assembly: AssemblyFileVersion("0.12.0")]
[assembly: AssemblyVersion("0.12.1")]
[assembly: AssemblyFileVersion("0.12.1")]
4 changes: 2 additions & 2 deletions AccessBattleServer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.12.0")]
[assembly: AssemblyFileVersion("0.12.0")]
[assembly: AssemblyVersion("0.12.1")]
[assembly: AssemblyFileVersion("0.12.1")]
4 changes: 2 additions & 2 deletions AccessBattleWpf/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.12.0")]
[assembly: AssemblyFileVersion("0.12.0")]
[assembly: AssemblyVersion("0.12.1")]
[assembly: AssemblyFileVersion("0.12.1")]

0 comments on commit 1befa58

Please sign in to comment.