~aleteoryx/bundlemania

ref: f15491d4b3f72d69bb27c53670f1ca851e43a639 bundlemania/stats.tcl -rwxr-xr-x 1.1 KiB
f15491d4Aleteoryx html table 9 months ago
                                                                                
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
#!/bin/env tclsh

package require json
package require sqlite3

proc fatal {reason {code -1}} {
  puts stderr "$argv0: fatal: $reason"
  exit $code
}

proc usage {} {
  global argv0
  puts stderr "usage: $argv0 DB-PATH ?GAME-URL ...?"
  exit -1
}
if {[llength $argv] < 1} usage
set gameurls [lassign $argv dbpath]

if {[catch {sqlite3 db $dbpath -create false}]} {
  fatal "can't open \"$dbpath\""
}

if {[llength $gameurls] == 0} {
  set total  [db eval {SELECT count(*) FROM titles WHERE ignore = 0}]
  set played [db eval {SELECT count(*) FROM titles WHERE played NOT NULL AND ignore = 0}]

  puts "database \"[file tail [file rootname $dbpath]]\":"
  puts "- $total games"
  puts "- $played played"
  puts [format "- %.2f%% complete" [expr {(1.0 * $played) / $total * 100}]]
} else {
  foreach url $gameurls {
    if {![db exists {SELECT * FROM titles WHERE href = :url}]} {
      puts stderr "game not in database: $url"
      continue
    }

    lassign [db eval {SELECT name,played FROM titles WHERE href = :url}] name played
    if {$played == ""} {
      puts "$name: unplayed"
    } else {
      puts "$name: played $played"
    }
  }
}