Skip to content

Commit

Permalink
Merge branch 'release/1.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chakratos committed Aug 22, 2023
2 parents 437ee48 + 69a16d5 commit 7d896d5
Show file tree
Hide file tree
Showing 40 changed files with 2,721 additions and 348 deletions.
131 changes: 106 additions & 25 deletions app/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,111 +4,192 @@
namespace MHFSaveManager\Controller;


use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\OptimisticLockException;
use MHFSaveManager\Database\EM;
use MHFSaveManager\Model\Distribution;
use MHFSaveManager\Service\ResponseService;
use MHFSaveManager\Service\UIService;

/**
*
*/
abstract class AbstractController
{
protected static string $itemName;
protected static string $itemClass;

/**
* @param string $string
* @return string
*/
public static function localeWS(string $string): string
{
$UILocale = UIService::getForLocale();
return str_replace(' ', '', $UILocale[$string]);
}

/**
* @param $saveData
* @param string $hexOffset
* @param string $hexValue
* @return false|mixed|resource
*/
protected static function writeToFile($saveData, string $hexOffset, string $hexValue)
{
$handle = $saveData;
$hexValue = strlen($hexValue) == 1 ? '0'.$hexValue : $hexValue;
$hexValue = strlen($hexValue) == 1 ? '0' . $hexValue : $hexValue;
if (!is_resource($saveData)) {
$handle = fopen('php://memory', 'br+');
$handle = fopen('php://memory', 'rb+');
fwrite($handle, $saveData);
}

fseek($handle, hexdec($hexOffset));
fwrite($handle, hex2bin($hexValue));
rewind($handle);

return $handle;
}

protected static function numberConvertEndian($number, $byteSize)
/**
* @param $number
* @param $byteSize
* @return string
*/
protected static function numberConvertEndian($number, $byteSize): string
{
$hexChars = $byteSize * 2;
$data = dechex((float)$number);

$data = str_pad($data, $hexChars, '0', STR_PAD_LEFT);

$unpack = unpack("H*", strrev(pack("H*", $data)));

$unpack = unpack('H*', strrev(pack('H*', $data)));
return strtoupper($unpack[1]);
}

protected static function stringToHex($string)
/**
* @param $string
* @return string
*/
protected static function stringToHex($string): string
{
$output = "";
foreach (mb_str_split($string) as $char) {
$char = self::uniord($char);
if ($char > 255) {
$char = self::numberConvertEndian($char,2);
$char = self::numberConvertEndian($char, 2);
} else {
$char = dechex($char);
}
$output .= sprintf("%02s",strtoupper($char));
$output .= sprintf('%02s', strtoupper($char));
}

return $output;
}

protected static function uniord($u) {
/**
* @param $u
* @return float|int
*/
protected static function uniord($u): float|int
{
$k = mb_convert_encoding($u, 'SJIS', 'UTF-8');
$k1 = ord(substr($k, 0, 1));
$k2 = ord(substr($k, 1, 1));

return $k2 * 256 + $k1;
}

protected static function arrayOfModelsToCSVDownload($records, $name)
/**
* @param $records
* @return void
*/
protected static function arrayOfModelsToCSVDownload($records): void
{
if(count($records)) {
if (count($records)) {
$handle = fopen('php://memory', 'w');

/*
* Really really smelly cheese to get names of protected properties!
*/
fputcsv($handle, array_map(fn($value) => ltrim(substr($value, 2)), array_keys((array)$records[0])));
foreach($records as $record) {
$data = (array) $record;
foreach ($records as $record) {
$data = (array)$record;
foreach ($data as &$field) {
if ($field instanceof \DateTime) {
$field = $field->format('Y-m-d H:i');
} else if (is_resource($field)) {
} elseif (is_resource($field)) {
$field = strtoupper(bin2hex(stream_get_contents($field)));
}
}
fputcsv($handle, $data);
}

rewind($handle);
ResponseService::SendDownloadResource($handle, $name . '.csv');
ResponseService::SendDownloadResource($handle, static::$itemName . '.csv');
}
}

protected static function importFromCSV($uploadName, $model, $deleteQuery)
/**
* @param string $deleteWhere
* @return void
* @throws ORMException
* @throws OptimisticLockException
*/
protected static function importFromCSV(string $deleteWhere = '1=1'): void
{
$lines = preg_split('/\r\n|\r|\n/', file_get_contents($_FILES[$uploadName]["tmp_name"]));
$lines = preg_split('/\r\n|\r|\n/', file_get_contents($_FILES[static::$itemName . 'CSV']["tmp_name"]));
$attributes = str_getcsv($lines[0]);
unset($lines[0]);
$em = EM::getInstance();
foreach ($lines as $line) {
if ($line == "") {
continue;
}

$lineValues = str_getcsv($line);
$item = new $model();
$item = new static::$itemClass();
foreach ($attributes as $key => $attribute) {
$setter = "set".implode('', array_map('ucfirst', explode('_', $attribute)));
$setter = "set" . implode('', array_map('ucfirst', explode('_', $attribute)));
$item->$setter($lineValues[$key]);
}
$em->persist($item);
}

$em->createQuery($deleteQuery)->execute();
$em->createQuery('delete from ' . static::$itemClass . ' n where ' . $deleteWhere)->execute();
$em->flush();

ResponseService::SendOk();
}

/**
* @return void
* @throws ORMException
*/
protected static function SaveItem(callable $callback): void
{
$item = new static::$itemClass();

if (isset($_POST[self::localeWS('ID')]) && $_POST[self::localeWS('ID')] > 0) {
$item = EM::getInstance()->getRepository(static::$itemClass)->find($_POST[self::localeWS('ID')]);
} else {
$highestId = EM::getInstance()->getRepository(static::$itemClass)->matching(
Criteria::create()->orderBy(['id' => 'desc']))->first();
if (!empty($highestId)) {
$item->setId($highestId->getId() + 1);
} else {
$item->setId(1);
}

EM::getInstance()->persist($item);
}

$callback($item);

EM::getInstance()->flush();

ResponseService::SendOk($item->getId());
}
}
51 changes: 34 additions & 17 deletions app/Controller/DistributionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,50 @@
namespace MHFSaveManager\Controller;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\OptimisticLockException;
use MHFSaveManager\Database\EM;
use MHFSaveManager\Model\Distribution;
use MHFSaveManager\Model\DistributionItem;
use MHFSaveManager\Service\ItemsService;
use MHFSaveManager\Service\ResponseService;
use MHFSaveManager\Model\Character;

/**
*
*/
class DistributionsController extends AbstractController
{
protected static string $itemName = 'distribution';
protected static string $itemClass = Distribution::class;

public static function Index()
{
$distributions = EM::getInstance()->getRepository('MHFSaveManager\Model\Distribution')->findAll();
$characters = \MHFSaveManager\Database\EM::getInstance()->getRepository('MHFSaveManager\Model\Character')->findAll();
$distributions = EM::getInstance()->getRepository(self::$itemClass)->findAll();
$characters = \MHFSaveManager\Database\EM::getInstance()->getRepository(Character::class)->findAll();


include_once ROOT_DIR . '/app/Views/distributions.php';
}

public static function IndexTest()
{
$distributions = EM::getInstance()->getRepository('MHFSaveManager\Model\Distribution')->findAll();
$distributions = EM::getInstance()->getRepository(self::$itemClass)->findAll();

/** @var Distribution $distribution */
foreach ($distributions as $distribution) {
echo "<hr>";
printf("Name: %s <br>", $distribution->getEventName());
printf("Desc: %s <br>", $distribution->getDescription());
printf("Type: %s <br>", Distribution::$types[$distribution->getType()]);
echo "<br><b>Items:</b><br>";
echo '<hr>';
printf('Name: %s <br>', $distribution->getEventName());
printf('Desc: %s <br>', $distribution->getDescription());
printf('Type: %s <br>', Distribution::$types[$distribution->getType()]);
echo '<br><b>Items:</b><br>';
$data = $distribution->getData();
$numberOfItems = hexdec(bin2hex(fread($data, 2)));

for ($i = 0; $i < $numberOfItems; $i++) {
$item = new DistributionItem(bin2hex(fread($data, 13)));
printf("Reencoded hex value: %s<br>", $item);
printf("ItemNr: %s <br>Type: %s <br>Item: %s <br>Amount: %s<br><br>", $i+1, DistributionItem::$types[$item->getType()], ItemsService::getForLocale()[$item->getItemId()]['name'], $item->getAmount());
printf('Reencoded hex value: %s<br>', $item);
printf('ItemNr: %s <br>Type: %s <br>Item: %s <br>Amount: %s<br><br>', $i+1, DistributionItem::$types[$item->getType()], ItemsService::getForLocale()[$item->getItemId()]['name'], $item->getAmount());

}

Expand All @@ -50,7 +59,7 @@ public static function EditDistribution()
$distribution = new Distribution();

if (isset($_POST['id']) && $_POST['id'] > 0) {
$distribution = EM::getInstance()->getRepository('MHFSaveManager\Model\Distribution')->find($_POST['id']);
$distribution = EM::getInstance()->getRepository(self::$itemClass)->find($_POST['id']);
} else {
EM::getInstance()->persist($distribution);
}
Expand All @@ -73,7 +82,7 @@ public static function EditDistribution()
foreach ($_POST['items'] as $item) {
$itemString .= (new DistributionItem())->setType((int)$item['type'])->setAmount((int)$item['amount'])->setItemId($item['itemId']);
}
$handle = fopen('php://memory', 'br+');
$handle = fopen('php://memory', 'rb+');
fwrite($handle, hex2bin($itemString));
rewind($handle);
$distribution->setData($handle);
Expand All @@ -84,14 +93,22 @@ public static function EditDistribution()

}

public static function ExportDistributions()
/**
* @return void
*/
public static function ExportDistributions(): void
{
$records = EM::getInstance()->getRepository('MHFSaveManager\Model\Distribution')->findAll();
self::arrayOfModelsToCSVDownload($records, 'Distributions');
$records = EM::getInstance()->getRepository(self::$itemClass)->findAll();
self::arrayOfModelsToCSVDownload($records);
}

public static function ImportDistributions()
/**
* @return void
* @throws ORMException
* @throws OptimisticLockException
*/
public static function ImportDistributions(): void
{
self::importFromCSV('distributionCSV', Distribution::class, 'delete from MHFSaveManager\Model\Distribution n where 1=1');
self::importFromCSV();
}
}
Loading

0 comments on commit 7d896d5

Please sign in to comment.