PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/boca-1.5.0/src/fscore.php

https://bitbucket.org/jorgenio/boca
PHP | 349 lines | 308 code | 13 blank | 28 comment | 122 complexity | 35128f6fba10f8216a336bdca3a99a3c MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. //BOCA Online Contest Administrator
  4. // Copyright (C) 2003-2012 by BOCA Development Team (bocasystem@gmail.com)
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // Last modified 05/aug/2012 by cassio@ime.usp.br
  19. ////////////////////////////funcoes para placar///////////////////////////////////////////////
  20. function ScoreCMP($a,$b) {
  21. if ($a["totalcount"]=="") $a["totalcount"]=0;
  22. if ($b["totalcount"]=="") $b["totalcount"]=0;
  23. if ($a["totaltime"]=="") $a["totaltime"]=0;
  24. if ($b["totaltime"]=="") $b["totaltime"]=0;
  25. if ($a["totalcount"]>$b["totalcount"]) return 1;
  26. else if ($a["totalcount"]<$b["totalcount"]) return -1;
  27. else {
  28. if ($a["totaltime"]<$b["totaltime"]) return 1;
  29. else if ($a["totaltime"]>$b["totaltime"]) return -1;
  30. else {
  31. if(isset($a["first"]) && $a["first"] != 0) {
  32. if($a["first"]<$b["first"]) return 1;
  33. else if($a["first"]>$b["first"]) return -1;
  34. }
  35. if ($a["user"]<$b["user"]) return 1;
  36. else if ($a["user"]>$b["user"]) return -1;
  37. else return 0;
  38. }
  39. }
  40. }
  41. function ordena($a) {
  42. $r = array();
  43. for ($i=0; $i<count($a); $i++) {
  44. $max=null;
  45. reset($a);
  46. while ( list ($e, $c) = each($a) ) {
  47. if ($a[$e]!=null && ($max==null || ScoreCMP($a[$e],$max) > 0)) {
  48. $j=0;
  49. for(;$j<$i;$j++)
  50. if($r[$j]['user']==$a[$e]['user'] && $r[$j]['site']==$a[$e]['site']) break;
  51. if($j>=$i) {
  52. $max = $a[$e];
  53. $maxe = $e;
  54. }
  55. }
  56. }
  57. if ($max==null) break;
  58. $r[$i] = $max;
  59. $a[$maxe] = null;
  60. }
  61. return $r;
  62. }
  63. function DBScore($contest, $verifylastmile, $hor=-1, $globalsite='0') {
  64. $c = DBConnect();
  65. $r = DBExec($c, "select sitenumber as number from sitetable where contestnumber=$contest and siteactive='t'",
  66. "DBScore(get site)");
  67. $n = DBnlines($r);
  68. if ($n == 0) {
  69. LOGError("Unable to get site information. No active sites available (contest=$contest)");
  70. MSGError("Unable to get site information. No active sites available. Contact an admin now!");
  71. exit;
  72. }
  73. $a = array();
  74. $resp = array();
  75. $whichsites=explode(',',$globalsite);
  76. for ($i=0;$i<$n;$i++) {
  77. $a = DBRow($r,$i);
  78. if(in_array($a["number"], $whichsites) || in_array(0,$whichsites)) {
  79. list($resp1,$data0) = DBScoreSite($contest, $a["number"], $verifylastmile, $hor);
  80. $resp = array_merge($resp, $resp1);
  81. }
  82. }
  83. $ds = DIRECTORY_SEPARATOR;
  84. if($ds=="") $ds = "/";
  85. $probs=DBGetProblems($contest); $nprobs=count($probs);
  86. foreach (glob($_SESSION['locr'] . $ds . "private" .$ds . "remotescores" . $ds . "score*.dat") as $fname) {
  87. $fc=file_get_contents($fname);
  88. if(($arr = unserialize(base64_decode($fc)))===false) {
  89. LOGError("File " . sanitizeText($fname) . " is not compatible");
  90. } else {
  91. if(is_array($arr)) {
  92. if(isset($arr['site'])) {
  93. $site=$arr['site'];
  94. if(!in_array($site, $whichsites) && !in_array(0,$whichsites)) continue;
  95. $fine=1;
  96. reset($resp);
  97. while(list($e, $c) = each($resp)) {
  98. if($resp[$e]['site']==$site) { $fine=0; break; }
  99. }
  100. if($fine) {
  101. list($arr,$data0) = DBScoreSite($contest, $site, $verifylastmile, $hor, $arr);
  102. reset($arr);
  103. while(list($ee,$cc) = each($arr)) {
  104. if($site != $arr[$ee]['site']) {
  105. $arr[$ee]=null;
  106. unset($arr[$ee]);
  107. } else {
  108. // just to make the color of the other site changed to the color of the problem in this site
  109. while(list($e2,$c2) = each($arr[$ee]["problem"])) {
  110. for($prob=0; $prob<$nprobs; $prob++) {
  111. if($probs[$prob]['number']==$e2) {
  112. $arr[$ee]['problem'][$e2]['color'] = $probs[$prob]['color'];
  113. $arr[$ee]['problem'][$e2]['colorname'] = $probs[$prob]['colorname'];
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. $resp = array_merge($resp, $arr);
  121. }
  122. } else {
  123. // old version -- just for compatibility ---
  124. while(list($ee,$cc) = each($arr)) {
  125. $fine=1;
  126. reset($resp);
  127. while(list($e, $c) = each($resp)) {
  128. if($resp[$e]['site']==$arr[$ee]['site']) { $fine=0; break; }
  129. }
  130. if($fine==0) $arr[$ee]=null;
  131. else {
  132. // just to make the color of the other site changed to the color of the problem in this site
  133. while(list($e2,$c2) = each($arr[$ee]["problem"])) {
  134. for($prob=0; $prob<$nprobs; $prob++) {
  135. if($probs[$prob]['number']==$e2) {
  136. $arr[$ee]['problem'][$e2]['color'] = $probs[$prob]['color'];
  137. $arr[$ee]['problem'][$e2]['colorname'] = $probs[$prob]['colorname'];
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. $resp = array_merge($resp, $arr);
  145. // ---- end of old version ---
  146. }
  147. }
  148. // MSGError("got scores from $fname");
  149. }
  150. }
  151. if (($result = ordena ($resp)) === false) {
  152. LOGError("Error while sorting scores (contest=$contest).");
  153. MSGError("Error while sorting scores. Contact an admin now!");
  154. }
  155. return $result;
  156. }
  157. function DBBalloon($contest, $site, $user, $problem, $localsite=true, $c=null) {
  158. if($c==null)
  159. $c = DBConnect();
  160. if (($b = DBSiteInfo($contest, $site, $c)) == null)
  161. exit;
  162. if ($localsite) {
  163. if (($blocal = DBSiteInfo($contest, $_SESSION["usertable"]["usersitenumber"], $c)) == null)
  164. exit;
  165. } else $blocal = $b;
  166. if (($ct = DBContestInfo($contest,$c)) == null)
  167. exit;
  168. $t = time();
  169. $ta = $blocal["currenttime"];
  170. // if ($verifylastmile)
  171. $tf = $b["sitelastmilescore"];
  172. // else {
  173. // $tf = $b["siteduration"];
  174. // }
  175. $r = DBExec($c, "select r.rundatediff as time, a.yes as yes from " .
  176. "runtable as r, answertable as a where r.runanswer=a.answernumber and " .
  177. "a.contestnumber=$contest and r.usernumber=$user and r.runproblem=$problem and " .
  178. "r.contestnumber=$contest and r.runsitenumber=$site and (r.runstatus ~ 'judged' or r.runstatus ~ 'judged+') and " .
  179. "r.rundatediff>=0 and r.rundatediff<=$tf and r.rundatediffans<=$ta " .
  180. "order by r.rundatediff", "DBBalloon(get runs)");
  181. $n = DBnlines($r);
  182. for ($i=0;$i<$n;$i++) {
  183. $a = DBRow($r,$i);
  184. if($a["yes"]=='t') return true;
  185. }
  186. return false;
  187. }
  188. function DBRecentNews($contest, $site, $verifylastmile, $minutes=3) {
  189. if (($b = DBSiteInfo($contest, $site)) == null)
  190. exit;
  191. if (($blocal = DBSiteInfo($contest, $_SESSION["usertable"]["usersitenumber"])) == null)
  192. exit;
  193. if (($ct = DBContestInfo($contest)) == null)
  194. exit;
  195. $t = time();
  196. $ta = $blocal["currenttime"];
  197. $taa = $ta - $minutes*60;
  198. if ($verifylastmile)
  199. $tf = $b["sitelastmilescore"];
  200. else {
  201. $tf = $b["siteduration"];
  202. }
  203. $c = DBConnect();
  204. $r = DBExec($c, "select a.yes as yes, p.problemcolor as color, p.problemcolorname as colorname, u.userfullname as userfullname, " .
  205. "u.usernumber as usernumber, p.problemnumber as problemnumber, p.problemname, (r.rundatediffans>$ta) as fut, min(r.rundatediff) as time from " .
  206. "runtable as r, answertable as a, problemtable as p, usertable as u where r.runanswer=a.answernumber and " .
  207. "p.contestnumber=$contest and a.contestnumber=$contest and r.usernumber = u.usernumber and u.usertype='team' and " .
  208. "p.problemnumber=r.runproblem and r.contestnumber=$contest and r.runsitenumber=$site and u.userenabled='t' and " .
  209. "r.rundatediff>=$taa and r.rundatediff<=$tf and r.rundatediff<=$ta and u.contestnumber=$contest and u.usersitenumber=$site and " .
  210. "((a.yes='t' and r.rundatediffans<=$ta) or (r.rundatediffans>$ta)) " .
  211. "group by a.yes,p.problemcolor,p.problemcolorname,p.problemname,u.userfullname,u.usernumber,p.problemnumber,fut order by time", "DBRecentNews(get runs)");
  212. $n = DBnlines($r);
  213. $a = array();
  214. for ($i=0;$i<$n;$i++) {
  215. $a[$i] = DBRow($r,$i);
  216. if($a[$i]["fut"]=='t' && $a[$i]["yes"]=='t') $a[$i]["yes"]='f';
  217. }
  218. return $a;
  219. }
  220. function DBScoreSite($contest, $site, $verifylastmile, $hor=-1, $data=null) {
  221. if (($blocal = DBSiteInfo($contest, $_SESSION["usertable"]["usersitenumber"])) == null)
  222. exit;
  223. if (($b = DBSiteInfo($contest, $site, null, false)) == null)
  224. $b=$blocal;
  225. if (($ct = DBContestInfo($contest)) == null)
  226. exit;
  227. $t = time();
  228. $ta = $blocal["currenttime"];
  229. if($hor >= 0) $ta = $hor;
  230. if ($verifylastmile)
  231. $tf = $b["sitelastmilescore"];
  232. else {
  233. $tf = $b["siteduration"];
  234. }
  235. if($data != null && is_numeric($data)) {
  236. if($data < $ta) $ta = $data;
  237. $data=null;
  238. }
  239. $data0=array();
  240. if($data==null) {
  241. $c = DBConnect();
  242. $resp = array();
  243. $r = DBExec($c, "select * from usertable where contestnumber=$contest and usersitenumber=$site and ".
  244. "usertype='team' and userlastlogin is not null and userenabled='t'", "DBScoreSite(get users)");
  245. $n = DBnlines($r);
  246. for ($i=0;$i<$n;$i++) {
  247. $a = DBRow($r,$i);
  248. $resp[$a["usernumber"]]["user"]=$a["usernumber"];
  249. $resp[$a["usernumber"]]["site"]=$a["usersitenumber"];
  250. $resp[$a["usernumber"]]["username"]=$a["username"];
  251. $resp[$a["usernumber"]]["usertype"]=$a["usertype"];
  252. $resp[$a["usernumber"]]["userfullname"]=$a["userfullname"];
  253. $resp[$a["usernumber"]]["totaltime"]=0;
  254. $resp[$a["usernumber"]]["totalcount"]=0;
  255. $resp[$a["usernumber"]]["problem"]=array();
  256. }
  257. $r = DBExec($c, "select r.usernumber as user, p.problemname as problemname, r.runproblem as problem, ".
  258. "p.problemcolor as color, p.problemcolorname as colorname, " .
  259. "r.rundatediff as time, r.rundatediffans as anstime, a.yes as yes, r.runanswer as answer from " .
  260. "runtable as r, answertable as a, problemtable as p where r.runanswer=a.answernumber and " .
  261. "a.contestnumber=$contest and p.problemnumber=r.runproblem and p.contestnumber=$contest and " .
  262. "r.contestnumber=$contest and r.runsitenumber=$site and (r.runstatus ~ 'judged' or r.runstatus ~ 'judged+') and " .
  263. "r.rundatediff>=0 and r.rundatediff<=$tf and r.rundatediffans<=$ta " .
  264. "order by r.usernumber, r.runproblem, r.rundatediff", "DBScoreSite(get runs)");
  265. $n = DBnlines($r);
  266. $a = array();
  267. for ($i=0;$i<$n;$i++) {
  268. $a[$i] = DBRow($r,$i);
  269. }
  270. $data0['n']=$n;
  271. $data0['resp']=$resp;
  272. $data0['a']=$a;
  273. $data0['site']=$site;
  274. } else {
  275. $resp=$data['resp'];
  276. $n=$data['n'];
  277. $a=$data['a'];
  278. }
  279. $i=0;
  280. while ($i<$n) {
  281. if($a[$i]["anstime"] > $ta) { $i++; continue; }
  282. $user = $a[$i]["user"];
  283. $problem = $a[$i]["problem"];
  284. $time = 0;
  285. $k = 0;
  286. if(!isset($resp[$user])) { $i++; continue; }
  287. $resp[$user]["user"] = $user;
  288. $resp[$user]["site"] = $site;
  289. $resp[$user]["problem"][$problem]["name"] = $a[$i]["problemname"];
  290. $resp[$user]["problem"][$problem]["color"] = $a[$i]["color"];
  291. $resp[$user]["problem"][$problem]["colorname"] = $a[$i]["colorname"];
  292. $resp[$user]["problem"][$problem]["solved"] = false;
  293. $resp[$user]["problem"][$problem]["judging"] = false;
  294. $resp[$user]["problem"][$problem]["time"] = 0;
  295. $resp[$user]["problem"][$problem]["penalty"] = 0;
  296. $resp[$user]["problem"][$problem]["count"] = 0;
  297. while ($i<$n && $a[$i]["user"]==$user && $a[$i]["problem"]==$problem && $a[$i]["yes"]!='t') {
  298. $time += (int) (($ct["contestpenalty"])/60);
  299. $k++;
  300. $i++;
  301. }
  302. $resp[$user]["problem"][$problem]["count"] = $k;
  303. if ($i>=$n) break;
  304. if($a[$i]["user"]==$user && $a[$i]["problem"]==$problem && $a[$i]["yes"]=='t') {
  305. $timet = (int) (($a[$i]["time"])/60);
  306. if(!isset($resp[$user]["first"]) || $timet < $resp[$user]["first"])
  307. $resp[$user]["first"] = $timet;
  308. $time += $timet;
  309. $resp[$user]["problem"][$problem]["time"] = $timet;
  310. $resp[$user]["problem"][$problem]["penalty"] = $time;
  311. $resp[$user]["problem"][$problem]["solved"] = true;
  312. $resp[$user]["problem"][$problem]["count"]++;
  313. $resp[$user]["totaltime"] += $time;
  314. $resp[$user]["totalcount"]++;
  315. }
  316. while ($i<$n && $a[$i]["user"]==$user && $a[$i]["problem"]==$problem) {
  317. $i++;
  318. }
  319. }
  320. if($data==null) {
  321. $aa = DBRecentNews($contest, $site, $verifylastmile, $ta);
  322. $data0['aa']=$aa;
  323. } else $aa=$data['aa'];
  324. for($i=0; $i<count($aa); $i++) {
  325. if($aa[$i]["fut"]=='t') {
  326. $resp[$aa[$i]["usernumber"]]["problem"][$aa[$i]["problemnumber"]]["judging"] = true;
  327. }
  328. }
  329. if (($result = ordena ($resp)) === false) {
  330. LOGError("Error while sorting scores (contest=$contest, site=$site).");
  331. MSGError("Error while sorting scores. Contact an admin now!");
  332. }
  333. return array($result,$data0);
  334. }
  335. // eof
  336. ?>