<?php
ini_set("display_errors", "on");
error_reporting(E_ALL ^ E_NOTICE);

$id = $_GET['id'];
$action= $_GET['action'];

$jobListIn = array('WAR', 'MNK', 'WHM', 'BLM', 'RDM', 'THF', 'PLD', 'DRK', 'BST', 'BRD', 'RNG', 'SAM', 'NIN', 'DRG', 'SMN', 'BLU', 'COR', 'PUP', 'DNC', 'SCH');
//$jobListOut = array('Warrior', 'Monk', 'White Mage', 'Black Mage', 'Red Mage', 'Thief', 'Paladin', 'Dark Knight', 'Beastmaster', 'Bard', 'Ranger', 'Samurai', 'Ninja', 'Dragoon', 'Summoner', 'Blue Mage', 'Corsair', 'Puppetmaster', 'Dancer', 'Scholar');
$jobListOut = array(',Warrior', ',Monk', ',White Mage', ',Black Mage', ',Red Mage', ',Thief', ',Paladin', ',Dark Knight', ',Beastmaster', ',Bard', ',Ranger', ',Samurai', ',Ninja', ',Dragoon', ',Summoner', ',Blue Mage', ',Corsair', ',Puppetmaster', ',Dancer', ',Scholar');

$magicListIn = array('WhiteMagic', 'BlackMagic', 'SummonerPact', 'BardSong', 'BlueMagic', 'Ninjutsu');
$magicListOut = array('White Magic', 'Black Magic', '', '', '', '');

$skillListIn = array('HealingMagic', 'EnfeeblingMagic', 'EnhancingMagic', 'DivineMagic', 'ElementalMagic', 'DarkMagic', 'SummoningMagic', 'Ninjutsu', 'Singing', 'BlueMagic', '43');
$skillListOut = array('Healing Magic', 'Enfeebling Magic', 'Enhancing Magic', 'Divine Magic', 'Elemental Magic', 'Dark Magic', 'Summoning Magic', 'Ninjutsu', 'Bard Songs', 'Blue Magic', 'Blue Magic');

$timeListIn = array('s', 'm');
//$timeListOut = array(' seconds', ' minutes');
$timeListOut = array(' second', ' minute');

$array = @file('MagicParse.txt');
$exclude = array('Paralyga', 'Slowga', 'Hastega', 'Silencega', 'Dispelga', 'Blindga', 'Bindga', 'Breakga', 'Virus', 'Curse');

if (strlen($id) == 0) {
    print "<b>Listing</b><br />\r\n";
    $count = 0;
    foreach ($array as $key => $curSpell) {
        $currow = explode('|', $curSpell);
        if (strlen($action) == 0) {
           $count++;
           print "* [[<a href=\"Magic-O-Matic.php?id=".$key."\">".$currow[2]."</a>]]<br />\r\n";
       } else {
           $ex = str_replace($exclude, 'Exclude', $currow[2]);
           if (strlen($currow[8]) > 0) {
               $count++;
           }
           if (strlen($currow[8]) > 0 && $ex != 'Exclude') {
               $jobs = str_replace($jobListIn, $jobListOut, $currow[8]);
               $jobSplode = explode('/', trim($jobs));
               $jobList = array();
               $jobShort = array();
               foreach ($jobSplode as $k => $curjob) {
                   $curSplode = explode(',', $curjob);
                   $jobList[$k] = array();
                   $curSplode[0]++;
                   $curSplode[0]--;
                   array_push ($jobShort, $curSplode[1]." Lv.".$curSplode[0]);
               }
               $jobs = implode(', ', $jobShort);
               print "* [[<a href=\"Magic-O-Matic.php?id=".$key."\">".$currow[2]."</a>]]- $jobs<br />\r\n";
           }
       }
    }
    print $count;
} else {
    $row = explode('|', $array[$id]);
    $name = $row[2];
    $desc = $row[3];
    $magic = str_replace($magicListIn, $magicListOut, $row[4]);
    $element = str_replace('Air', 'Wind', $row[5]);
    $target = $row[6];
    $skill = str_replace($skillListIn, $skillListOut, $row[7]);
    $jobs = str_replace($jobListIn, $jobListOut, $row[8]);
    $mp = $row[9];
    $cast = str_replace($timeListIn, $timeListOut, $row[10]);
    $recast = str_replace($timeListIn, $timeListOut, $row[11]);

    $jobSplode = explode('/', trim($jobs));
    $jobList = array();
    $jobShort = array();
    foreach ($jobSplode as $key => $curjob) {
        $curSplode = explode(',', $curjob);
        $jobList[$key] = array();
        $curSplode[0]++;
        $curSplode[0]--;
        $jobList[$key][0] = $curSplode[0];
        $jobList[$key][1] = '[['.$curSplode[1].']]';
        array_push ($jobShort, '[['.$curSplode[1].']]');
    }
    $jobs = implode(', ', $jobShort);
    $targ = ($target == "Self") ? 'me' : 't';
    $castSplode = explode(" ", $cast);
    $recastSplode = explode(" ", $recast);
    $castSplode[0]++;
    $castSplode[0]--;
    $recastSplode[0]++;
    $recastSplode[0]--;
    $add = '';
    if ($skill == 'Ninjutsu') {
        $mpSplode = explode('"', $desc);
        $mpSplode[1] = str_replace('.', '', $mpSplode[1]);
        $mpSplode[1] = trim($mpSplode[1]);
        $mp = '[['.ucwords($mpSplode[1]).']]';
        $add = "&lt;br /&gt;or&lt;br /&gt;/nin \"$name\" &lt;$targ&gt;";
    } elseif ($skill == 'Bard Songs') {
        $mp = 'N/A';
        $add = "&lt;br /&gt;or&lt;br /&gt;/so \"$name\" &lt;$targ&gt;";
    }
    if ($castSplode[0] != "1") {
        $castSplode[1] .= 's';
    }
    if ($recastSplode[0] != "1") {
        $recastSplode[1] .= 's';
    }

    $cast = implode(' ', $castSplode);
    $recast = implode(' ', $recastSplode);
    $imageName = str_replace(':', '_', $name);
    $categories = "[[Category:Spells]] [[Category:$skill]]";
    if (strlen($magic) > 0) {
        $categories .= " [[Category:$magic]]";
    }
    if (count($jobList) > 1) {
        $rowSpan = "rowspan=\"".(count($jobList))."\"";
    } else {
        $rowSpan = '';
    }
    if ($skill == "Blue Magic") {
        if (stripos($desc, "within range") !== false || stripos($desc, "area attack") !== false || stripos($desc, "area of effect") !== false) {
            $tar = "AOE";
        } elseif (stripos($desc, "fan-shaped area") !== false || stripos($desc, "fan- shaped area") !== false) {
            $tar = "Cone";
        } else if ($name == 'Bomb Toss') {
            $tar = "AOE";
        } else {
            $tar = "Single";
        }

        if (strpos($desc, "Additional effect:") !== false) {
            $splode = explode("Additional effect:", $desc);
            $cutListIn = array('Chance of effect', 'Duration of effect', 'Accuracy varies with TP', 'Damage varies with TP', '.', '"');
            $cutListOut = array('Chance of Effect', 'Chance of Effect', '', '', '', '');
            $splode[1] = str_replace($cutListIn, $cutListOut, $splode[1]);
            $rebasplosion = explode ('Chance of Effect', $splode[1]);
            $ae = trim($rebasplosion[0]);
        } else {
            $ae = '';
        }
        $type = '';
        $elemental = '';
        $noHits = '';
        $class = ($element == 'Special') ? 'Physical' : 'Magical';
        if ($class == 'Physical') {
            $subclass = 'damage';
            if (stripos($desc, "delivers a") !== false && stripos($desc, "fold attack") !== false) {
                $splode = explode("fold attack", $desc);
                $num = str_replace("Delivers a ", '', $splode[0]);
                $num = trim($num);
                $numsIn = array("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");
                $numsOut = array("2", "3", "4", "5", "6", "7", "8", "9", "10");
                $noHits = "\r\n<br />|nohits=".str_replace($numsIn, $numsOut, $num);
            } elseif (stripos($desc, "restore") !== false && stripos($desc, "hp") !== false) {
                $subclass = 'healing';
                $type = 'healing';
                $class = 'Magical';
            } elseif ($target == 'Self') {
                $subclass = 'buff';
                $type = 'buff';
                $class = 'Magical';
            }
        } else {
            if ($ae != '' || stripos($desc, "damage") !== false) {
                $subclass = 'nuke';
                $type = $element;
                $elemental = $element;
            } elseif (stripos($desc, "Steals") !== false) {
                $subclass = 'drain';
                $type = $element;
                $elemental = $element;
            } elseif (stripos($desc, "restore") !== false && stripos($desc, "hp") !== false) {
                $subclass = 'healing';
                $type = 'healing';
            } elseif ($target == 'Enemy') {
                $subclass = 'enfeeble';
                $type = 'enfeeble';
                $elemental = $element;
            } else {
                $subclass = 'buff';
                $type = 'buff';
                $elemental = $element;
            }
        }
        if ($elemental != '') {
            $elemental = "{{Template:$elemental}}";
        }
        print "[[Image:$imageName (Scroll) description.png]]
<br />{{Blue Magic
<br />|class=$class
<br />|subclass=$subclass
<br />|type=$type$noHits
<br />|desc=$desc
<br />|mp=$mp
<br />|blupoint=
<br />|cast=$cast
<br />|recast=$recast
<br />|target=$tar
<br />|range=
<br />|stats=
<br />|effects=$ae
<br />|duration=
<br />|trait=
<br />|level=".$jobList[0][0]."
<br />|family=
<br />|blucap=
<br />|fstrcap=
<br />|tm=
<br />|primod=
<br />|element=$elemental
<br />|hppercent=
<br />|flv=
<br />|wsc=
<br />|ftp100=
<br />|ftp200=
<br />|ftp300=
<br />|scprop=
<br />|ve=
<br />|ce=
<br />|ale=
<br />|notes=
<br />}}";
    } else {
        print "[[Image:$imageName (Scroll) description.png]]
<br />{| border=\"1\" cellpadding=\"0\" cellspacing=\"0\" BGCOLOR=\"#cee0f2\" width=\"55%\"
<br />|BGCOLOR=\"#cee0f2\" colspan=\"3\" align=\"center\"|'''Spell Information'''
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"20%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;'''Job:'''
<br />|BGCOLOR=\"#ffffff\"  width=\"80%\" align=\"left\" colspan=\"2\"|&amp;nbsp;$jobs
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"20%\" align=\"left\" colspan=\"1\" $rowSpan valign=\"center\"|&amp;nbsp;'''Level Obtained:'''";
        foreach ($jobList as $curjob) {
            print "<br />|BGCOLOR=\"#ffffff\"  width=\"30%\" align=\"left\"|&amp;nbsp;".$curjob[1]."
<br />|BGCOLOR=\"#ffffff\"  width=\"40%\" align=\"left\"|&amp;nbsp; Level ".$curjob[0]."
<br />|-";
        }

        print "<br />|BGCOLOR=\"#cee0f2\" width=\"20%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;'''Description:'''
<br />|BGCOLOR=\"#ffffff\"  width=\"80%\" align=\"left\" colspan=\"2\"|&amp;nbsp;$desc
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"30%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;'''Spell Category:'''
<br />|BGCOLOR=\"#ffffff\"  width=\"70%\" align=\"left\" colspan=\"2\"|&amp;nbsp;[[:Category:$skill|$skill]]
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"30%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;'''Element:'''
<br />|BGCOLOR=\"#ffffff\"  width=\"70%\" align=\"left\" colspan=\"2\"|&amp;nbsp;{{Template:$element}}
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"30%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;'''MP Cost:'''
<br />|BGCOLOR=\"#ffffff\"  width=\"70%\" align=\"left\" colspan=\"2\"|&amp;nbsp;$mp
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"30%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;'''Casting Time:'''
<br />|BGCOLOR=\"#ffffff\"  width=\"70%\" align=\"left\" colspan=\"2\"|&amp;nbsp;$cast
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"30%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;'''Duration:'''
<br />|BGCOLOR=\"#ffffff\"  width=\"70%\" align=\"left\" colspan=\"2\"|&amp;nbsp;
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"30%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;'''Recast Time:'''
<br />|BGCOLOR=\"#ffffff\"  width=\"70%\" align=\"left\" colspan=\"2\"|&amp;nbsp;$recast
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"20%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;'''From Merits?'''
<br />|BGCOLOR=\"#ffffff\"  width=\"80%\" colspan=\"2\" align=\"left\"|&amp;nbsp;No
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"30%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;'''Command:'''
<br />|BGCOLOR=\"#ffffff\"  width=\"70%\" align=\"left\" colspan=\"2\"|&amp;nbsp;/ma \"$name\" &lt;$targ&gt;$add
<br />|-
<br />|}
<br />==Equipment==
<br />{| border=\"1\" cellpadding=\"0\" cellspacing=\"0\" BGCOLOR=\"#cee0f2\" width=\"50%\"
<br />|BGCOLOR=\"#cee0f2\" colspan=\"2\" align=\"center\"|'''Modified by Following Equipment '''
<br />|-
<br />|BGCOLOR=\"#cee0f2\" width=\"30%\" align=\"left\" colspan=\"1\" valign=\"top\"|&amp;nbsp;
<br />|BGCOLOR=\"#ffffff\"  width=\"70%\" align=\"left\"|&amp;nbsp;
<br />|-
<br />|}
<br />$categories";
    }
}
?>
