~aleteoryx/bundlemania

bundlemania/s/stats -rwxr-xr-x 1.2 KiB
2a09b9a3Aleteoryx comment in s/pick 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
46
47
#!/bin/env tclsh

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,comment FROM titles WHERE href = :url}] name played comment
    if {$played == ""} {
      puts "$name: unplayed"
    } else {
      puts "$name: played $played"
    }
    if {$comment != ""} {
      puts "  comment: $comment"
    }
  }
}