<?php
    /*
        Template Name: Popular node
    */

    $count = (int)get_option('popularnode_count');
    $count = $count+1;
    update_option('popularnode_count', $count);

    $posts = array_slice(json_decode(get_option('ppp_options')), 0, 10);
    $rates = array();

    foreach ($posts as $item){
        $rate = 100 * $item->rate/array_sum(array_map(function($obj){return $obj->rate;}, $posts));
        $rates[$item->id] = $rate;
    }

    wp_redirect(get_permalink(chance($rates)).'/amp/', 301);
    exit;
