/
rankings.cgi
executable file
·139 lines (129 loc) · 4.02 KB
/
rankings.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/ruby
print "Content-type: text/html\r\n\r\n"
require 'cgi'
require 'cgi/session'
load 'functions.rb'
$cgi = CGI.new
user_id = get_validated_id
$user = User.new(user_id) if user_id
rank = $cgi['metric']
rank = 'frags' if rank == nil
case rank
when "deaths" then type, metric = "players", "deaths"
when "frags"
type, metric = "players", "frags"
blurb = "Frags measure not just the number of foes killed, " +
"but the quality of one's opponents. New players begin with " +
"one frag, and when a character is knocked out, the attacker " +
"takes half their frags (rounded up)."
when "kills" then type, metric = "players", "kills"
when "points" then type, metric = "players", "points"
when "revives" then type, metric = "players", "revives"
when "survival"
type, metric, order = "players", "lastrevive", "ASC"
column = "Last Revived"
display = Proc.new {|date| Time.str_to_time(date).ago}
clause = "`hp` != '0' "
when "oldies"
type, metric, order = "players", "joined", "ASC"
display = Proc.new {|date| Time.str_to_time(date).ago}
when "younguns"
type, metric = "players", "joined"
display = Proc.new {|date| Time.str_to_time(date).ago}
when "bigtowns" then type, metric = "settlements", "population"
when "newtowns"
type, metric = "settlements", "founded"
display = Proc.new {|date| Time.str_to_time(date).ago}
when "oldtowns"
type, metric, order = "settlements", "founded", "ASC"
display = Proc.new {|date| Time.str_to_time(date).ago}
end
if type == "players"
order = "DESC" if order == nil
query = "SELECT * FROM `users`, `accounts` " +
"WHERE `users`.`id` = `accounts`.`id` " +
if clause != nil then " AND #{clause} " else '' end +
"AND `active` = '1' " +
"ORDER BY `#{metric}` #{order} " +
"LIMIT 0, 100"
result = $mysql.query(query)
i = 1
column = metric.capitalize if column == nil
$rankings = "<tr>" +
"<td><b>Rank</b></td>" +
"<td><b>Name</b></td>" +
"<td><b>#{column}</b></td></tr>\n"
result.each_hash do
|row|
user = User.new(row['id'])
disp =
if display != nil then display.call(row[metric])
else row[metric] end
$rankings += "<tr>\n" +
"<td>#{i}</td>" +
"<td>#{user.link}</td>" +
"<td>#{disp}</td>\n" +
"<tr>\n"
i += 1
end
end
if type == "settlements"
result = mysql_select_all('settlements')
settlements = []
result.each_hash {|row| settlements << Settlement.new(row['id'])}
settlements.sort! {|x, y| y.send(metric) <=> x.send(metric)}
settlements.reverse! if order == "ASC"
column = metric.capitalize if column == nil
$rankings = "<tr>" +
"<td><b>Rank</b></td>" +
"<td><b>Name</b></td>" +
"<td><b>Region</b></td>" +
"<td><b>#{column}</b></td></tr>\n"
settlements.each_with_index do
|settlement, i|
disp =
if display != nil then display.call(settlement.send(metric))
else settlement.send(metric) end
$rankings += "<tr>\n" +
"<td>#{i + 1}</td>" +
"<td>#{settlement.link}</td>" +
"<td><i>#{settlement.region_name}</i></td>" +
"<td>#{disp}</td>\n" +
"<tr>\n"
end
end
puts <<ENDTEXT
<html>
<head>
<link rel='stylesheet' type='text/css' href='shintolin.css' />
<title>Shintolin - Rankings</title>
</head>
<body>
<hr>
<a class='buttonlink' href='game.cgi'>Return</a>
<hr>
<form method='get' action='rankings.cgi'>
<b>Rank by:</b>
<select width='300px' name='metric'>
<option value='frags'>Frags</option>
<option value='deaths'>Deaths</option>
<option value='kills'>Kills</option>
<option value='revives'>Players Revived</option>
<option value='younguns'>Newest players</option>
<option value='oldies'>Oldest players</option>
<option value='survival'>Longest surviving players</option>
<option value=''>-----</option>
<option value='oldtowns'>Oldest Settlements</option>
<option value='newtowns'>Newest Settlements</option>
<option value='bigtowns'>Most Populous Settlements</option>
</select>
<input type='submit' value='View' />
</form>
<i>#{blurb}</i>
<hr>
<table>
#{$rankings}
</table>
</body>
</html>
ENDTEXT