TriviaEngine-sqlite.tcl 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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
  1123. putloglev d * "trivia watchdog: tick"
  1124. if {$trivia_last_ts == 0} {
  1125. #never asked a question
  1126. set trivia_watchdog_timer [utimer 45 trivia_watchdog]
  1127. return 0
  1128. }
  1129. set current_ts [clock seconds]
  1130. set difference [expr $current_ts - $trivia_last_ts]
  1131. if {$difference > 0} {
  1132. if {$trivia_status == 1} {
  1133. putlog "watchdog: trivia is broken"
  1134. #putserv "PRIVMSG $trivia_channel :Oops, I think I'm broken. Attempting to recover..."
  1135. #set trivia_status 0
  1136. #trivia_start
  1137. putlog "trivia watchdog: current: $current_ts, last: $trivia_last_ts, difference: $difference"
  1138. }
  1139. }
  1140. set trivia_watchdog_timer [utimer 10 trivia_watchdog]
  1141. return 0
  1142. }
  1143. #>>>
  1144. # Kill the watchdog timeer
  1145. proc trivia_killwatchdog { } {
  1146. #<<<
  1147. global trivia_watchdog_timer
  1148. if {$trivia_watchdog_timer != ""} {
  1149. killutimer $trivia_watchdog_timer
  1150. }
  1151. }
  1152. #>>>
  1153. trivia_killwatchdog
  1154. set trivia_watchdog_timer [utimer 45 trivia_watchdog]
  1155. #>>>
  1156. trivia_connect
  1157. putlog {TriviaEngine ENGAGED(*$£&($}
  1158. if {$trivia_must_rehash == 2} {
  1159. putlog "Auto-restarting trivia..."
  1160. trivia_start
  1161. set trivia_must_rehash 0
  1162. }