TriviaEngine-sqlite.tcl 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. # The trivia engine YAY
  2. # vim: foldmethod=marker:foldcolumn=2:foldmarker=<<<,>>>:sw=2:ts=2
  3. ### INIT <<<
  4. package require sqlite
  5. # the channel to play in
  6. set trivia_channel "#triviacow"
  7. # the time between hints (sec)
  8. set trivia_speed 20
  9. # the time between rounds (sec)
  10. set trivia_delay 30
  11. # hold the current question info <<<
  12. set trivia_q_id ""
  13. set trivia_q_cat ""
  14. set trivia_q_question ""
  15. set trivia_q_answer ""
  16. set trivia_q_hint ""
  17. set trivia_q_attempts 0
  18. set trivia_unanswered 0
  19. set trivia_run_last 0
  20. set trivia_run_qty 0
  21. set trivia_run_nick ""
  22. set trivia_guesser ""
  23. set trivia_guesser_count 0
  24. set trivia_last_qid 0
  25. if [info exists trivia_must_rehash] {
  26. if {$trivia_must_rehash == 1} {
  27. set trivia_must_rehash 2
  28. } else {
  29. set trivia_must_rehash 0
  30. }
  31. } else {
  32. set trivia_must_rehash 0
  33. }
  34. #>>>
  35. # 0 = off
  36. # 1 = on
  37. # -1 = disabled
  38. set trivia_status 0
  39. set trivia_timer ""
  40. set trivia_watchdog_timer ""
  41. set trivia_last_ts 0
  42. #colours <<<
  43. set trivia_c(off) "\003"
  44. set trivia_c(red) "\0034"
  45. set trivia_c(blue) "\0033"
  46. set trivia_c(purple) "\0036"
  47. set trivia_c(bold) "\002"
  48. #>>>
  49. bind pubm - * trivia_input
  50. bind msg - trivia trivia_msg
  51. # connect database <<<
  52. proc trivia_connect { } {
  53. global trivia_db_handle
  54. sqlite3 trivia_db_handle trivia.db
  55. }
  56. #>>>
  57. #>>>
  58. ### SETTINGS <<<
  59. set trivia_flag T
  60. set trivia_admin S
  61. # >>>
  62. # Handle a /msg
  63. proc trivia_msg { nick host handle cmd } {
  64. #<<<
  65. global trivia_db_handle trivia_last_qid
  66. global trivia_q_cat trivia_c
  67. regsub "(trivia )" $cmd "" cmd
  68. putlog "trivia: msg command $cmd from $nick"
  69. if {($nick == "Greeneyez") || ($nick == "JamesOff")} {
  70. #<<< set a question's category
  71. if [regexp -nocase "^setcat (.+)" $cmd matches category] {
  72. if {$trivia_last_qid == 0} {
  73. puthelp "PRIVMSG $nick :Can't fathom last question, unable to update category"
  74. return
  75. }
  76. set orig_cat $category
  77. set category [trivia_sqlite_escape $category]
  78. set sql "SELECT cat_id FROM categories WHERE cat_name='$category'"
  79. set cat_id 0
  80. set cat_id [trivia_db_handle eval $sql]
  81. if {$cat_id != 0} {
  82. puthelp "PRIVMSG $nick :Moving question $trivia_last_qid to category$trivia_c(purple) $category$trivia_c(off) ($cat_id)"
  83. set sql "UPDATE questions SET cat_id='$cat_id' WHERE question_id='$trivia_last_qid'"
  84. trivia_db_handle eval $sql
  85. set trivia_q_cat $orig_cat
  86. return
  87. } else {
  88. puthelp "PRIVMSG $nick :Creating new category$trivia_c(purple) $category"
  89. set sql "INSERT INTO categories VALUES (null, '$category', 1)"
  90. trivia_db_handle eval $sql
  91. set cat_id [trivia_db_handle last_insert_rowid]
  92. puthelp "PRIVMSG $nick :Moving question $trivia_last_qid to category$trivia_c(purple) $category$trivia_c(off) ($cat_id)"
  93. set sql "UPDATE questions SET cat_id='$cat_id' WHERE question_id='$trivia_last_qid'"
  94. trivia_db_handle eval $sql
  95. set trivia_q_cat $orig_cat
  96. return
  97. }
  98. }
  99. #>>>
  100. #<<< handle reports
  101. if [regexp -nocase {^report (help|list|fix|view|done)?( .+)?} $cmd matches func arg] {
  102. if {($func == "") || ($func == "help")} {
  103. puthelp "PRIVMSG $nick :Use: report (list|view|fix|done)"
  104. puthelp "PRIVMSG $nick :report list: see 10 reports"
  105. puthelp "PRIVMSG $nick :report view <id>: see the question and answer associated with a report"
  106. puthelp "PRIVMSG $nick :report fix <id> (question|answer) <new text>: update a question or answer"
  107. puthelp "PRIVMSG $nick :report done <id>: mark a report as done"
  108. return 0
  109. }
  110. if {$func == "list"} {
  111. putserv "PRIVMSG $nick :Gathering 10 trivia reports..."
  112. set sql "SELECT * FROM reports WHERE resolved = 'N' LIMIT 10"
  113. trivia_db_handle eval $sql result {
  114. putserv "PRIVMSG $nick :$result(report_id): [format %10s $result(who)] $result(message)"
  115. }
  116. return 0
  117. }
  118. if {$func == "view"} {
  119. if {$arg == ""} {
  120. puthelp "PRIVMSG $nick :Use: report view <id>"
  121. return 0
  122. }
  123. set arg [string trim $arg]
  124. set arg [trivia_sqlite_escape $arg]
  125. set sql "SELECT * FROM reports, questions WHERE report_id = '$arg' AND reports.question_id = questions.question_id"
  126. trivia_db_handle eval $sql result {
  127. putserv "PRIVMSG $nick :Report$trivia_c(purple) $result(report_id) $trivia_c(off)by$trivia_c(purple) $result(who) $trivia_c(off)on$trivia_c(blue) [clock format $result(when) -gmt 1]"
  128. putserv "PRIVMSG $nick :Question: $result(question)"
  129. putserv "PRIVMSG $nick : Answer: $result(answer)"
  130. putserv "PRIVMSG $nick : Report: $result(message)"
  131. }
  132. return 0
  133. }
  134. if {$func == "fix"} {
  135. set arg [string trim $arg]
  136. if [regexp -nocase {([0-9]+) (question|answer) (.+)} $arg matches report_id thing fix] {
  137. putserv "PRIVMSG $nick :Attempting to fix $trivia_c(purple)$thing$trivia_c(off) from report$trivia_c(purple) $report_id"
  138. #get question ID for this report
  139. set sql "SELECT question_id FROM reports WHERE report_id = '$report_id'"
  140. putloglev d * $sql
  141. set question_id 0
  142. trivia_db_handle eval $sql result {
  143. set question_id $result(question_id)
  144. }
  145. if {$question_id == 0} {
  146. putserv "PRIVMSG $nick :I can't seem to find that report, sorry :("
  147. return 0
  148. }
  149. set fix [trivia_sqlite_escape $fix]
  150. set sql "UPDATE questions SET $thing = '$fix' WHERE question_id = '$question_id'"
  151. putloglev d * $sql
  152. trivia_db_handle eval $sql
  153. putserv "PRIVMSG $nick :Updated!"
  154. return 0
  155. } else {
  156. puthelp "PRIVMSG $nick :Use: report fix <id> (question|answer) <new text>"
  157. return 0
  158. }
  159. }
  160. if {$func == "done"} {
  161. set arg [string trim $arg]
  162. set arg [trivia_sqlite_escape $arg]
  163. putserv "PRIVMSG $nick :Marking report$trivia_c(purple) $arg $trivia_c(off)as done."
  164. set sql "UPDATE reports SET resolved = 'Y' WHERE report_id = '$arg'"
  165. putloglev d * $sql
  166. trivia_db_handle eval $sql
  167. return 0
  168. }
  169. }
  170. #>>>
  171. }
  172. }
  173. #>>>
  174. # Handle input coming from a channel
  175. proc trivia_input { nick host handle channel arg } {
  176. #<<<
  177. global trivia_channel trivia_status trivia_q_answer
  178. global trivia_guesser_count trivia_guesser trivia_c
  179. if {[string tolower $trivia_channel] != [string tolower $channel]} {
  180. return 0
  181. }
  182. if [regexp -nocase "^!t(rivia)? (.+)" $arg matches cmd param] {
  183. trivia_command $nick $host $handle $channel $param
  184. return 0
  185. }
  186. if [regexp -nocase "^!t(rivia)?$" $arg] {
  187. if {$trivia_status == 1} {
  188. puthelp "PRIVMSG $trivia_channel :!trivia ... ?"
  189. return 0
  190. } else {
  191. puthelp "PRIGMSG $trivia_channel :Perhaps you want $trivia_c(purple)!trivia start"
  192. return 0
  193. }
  194. }
  195. # need to be running below here
  196. if {$trivia_status == -1} {
  197. return 0
  198. }
  199. if {$trivia_q_answer == ""} {
  200. return 0
  201. }
  202. #see if someone else is playing
  203. if {($nick == $trivia_guesser) && ($nick != "NoTopic")} {
  204. incr trivia_guesser_count
  205. putloglev d * "$nick has talked $trivia_guesser_count times in a row..."
  206. } else {
  207. set trivia_guesser_count 0
  208. set trivia_guesser $nick
  209. }
  210. #tidy the string up
  211. set arg [string tolower $arg]
  212. set arg [string trim $arg]
  213. if {$arg == [string tolower $trivia_q_answer]} {
  214. # try to stop any other output
  215. clearqueue server
  216. trivia_correct $nick
  217. return 0
  218. }
  219. #if they didn't get it right, and it's been more than 20 lines, taunt!
  220. if {($trivia_guesser_count > 0) && (($trivia_guesser_count % 20)) == 0} {
  221. putserv "PRIVMSG $trivia_channel :Playing with yourself, $nick? ;)"
  222. putlog "Is $nick playing with themselves?"
  223. }
  224. }
  225. #>>>
  226. # Someone got it right!
  227. proc trivia_correct { nick } {
  228. #<<<
  229. global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status
  230. global trivia_timer trivia_delay trivia_db_handle trivia_unanswered trivia_run_qty trivia_run_last trivia_run_nick trivia_c
  231. if {$trivia_status != 1} {
  232. #something strange going on
  233. return 0
  234. }
  235. trivia_killtimer
  236. set answer $trivia_q_answer
  237. set trivia_q_answer ""
  238. set newuser 0
  239. set uid [trivia_get_uid $nick]
  240. if {$uid == 0} {
  241. putlog "$nick does not have entry in database... creating"
  242. set uid [trivia_create_user $nick]
  243. set newuser 1
  244. }
  245. putlog "$nick has uid $uid"
  246. trivia_incr_score $uid
  247. putquick "PRIVMSG $trivia_channel :Congratulations $trivia_c(purple)$nick$trivia_c(off)! The answer was$trivia_c(purple) $answer$trivia_c(off)."
  248. if {$newuser == 1} {
  249. putquick "PRIVMSG $trivia_channel :Welcome to our newest player, $trivia_c(purple)$nick$trivia_c(off) :)"
  250. }
  251. putquick "PRIVMSG $trivia_channel :rank0rs: [trivia_near_five2 $uid]"
  252. #set score [trivia_get_score $uid]
  253. #putserv "PRIVMSG $trivia_channel :$nick now has $score points."
  254. set trivia_timer [utimer $trivia_delay trivia_start_round]
  255. set trivia_unanswered 0
  256. if {$trivia_run_last == $uid} {
  257. incr trivia_run_qty
  258. if {$trivia_run_qty > 2} {
  259. putquick "PRIVMSG $trivia_channel :$nick [trivia_get_run $trivia_run_qty] $trivia_run_qty in a row!"
  260. }
  261. } else {
  262. #end of streak
  263. if {$trivia_run_qty > 2} {
  264. putquick "PRIVMSG $trivia_channel :$nick ended $trivia_run_nick's winning spree."
  265. }
  266. set trivia_run_qty 1
  267. set trivia_run_last $uid
  268. set trivia_run_nick $nick
  269. }
  270. if {[rand 100] > 95} {
  271. putserv "PRIVMSG $trivia_channel :Remember, to report a problem with a question or answer, type $trivia_c(purple)!trivia report <description of problem>"
  272. }
  273. trivia_check_rehash
  274. }
  275. #>>>
  276. #See if we need to rehash
  277. proc trivia_check_rehash { } {
  278. #<<<
  279. global trivia_must_rehash trivia_channel trivia_c
  280. if {$trivia_must_rehash == 1} {
  281. putquick "PRIVMSG $trivia_channel :$trivia_c(red)### Please wait, reloading trivia script ###"
  282. trivia_killtimer
  283. rehash
  284. }
  285. }
  286. #>>>
  287. # Turn a winning spree into text
  288. proc trivia_get_run { row } {
  289. #<<<
  290. switch $row {
  291. 3 {
  292. return "is on a winning spree!"
  293. }
  294. 5 {
  295. return "is on a rampage!"
  296. }
  297. 6 {
  298. return "is unstoppable!"
  299. }
  300. 8 {
  301. return "is on a Google spree!"
  302. }
  303. 10 {
  304. return "is GODLIKE!"
  305. }
  306. 12 {
  307. return "needs to get out more."
  308. }
  309. }
  310. return "is on a roll ..."
  311. }
  312. #>>>
  313. # Escape stuff for SQL
  314. proc trivia_sqlite_escape { text } {
  315. return [string map {' ''} $text]
  316. }
  317. # Get someone's user ID from their nick
  318. proc trivia_get_uid { nick } {
  319. #<<<
  320. global trivia_db_handle
  321. putloglev 4 * "trivia_get_uid ($nick)"
  322. set nick [trivia_sqlite_escape $nick]
  323. set sql "SELECT user_id FROM users WHERE user_name = '$nick'"
  324. trivia_db_handle eval $sql {
  325. return $user_id
  326. }
  327. return 0
  328. }
  329. #>>>
  330. # Get a period
  331. proc trivia_get_period { } {
  332. #<<<
  333. return [expr [clock scan "last friday 23:59" -gmt true] + 60]
  334. }
  335. #>>>
  336. # Get a score from a UID
  337. proc trivia_get_score { user_id } {
  338. #<<<
  339. global trivia_db_handle
  340. putloglev 4 * "trivia_get_score ($user_id)"
  341. set dt [trivia_get_period]
  342. set sql "SELECT COUNT(*) AS yarr FROM scores WHERE dt > '$dt' AND user_id='$user_id'"
  343. trivia_db_handle eval $sql {
  344. return $yarr
  345. }
  346. return 0
  347. }
  348. #>>>
  349. # Turn a timestamp into a date
  350. proc trivia_ts_to_date { ts } {
  351. #<<<
  352. return [clock format $ts -format "%Y/%m/%d %H:%M"]
  353. }
  354. #>>>
  355. # Get someone's userinfo from their UID
  356. proc trivia_get_userinfo { user_id } {
  357. #<<<
  358. global trivia_db_handle
  359. putloglev 4 * "trivia_get_userinfo ($user_id)"
  360. set sql "SELECT user_score, user_last, user_reg FROM users WHERE user_id = '$user_id'"
  361. trivia_db_handle eval $sql {
  362. if {$user_last == ""} {
  363. set last "Unknown"
  364. } else {
  365. set last [trivia_ts_to_date $user_last]
  366. }
  367. if {$user_reg == ""} {
  368. set reg "Unkown"
  369. } else {
  370. set reg [trivia_ts_to_date $user_reg]
  371. }
  372. return "$user_score|$last|$reg"
  373. }
  374. return "No stats"
  375. }
  376. #>>>
  377. # Get someone's stats from their username
  378. proc trivia_user_stats { user_name } {
  379. #<<<
  380. global trivia_channel
  381. set uid [trivia_get_uid $user_name]
  382. if {$uid == 0} {
  383. putserv "PRIVMSG $trivia_channel :Unknown user '$user_name'"
  384. return
  385. }
  386. set stats [trivia_get_userinfo $uid]
  387. if {$stats == "No stats"} {
  388. putserv "PRIVMSG $trivia_channel :No stats available."
  389. return
  390. }
  391. set stats2 [split $stats "|"]
  392. putserv "PRIVMSG $trivia_channel :Trivia stats for $user_name:"
  393. putserv "PRIVMSG $trivia_channel : Current score: [lindex $stats2 0]"
  394. putserv "PRIVMSG $trivia_channel : Ranking: [trivia_get_rank $uid]"
  395. putserv "PRIVMSG $trivia_channel : First seen: [lindex $stats2 2]"
  396. putserv "PRIVMSG $trivia_channel : Last scored: [lindex $stats2 1]"
  397. }
  398. #>>>
  399. # Increase a UID's score
  400. proc trivia_incr_score { id { howmuch 1 } } {
  401. #<<<
  402. global trivia_db_handle
  403. putloglev 4 * "trivia_incr_score ($id, $howmuch)"
  404. set sql "UPDATE users SET user_last = [clock seconds] WHERE user_id = '$id'"
  405. trivia_db_handle eval $sql
  406. set sql "INSERT INTO scores VALUES ('$id', '[clock seconds]')"
  407. trivia_db_handle eval $sql
  408. }
  409. #>>>
  410. # Create a new user
  411. proc trivia_create_user { nick } {
  412. #<<<
  413. global trivia_db_handle
  414. putloglev 4 * "trivia_create_user ($nick)"
  415. set nick [trivia_sqlite_escape $nick]
  416. set sql "INSERT INTO users VALUES (null, '$nick', '', 0, [clock seconds], [clock seconds])"
  417. trivia_db_handle eval $sql
  418. set uid [trivia_db_handle last_insert_rowid]
  419. return $uid
  420. }
  421. #>>>
  422. # Handle a !trivia command
  423. proc trivia_command { nick host handle channel param } {
  424. #<<<
  425. global trivia_channel trivia_status trivia_flag trivia_admin trivia_c
  426. set arg ""
  427. regexp {^([^ ]+)( .+)?$} $param matches cmd arg
  428. set param [string tolower $cmd]
  429. set arg [string trim $arg]
  430. putloglev d * "trivia command: $param from: $nick ($arg)"
  431. if {[matchattr $handle |$trivia_admin $trivia_channel] && ($param != "enable") && ($trivia_status == -1)} {
  432. return 0
  433. }
  434. if [regexp -nocase "^(score|place)( .+)?" $param] {
  435. putserv "PRIVMSG $trivia_channel :[trivia_score $arg $nick]"
  436. if [string match -nocase $nick $arg] {
  437. putserv "PRIVMSG $trivia_channel :(You know putting your own nick is optional, right? :)"
  438. }
  439. return 0
  440. }
  441. if {$param == "top10"} {
  442. putserv "PRIVMSG $trivia_channel :[trivia_get_top10]"
  443. return 0
  444. }
  445. if {$param == "info"} {
  446. trivia_user_stats $arg
  447. return 0
  448. }
  449. if {$param == "start"} {
  450. trivia_start
  451. return 0
  452. }
  453. if {$param == "report"} {
  454. trivia_report $nick $arg
  455. return 0
  456. }
  457. if {![matchattr $handle |$trivia_flag $channel]} {
  458. putserv "PRIVMSG $nick :use: !trivia \[score|top10|start\]"
  459. return 0
  460. }
  461. if {$param == "stop"} {
  462. trivia_stop
  463. return 0
  464. }
  465. if {$param == "skip"} {
  466. trivia_skip $nick
  467. return 0
  468. }
  469. if {$param == "stats"} {
  470. trivia_stats
  471. return 0
  472. }
  473. if {![matchattr $handle |$trivia_admin $channel]} {
  474. putserv "PRIVMSG $nick :use: !trivia \[score|top10|start|stop|skip|stats\]"
  475. return 0
  476. }
  477. if {$param == "disable"} {
  478. trivia_disable
  479. return 0
  480. }
  481. if {$param == "enable"} {
  482. trivia_enable
  483. return 0
  484. }
  485. if {$param == "merge"} {
  486. trivia_merge $nick $arg
  487. #puthelp "PRIGMSG $trivia_channel :Score merging is disabled"
  488. return 0
  489. }
  490. if {$param == "rehash"} {
  491. trivia_rehash
  492. return 0
  493. }
  494. putserv "PRIVMSG $nick :use: !trivia \[start|stop|score|top10|skip|stats|enable|disable|merge\]"
  495. return 0
  496. }
  497. #>>>
  498. # Rehash
  499. proc trivia_rehash { } {
  500. #<<<
  501. global trivia_status trivia_channel trivia_must_rehash
  502. if {$trivia_must_rehash == 1} {
  503. putserv "PRIVMSG $trivia_channel :! Reloading trivia now..."
  504. rehash
  505. return 0
  506. }
  507. if {$trivia_status != 1} {
  508. putserv "PRIVMSG $trivia_channel :! Reloading trivia now..."
  509. rehash
  510. return 0
  511. }
  512. set trivia_must_rehash 1
  513. putserv "PRIVMSG $trivia_channel :Trivia will reload after current round"
  514. }
  515. #>>>
  516. # Disable the script
  517. proc trivia_disable { } {
  518. #<<<
  519. global trivia_status trivia_channel
  520. if {$trivia_status == 1} {
  521. #stop the current game
  522. trivia_stop
  523. }
  524. set trivia_status -1
  525. putserv "PRIVMSG $trivia_channel :Trivia disabled."
  526. return 0
  527. }
  528. #>>>
  529. proc trivia_ping { } {
  530. return
  531. }
  532. # Enable the script
  533. proc trivia_enable {} {
  534. #<<<
  535. global trivia_status trivia_channel trivia_db_handle
  536. set trivia_status 0
  537. putserv "PRIVMSG $trivia_channel :Trivia enabled."
  538. trivia_ping
  539. return 0
  540. }
  541. #>>>
  542. #Make a hint
  543. proc trivia_make_hint { hint answer } {
  544. #<<<
  545. set hint [string toupper $hint]
  546. set answer [string toupper $answer]
  547. set answer_words [split $answer { }]
  548. set final_hint ""
  549. #are we making the very first hint?
  550. if {$hint == ""} {
  551. foreach word $answer_words {
  552. append final_hint [string repeat "_" [string length $word]]
  553. append final_hint " "
  554. }
  555. set final_hint [string trim $final_hint]
  556. #now put the punctuation in
  557. set letters [split $answer {}]
  558. set i 0
  559. foreach letter $letters {
  560. if {![regexp -nocase {[A-Z0-9 ]} $letter]} {
  561. set final_hint [string replace $final_hint $i $i $letter]
  562. }
  563. incr i
  564. }
  565. return [string trim $final_hint]
  566. }
  567. # explode the into words
  568. set hint_words [split $hint { }]
  569. set i 0
  570. foreach word $hint_words {
  571. set answer_word [lindex $answer_words $i]
  572. putloglev 1 * "considering $answer_word ($word)"
  573. #are we on the first iteration?
  574. if [regexp "^_+$" $word] {
  575. #use the first letter
  576. set letters [string length $word]
  577. #don't hint for single-letter words
  578. if {$letters > 1} {
  579. set word [string index $answer_word 0]
  580. append word [string repeat "_" [expr $letters - 1]]
  581. } else {
  582. set word "_"
  583. }
  584. append final_hint "$word "
  585. } else {
  586. #not the first iteration so figure out where the gaps are
  587. set gaps [list]
  588. set letters [split $word {}]
  589. set j 0
  590. foreach letter $letters {
  591. if {$letter == "_"} {
  592. lappend gaps $j
  593. }
  594. incr j
  595. }
  596. if {[llength $gaps] <= 1} {
  597. #eek no letters to replace, or only one gap
  598. putloglev 1 * " insufficient gaps, using $word"
  599. append final_hint "$word "
  600. } else {
  601. #now we pick a random letter position
  602. set pos [trivia_random_element $gaps]
  603. putloglev 1 * " filling in $pos"
  604. set word [string replace $word $pos $pos [string index $answer_word $pos]]
  605. putloglev 1 * " --> $word"
  606. append final_hint "$word "
  607. }
  608. }
  609. incr i
  610. }
  611. set final_hint [string trim $final_hint]
  612. return $final_hint
  613. }
  614. #>>>
  615. # Fetch a question
  616. proc trivia_get_question { } {
  617. #<<<
  618. global trivia_db_handle trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_channel
  619. set trivia_q_id ""
  620. set sql "SELECT q.question, q.question_id, q.answer, c.cat_name FROM questions q LEFT JOIN categories c USING (cat_id) WHERE c.cat_enabled=1 ORDER BY count ASC, random() LIMIT 1"
  621. trivia_db_handle eval $sql {
  622. set trivia_q_id $question_id
  623. set trivia_q_cat $cat_name
  624. set trivia_q_question $question
  625. set trivia_q_answer [string toupper $answer]
  626. set trivia_q_hint ""
  627. #tidy up question and answer
  628. regsub -all " +" $trivia_q_question " " trivia_q_question
  629. regsub -all " +" $trivia_q_answer " " trivia_q_answer
  630. set trivia_q_question [string trim $trivia_q_question]
  631. set trivia_q_answer [string trim $trivia_q_answer]
  632. }
  633. if {$trivia_q_id == ""} {
  634. return
  635. }
  636. #update the times used
  637. set sql "UPDATE questions SET count = count + 1 WHERE question_id = '$trivia_q_id'"
  638. trivia_db_handle eval $sql
  639. }
  640. #>>>
  641. # Start a round
  642. proc trivia_start_round { } {
  643. #<<<
  644. global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status trivia_last_qid
  645. if {$trivia_status != 1} {
  646. #we're switched off, abort
  647. return 0
  648. }
  649. #init variables
  650. set trivia_q_id ""
  651. set trivia_q_cat ""
  652. set trivia_q_question ""
  653. set trivia_q_answer ""
  654. set trivia_q_hint ""
  655. putlog "Fetching next question..."
  656. trivia_get_question
  657. if {$trivia_q_id == ""} {
  658. putlog "Couldn't init trivia round, aborting."
  659. return
  660. }
  661. putlog "Successfully fetched question $trivia_q_id from database"
  662. putloglev 4 * "question = $trivia_q_question"
  663. putloglev 4 * "answer = $trivia_q_answer"
  664. set trivia_q_attempts 1
  665. set trivia_last_qid $trivia_q_id
  666. trivia_round
  667. }
  668. #>>>
  669. # Run a round
  670. proc trivia_round { } {
  671. #<<<
  672. global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status
  673. global trivia_timer trivia_speed trivia_c trivia_last_ts
  674. if {$trivia_status != 1} {
  675. #we're switched off, abort
  676. return 0
  677. }
  678. if {$trivia_q_attempts == 5} {
  679. trivia_end_round
  680. return 0
  681. }
  682. if {$trivia_q_answer == ""} {
  683. return 0
  684. }
  685. #update the hint
  686. set trivia_q_hint [trivia_make_hint $trivia_q_hint $trivia_q_answer]
  687. #say our stuff
  688. if {$trivia_q_attempts > 1} {
  689. set hint " \[[expr $trivia_q_attempts - 1] of 3\]"
  690. } else {
  691. set hint ""
  692. }
  693. putserv "PRIVMSG $trivia_channel :$trivia_c(red)--== Trivia ==--$trivia_c(off) \[category: \002$trivia_q_cat\002\]"
  694. #\[question id: \002$trivia_q_id\002\]"
  695. set split_question [trivia_question_split $trivia_q_question]
  696. foreach q $split_question {
  697. if {$q != ""} {
  698. putserv "PRIVMSG $trivia_channel :$trivia_c(blue) [trivia_question_inject $q]"
  699. }
  700. }
  701. #set new_question [trivia_question_inject $trivia_q_question]
  702. #putserv "PRIVMSG $trivia_channel :$trivia_c(blue) $new_question"
  703. putserv "PRIVMSG $trivia_channel :Hint$hint: [trivia_explode $trivia_q_hint]"
  704. incr trivia_q_attempts
  705. set trivia_last_ts [clock seconds]
  706. global trivia_must_rehash
  707. if {$trivia_must_rehash != 2} {
  708. set trivia_timer [utimer $trivia_speed trivia_round]
  709. }
  710. }
  711. #>>>
  712. # Make it harder for things to break questions (inject bold codes)
  713. proc trivia_question_inject { question } {
  714. #<<<
  715. putlog "trivia_question_inject $question"
  716. #inject random bold/underline/colour codes into question
  717. global trivia_c
  718. set l [string length $question]
  719. putlog "length: $l"
  720. if {$l < 1} {
  721. return $question
  722. }
  723. set pos [rand $l]
  724. set first [string range $question 0 $pos]
  725. incr pos
  726. set second [string range $question $pos end]
  727. switch [rand 1] {
  728. 0 {
  729. putlog "question_inject: using bold at pos $pos"
  730. return $first$trivia_c(bold)$trivia_c(bold)$second
  731. }
  732. 1 {
  733. putlog "question_inject: using purple at pos $pos"
  734. return $first$trivia_c(purple)$trivia_c(off)$second
  735. }
  736. }
  737. return $question
  738. }
  739. #>>>
  740. # Make it harder for things to break questions (inject line breaks)
  741. proc trivia_question_split { question } {
  742. #<<<
  743. putlog "trivia_question_split $question"
  744. #explodes a question into two lines at word boundaries, if it's long enough
  745. #don't split unscrambles incorrectly
  746. if [regexp -nocase "(unscramble .+:) (\[A-Z \]+)" $question matches first second] {
  747. return [list $first $second]
  748. }
  749. set words [split $question " "]
  750. set wordcount [llength $words]
  751. if {$wordcount < 4} {
  752. putlog "aborting, too short"
  753. return [list $question]
  754. }
  755. #enough words to split
  756. #we want at least two on the first line
  757. putlog "wordcount: $wordcount"
  758. incr wordcount -2
  759. if {$wordcount < 0} {
  760. return [list $question]
  761. }
  762. set pos [rand $wordcount]
  763. incr pos 2
  764. putlog "picked pos $pos"
  765. set wordlist [list]
  766. set i 0
  767. set line ""
  768. foreach word $words {
  769. #putlog "word = $word, i = $i"
  770. append line "$word "
  771. if {$i == $pos} {
  772. #putlog "splitting here, line = $line"
  773. lappend wordlist [string trim $line]
  774. set line ""
  775. }
  776. incr i
  777. }
  778. #putlog "done, appending $line to list"
  779. if {$line != ""} {
  780. lappend wordlist [string trim $line]
  781. }
  782. #putlog "split question list is: $wordlist"
  783. return $wordlist
  784. }
  785. #>>>
  786. # Finish a round (without a winner)
  787. proc trivia_end_round { } {
  788. #<<<
  789. global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status
  790. global trivia_timer trivia_delay trivia_db_handle trivia_unanswered
  791. global trivia_run_last trivia_run_nick trivia_run_qty trivia_c
  792. if {$trivia_status != 1} {
  793. #we're switched off, abort
  794. return 0
  795. }
  796. set trivia_q_answer [string toupper $trivia_q_answer]
  797. putquick "PRIVMSG $trivia_channel :Time's up! Nobody got it right. The answer was$trivia_c(purple) $trivia_q_answer"
  798. set trivia_q_answer ""
  799. incr trivia_unanswered
  800. if {$trivia_run_qty > 2} {
  801. putserv "PRIVMSG $trivia_channel :So much for $trivia_run_nick's winning spree."
  802. }
  803. set trivia_run_last 0
  804. set trivia_run_qty 0
  805. set trivia_run_nick ""
  806. if {[rand 100] > 90} {
  807. putserv "PRIVMSG $trivia_channel :Remember, to report a problem with a question or answer, type $trivia_c(purple)!trivia report <description of problem>"
  808. }
  809. if {$trivia_unanswered > 3} {
  810. putserv "PRIVMSG $trivia_channel :Three unanswered in a row, stopping the game."
  811. set trivia_status 0
  812. } else {
  813. set trivia_timer [utimer $trivia_delay trivia_start_round]
  814. trivia_check_rehash
  815. }
  816. }
  817. #>>>
  818. # Skip the rest of this question
  819. proc trivia_skip { nick } {
  820. #<<<
  821. global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status
  822. global trivia_timer trivia_delay trivia_db_handle trivia_unanswered
  823. if {$trivia_status != 1} {
  824. #we're switched off, abort
  825. return 0
  826. }
  827. if {$trivia_q_answer == ""} {
  828. #no round in progress
  829. return 0
  830. }
  831. set trivia_q_question ""
  832. set trivia_q_answer ""
  833. trivia_killtimer
  834. putserv "PRIVMSG $trivia_channel :Skipping this question by $nick's request."
  835. set trivia_timer [utimer $trivia_delay trivia_start_round]
  836. }
  837. #>>>
  838. # Utility function to fetch a random item from a list
  839. proc trivia_random_element { l } {
  840. #<<<
  841. return [lindex $l [rand [llength $l]]]
  842. }
  843. #>>>
  844. # Start the game
  845. proc trivia_start { } {
  846. #<<<
  847. global trivia_status trivia_channel trivia_unanswered trivia_run_last trivia_run_qty trivia_c trivia_must_rehash trivia_db_handle
  848. if {$trivia_status == 1} {
  849. putserv "PRIVMSG $trivia_channel :Trivia is already running."
  850. return 0
  851. }
  852. if {$trivia_status == -1} {
  853. putserv "PRIVMSG $trivia_channel :Trivia is currently disabled."
  854. return 0
  855. }
  856. trivia_ping
  857. # go go go
  858. set trivia_status 1
  859. if {$trivia_must_rehash == 2 } {
  860. putquick "PRIVMSG $trivia_channel :$trivia_c(blue)### Resuming trivia game ###" -next
  861. } else {
  862. putquick "PRIVMSG $trivia_channel :Trivia game started!" -next
  863. }
  864. #trivia_stats
  865. set trivia_unanswered 0
  866. set trivia_run_last 0
  867. set trivia_run_qty 0
  868. trivia_start_round
  869. return 0
  870. }
  871. #>>>
  872. # Stop the game
  873. proc trivia_stop { } {
  874. #<<<
  875. global trivia_channel trivia_status
  876. if {$trivia_status == 1} {
  877. putserv "PRIVMSG $trivia_channel :Trivia game stopped."
  878. set trivia_status 0
  879. trivia_killtimer
  880. }
  881. return 0
  882. }
  883. #>>>
  884. # Kill our timer
  885. proc trivia_killtimer { } {
  886. #<<<
  887. global trivia_timer
  888. if {$trivia_timer != ""} {
  889. killutimer $trivia_timer
  890. }
  891. }
  892. #>>>
  893. # Utility function to explode line into letters
  894. proc trivia_explode { line } {
  895. #<<<
  896. set letters [split $line {}]
  897. set newline ""
  898. foreach letter $letters {
  899. append newline "$letter "
  900. }
  901. return [string trim $newline]
  902. }
  903. #>>>
  904. # Get a UID's ranking
  905. proc trivia_get_rank { uid } {
  906. #<<<
  907. global trivia_db_handle
  908. putloglev 4 * "trivia_get_rank ($uid)"
  909. set sql "SELECT user_id FROM users ORDER by user_score DESC"
  910. set dt [trivia_get_period]
  911. set sql "select user_id, count(dt) as d from scores where dt > $dt group by user_id order by d desc"
  912. set pos 0
  913. trivia_db_handle eval $sql {
  914. incr pos
  915. putloglev d * "considering user_id $user_id, score $d"
  916. if {$user_id == $uid} {
  917. return $pos
  918. }
  919. }
  920. return 0
  921. }
  922. #>>>
  923. # Get the top 10 users
  924. proc trivia_get_top10 { } {
  925. #<<<
  926. global trivia_db_handle
  927. set dt [trivia_get_period]
  928. set sql "select users.user_name as u, count(dt) as d from scores left join users using (user_id) where dt > $dt group by scores.user_id order by d desc limit 10"
  929. set output ""
  930. set index 0
  931. trivia_db_handle eval $sql {
  932. incr index
  933. append output "\002$index:\002 "
  934. append output $u
  935. append output " ("
  936. append output $d
  937. append output ") "
  938. }
  939. set output "The top $index players are... $output"
  940. return $output
  941. }
  942. #>>>
  943. # Fix a number into text
  944. proc trivia_ordinal { number } {
  945. #<<<
  946. if [regexp {1[0-9]$} $number] {
  947. return "th"
  948. }
  949. set last [string range $number end end]
  950. if {$last == "1"} {
  951. return "st"
  952. }
  953. if {$last == "2"} {
  954. return "nd"
  955. }
  956. if {$last == "3"} {
  957. return "rd"
  958. }
  959. return "th"
  960. }
  961. #>>>
  962. # Get a score
  963. proc trivia_score { n { nick "" } } {
  964. #<<<
  965. set n [string trim $n]
  966. putloglev 4 * "trivia_score($n, $nick)"
  967. if {$n == ""} {
  968. set ni $nick
  969. set o "You are"
  970. } else {
  971. set ni $n
  972. set o "$n is"
  973. }
  974. set uid [trivia_get_uid $ni]
  975. set score [trivia_get_score $uid]
  976. set pos [trivia_get_rank $uid]
  977. if {$score == 0} {
  978. return "No score found for $n."
  979. }
  980. return "$o ranked $pos[trivia_ordinal $pos] with $score points."
  981. }
  982. #>>>
  983. # Turn a list into a stats list
  984. proc trivia_list_to_stats { pos user_id l } {
  985. #<<<
  986. putloglev 4 * "trivia_list_to_stats ($pos, $user_id, $l)"
  987. global trivia_c
  988. set uid [lindex $l 0]
  989. set nick [lindex $l 1]
  990. set score [lindex $l 2]
  991. incr pos
  992. set result ""
  993. if {$user_id == $uid} {
  994. set result "$trivia_c(purple)$trivia_c(bold)"
  995. }
  996. append result "$pos[trivia_ordinal $pos]: $nick ($score) "
  997. if {$user_id == $uid} {
  998. append result "$trivia_c(off)$trivia_c(bold)"
  999. }
  1000. return $result
  1001. }
  1002. #>>>
  1003. # Another function to get the nearest users to your score
  1004. proc trivia_near_five2 { uid } {
  1005. #<<<
  1006. global trivia_db_handle trivia_c
  1007. set sql "SELECT user_id, user_name, user_score FROM users ORDER BY user_score DESC"
  1008. set dt [trivia_get_period]
  1009. set sql "select users.user_id as user_id, users.user_name as user_name, count(dt) as user_score from scores left join users using (user_id) where dt > $dt group by scores.user_id order by count(dt) desc"
  1010. putlog $sql
  1011. set result [list]
  1012. trivia_db_handle eval $sql {
  1013. set line [list]
  1014. lappend line $user_id
  1015. lappend line $user_name
  1016. lappend line $user_score
  1017. putlog "adding line $line"
  1018. lappend result $line
  1019. }
  1020. putlog "results list: $result"
  1021. set position 0
  1022. foreach item $result {
  1023. if {[lindex $item 0] == $uid} {
  1024. putlog "found at position $position"
  1025. break
  1026. }
  1027. incr position
  1028. }
  1029. set line ""
  1030. if {$position > 2} {
  1031. append line [trivia_list_to_stats [expr $position - 2] $uid [lindex $result [expr $position - 2]]]
  1032. }
  1033. if {$position > 1} {
  1034. append line [trivia_list_to_stats [expr $position - 1] $uid [lindex $result [expr $position - 1]]]
  1035. }
  1036. append line [trivia_list_to_stats $position $uid [lindex $result $position]]
  1037. if {$position < [expr [llength $result] - 1]} {
  1038. append line [trivia_list_to_stats [expr $position + 1] $uid [lindex $result [expr $position + 1]]]
  1039. }
  1040. if {$position < [expr [llength $result] - 2]} {
  1041. append line [trivia_list_to_stats [expr $position + 2] $uid [lindex $result [expr $position + 2]]]
  1042. }
  1043. return $line
  1044. }
  1045. #>>>
  1046. # Get some stats from the DB
  1047. proc trivia_stats { } {
  1048. #<<<
  1049. global trivia_db_handle trivia_channel
  1050. set sql "SELECT COUNT(*) AS total FROM questions LEFT JOIN categories USING (cat_id) WHERE categories.cat_enabled = 1;"
  1051. set stat_total_questions [trivia_db_handle eval $sql]
  1052. set sql "SELECT COUNT(*) AS total FROM categories WHERE cat_enabled = 1"
  1053. set stat_total_cats [trivia_db_handle eval $sql]
  1054. putserv "PRIVMSG $trivia_channel :Using\002 $stat_total_questions\002 questions in\002 $stat_total_cats\002 categories."
  1055. }
  1056. #>>>
  1057. # Merge two users
  1058. proc trivia_merge { nick param } {
  1059. #<<<
  1060. global trivia_db_handle
  1061. putloglev 4 * "trivia_merge ($nick, $param)"
  1062. if [regexp {^([^ ]+) (.+)$} $param matches nick1 nick2] {
  1063. set olduid [trivia_get_uid [trivia_sqlite_escape $nick1]]
  1064. set newuid [trivia_get_uid [trivia_sqlite_escape $nick2]]
  1065. if {$olduid == 0} {
  1066. puthelp "PRIVMSG $nick :Can't find user '$nick1'"
  1067. return 0
  1068. }
  1069. if {$newuid == 0} {
  1070. puthelp "PRIVMSG $nick :Can't find user '$nick2'"
  1071. return 0
  1072. }
  1073. puthelp "PRIVMSG $nick :Meging score for $nick1 into score for $nick2"
  1074. set sql "UPDATE scores SET user_id=$newuid WHERE user_id=$olduid"
  1075. trivia_db_handle eval $sql
  1076. set newscore [trivia_get_score $newuid]
  1077. puthelp "PRIVMSG $nick :$nick2 now has $newscore points."
  1078. set sql "DELETE FROM users WHERE user_id = $olduid"
  1079. trivia_db_handle eval $sql
  1080. puthelp "PRIVMSG $nick :User $nick1 has been deleted."
  1081. return 0
  1082. } else {
  1083. puthelp "PRIVMSG $nick :Use: !trivia merge <olduser> <newuser>"
  1084. puthelp "PRIVMSG $nick : olduser's score is merged with newuser's and olduser is deleted."
  1085. }
  1086. }
  1087. #>>>
  1088. # Report a broken question
  1089. proc trivia_report { nick msg } {
  1090. #<<<
  1091. global trivia_channel trivia_db_handle trivia_last_qid trivia_c
  1092. if {$trivia_last_qid == 0} {
  1093. puthelp "PRIVMSG $trivia_channel :Sorry, unable to work out which question to report on :("
  1094. return 0
  1095. }
  1096. set nick [trivia_sqlite_escape $nick]
  1097. set msg [trivia_sqlite_escape $msg]
  1098. set sql "INSERT INTO reports VALUES (null, [clock seconds], '$nick', '$trivia_last_qid', '$msg', 'N')"
  1099. trivia_db_handle eval $sql
  1100. puthelp "PRIVMSG $trivia_channel :Added a report against question ID$trivia_c(purple) $trivia_last_qid$trivia_c(off) from $trivia_c(purple)$nick$trivia_c(off)."
  1101. set trivia_last_qid 0
  1102. }
  1103. #>>>
  1104. ### WATCHDOG STUFF <<<
  1105. # Watchdog timer to make sure we're not ruined
  1106. proc trivia_watchdog { } {
  1107. #<<<
  1108. global trivia_last_ts trivia_watchdog_timer trivia_status trivia_channel
  1109. putloglev d * "trivia watchdog: tick"
  1110. if {$trivia_last_ts == 0} {
  1111. #never asked a question
  1112. set trivia_watchdog_timer [utimer 45 trivia_watchdog]
  1113. return 0
  1114. }
  1115. set current_ts [clock seconds]
  1116. set difference [expr $current_ts - $trivia_last_ts]
  1117. if {$difference > 0} {
  1118. if {$trivia_status == 1} {
  1119. putlog "watchdog: trivia is broken"
  1120. #putserv "PRIVMSG $trivia_channel :Oops, I think I'm broken. Attempting to recover..."
  1121. #set trivia_status 0
  1122. #trivia_start
  1123. putlog "trivia watchdog: current: $current_ts, last: $trivia_last_ts, difference: $difference"
  1124. }
  1125. }
  1126. set trivia_watchdog_timer [utimer 10 trivia_watchdog]
  1127. return 0
  1128. }
  1129. #>>>
  1130. # Kill the watchdog timeer
  1131. proc trivia_killwatchdog { } {
  1132. #<<<
  1133. global trivia_watchdog_timer
  1134. if {$trivia_watchdog_timer != ""} {
  1135. killutimer $trivia_watchdog_timer
  1136. }
  1137. }
  1138. #>>>
  1139. trivia_killwatchdog
  1140. set trivia_watchdog_timer [utimer 45 trivia_watchdog]
  1141. #>>>
  1142. trivia_connect
  1143. putlog {TriviaEngine ENGAGED(*$£&($}
  1144. if {$trivia_must_rehash == 2} {
  1145. putlog "Auto-restarting trivia..."
  1146. trivia_start
  1147. set trivia_must_rehash 0
  1148. }