TriviaEngine-sqlite.tcl 30 KB

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