arsort(...)创建重复项

时间:2011-08-08 02:48:49

标签: php mysql

我正在尝试为在线游戏制作跟踪程序,我需要通过获得最多经验的人来获得经验。

但是,会发生的事情是,我正在跟踪用户(a,b,c,d,e,f,g),而用户(b,e,g)已获得0经验。它将显示(b)3次,因为有3个人获得0经验,(b)是第3个人中获得0经验的第一个人。我想不出如何在没有实际文件的情况下向您展示我正在做的事情,所以这里是文件的内容:

<?php

    $participants = array("Quuxx", "Aeterna", "Ts Danne", "Marsman", "PFC Mage");

    $skills = array("overall", "attack", "defense", "strength", "constitution", "ranged", "prayer", "magic", "cooking", "woodcutting", "fletching", "fishing", "firemaking", "crafting", "smithing", "mining", "herblore", "agility", "thieving", "slayer", "farming", "runecrafting", "hunter", "construction", "summoning", "dungeoneering");

    $database = mysql_connect("mysql.alwaysdata.com", "*", "*");

    if (!$database) {
        die('Could not connect to database: ' . mysql_error());
    }

    mysql_select_db("tracker_tkoblitz", $database);

    if (isset($_GET['track'])) {
        for ($i = 0; $i < count($participants); $i++) {
            startTracker($participants[$i]);
        }
    }

    shit(array_search($_GET['skill'], $skills));

    function startTracker($username) {
        $stats = getStats($username);
        mysql_query("INSERT IGNORE INTO stats (username, stats) VALUES ('$username', '$stats')");
    }

    function grabStats($username) {
        $query = mysql_query("SELECT * FROM stats WHERE username LIKE '$username'");
        while ($row = mysql_fetch_array($query)) {
            return $row['stats'];
        }
    }

    function shit($lol) {
        global $participants, $skills;
        sort($participants);
        echo '<head><title>Fight Tracker</title><link rel="stylesheet" href="style.css"></head><body><div align="center"><img src="http://www.runehead.com/clans/banners/clansolace-6617078.png"><table id="atable"><thead><tr><th scope="col">Username</th><th scope="col">Skill</th><th scope="col">Level</th><th scope="col">Starting experience</th><th scope="col">Ending experience</th><th scope="col">Gained experience</th></tr></thead>';
        for ($i = 0; $i < count($participants); $i++) {
            $level = getStat(grabStats($participants[$i]), 1, $lol);
            $starting = getStat(grabStats($participants[$i]), 2, $lol); //2nd param: rank,lvl,xp
            $stats = getStats($participants[$i]); //2nd param: rank,lvl,xp
            $current = getStat($stats, 2, $lol);
            $gained[] = $current - $starting;
            //echo '<tr><td class="odd">' . $participants[$i] . '</td><td class="odd"><img src="images/' . $skills[$lol] . '.png"></td><td class="odd">' . number_format($level) . '</td><td class="odd">' . number_format($starting) . '</td><td class="odd">' . number_format($current) . '</td><td class="odd">' . number_format($gained) . '</td></tr>';
            $statss[] = array($participants[$i], $level, $starting, $current, $gained[$i]);
        }
        arsort($gained);
        foreach ($gained as $gain) {
            $skillData = getSkillData($gain, $statss);
            $name = $skillData[0];
            $level = $skillData[1];
            $start = $skillData[2];
            $current = $skillData[3];
            $gainedXP = $skillData[4];
            echo '<tr><td class="odd">' . $name . '</td><td class="odd"><img src="images/' . $skills[$lol] . '.png"></td><td class="odd">' . number_format($level) . '</td><td class="odd">' . number_format($start) . '</td><td class="odd">' . number_format($current) . '</td><td class="odd">' . number_format($gainedXP) . '</td></tr>';
        }
        echo '</tbody></table></div><br><form name="skill_select" action="view_tracker.php" method="GET"><div align="center"><font color="#03F">View a different skill: </font><select name="skill"><option value="overall">Overall</option><option value="attack">Attack</option><option value="defence">Defence</option><option value="strength">Strength</option><option value="constitution">Constitution</option><option value="ranged">Ranged</option><option value="prayer">Prayer</option><option value="magic">Magic</option><option value="cooking">Cooking</option><option value="woodcutting">Woodcutting</option><option value="fletching">Fletching</option><option value="fishing">Fishing</option><option value="firemaking">Firemaking</option><option value="crafting">Crafting</option><option value="smithing">Smithing</option><option value="mining">Mining</option><option value="herblore">Herblore</option><option value="agility">Agility</option><option value="thieving">Thieving</option><option value="slayer">Slayer</option><option value="farming">Farming</option><option value="runecrafting">Runecrafting</option><option value="hunter">Hunter</option><option value="construction">Construction</option><option value="summoning">Summoning</option><option value="dungeoneering">Dungeoneering</option></select>      <input type="submit" value="View" /></div></form></body>';
    }

    function read($file) {
        if (($handle = fopen($file, "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                for ($c = 0; $c < count($data); $c++) {
                    $lines[] = $data[$c];
                }
            }
            fclose($handle);
        }
        return $lines;
    }

    function getSkillData($gain, $stats) {
        foreach ($stats as $stat) {
            if ($stat[4] == $gain) {
                return $stat;
            }
        }
    }

    function getStats($username) {
        $curl = curl_init();

        curl_setopt ($curl, CURLOPT_URL, "http://hiscore.runescape.com/index_lite.ws?player=" . $username);
        curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0", rand(4, 5)));
        curl_setopt ($curl, CURLOPT_HEADER, (int) $header);
        curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt ($curl, CURLOPT_VERBOSE, 1);

        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $output = curl_exec($curl);

        curl_close ($curl);

        if (strstr($output, "<html><head><title>")) {
            return false;
        }
        return $output;
    }

    function getStat($stats, $row, $skill) {
        $stats = explode("\n", $stats);
        $levels = explode(",", $stats[$skill]);
        return $levels[$row];
    }

    mysql_close($database);

?>

以下是同一个文件的实时演示:http://tracker.alwaysdata.net/solace/view_tracker.php

注意PFC Mage有两次,它应该分别显示PFC Mage和Quuxx。其他一切都很好,有什么想法吗?

1 个答案:

答案 0 :(得分:3)

问题出在这里:

foreach ($stats as $stat) {
    if ($stat[4] == $gain) {
        return $stat;
    }
}

此函数返回满足条件的第一个元素。因此,如果您有两个或更多具有等效exp的人,则始终仅获得第一个。考虑传递另一个参数,如nth-occurence或startIndex,如下所示:

function getSkillData($gain, $stats, $nthOccurence=1) {
    $occurence = 0;
    foreach ($stats as $stat) {
        if ($stat[4] == $gain) {
             ++$occurence;
             if ($occurence == $nthOccurence)
                  return $stat;
        }
    }
}