private string TransformNode(NodeActionSubmission submission) { //the faction in the submsision is the current faction string newFaction; if (submission.Faction == Constants.BlueFaction) { newFaction = Constants.RedFaction; } else { newFaction = Constants.BlueFaction; } var queryString = string.Format("{0} {1} = @{1}, {2} = @{2} WHERE {3} = @{3}", CommandBuilder.InitUpdateStatement(Constants.NodeTableName), Constants.FactionColumn, Constants.HPColumn, Constants.NameColumn); var command = new SqlCommand(queryString); var paramDictionary = new Dictionary<string, Tuple<SqlDbType, object>>(); paramDictionary.Add(Constants.FactionColumn, new Tuple<SqlDbType, object>(SqlDbType.NVarChar, newFaction)); paramDictionary.Add(Constants.HPColumn, new Tuple<SqlDbType, object>(SqlDbType.BigInt, BaseHP)); paramDictionary.Add(Constants.NameColumn, new Tuple<SqlDbType, object>(SqlDbType.NVarChar, submission.Name)); CommandBuilder.AddParameters(paramDictionary, command); var result = _connector.ExecuteNonQuery(command); if (result != 1) { throw new DataException("Transforming a node is not successful"); } return newFaction; }
public NodeActionResponse SubmitNodeAction(NodeActionSubmission submission) { long hp = ObtainNodeHP(submission.Name); if (hp - submission.HP <= 0 && submission.Action == Constants.DrainSubmission) { var newFaction = TransformNode(submission); return new NodeActionResponse(submission.Name, newFaction, BaseHP, Constants.NodeTransformedResponse); } else { var newHp = AdjustNode(submission); return new NodeActionResponse(submission.Name, submission.Faction, newHp, Constants.SuccessResponse); } }
private long AdjustNode(NodeActionSubmission submission) { int hpAdjustment; if (submission.Action == Constants.DrainSubmission) { hpAdjustment = -1*submission.HP; } else { hpAdjustment = submission.HP; } var queryString = string.Format("{0} {1} = ((SELECT {1} FROM [{2}] WHERE {3} = @{4}) + @{5}) WHERE {3} = @{4}2;", CommandBuilder.InitUpdateStatement(Constants.NodeTableName), Constants.HPColumn, Constants.NodeTableName, Constants.NameColumn, Constants.NameColumn, Constants.HPColumn); var command = new SqlCommand(queryString); var paramDictionary = new Dictionary<string, Tuple<SqlDbType, object>>(); paramDictionary.Add(Constants.NameColumn, new Tuple<SqlDbType, object>(SqlDbType.NVarChar, submission.Name)); paramDictionary.Add(Constants.HPColumn, new Tuple<SqlDbType, object>(SqlDbType.BigInt, hpAdjustment)); paramDictionary.Add(Constants.NameColumn + 2, new Tuple<SqlDbType, object>(SqlDbType.NVarChar, submission.Name)); CommandBuilder.AddParameters(paramDictionary, command); var result = _connector.ExecuteNonQuery(command); if (result != 1) { throw new DataException("Adjusting a node is not successful"); } return ObtainNodeHP(submission.Name); }