2022-03-27 22:35:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-03-28 15:20:00 +00:00
|
|
|
"database/sql"
|
2022-03-27 22:58:47 +00:00
|
|
|
"flag"
|
2022-03-27 23:09:22 +00:00
|
|
|
"fmt"
|
2022-03-28 16:16:29 +00:00
|
|
|
"math/rand"
|
2022-03-27 22:58:47 +00:00
|
|
|
"os"
|
2022-03-27 23:09:22 +00:00
|
|
|
"os/exec"
|
2022-03-29 12:46:37 +00:00
|
|
|
"time"
|
2022-03-28 15:20:00 +00:00
|
|
|
|
|
|
|
_ "modernc.org/sqlite"
|
2022-03-27 22:35:46 +00:00
|
|
|
)
|
|
|
|
|
2022-03-29 02:20:38 +00:00
|
|
|
var (
|
|
|
|
verbose = flag.Bool("v", false, "debug output")
|
|
|
|
file = flag.String("f", "cards", "cards file")
|
|
|
|
)
|
2022-03-27 22:58:47 +00:00
|
|
|
|
2022-03-27 22:35:46 +00:00
|
|
|
func main() {
|
2022-03-29 12:46:37 +00:00
|
|
|
if !*verbose {
|
|
|
|
// Seed the RNG
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
2022-03-29 12:49:20 +00:00
|
|
|
|
2022-03-27 22:58:47 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2022-03-28 15:20:00 +00:00
|
|
|
db, err := sql.Open("sqlite", *file)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get number of cards
|
|
|
|
var N int
|
|
|
|
_ = db.QueryRow("SELECT COUNT(*) FROM cards").Scan(&N)
|
2022-03-28 17:04:13 +00:00
|
|
|
s := segmentTree{N, make([]int, 4*N)}
|
2022-03-28 15:20:00 +00:00
|
|
|
|
|
|
|
// Build segment tree
|
2022-03-28 16:16:29 +00:00
|
|
|
rows, err := db.Query("SELECT weight FROM cards")
|
2022-03-28 15:20:00 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-03-29 12:51:11 +00:00
|
|
|
s.Build(rows, 0, N-1, 1)
|
2022-03-28 16:35:27 +00:00
|
|
|
rows.Close()
|
2022-03-28 15:20:00 +00:00
|
|
|
|
2022-03-28 16:51:19 +00:00
|
|
|
if *verbose {
|
2022-03-28 17:04:13 +00:00
|
|
|
fmt.Println(s)
|
2022-03-28 16:51:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 23:09:22 +00:00
|
|
|
// https://stackoverflow.com/questions/14094190/function-similar-to-getchar
|
|
|
|
// disable input buffering
|
|
|
|
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
|
|
|
|
// do not display entered characters on the screen
|
|
|
|
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
|
|
|
|
// restore the echoing state when exiting
|
|
|
|
defer exec.Command("stty", "-F", "/dev/tty", "echo").Run()
|
2022-03-27 23:02:49 +00:00
|
|
|
|
2022-03-27 23:09:22 +00:00
|
|
|
for {
|
2022-03-28 16:16:29 +00:00
|
|
|
// Choose a random card
|
2022-03-28 20:11:08 +00:00
|
|
|
x := rand.Intn(s.seg[1])
|
2022-03-29 12:51:11 +00:00
|
|
|
w, i := s.Query(x, 0, N-1, 1)
|
2022-03-28 17:08:49 +00:00
|
|
|
|
2022-03-28 16:51:19 +00:00
|
|
|
if *verbose {
|
2022-03-29 12:43:57 +00:00
|
|
|
fmt.Println(s.seg[1])
|
2022-03-28 16:51:19 +00:00
|
|
|
fmt.Println(x)
|
2022-03-28 17:08:49 +00:00
|
|
|
fmt.Println(w)
|
|
|
|
fmt.Println(i)
|
2022-03-28 16:51:19 +00:00
|
|
|
}
|
2022-03-28 16:39:43 +00:00
|
|
|
|
2022-03-28 16:35:27 +00:00
|
|
|
// Get card contents from database
|
2022-03-28 16:16:29 +00:00
|
|
|
var key, val string
|
2022-03-28 16:35:27 +00:00
|
|
|
db.QueryRow("SELECT key, val FROM cards WHERE idx=?", i).Scan(&key, &val)
|
2022-03-30 14:06:00 +00:00
|
|
|
fmt.Println("> " + key)
|
2022-03-28 16:44:03 +00:00
|
|
|
|
|
|
|
// Wait for confirmation
|
|
|
|
var b []byte = make([]byte, 1)
|
|
|
|
os.Stdin.Read(b)
|
2022-03-28 16:16:29 +00:00
|
|
|
fmt.Println(val)
|
2022-03-27 23:26:22 +00:00
|
|
|
|
2022-03-28 16:35:27 +00:00
|
|
|
// Read user input
|
2022-03-27 23:09:22 +00:00
|
|
|
os.Stdin.Read(b)
|
2022-03-28 15:20:00 +00:00
|
|
|
if b[0] == byte('y') {
|
2022-03-28 16:16:29 +00:00
|
|
|
w >>= 1
|
2022-03-28 15:20:00 +00:00
|
|
|
} else if b[0] == byte('n') {
|
2022-03-31 03:27:55 +00:00
|
|
|
w <<= 3
|
2022-03-27 23:09:22 +00:00
|
|
|
} else {
|
2022-03-29 12:49:20 +00:00
|
|
|
return
|
2022-03-27 23:09:22 +00:00
|
|
|
}
|
2022-03-28 16:35:27 +00:00
|
|
|
|
|
|
|
// Update segment tree and database
|
2022-03-29 12:51:11 +00:00
|
|
|
s.Update(i, w, 0, N-1, 1)
|
2022-03-28 16:35:27 +00:00
|
|
|
_, err = db.Exec("UPDATE cards SET weight=? WHERE idx=?", w, i)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-03-27 23:09:22 +00:00
|
|
|
}
|
2022-03-27 22:35:46 +00:00
|
|
|
}
|