diff --git a/gts/pokemondpds.ashx.cs b/gts/pokemondpds.ashx.cs index 05c9a543..638553d5 100644 --- a/gts/pokemondpds.ashx.cs +++ b/gts/pokemondpds.ashx.cs @@ -334,6 +334,8 @@ namespace PkmnFoundations.GTS response.Write(record.Save(), 0, 292); } + Database.Instance.GtsSetLastSearch4(pid); + } break; // the exchange request uploads a record of the exchangee pokemon @@ -353,8 +355,11 @@ namespace PkmnFoundations.GTS upload.IsExchanged = 0; int targetPid = BitConverter.ToInt32(data, 292); GtsRecord4 result = Database.Instance.GtsDataForUser4(targetPid); + DateTime ? searchTime = Database.Instance.GtsGetLastSearch4(pid); - if (result == null || result.IsExchanged != 0) + if (result == null || searchTime == null || + result.TimeDeposited > (DateTime)searchTime || // If this condition is met, it means the pokemon in the system is DIFFERENT from the one the user is trying to trade for, ie. it was deposited AFTER the user did their search. The one the user wants was either taken back or traded. + result.IsExchanged != 0) { // Pokémon is traded (or was never here to begin with) // todo: I only checked this on GenV. Check that this diff --git a/gts/syachi2ds.ashx.cs b/gts/syachi2ds.ashx.cs index 4e8ca31e..7870eee0 100644 --- a/gts/syachi2ds.ashx.cs +++ b/gts/syachi2ds.ashx.cs @@ -335,6 +335,8 @@ namespace PkmnFoundations.GTS response.Write(record.Save(), 0, 296); } + Database.Instance.GtsSetLastSearch5(pid); + } break; // the exchange request uploads a record of the exchangee pokemon @@ -354,8 +356,11 @@ namespace PkmnFoundations.GTS upload.IsExchanged = 0; int targetPid = BitConverter.ToInt32(request, 296); GtsRecord5 result = Database.Instance.GtsDataForUser5(targetPid); + DateTime ? searchTime = Database.Instance.GtsGetLastSearch5(pid); - if (result == null || result.IsExchanged != 0) + if (result == null || searchTime == null || + result.TimeDeposited > (DateTime)searchTime || // If this condition is met, it means the pokemon in the system is DIFFERENT from the one the user is trying to trade for, ie. it was deposited AFTER the user did their search. The one the user wants was either taken back or traded. + result.IsExchanged != 0) { // Pokémon is traded (or was never here to begin with) SessionManager.Remove(session); diff --git a/library/Data/DataMysql.cs b/library/Data/DataMysql.cs index 0340d8db..cc9f8865 100644 --- a/library/Data/DataMysql.cs +++ b/library/Data/DataMysql.cs @@ -502,6 +502,31 @@ namespace PkmnFoundations.Data WithTransaction(tran => GtsLogTrade4(tran, record, timeWithdrawn, partner_pid)); } + public void GtsSetLastSearch4(MySqlTransaction tran, int pid) + { + tran.ExecuteNonQuery("UPDATE GtsProfiles4 SET TimeLastSearch = " + + "@now WHERE pid = @pid", new MySqlParameter("@now", DateTime.UtcNow), + new MySqlParameter("@pid", pid)); + } + + public override void GtsSetLastSearch4(int pid) + { + WithTransaction(tran => GtsSetLastSearch4(tran, pid)); + } + + public DateTime ? GtsGetLastSearch4(MySqlTransaction tran, int pid) + { + object result = tran.ExecuteScalar("SELECT TimeLastSearch " + + "FROM GtsProfiles4 WHERE pid = @pid", new MySqlParameter("@pid", pid)); + if (result == null || result is DBNull) return null; + return (DateTime)result; + } + + public override DateTime ? GtsGetLastSearch4(int pid) + { + return WithTransaction(tran => GtsGetLastSearch4(tran, pid)); + } + #endregion #region Battle Tower 4 @@ -1432,6 +1457,31 @@ namespace PkmnFoundations.Data _params2); } + public void GtsSetLastSearch5(MySqlTransaction tran, int pid) + { + tran.ExecuteNonQuery("UPDATE GtsProfiles5 SET TimeLastSearch = " + + "@now WHERE pid = @pid", new MySqlParameter("@now", DateTime.UtcNow), + new MySqlParameter("@pid", pid)); + } + + public override void GtsSetLastSearch5(int pid) + { + WithTransaction(tran => GtsSetLastSearch5(tran, pid)); + } + + public DateTime? GtsGetLastSearch5(MySqlTransaction tran, int pid) + { + object result = tran.ExecuteScalar("SELECT TimeLastSearch " + + "FROM GtsProfiles5 WHERE pid = @pid", new MySqlParameter("@pid", pid)); + if (result == null || result is DBNull) return null; + return (DateTime)result; + } + + public override DateTime? GtsGetLastSearch5(int pid) + { + return WithTransaction(tran => GtsGetLastSearch5(tran, pid)); + } + #endregion #region Battle Subway 5 diff --git a/library/Data/Database.cs b/library/Data/Database.cs index 474934f7..f7e0bc0f 100644 --- a/library/Data/Database.cs +++ b/library/Data/Database.cs @@ -77,6 +77,8 @@ namespace PkmnFoundations.Data public abstract int GtsAvailablePokemon4(); public abstract void GtsLogTrade4(GtsRecord4 record, DateTime ? timeWithdrawn, int ? partner_pid); + public abstract void GtsSetLastSearch4(int pid); + public abstract DateTime ? GtsGetLastSearch4(int pid); #endregion #region Battle Tower 4 @@ -113,6 +115,8 @@ namespace PkmnFoundations.Data public abstract int GtsAvailablePokemon5(); public abstract void GtsLogTrade5(GtsRecord5 record, DateTime ? timeWithdrawn, int ? partner_pid); + public abstract void GtsSetLastSearch5(int pid); + public abstract DateTime ? GtsGetLastSearch5(int pid); #endregion #region Other Gamestats 5