topguilds.php

main 143 lines · 3.4 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1<?php require_once 'engine/init.php'; theme_open();
2
3 // Cache the results
4 $cache = new Cache('engine/cache/topGuilds');
5 if ($cache->hasExpired()) {
6 $guilds = db()->fetchAll("
7 SELECT `g`.`id` AS `id`, `g`.`name` AS `name`, COALESCE(`kills`.`frags`, 0) AS `frags`
8 FROM `guilds` g
9 LEFT JOIN (
10 SELECT `gm`.`guild_id` AS `guild_id`, COUNT(*) AS `frags`
11 FROM `player_deaths` pd
12 INNER JOIN `players` p ON `p`.`name` = `pd`.`killed_by`
13 INNER JOIN `guild_membership` gm ON `gm`.`player_id` = `p`.`id`
14 WHERE `pd`.`unjustified` = 1
15 GROUP BY `gm`.`guild_id`
16 ) kills ON `kills`.`guild_id` = `g`.`id`
17 ORDER BY `frags` DESC, `g`.`name` ASC
18 LIMIT 0, 10;
19 ");
20
21 $cache->setContent($guilds);
22 $cache->save();
23 } else {
24 $guilds = $cache->load();
25 }
26 $count = 1;
27
28 function convert_number_to_words($number) {
29
30 $hyphen = '-';
31 $conjunction = ' and ';
32 $separator = ', ';
33 $negative= 'negative ';
34 $decimal = ' point ';
35 $dictionary = array(
36 0 => 'zero',
37 1 => 'first',
38 2 => 'second',
39 3 => 'third',
40 4 => 'fourth',
41 5 => 'fifth',
42 6 => 'sixth',
43 7 => 'seventh',
44 8 => 'eighth',
45 9 => 'ninth',
46 10 => 'tenth',
47 11 => 'eleventh',
48 12 => 'twelve',
49 13 => 'thirteen',
50 14 => 'fourteen',
51 15 => 'fifteen',
52 16 => 'sixteen',
53 17 => 'seventeen',
54 18 => 'eighteen',
55 19 => 'nineteen',
56 20 => 'twenty',
57 30 => 'thirty',
58 40 => 'fourty',
59 50 => 'fifty',
60 60 => 'sixty',
61 70 => 'seventy',
62 80 => 'eighty',
63 90 => 'ninety',
64 100 => 'hundred',
65 1000 => 'thousand',
66 1000000 => 'million',
67 1000000000 => 'billion',
68 1000000000000 => 'trillion',
69 1000000000000000 => 'quadrillion',
70 1000000000000000000 => 'quintillion'
71 );
72
73 if (!is_numeric($number)) {
74 return false;
75 }
76
77 if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
78 // overflow
79 trigger_error(
80 'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
81 E_USER_WARNING
82 );
83 return false;
84 }
85
86 if ($number < 0) {
87 return $negative . convert_number_to_words(abs($number));
88 }
89
90 $string = $fraction = null;
91
92 if (strpos($number, '.') !== false) {
93 list($number, $fraction) = explode('.', $number);
94 }
95
96 switch (true) {
97 case $number < 21:
98 $string = $dictionary[$number];
99 break;
100 case $number < 100:
101 $tens = ((int) ($number / 10)) * 10;
102 $units = $number % 10;
103 $string = $dictionary[$tens];
104 if ($units) {
105 $string .= $hyphen . $dictionary[$units];
106 }
107 break;
108 case $number < 1000:
109 $hundreds = $number / 100;
110 $remainder = $number % 100;
111 $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
112 if ($remainder) {
113 $string .= $conjunction . convert_number_to_words($remainder);
114 }
115 break;
116 default:
117 $baseUnit = pow(1000, floor(log($number, 1000)));
118 $numBaseUnits = (int) ($number / $baseUnit);
119 $remainder = $number % $baseUnit;
120 $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
121 if ($remainder) {
122 $string .= $remainder < 100 ? $conjunction : $separator;
123 $string .= convert_number_to_words($remainder);
124 }
125 break;
126 }
127
128 if (null !== $fraction && is_numeric($fraction)) {
129 $string .= $decimal;
130 $words = array();
131 foreach (str_split((string) $fraction) as $number) {
132 $words[] = $dictionary[$number];
133 }
134 $string .= implode(' ', $words);
135 }
136
137 return $string;
138}
139
140view('topguilds');
141
142theme_close();
143
Top