Skip to content

Instantly share code, notes, and snippets.

@ethaizone
Last active September 16, 2016 07:43
Show Gist options
  • Select an option

  • Save ethaizone/ed070ce7a06f29d25fbd9541cb87f996 to your computer and use it in GitHub Desktop.

Select an option

Save ethaizone/ed070ce7a06f29d25fbd9541cb87f996 to your computer and use it in GitHub Desktop.
Create chmod number notation
<?php
/**
* Copyright @ 2016 by Nimit Suwannagate <[email protected]>. All rights reserved.
*/
// Run this file in cli mode for best view.
// Try to edit this one.
$maxNumber = 500000000000;
// DON'T EDIT ANYTHINGS AFTER THIS LINE
$number = 0;
$numberChunk = [];
$lastNumber = 1;
$countLoop = 0;
$start = microtime(true);
while ($number <= $maxNumber) {
$sumCurrent = array_sum($numberChunk);
if ($sumCurrent != $number) {
$number += $lastNumber;
$lastNumber = $number;
}
$number++;
$numberChunk[] = $number;
$countLoop++;
}
if (isset($_SERVER['HTTP_HOST'])) {
echo "<pre>";
}
echo "Result: \n";
$octColLabel = "Octal value";
$binColLabel = "Binary value";
$octColWidth = max(strlen(max($numberChunk)), strlen($octColLabel));
$binColWidth = max($countLoop, strlen($binColLabel));
$header = "| " . str_pad($octColLabel, $octColWidth, ' ', STR_PAD_BOTH)
. " | " . str_pad("Binary value", $binColWidth, ' ', STR_PAD_BOTH) . " |";
$line = str_repeat("-", strlen($header));
echo $line . "\n";
echo $header . "\n";
echo $line . "\n";
foreach($numberChunk as $number) {
echo "| " . str_pad($number, $octColWidth, ' ', STR_PAD_LEFT) . " | ". str_pad(base_convert($number, 10, 2), $binColWidth, ' ', STR_PAD_LEFT) . " |\n";
}
echo $line . "\n";
echo "Looped: " . ($countLoop) . "\n";
echo "Total member: " . count($numberChunk) . "\n";
echo "Time usaged: " . (microtime(true) - $start) . "\n";
if (isset($_SERVER['HTTP_HOST'])) {
echo "</pre>";
}
<?php
/**
* Copyright @ 2016 by Nimit Suwannagate <[email protected]>. All rights reserved.
*/
// Run this file in cli mode for best view.
// This one is demo in case generate rare item from number.
// Try to edit this one.
$attributes = [
'+1 STR',
'+1 DEF',
'+1 INT',
'+1 LUK',
'+10 STR',
'+10 DEF',
'+10 INT',
'+10 LUK',
'RANDOM REMOVE STATUSES',
'PROTECT ALL STATUSES',
'AUTO GENERATE HP',
'AUTO GENERATE MP',
];
// DON'T EDIT ANYTHINGS AFTER THIS LINE
// CORE LOGIC FOR GENERATE BASE NUMBER
// From another file. I create this generator
/**
* [GENERATOR] Create number notation
* @param int $count
* @return Generator
*/
function numberNotationGenerator($count)
{
if (! is_numeric($count)){
throw new Exception("Count is not numeric");
}
$number = 0;
$numberChunk = [];
$lastNumber = 1;
while (true) {
$sumCurrent = array_sum($numberChunk);
if ($sumCurrent != $number) {
$number += $lastNumber;
$lastNumber = $number;
}
$number++;
$numberChunk[] = $number;
yield $number;
if ($count == count($numberChunk)){
break;
}
}
}
// CREATE AS ITEM ATTRIBUTES
$generator = numberNotationGenerator(count($attributes));
$itemAttributes = [];
foreach ($generator as $index => $value) {
$itemAttributes[$value] = $attributes[$index];
}
$maximumPossible = array_sum(array_keys($itemAttributes));
// TRY GENERATE ITEM WITH RANDOM.
// MORE NUMBER IS MORE RARE ITEM!!
// Simple random!!
function lottery($maximum)
{
$chance = range(0, $maximum);
for ($i=0; $i < rand(0, 20); $i++) {
shuffle($chance);
}
return $chance[0];
}
$chance = lottery($maximumPossible);
$currentChance = $chance;
// CREATE ITEM FROM NUMBER
$userItemAttributes = [];
$remainingBits = array_reverse(array_keys($itemAttributes));
while ($chance > 0) {
foreach ($remainingBits as $i => $value) {
if ($value > $chance) {
unset($remainingBits[$i]);
} else {
$chance -= $value;
$userItemAttributes[] = $value;
break;
}
}
}
// RESPONSE
if (isset($_SERVER['HTTP_HOST'])) {
echo "<pre>";
}
echo "GAME Item attribute table: \n";
$maxLength = 0;
$maxLength2 = 0;
foreach($itemAttributes as $value => $name) {
$maxLength = max($maxLength, strlen($name));
$maxLength2 = max($maxLength2, strlen($value));
}
foreach($itemAttributes as $value => $name) {
echo " - [ " . str_pad($name, $maxLength, ' ', STR_PAD_LEFT) . " ] | "
. "Value: " . str_pad($value, $maxLength2, ' ', STR_PAD_LEFT) . "";
if (in_array($value, $userItemAttributes)) {
echo " <- You got this!";
}
echo "\n";
}
echo "\nTRY YOUR LUCK!! \n";
echo "Current chance: " . $currentChance . "\n";
echo "Maximum chance: " . $maximumPossible . "\n";
$luck = ($currentChance * (100/$maximumPossible));
echo "Current luck: " . sprintf("%.4F", $luck) . "%\n";
$amountAttribute = count($userItemAttributes);
if ($amountAttribute == 0) {
echo "You don't have luck. You got normal item. \n";
} elseif ($luck < 30) {
echo "You have a littie luck.\n";
} elseif ($luck < 70) {
echo "You have a good luck.\n";
} elseif ($luck < 90) {
echo "You have a best luck.\n";
} else {
echo "You like a god!!\n";
}
if ($amountAttribute > 0) {
if ($amountAttribute == 1) {
echo "Your item Attribute: \n";
} else {
echo "Your item Attributes: \n";
}
foreach($userItemAttributes as $attrKey) {
echo " - [ " . str_pad($itemAttributes[$attrKey], $maxLength, ' ', STR_PAD_LEFT) . " ] | "
. "Value: " . str_pad($attrKey, $maxLength2, ' ', STR_PAD_LEFT) . "\n";
}
}
if (isset($_SERVER['HTTP_HOST'])) {
echo "</pre>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment