libpairtwo/src/Sws.php

328 lines
11 KiB
PHP
Raw Normal View History

2019-01-10 20:05:23 +01:00
<?php
/*
* The MIT License
*
* Copyright 2019 Jeroen De Meerleer <schaak@jeroened.be>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace JeroenED\Libpairtwo;
use JeroenED\Libpairtwo\Models\Tournament;
2019-01-16 18:03:07 +01:00
use JeroenED\Libpairtwo\Models\Sws as MyModel;
2019-01-10 20:05:23 +01:00
/**
* This class reads a SWS file
*
* @author Jeroen De Meerleer
*/
2019-01-18 14:17:43 +01:00
class Sws
{
2019-01-18 18:11:53 +01:00
private const PT_DAYFACTOR = 32;
private const PT_MONTHFACTOR = 16;
private const PT_YEARFACTOR = 512;
private const PT_SYSTEMYEAR = 1900;
private const PT_PASTOFFSET = 117;
2019-01-18 14:17:43 +01:00
/**
* @param string $swsfile
* @return MyModel
*/
2019-01-10 20:05:23 +01:00
public static function ReadSws(string $swsfile)
{
$swshandle = fopen($swsfile, 'rb');
2019-01-18 14:17:43 +01:00
$swscontents = fread($swshandle, filesize($swsfile));
fclose($swshandle);
2019-01-16 18:03:07 +01:00
$sws = new MyModel();
2019-01-18 14:17:43 +01:00
$offset = 0;
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setRelease(self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
2019-01-16 18:03:07 +01:00
$sws->setTournament(new Tournament());
2019-01-18 14:17:43 +01:00
// UserCountry
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("UserCountry", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// SavedOffset
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("SavedOffset", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// NewPlayer
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("NewPlayer", hexdec(self::ReadHexData(substr($swscontents, $offset, $length))));
2019-01-18 14:17:43 +01:00
$offset += $length;
// AmericanHandicap
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("AmericanHandicap", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// LowOrder
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("LowOrder", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// PairingMethod
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("PairingMethod", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// AmericanPresence
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("AmericanPresence", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// CheckSameClub
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("CheckSameClub", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// NoColorCheck
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("NoColorCheck", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// SeparateCategories
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("SeparateCategories", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// EloUsed
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("EloUsed", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// AlternateColors
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("AlternateColors", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// MaxMeetings
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("MaxMeetings", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// MaxDistance
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("MaxDistance", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// MinimizeKeizer
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("MinimizeKeizer", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// MinRoundsMeetings
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("MinRoundsMeetings", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// MaxRoundsAbsent
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("MaxRoundsAbsent", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// SpecialPoints
$length = 4 * 6;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("SpecialPoints", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// NewNamePos
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("NewNamePos", hexdec(self::ReadHexData(substr($swscontents, $offset, $length))));
2019-01-18 14:17:43 +01:00
$offset += $length;
// CurrentRound
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("CurrentRound", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// CreatedRounds
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("CreatedRounds", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// CreatedPlayers
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("CreatedPlayers", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// MaxSelection
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("MaxSelection", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// NumberOfRounds
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("NumberOfRounds", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// NumberOfPairings
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("NumberOfPairings", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// CreatedPairings
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("CreatedPairings", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// PairingElems
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("PairingElems", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// RandomSeed
$length = 4;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("RandomSeed", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// TieOrder
$length = 4 * 5;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("TieOrder", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// Categorie
$length = 4 * 10;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("Categorie", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// ExtraPoints
$length = 4 * 20;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("ExtraPoints", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// SelectP
$length = 4 * 20;
2019-01-18 18:10:56 +01:00
$sws->setBinaryData("SelectP", self::ReadHexData(substr($swscontents, $offset, $length)));
2019-01-18 14:17:43 +01:00
$offset += $length;
// Players
$length = 68 * $sws->getBinaryData("NewPlayer");
$sws->setBinaryData("Players", substr($swscontents, $offset, $length));
$offset += $length;
// PlayerNames
$length = $sws->getBinaryData("NewNamePos");
$sws->setBinaryData("PlayerNames", substr($swscontents, $offset, $length));
$offset += $length;
// TournamentName
$length = 80;
$sws->getTournament()->setName(substr($swscontents, $offset, $length));
$offset += $length;
// TournamentOrganiser
$length = 50;
$sws->getTournament()->setOrganiser(substr($swscontents, $offset, $length));
$offset += $length;
// TournamentTempo
$length = 50;
$sws->getTournament()->setTempo(substr($swscontents, $offset, $length));
$offset += $length;
// TournamentCountry
$length = 32;
$sws->getTournament()->setOrganiserCountry(substr($swscontents, $offset, $length));
$offset += $length;
// Arbiters
$length = 128;
$sws->getTournament()->setArbiter(substr($swscontents, $offset, $length));
$offset += $length;
2019-01-18 18:11:53 +01:00
// Rounds
$length = 4;
$sws->getTournament()->setRounds(hexdec(substr($swscontents, $offset, $length)));
$offset += $length;
// Participants
$length = 4;
$sws->getTournament()->setParticipants(hexdec(substr($swscontents, $offset, $length)));
$offset += $length;
// Fidehomol
$length = 4;
$sws->getTournament()->setFideHomol(hexdec(substr($swscontents, $offset, $length)));
$offset += $length;
// StartDate
$length = 4;
//echo self::readhexdata(substr($swscontents, $offset, $length));
$sws->getTournament()->setStartDate(self::UIntToTimestamp(hexdec(self::readhexdata(substr($swscontents, $offset, $length)))));
$offset += $length;
// EndDate
$length = 4;
$sws->getTournament()->setEndDate(self::UIntToTimestamp(hexdec(self::readhexdata(substr($swscontents, $offset, $length)))));
$offset += $length;
2019-01-10 20:05:23 +01:00
return $sws;
}
2019-01-18 14:17:43 +01:00
private static function ReadHexData(String $data)
{
$hex = implode(unpack("H*", $data));
2019-01-18 18:11:53 +01:00
$hex = implode(array_reverse(str_split($hex, 2)));
2019-01-18 14:17:43 +01:00
$hex = str_replace("00", "", $hex);
return $hex;
}
2019-01-18 18:11:53 +01:00
private static function UIntToTimestamp($date)
{
//echo $date;
$curyear = date('Y');
$yearoffset = $curyear - self::PT_PASTOFFSET;
//$yearoffset = 100;
// Day
$day = $date % self::PT_DAYFACTOR;
if ($day < 1) {
$day = 1;
}
// Month
$month = ($date / self::PT_DAYFACTOR) % self::PT_MONTHFACTOR;
if ($month < 1) {
$month = 1;
}
// Year
$year = ($date / self::PT_YEARFACTOR) + $yearoffset;
$concat = $month . '/' . $day . '/' . intval($year);
$format = 'm/d/Y';
echo $concat;
return \DateTime::createFromFormat($format, $concat);
}
2019-01-10 20:05:23 +01:00
}