using System;
using System.Collections.Generic;
using System.Linq;
using SabreTools.Library.Filtering;
using SabreTools.Library.Tools;
using Newtonsoft.Json;
namespace SabreTools.Library.DatItems
{
///
/// Represents one ListXML input
///
[JsonObject("input")]
public class Input : DatItem
{
#region Fields
///
/// Input service ID
///
[JsonProperty("service", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool? Service { get; set; }
///
/// Determins if this has a tilt sensor
///
[JsonProperty("tilt", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool? Tilt { get; set; }
///
/// Number of players on the input
///
[JsonProperty("players", DefaultValueHandling = DefaultValueHandling.Ignore)]
public long? Players { get; set; }
///
/// Number of coins required
///
[JsonProperty("coins", DefaultValueHandling = DefaultValueHandling.Ignore)]
public long? Coins { get; set; }
///
/// Set of controls for the input
///
[JsonProperty("controls", DefaultValueHandling = DefaultValueHandling.Ignore)]
public List Controls { get; set; }
#endregion
#region Accessors
///
/// Set fields with given values
///
/// Mappings dictionary
public override void SetFields(Dictionary mappings)
{
// Set base fields
base.SetFields(mappings);
// Handle Input-specific fields
if (mappings.Keys.Contains(Field.DatItem_Service))
Service = mappings[Field.DatItem_Service].AsYesNo();
if (mappings.Keys.Contains(Field.DatItem_Tilt))
Tilt = mappings[Field.DatItem_Tilt].AsYesNo();
if (mappings.Keys.Contains(Field.DatItem_Players))
{
if (Int64.TryParse(mappings[Field.DatItem_Players], out long players))
Players = players;
}
if (mappings.Keys.Contains(Field.DatItem_Coins))
{
if (Int64.TryParse(mappings[Field.DatItem_Coins], out long coins))
Coins = coins;
}
if (Controls != null)
{
foreach (Control control in Controls)
{
control.SetFields(mappings);
}
}
}
#endregion
#region Constructors
///
/// Create a default, empty Input object
///
public Input()
{
ItemType = ItemType.Input;
}
#endregion
#region Cloning Methods
public override object Clone()
{
return new Input()
{
ItemType = this.ItemType,
DupeType = this.DupeType,
Machine = this.Machine.Clone() as Machine,
Source = this.Source.Clone() as Source,
Remove = this.Remove,
Service = this.Service,
Tilt = this.Tilt,
Players = this.Players,
Coins = this.Coins,
Controls = this.Controls,
};
}
#endregion
#region Comparision Methods
public override bool Equals(DatItem other)
{
// If we don't have a Input, return false
if (ItemType != other.ItemType)
return false;
// Otherwise, treat it as a Input
Input newOther = other as Input;
// If the Input information matches
bool match = (Service == newOther.Service
&& Tilt == newOther.Tilt
&& Players == newOther.Players
&& Coins == newOther.Coins);
if (!match)
return match;
// If the controls match
if (Controls != null)
{
foreach (Control control in Controls)
{
match &= newOther.Controls.Contains(control);
}
}
return match;
}
#endregion
#region Filtering
///
/// Check to see if a DatItem passes the filter
///
/// Filter to check against
/// True if the item passed the filter, false otherwise
public override bool PassesFilter(Filter filter)
{
// Check common fields first
if (!base.PassesFilter(filter))
return false;
// Filter on service
if (filter.DatItem_Service.MatchesNeutral(null, Service) == false)
return false;
// Filter on tilt
if (filter.DatItem_Tilt.MatchesNeutral(null, Tilt) == false)
return false;
// Filter on players
if (filter.DatItem_Players.MatchesNeutral(null, Players) == false)
return false;
else if (filter.DatItem_Players.MatchesPositive(null, Players) == false)
return false;
else if (filter.DatItem_Players.MatchesNegative(null, Players) == false)
return false;
// Filter on coins
if (filter.DatItem_Coins.MatchesNeutral(null, Coins) == false)
return false;
else if (filter.DatItem_Coins.MatchesPositive(null, Coins) == false)
return false;
else if (filter.DatItem_Coins.MatchesNegative(null, Coins) == false)
return false;
// Filter on individual controls
if (Controls != null)
{
foreach (Control control in Controls)
{
if (!control.PassesFilter(filter))
return false;
}
}
return true;
}
///
/// Remove fields from the DatItem
///
/// List of Fields to remove
public override void RemoveFields(List fields)
{
// Remove common fields first
base.RemoveFields(fields);
// Remove the fields
if (fields.Contains(Field.DatItem_Service))
Service = null;
if (fields.Contains(Field.DatItem_Tilt))
Tilt = null;
if (fields.Contains(Field.DatItem_Players))
Players = 0;
if (fields.Contains(Field.DatItem_Coins))
Coins = null;
if (Controls != null)
{
foreach (Control control in Controls)
{
control.RemoveFields(fields);
}
}
}
#endregion
#region Sorting and Merging
///
/// Replace fields from another item
///
/// DatItem to pull new information from
/// List of Fields representing what should be updated
public override void ReplaceFields(DatItem item, List fields)
{
// Replace common fields first
base.ReplaceFields(item, fields);
// If we don't have a Input to replace from, ignore specific fields
if (item.ItemType != ItemType.Input)
return;
// Cast for easier access
Input newItem = item as Input;
// Replace the fields
if (fields.Contains(Field.DatItem_Service))
Service = newItem.Service;
if (fields.Contains(Field.DatItem_Tilt))
Tilt = newItem.Tilt;
if (fields.Contains(Field.DatItem_Players))
Players = newItem.Players;
if (fields.Contains(Field.DatItem_Coins))
Coins = newItem.Coins;
// DatItem_Control_* doesn't make sense here
// since not every control under the other item
// can replace every control under this item
}
#endregion
}
}