TriviaEngine-sqlite.tcl 34 KB

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