#! /bin/sh DATABASE=/home/rusty/wesnoth/wesnoth-uploads.db URL="http://ozlabs.org/~rusty/stats.wesnoth.org/query-wesnoth.cgi" print_header() { echo "

$1

" echo '
' echo '

From Wesnoth

' echo '
' } get_scenario_list() { # Before 1.1.2, the versions weren't generally numbered. Now they # should all be. case $1 in 1.1-svn|1.1+svn|1.1.1|1.1.1+svn) case $2 in CAMPAIGN_HEIR_TO_THE_THRONE) echo The_Elves_Besieged Blackwater_Port The_Isle_of_Anduin The_Bay_of_Pearls Muff_Malals_Peninsula Isle_of_the_Damned The_Siege_of_Elensefar Crossroads The_Princess_of_Wesnoth The_Valley_of_Death-The_Princesss_Revenge Gryphon_Mountain The_Ford_of_Abez Northern_Winter Mountain_Pass The_Dwarven_Doors Plunging_into_the_Darkness The_Lost_General Hasty_Alliance Scepter A_Choice_Must_Be_Made Snow_Plains Swamp_Of_Dread North_Elves Elven_Council valley_statue return_to_wesnoth trial_clans battle_for_wesnoth ;; CAMPAIGN_TWO_BROTHERS) # These are numbered! /usr/bin/sqlite3 $DATABASE "select DISTINCT scenario FROM campaign_view WHERE campaign = '$2' AND version = '$1';" | sort -n ;; *) /usr/bin/sqlite3 $DATABASE "select DISTINCT scenario FROM campaign_view WHERE campaign = '$2' AND version = '$1';" ;; esac ;; *) /usr/bin/sqlite3 $DATABASE "select DISTINCT scenario FROM campaign_view WHERE campaign = '$2' AND version = '$1';" | sort -n ;; esac } # Returns sets of up to 10 comma-separated scenarios, given a scenario list. chop_scenarios() { if [ $# = 1 ]; then echo $1 return fi while [ $# -gt 8 ]; do echo $1,$2,$3,$4,$5,$6,$7,$8 # Note that this deliberately repeats one. shift 7 done if [ $# -gt 1 ]; then echo $@ | tr ' ' ',' fi } colorize() { sed -e 's,>aborted<,>aborted<,g' -e 's,>victory<,>victory<,g' -e 's,>defeat<,>defeat<,g' } # add_href field extra add_href() { sed "s,^\([^<]*\),\1," } is_official() { case $1 in 1.1-svn) case $2 in CAMPAIGN_EASTERN_INVASION|CAMPAIGN_HEIR_TO_THE_THRONE|CAMPAIGN_SON_OF_THE_BLACK_EYE|CAMPAIGN_THE_RISE_OF_WESNOTH|CAMPAIGN_THE_DARK_HORDES|TUTORIAL) return 0 ;; esac return 1 ;; # Two brothers added. 1.1+svn|1.1.1) case $2 in CAMPAIGN_EASTERN_INVASION|CAMPAIGN_HEIR_TO_THE_THRONE|CAMPAIGN_SON_OF_THE_BLACK_EYE|CAMPAIGN_THE_RISE_OF_WESNOTH|CAMPAIGN_THE_DARK_HORDES|CAMPAIGN_TWO_BROTHERS|TUTORIAL) return 0 ;; esac return 1 ;; # 1.1.1+svn: Under the Burning Suns and South Guard added # Son of Black Eye and Dark Hordes removed *) case $2 in CAMPAIGN_DESERT|CAMPAIGN_THE_SOUTH_GUARD|CAMPAIGN_EASTERN_INVASION|CAMPAIGN_HEIR_TO_THE_THRONE|CAMPAIGN_THE_RISE_OF_WESNOTH|CAMPAIGN_TWO_BROTHERS|TUTORIAL) return 0 ;; esac return 1 ;; esac } print_main() { VERSIONS=`/usr/bin/sqlite3 $DATABASE "SELECT name FROM version_names;"` # By default, use latest non-svn version. if [ -z "$W_VERSION" ]; then W_VERSION=`echo "$VERSIONS" | grep -v svn | tail -1` fi print_header "Wesnoth Statistics for $W_VERSION" sep="Other versions available: " for ver in $VERSIONS; do if [ "$ver" != "$W_VERSION" ]; then echo "$sep $ver " sep="|" fi done echo '

Wesnoth Official Campaigns

' echo '' echo '' echo '' echo '' echo '' /usr/bin/sqlite3 $DATABASE "SELECT campaign,difficulty,count(*) FROM campaign_view WHERE version='$W_VERSION' GROUP BY campaign,difficulty ORDER BY campaign,count(*) DESC;" | while IFS="|" read campaign diff count; do if is_official $W_VERSION "$campaign"; then echo "" fi done echo '
Campaign NameDifficultyGames Uploaded
$campaign$diff$count
' echo '

Wesnoth Unofficial Campaigns

' echo '' echo '' echo '' echo '' echo '' /usr/bin/sqlite3 $DATABASE "SELECT campaign,difficulty,count(*) FROM campaign_view WHERE version='$W_VERSION' GROUP BY campaign,difficulty ORDER BY campaign,count(*) DESC;" | while IFS="|" read campaign diff count; do if ! is_official $W_VERSION "$campaign"; then echo "" fi done echo '
Campaign NameDifficultyGames Uploaded
$campaign$diff$count
' echo "

List of Wesnoth Players

" } print_players() { print_header "Wesnoth Players for $W_VERSION" echo '

Wesnoth Players

' echo '' echo '' echo '' echo '' echo '' /usr/bin/sqlite3 -html $DATABASE "SELECT DISTINCT campaign_view.player,game_count.games_received-1,game_count.last_upload FROM campaign_view,players,game_count WHERE campaign_view.version = '$W_VERSION' AND game_count.player_ref = players.rowid AND players.id = campaign_view.player;" | sed "s,^
Player IDGames uploadedLast upload
\([^<]*\),
\1," echo '
' } add_graph() { echo "" } # Graph out all campaigns a player played (unless specific requested, from graph) do_graph_player() { echo '(Green means victory, red means defeat, yellow means quit. Bars indicate start/finish turn).
' if [ -n "$W_CAMPAIGN" ]; then EXTRA="AND campaign='$W_CAMPAIGN'" fi for campaign_diff in `/usr/bin/sqlite3 $DATABASE "SELECT DISTINCT campaign,difficulty FROM campaign_view WHERE player='$W_PLAYER' AND version='$W_VERSION' $EXTRA;"`; do campaign=`echo "$campaign_diff" | cut -d\| -f1` diff=`echo "$campaign_diff" | cut -d\| -f2` SCENARIOS=$(chop_scenarios $(/usr/bin/sqlite3 $DATABASE "SELECT DISTINCT scenario FROM campaign_view WHERE player='$W_PLAYER' AND version='$W_VERSION' AND difficulty='$diff' AND campaign='$campaign';") ) echo "Campaign $campaign ($diff):" for scen in $SCENARIOS; do add_graph "W_SCENARIOS=$scen&W_VERSION=$W_VERSION&W_CAMPAIGN=$campaign&W_DIFF=$diff&W_PLAYER=$W_PLAYER&W_TYPE=player" "Player progress" done done echo "

Statistics in detail

" } graph_player() { print_header "Wesnoth Player $W_PLAYER" do_graph_player } # Print out all scenarios a player played. print_player() { print_header "Wesnoth Player $W_PLAYER" echo '' echo '' echo '' echo '' echo '' echo '' i=1 /usr/bin/sqlite3 $DATABASE "SELECT DISTINCT campaign,scenario,difficulty FROM campaign_view WHERE player='$W_PLAYERP' AND version='$W_VERSION';" | while IFS='|' read campaign scenario diff; do echo "" i=$(($i + 1)) done echo '
IDCampaign NameScenarioDifficulty
$i$campaign$scenario$diff
' } # "Type" "Name" scenarios... add_scenario_graphs() { ASG_TYPE=$1 ASG_NAME=$2 shift 2 for scen; do add_graph "W_SCENARIOS=$scen&W_VERSION=$W_VERSION&W_DIFF=$W_DIFF&W_CAMPAIGN=$W_CAMPAIGN&W_TYPE=$ASG_TYPE" "$ASG_NAME" done } # Graph the campaign on a given campaign_names.rowid graph_campaign() { print_header "Wesnoth $W_CAMPAIGN ($W_DIFF)" SCENARIOS=$(chop_scenarios $(get_scenario_list $W_VERSION $W_CAMPAIGN) ) echo "

Gold at start of game

" add_scenario_graphs "gold" "Gold" $SCENARIOS echo "

Losses/quits

" add_scenario_graphs "loss_abort" "Losses and quits" $SCENARIOS echo "

Wins

" add_scenario_graphs "wins" "Victories" $SCENARIOS echo "

Percent turns used on victory

" add_scenario_graphs "win_turn" "Percent turns used" $SCENARIOS echo "

Total minutes per player

" add_scenario_graphs "time" "Minutes spent" $SCENARIOS for level in 1 2 3; do echo "

Count of Level $level units at start of game

" add_scenario_graphs "level&W_LEVEL=$level" "Level $level units" $SCENARIOS done echo "

Statistics in detail

" } # Print out details on a given campaign print_campaign() { print_header "Wesnoth $W_CAMPAIGN ($W_DIFF)" echo '' echo '' echo '' /usr/bin/sqlite3 -html $DATABASE "SELECT DISTINCT scenario FROM campaign_view WHERE campaign = '$W_CAMPAIGNP' AND difficulty = '$W_DIFF' AND version = '$W_VERSION';"| sed "s,^
Scenario
\([^<]*\),
\1," echo '
' } # Print out details on a given scenario print_scenario() { print_header "Wesnoth Scenario $W_SCENARIO ($W_CAMPAIGN $W_DIFF)" echo '' echo '' echo '' echo '' echo '' echo '' echo '' echo '' echo '' echo '' /usr/bin/sqlite3 -html $DATABASE "SELECT game,player,start_turn,gold,time,CASE result WHEN 0 THEN 'aborted' WHEN 1 THEN 'victory' ELSE 'defeat' END, end_turn, num_turns FROM campaign_view WHERE scenario = '$W_SCENARIO' AND campaign = '$W_CAMPAIGN' AND difficulty = '$W_DIFF';" | sed "s,^
Game IDPlayerStart turnStart goldTime taken (sec)ResultEnd turnNum turns
\([^<]*\),
\1," | colorize echo '
' } # Print out details on a given scenario. print_player_scenario() { print_header "Wesnoth Player $W_PLAYER playing $W_SCENARIO ($W_CAMPAIGN $W_DIFF)" echo "Number of turns in scenario: " # Ideally, only returns one number... /usr/bin/sqlite3 $DATABASE "SELECT DISTINCT num_turns from campaign_view WHERE scenario='$W_SCENARIO' AND campaign='$W_CAMPAIGN' AND player='$W_PLAYER' AND difficulty='$W_DIFF' AND version='$W_VERSION';" echo '' echo '' echo '' echo '' echo '' echo '' echo '' echo '' /usr/bin/sqlite3 -html $DATABASE "SELECT game,start_turn,gold,time,end_turn,CASE result WHEN 0 THEN 'aborted' WHEN 1 THEN 'victory' ELSE 'defeat' END FROM campaign_view WHERE version='$W_VERSION' AND player='$W_PLAYER' AND scenario='$W_SCENARIO' AND difficulty='$W_DIFF' AND campaign='$W_CAMPAIGN' ORDER BY game;" | sed "s,^
IDStart turnStart goldTime taken (sec)End turnResult
\([^<]*\),
\1," | colorize echo '
' } # Print out details and units for a given game. print_game() { print_header "Details of game $W_GAME" echo '' /usr/bin/sqlite3 $DATABASE "SELECT campaign,difficulty,scenario,player,version,gold,start_turn,end_turn,num_turns,time,result FROM campaign_view WHERE game='$W_GAME';" | (IFS="|" read cam diff scen player ver gold st et nt time result echo "Campaign: $cam
" echo "Difficulty: $diff
" echo "Scenario: $scen
" echo "Player: $player
" echo "Version: $ver
" echo "Start gold: $gold
" echo "Start turn: $st
" echo "End turn: $et
" echo "Num turns: $nt
" echo "Time taken (sec): $time
" echo "Result:" case $result in 0) echo "aborted";; 1) echo "victory";; 2) echo "defeat";; esac echo "
") echo "

Important Unit Stats (at Start of Game)

" echo '
' echo '' echo '' echo '' echo '' /usr/bin/sqlite3 -html $DATABASE "SELECT unit_names.name,special_units.level,special_units.experience FROM special_units,unit_names WHERE special_units.game_ref=$W_GAME AND unit_names.rowid=special_units.unit_name_ref;" echo '
NameLevelExperience
' echo "

Wesnoth Unit Summary (at Start of Game)

" echo '' echo '' echo '' echo '' echo '' /usr/bin/sqlite3 -html $DATABASE "SELECT unit_types.name,unit_types.level,unit_tallies.count FROM unit_types,unit_tallies WHERE unit_tallies.game_ref=$W_GAME AND unit_types.rowid=unit_tallies.unit_type_ref ORDER BY unit_types.level DESC,unit_types.name;" echo '
TypeLevelNumber
' } # Simple access page which is given in the Help Wesnoth dialog my_page() { VERSIONS=`/usr/bin/sqlite3 $DATABASE "SELECT DISTINCT version FROM campaign_view WHERE player=$W_P;"` # By default, use latest version for this player if [ -z "$W_VERSION" ]; then W_VERSION=`echo "$VERSIONS" | tail -1` fi print_header "Wesnoth Page for Player $W_PLAYER" echo "Versions played: " sep="" for ver in $VERSIONS; do if [ "$ver" = "$W_VERSION" ]; then echo "$sep $W_VERSION" else echo "$sep $ver " fi sep="|" done W_PLAYER=$W_P do_graph_player } echo "Content-type: text/html" echo cat /home/rusty/public_html/stats.wesnoth.org/header.html # We accept a simple numeric arg for player id. if echo "$QUERY_STRING" | grep -q '^[0-9][0-9]*$'; then QUERY_STRING="W_P=$QUERY_STRING" fi . check_args.sh case "$QUERY_STRING" in W_PLAYER=*) graph_player ;; W_PLAYERP=*) print_player ;; W_CAMPAIGN=*) graph_campaign ;; W_CAMPAIGNP=*) print_campaign ;; W_SCENARIO=*) print_scenario ;; W_PLAYERSCENARIO=*) print_player_scenario ;; W_GAME=*) print_game ;; W_P=*) my_page ;; W_PLAYERS=*) print_players ;; *) print_main ;; esac echo '
This page based generated from a database, which was last modified '`date -u -r $DATABASE`'.
' cat /home/rusty/public_html/stats.wesnoth.org/footer.html