<?php

//EXAMPLE FUNCTION IN THIS CASE 8BALL
function ball()
{
    $answers = [
        // Positive
        "It is certain",
        "Without a doubt",
        "Yes, definitely",
        "It is decidedly so",

        // Half positive
        "Yes",
        "Most likely",
        "The outlook good",
        "As I see it, yes",
        "All signs point to yes",
        "You may rely on it",

        // Neutral
        "You should ask again later",
        "I cannot predict now",
        "Reply hazy try again",
        "Better not tell you now",
        "Concentrate and ask again",

        // Negative
        "Don't count on it",
        "My reply is no",
        "My sources say no",
        "The outlook not so good",
        "Its very doubtful",
    ];

    $rnd = rand(1, 20);`
    $rnd = rand(1, 20);
    $rnd = rand(1, 20);

    return $answers[$rnd];
}

function isadmin($user, $userid)
{
    if (!isset($user) or !isset($userid)) {
        return 0;
    }
    $isadmin = 0;
    if ($user == ADMINNAME && $userid == ADMINID) {
        return 1;
    } else {
        return 0;
    }
}

//The following 2 functions show examples for accessing the EQ2EMu Database.
//function onlineusers($isadmin) -- This creates the list of online users and formats it. Hides zone if not an admin
//function getzone($zone) -- Simple function that takes zoneID and gives you a speakable name.

function onlineusers($isadmin = 0)
{
    $link = mysqli_connect(MYSQLHOST, MYSQLUSER, MYSQLPASS, MYSQLDB);
    $sql = "SELECT * from characters where is_online=1 order by name ASC limit 25";
    $result = $link->query($sql);

    if (mysqli_num_rows($result) == 0 or mysqli_num_rows($result) == null) {
        $msg = "There are currently no players online!\n";
        mysqli_close($link);
        return $msg;
    }

    $msg = "```\nThe following players are online (limit 25):";
    $numb = 1;
    while ($row = $result->fetch_array()) {
        if ($numb == 1) {
            if ($isadmin != 1) {
                $zoneid = "**HIDDEN**";
            } else {
                $zoneid = getzone($row[10]);
            }

            $level = $row[11];
            $msg .= "\n" . $row[3] . "(" . $level . "/" . $zoneid . ")";
            $numb++;
        } else {
            if ($isadmin != 1) {
                $zoneid = "**HIDDEN**";
            } else {
                $zoneid = getzone($row[10]);
            }

            $level = $row[11];
            $msg .= " || " . $row[3] . "(" . $level . "/" . $zoneid . ")";
        }
    }

    mysqli_close($link);
    return $msg;
}

function getzone($zone)
{
    $link = mysqli_connect(MYSQLHOST, MYSQLUSER, MYSQLPASS, MYSQLDB);
    $sql = "SELECT * from zones where id=$zone limit 1";
    $query = $link->query($sql);
    $row = mysqli_fetch_row($query);

    $zone = $row[2];
    mysqli_close($link);
    return $zone;
}
?>
