Jump to content
Korean Random
Sign in to follow this  
Aydomir

Вычисление среднего уровня

Recommended Posts

Помогите правильно вычислить средний уровень игрока. У меня получилось только на PHP (http://k-94.ru/test/GetLvl.php?n=31745329&b=20099), где n=31745329 = ID игрока, а b=20099 = кол-во боев.

 

Так выглядит мой PHP-код

<? header("Content-type: application/json; encoding = utf-8");
if($_GET['n'] && $_GET['b']){

$tanks = file_get_contents('http://api.worldoftanks.ru/wot/account/tanks/?application_id=demo&account_id='.$_GET['n'].'&fields=tank_id,statistics.battles');
$tanks = json_decode($tanks, true);
$tanksd = $tanks['data'][''.$_GET['n'].''];
$ctns = count($tanksd);
$tanklvl = array();
for($i=0;$i<$ctns;$i++){array_push($tanklvl,array('id'=>$tanksd[$i]['tank_id']));}

$json = file_get_contents('http://api.worldoftanks.ru/wot/encyclopedia/tanks/?application_id=demo&fields=level');
$json = json_decode($json, true);

$lvl1 = array();$lvl2 = array();$lvl3 = array();$lvl4 = array();$lvl5 = array();$lvl6 = array();$lvl7 = array();$lvl8 = array();$lvl9 = array();$lvl10 = array();

for($i = 0;$i < count($tanklvl);$i++){
$getlvl = $json['data'][$tanklvl[$i]['id']]['level'];
$getbrs = $tanksd[$i]['statistics']['battles'];
if($getlvl == 1){array_push($lvl1,$getbrs);}
if($getlvl == 2){array_push($lvl2,$getbrs);}
if($getlvl == 3){array_push($lvl3,$getbrs);}
if($getlvl == 4){array_push($lvl4,$getbrs);}
if($getlvl == 5){array_push($lvl5,$getbrs);}
if($getlvl == 6){array_push($lvl6,$getbrs);}
if($getlvl == 7){array_push($lvl7,$getbrs);}
if($getlvl == 8){array_push($lvl8,$getbrs);}
if($getlvl == 9){array_push($lvl9,$getbrs);}
if($getlvl == 10){array_push($lvl10,$getbrs);}
}

$mid = (array_sum($lvl1)*1+array_sum($lvl2)*2+array_sum($lvl3)*3+array_sum($lvl4)*4+array_sum($lvl5)*5+array_sum($lvl6)*6+array_sum($lvl7)*7+array_sum($lvl8)*8+array_sum($lvl9)*9+array_sum($lvl10)*10)/$_GET['b'];

$b = array('mid'=>$mid);
echo json_encode($b);
}
?> 

 

Но загрузка через сервер намного задерживает мод. Помогите вычислить средний уровень не таким способом.

 

Мне предлагали это, но здесь что-то не так:

veh_arr = []
    def get_unlocks(code, value):
        for item in value:
            obj = g_itemsCache.items.getItemByCD(item)
            if hasattr(obj, 'itemTypeName') and obj.itemTypeName == 'vehicle':
                veh_arr.append(obj.level)
                
    player.stats.get('unlocks', get_unlocks)
    print float(sum(veh_arr)) / len(veh_arr) 

У меня на самом деле средний уровень 6.99 (http://wot-news.com/stat/calc/ru/ru/aydomir), а данный код показывает 5.37

  • Upvote 1
  • Downvote 1

Share this post


Link to post

Short link
Share on other sites

<?php
$start_time = microtime(true);

$language = 'ru';
$account_id = (int)$_GET['account_id'];

function _post($url,$f_)
{
  global $language;
  if ($f_ != null)
  {
    foreach($f_ as $k=>$v) $f .= "&$k=$v";
  } else
  {
    $f = '';
  }
  $ch=curl_init();
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_POST,1);
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_POSTFIELDS, "application_id=demo&language=$language$f");
  $data=curl_exec($ch);
  curl_close($ch);
  return $data;
}

$account_info_ = _post('http://api.worldoftanks.ru/wot/account/info/', array('account_id'=>$account_id, 'fields'=>'statistics.all.battles'));
$account_info = json_decode($account_info_, true);

$tanks_stats = _post('http://api.worldoftanks.ru/wot/tanks/stats/', array('account_id'=>$account_id, 'fields'=>'tank_id,all.battles'));

$tanks_stats = json_decode($tanks_stats, true);
$tanks_stats = $tanks_stats['data'][$account_id];

$encyclopedia = _post('http://api.worldoftanks.ru/wot/encyclopedia/tanks/', array('fields'=>'level'));
$encyclopedia = json_decode($encyclopedia, true);
$encyclopedia = $encyclopedia['data'];

$battles = $account_info['data'][$account_id]['statistics']['all']['battles'];

$mid_lvl = 0;
if ($battles)
{
foreach ($tanks_stats as $mini_array)
{
  if ($mini_array['all']['battles'] != 0)
  {
   $tank_id = (int)$mini_array['tank_id'];
   $tank_level = $encyclopedia[$tank_id]['level'];
   $mid_lvl += $tank_level * $mini_array['all']['battles'];
  }
}
$MID = round($mid_lvl/$battles, 2);
}
$b = array(
  'mid'=>$MID,
  'request_time'=>microtime(true) - $start_time
);
echo json_encode($b);
?>

Вот код попроще)

И забудь о file_get_contents навсегда!!!

cUrl в PHP решает!!!

Скорость загрузки намного выше!!!

Ссылка на мой способ: тыц

Ссылка на твой способ: тыц (добавил только показатель вемени запроса)

Я бы хотел вычислить без php.

Share this post


Link to post

Short link
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...