diff --git a/gts/pokemondpds.ashx.cs b/gts/pokemondpds.ashx.cs index 06f491f0..bc071873 100644 --- a/gts/pokemondpds.ashx.cs +++ b/gts/pokemondpds.ashx.cs @@ -201,7 +201,12 @@ namespace PkmnFoundations.GTS bool success = DataAbstract.Instance.GtsDeletePokemon4(pid); if (success) { - manager.RefreshStats(); + try + { + DataAbstract.Instance.GtsLogTrade4(record, DateTime.UtcNow); + manager.RefreshStats(); + } + catch { } response.Write(new byte[] { 0x01, 0x00 }, 0, 2); } else @@ -470,7 +475,12 @@ namespace PkmnFoundations.GTS if (DataAbstract.Instance.GtsTradePokemon4(upload, result)) { - manager.RefreshStats(); + try + { + DataAbstract.Instance.GtsLogTrade4(result, null); + manager.RefreshStats(); + } + catch { } response.Write(new byte[] { 0x01, 0x00 }, 0, 2); } else diff --git a/gts/syachi2ds.ashx.cs b/gts/syachi2ds.ashx.cs index a8d9ffca..36153438 100644 --- a/gts/syachi2ds.ashx.cs +++ b/gts/syachi2ds.ashx.cs @@ -203,8 +203,13 @@ namespace PkmnFoundations.GTS bool success = DataAbstract.Instance.GtsDeletePokemon5(pid); if (success) { + try + { + DataAbstract.Instance.GtsLogTrade5(record, DateTime.UtcNow); + manager.RefreshStats(); + } + catch { } response.Write(new byte[] { 0x01, 0x00 }, 0, 2); - manager.RefreshStats(); } else { @@ -469,7 +474,12 @@ namespace PkmnFoundations.GTS if (DataAbstract.Instance.GtsTradePokemon5(upload, result)) { - manager.RefreshStats(); + try + { + DataAbstract.Instance.GtsLogTrade5(result, null); + manager.RefreshStats(); + } + catch { } response.Write(new byte[] { 0x01, 0x00 }, 0, 2); } else diff --git a/library/Data/DataAbstract.cs b/library/Data/DataAbstract.cs index f9e7cf7e..2e7fd686 100644 --- a/library/Data/DataAbstract.cs +++ b/library/Data/DataAbstract.cs @@ -60,6 +60,8 @@ namespace PkmnFoundations.Data public abstract GtsRecord4[] GtsSearch4(int pid, ushort species, Genders gender, byte minLevel, byte maxLevel, byte country, int count); public abstract int GtsAvailablePokemon4(); + + public abstract void GtsLogTrade4(GtsRecord4 record, DateTime ? timeWithdrawn); #endregion #region GTS 5 @@ -76,6 +78,8 @@ namespace PkmnFoundations.Data public abstract GtsRecord5[] GtsSearch5(int pid, ushort species, Genders gender, byte minLevel, byte maxLevel, byte country, int count); public abstract int GtsAvailablePokemon5(); + + public abstract void GtsLogTrade5(GtsRecord5 record, DateTime ? timeWithdrawn); #endregion #region Global Terminal 4 diff --git a/library/Data/DataMysql.cs b/library/Data/DataMysql.cs index 6b58b051..f5eabf86 100644 --- a/library/Data/DataMysql.cs +++ b/library/Data/DataMysql.cs @@ -367,6 +367,45 @@ namespace PkmnFoundations.Data return (int)(long)db.ExecuteScalar("SELECT Count(*) FROM GtsPokemon4 WHERE IsExchanged = 0"); } } + + public override void GtsLogTrade4(GtsRecord4 record, DateTime ? timeWithdrawn) + { + using (MySqlConnection db = CreateConnection()) + { + db.Open(); + using (MySqlTransaction tran = db.BeginTransaction()) + { + GtsLogTrade4(tran, record, timeWithdrawn); + tran.Commit(); + } + } + } + + public void GtsLogTrade4(MySqlTransaction tran, GtsRecord4 record, DateTime ? timeWithdrawn) + { + if (record.Data.Length != 236) throw new FormatException("pkm data must be 236 bytes."); + if (record.TrainerName.RawData.Length != 16) throw new FormatException("Trainer name must be 16 bytes."); + // note that IsTraded being true in the record is not an error condition + // since it might have use later on. You should check for this in the upload handler. + + MySqlParameter[] _params = ParamsFromRecord4(record); + MySqlParameter[] _params2 = new MySqlParameter[23]; + Array.Copy(_params, _params2, 22); + _params2[22] = new MySqlParameter("@TimeWithdrawn", timeWithdrawn); + + tran.ExecuteNonQuery("INSERT INTO GtsHistory4 " + + "(Data, Species, Gender, Level, RequestedSpecies, RequestedGender, " + + "RequestedMinLevel, RequestedMaxLevel, Unknown1, TrainerGender, " + + "Unknown2, TimeDeposited, TimeExchanged, pid, TrainerName, TrainerOT, " + + "TrainerCountry, TrainerRegion, TrainerClass, IsExchanged, TrainerVersion, " + + "TrainerLanguage, TimeWithdrawn) " + + "VALUES (@Data, @Species, @Gender, @Level, @RequestedSpecies, " + + "@RequestedGender, @RequestedMinLevel, @RequestedMaxLevel, @Unknown1, " + + "@TrainerGender, @Unknown2, @TimeDeposited, @TimeExchanged, @pid, " + + "@TrainerName, @TrainerOT, @TrainerCountry, @TrainerRegion, @TrainerClass, " + + "@IsExchanged, @TrainerVersion, @TrainerLanguage, @TimeWithdrawn)", + _params2); + } #endregion #region GTS 5 @@ -713,6 +752,50 @@ namespace PkmnFoundations.Data return (int)(long)db.ExecuteScalar("SELECT Count(*) FROM GtsPokemon5 WHERE IsExchanged = 0"); } } + + public override void GtsLogTrade5(GtsRecord5 record, DateTime ? timeWithdrawn) + { + using (MySqlConnection db = CreateConnection()) + { + db.Open(); + using (MySqlTransaction tran = db.BeginTransaction()) + { + GtsLogTrade5(tran, record, timeWithdrawn); + tran.Commit(); + } + } + } + + public void GtsLogTrade5(MySqlTransaction tran, GtsRecord5 record, DateTime ? timeWithdrawn) + { + // todo: Bring these out into a ValidateRecord5 method + if (record == null) throw new ArgumentNullException("record"); + if (record.Data.Length != 220) throw new FormatException("pkm data must be 220 bytes."); + if (record.Unknown0.Length != 16) throw new FormatException("pkm padding must be 16 bytes."); + if (record.TrainerName.RawData.Length != 16) throw new FormatException("Trainer name must be 16 bytes."); + // note that IsTraded being true in the record is not an error condition + // since it might have use later on. You should check for this in the upload handler. + + MySqlParameter[] _params = ParamsFromRecord5(record); + MySqlParameter[] _params2 = new MySqlParameter[26]; + Array.Copy(_params, _params2, 25); + _params2[25] = new MySqlParameter("@TimeWithdrawn", timeWithdrawn); + + tran.ExecuteNonQuery("INSERT INTO GtsHistory5 " + + "(Data, Unknown0, Species, Gender, Level, RequestedSpecies, RequestedGender, " + + "RequestedMinLevel, RequestedMaxLevel, Unknown1, TrainerGender, " + + "Unknown2, TimeDeposited, TimeExchanged, pid, TrainerOT, TrainerName, " + + "TrainerCountry, TrainerRegion, TrainerClass, IsExchanged, TrainerVersion, " + + "TrainerLanguage, TrainerBadges, TrainerUnityTower, TimeWithdrawn) " + + "VALUES (@Data, @Unknown0, @Species, @Gender, @Level, @RequestedSpecies, " + + "@RequestedGender, @RequestedMinLevel, @RequestedMaxLevel, @Unknown1, " + + "@TrainerGender, @Unknown2, @TimeDeposited, @TimeExchanged, @pid, " + + "@TrainerOT, @TrainerName, @TrainerCountry, @TrainerRegion, @TrainerClass, " + + "@IsExchanged, @TrainerVersion, @TrainerLanguage, @TrainerBadges, " + + "@TrainerUnityTower, @TimeWithdrawn)", + _params2); + } + #endregion #region Global Terminal 4 diff --git a/library/database.sql b/library/database.sql index 0b883b5a..a412e540 100644 --- a/library/database.sql +++ b/library/database.sql @@ -7,7 +7,7 @@ # # Host: 127.0.0.1 (MySQL 5.5.27) # Database: gts -# Generation Time: 2014-07-16 00:39:24 +0000 +# Generation Time: 2014-07-16 01:32:50 +0000 # ************************************************************ @@ -209,6 +209,89 @@ CREATE TABLE `FoundationsTypes` ( +# Dump of table GtsHistory4 +# ------------------------------------------------------------ + +DROP TABLE IF EXISTS `GtsHistory4`; + +CREATE TABLE `GtsHistory4` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `Data` blob NOT NULL, + `Species` smallint(5) unsigned NOT NULL, + `Gender` tinyint(3) unsigned NOT NULL, + `Level` tinyint(3) unsigned NOT NULL, + `RequestedSpecies` smallint(5) unsigned NOT NULL, + `RequestedGender` tinyint(3) unsigned NOT NULL, + `RequestedMinLevel` tinyint(3) unsigned NOT NULL, + `RequestedMaxLevel` tinyint(3) unsigned NOT NULL, + `Unknown1` tinyint(3) unsigned NOT NULL, + `TrainerGender` tinyint(3) unsigned NOT NULL, + `Unknown2` tinyint(3) unsigned NOT NULL, + `TimeDeposited` datetime DEFAULT NULL, + `TimeExchanged` datetime DEFAULT NULL, + `pid` int(11) NOT NULL, + `TrainerName` blob NOT NULL, + `TrainerOT` smallint(5) unsigned NOT NULL, + `TrainerCountry` tinyint(3) unsigned NOT NULL, + `TrainerRegion` tinyint(3) unsigned NOT NULL, + `TrainerClass` tinyint(3) unsigned NOT NULL, + `IsExchanged` tinyint(3) unsigned NOT NULL, + `TrainerVersion` tinyint(3) unsigned NOT NULL, + `TrainerLanguage` tinyint(3) unsigned NOT NULL, + `ParseVersion` int(11) unsigned DEFAULT NULL, + `TimeWithdrawn` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `pid` (`pid`), + KEY `Species` (`Species`), + KEY `Gender` (`Gender`), + KEY `Level` (`Level`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + + + +# Dump of table GtsHistory5 +# ------------------------------------------------------------ + +DROP TABLE IF EXISTS `GtsHistory5`; + +CREATE TABLE `GtsHistory5` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `Data` blob NOT NULL, + `Unknown0` blob NOT NULL, + `Species` smallint(5) unsigned NOT NULL, + `Gender` tinyint(3) unsigned NOT NULL, + `Level` tinyint(3) unsigned NOT NULL, + `RequestedSpecies` smallint(5) unsigned NOT NULL, + `RequestedGender` tinyint(3) unsigned NOT NULL, + `RequestedMinLevel` tinyint(3) unsigned NOT NULL, + `RequestedMaxLevel` tinyint(3) unsigned NOT NULL, + `Unknown1` tinyint(3) unsigned NOT NULL, + `TrainerGender` tinyint(3) unsigned NOT NULL, + `Unknown2` tinyint(3) unsigned NOT NULL, + `TimeDeposited` datetime DEFAULT NULL, + `TimeExchanged` datetime DEFAULT NULL, + `pid` int(11) NOT NULL, + `TrainerOT` int(11) unsigned NOT NULL, + `TrainerName` blob NOT NULL, + `TrainerCountry` tinyint(3) unsigned NOT NULL, + `TrainerRegion` tinyint(3) unsigned NOT NULL, + `TrainerClass` tinyint(3) unsigned NOT NULL, + `IsExchanged` tinyint(3) unsigned NOT NULL, + `TrainerVersion` tinyint(3) unsigned NOT NULL, + `TrainerLanguage` tinyint(3) unsigned NOT NULL, + `TrainerBadges` tinyint(3) unsigned NOT NULL, + `TrainerUnityTower` tinyint(3) unsigned NOT NULL, + `ParseVersion` int(10) unsigned DEFAULT NULL, + `TimeWithdrawn` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `pid` (`pid`), + KEY `Species` (`Species`), + KEY `Gender` (`Gender`), + KEY `Level` (`Level`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + + + # Dump of table GtsPokemon4 # ------------------------------------------------------------