TriviaEngine-sqlite.tcl 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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. # allow new categories to be created?
  12. set trivia_allow_new_cats 0
  13. # time before end of round to start counting down
  14. set trivia_time_left_warning 86400
  15. # hold the current question info <<<
  16. set trivia_q_id ""
  17. set trivia_q_cat ""
  18. set trivia_q_question ""
  19. set trivia_q_answer ""
  20. set trivia_q_hint ""
  21. set trivia_q_attempts 0
  22. set trivia_unanswered 0
  23. set trivia_run_last 0
  24. set trivia_run_qty 0
  25. set trivia_run_nick ""
  26. set trivia_guesser ""
  27. set trivia_guesser_count 0
  28. set trivia_last_qid 0
  29. if [info exists trivia_must_rehash] {
  30. if {$trivia_must_rehash == 1} {
  31. set trivia_must_rehash 2
  32. } else {
  33. set trivia_must_rehash 0
  34. }
  35. } else {
  36. set trivia_must_rehash 0
  37. }
  38. #>>>
  39. # 0 = off
  40. # 1 = on
  41. # -1 = disabled
  42. set trivia_status 0
  43. set trivia_timer ""
  44. set trivia_watchdog_timer ""
  45. set trivia_last_ts 0
  46. set trivia_score_time 0
  47. set trivia_debug_mode 0
  48. set trivia_warned 0
  49. set trivia_asking_question 0
  50. set trivia_current_leader -1
  51. #colours <<<
  52. set trivia_c(off) "\003"
  53. set trivia_c(red) "\0034"
  54. set trivia_c(blue) "\0033"
  55. set trivia_c(purple) "\0036"
  56. set trivia_c(bold) "\002"
  57. #>>>
  58. bind pubm - * trivia_input
  59. bind msg - trivia trivia_msg
  60. # connect database <<<
  61. proc trivia_connect { } {
  62. global trivia_db_handle
  63. sqlite3 trivia_db_handle trivia.db
  64. }
  65. #>>>
  66. #>>>
  67. ### SETTINGS <<<
  68. set trivia_flag T
  69. set trivia_admin S
  70. #botnet handle of the bmotion bot we're going to play with (set blank to disable)
  71. set trivia_bmotion_bot "NoTopic"
  72. # >>>
  73. # Send a botnet command to the bmotion bot
  74. proc trivia_bmotion_send { command params } {
  75. global trivia_bmotion_bot
  76. if {$trivia_bmotion_bot == ""} {
  77. return 0
  78. }
  79. if [islinked $trivia_bmotion_bot] {
  80. putbot $trivia_bmotion_bot "trivia $command :$params"
  81. putloglev d * "triviaengine: send $command to $trivia_bmotion_bot with params $params"
  82. } else {
  83. putlog "triviaengine: trying to communicate with bmotion bot $trivia_bmotion_bot but it's not linked"
  84. }
  85. }
  86. # Handle a /msg
  87. proc trivia_msg { nick host handle cmd } {
  88. #<<<
  89. global trivia_db_handle trivia_last_qid
  90. global trivia_q_cat trivia_c trivia_allow_new_cats
  91. regsub "(trivia )" $cmd "" cmd
  92. putlog "trivia: msg command $cmd from $nick"
  93. if {($nick == "Greeneyez") || ($nick == "JamesOff")} {
  94. #<<< set a question's category
  95. if [regexp -nocase "^setcat (.+)" $cmd matches category] {
  96. if {$trivia_last_qid == 0} {
  97. puthelp "PRIVMSG $nick :Can't fathom last question, unable to update category"
  98. return
  99. }
  100. set orig_cat $category
  101. set category [trivia_sqlite_escape $category]
  102. set sql "SELECT cat_id FROM categories WHERE cat_name='$category'"
  103. set cat_id 0
  104. set cat_id [trivia_db_handle eval $sql]
  105. if {$cat_id != ""} {
  106. puthelp "PRIVMSG $nick :Moving question $trivia_last_qid to category$trivia_c(purple) $category$trivia_c(off) ($cat_id)"
  107. set sql "UPDATE questions SET cat_id='$cat_id' WHERE question_id='$trivia_last_qid'"
  108. trivia_db_handle eval $sql
  109. set trivia_q_cat $orig_cat
  110. return
  111. } else {
  112. if {$trivia_allow_new_cats == 1} {
  113. puthelp "PRIVMSG $nick :Creating new category$trivia_c(purple) $category"
  114. set sql "INSERT INTO categories VALUES (null, '$category', 1)"
  115. putloglev d * $sql
  116. trivia_db_handle eval $sql
  117. set cat_id [trivia_db_handle last_insert_rowid]
  118. puthelp "PRIVMSG $nick :Moving question $trivia_last_qid to category$trivia_c(purple) $category$trivia_c(off) ($cat_id)"
  119. set sql "UPDATE questions SET cat_id='$cat_id' WHERE question_id='$trivia_last_qid'"
  120. putloglev d * $sql
  121. trivia_db_handle eval $sql
  122. set trivia_q_cat $orig_cat
  123. } else {
  124. puthelp "PRIVMSG $nick :Creating new categories is $trivia_c(purple)DISABLED$trivia_c(off). ('trivia newcat enable' to enable.)"
  125. }
  126. return
  127. }
  128. }
  129. #>>>
  130. #<<< enable or disable new category creation
  131. if [regexp -nocase "^newcat (enable|disable)" $cmd matches toggle] {
  132. if {$toggle == "enable"} {
  133. puthelp "PRIVMSG $nick :$trivia_c(purple)ENABLING$trivia_c(off) new category creation"
  134. set trivia_allow_new_cats 1
  135. return
  136. }
  137. if {$toggle == "disable"} {
  138. puthelp "PRIVMSG $nick :$trivia_c(purple)DISABLING$trivia_c(off) new category creation"
  139. set trivia_allow_new_cats 0
  140. return
  141. }
  142. puthelp "PRIVMSG $nick :Use: trivia newcat enable|disable"
  143. return
  144. }
  145. #>>>
  146. #<<< debug
  147. if [regexp -nocase {^debug (.+)} $cmd matches func] {
  148. global trivia_debug_mode
  149. if {$trivia_debug_mode == 0} {
  150. putquick "PRIVMSG $nick :Debug mode is disabled. Cannot use debug commands."
  151. return 0
  152. }
  153. if {$func == "endweek"} {
  154. putquick "PRIVMSG $nick :Ending this week in 10 seconds..."
  155. global trivia_score_time
  156. set trivia_score_time [expr [clock seconds] + 10]
  157. return 0
  158. }
  159. }
  160. #>>>
  161. #<<< handle reports
  162. if [regexp -nocase {^report (help|list|fix|view|done|delete)?( .+)?} $cmd matches func arg] {
  163. if {($func == "") || ($func == "help")} {
  164. puthelp "PRIVMSG $nick :Use: report (list|view|fix|done|delete)"
  165. puthelp "PRIVMSG $nick :report list: see 10 reports"
  166. puthelp "PRIVMSG $nick :report view <id>: see the question and answer associated with a report"
  167. puthelp "PRIVMSG $nick :report fix <id> (question|answer) <new text>: update a question or answer"
  168. puthelp "PRIVMSG $nick :report done <id>: mark a report as done"
  169. return 0
  170. }
  171. if {$func == "list"} {
  172. putserv "PRIVMSG $nick :Gathering 10 trivia reports..."
  173. set sql "SELECT * FROM reports WHERE resolved = 'N' LIMIT 10"
  174. trivia_db_handle eval $sql result {
  175. putserv "PRIVMSG $nick :$result(report_id): [format %10s $result(who)] $result(message)"
  176. }
  177. return 0
  178. }
  179. if {$func == "view"} {
  180. if {$arg == ""} {
  181. puthelp "PRIVMSG $nick :Use: report view <id>"
  182. return 0
  183. }
  184. set arg [string trim $arg]
  185. set arg [trivia_sqlite_escape $arg]
  186. set sql "SELECT * FROM reports, questions WHERE report_id = '$arg' AND reports.question_id = questions.question_id"
  187. trivia_db_handle eval $sql result {
  188. 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]"
  189. putserv "PRIVMSG $nick :Question: $result(question)"
  190. putserv "PRIVMSG $nick : Answer: $result(answer)"
  191. putserv "PRIVMSG $nick : Report: $result(message)"
  192. }
  193. return 0
  194. }
  195. if {$func == "fix"} {
  196. set arg [string trim $arg]
  197. if [regexp -nocase {([0-9]+) (question|answer) (.+)} $arg matches report_id thing fix] {
  198. putserv "PRIVMSG $nick :Attempting to fix $trivia_c(purple)$thing$trivia_c(off) from report$trivia_c(purple) $report_id"
  199. #get question ID for this report
  200. set sql "SELECT question_id FROM reports WHERE report_id = '$report_id'"
  201. putloglev d * $sql
  202. set question_id 0
  203. trivia_db_handle eval $sql result {
  204. set question_id $result(question_id)
  205. }
  206. if {$question_id == 0} {
  207. putserv "PRIVMSG $nick :I can't seem to find that report, sorry :("
  208. return 0
  209. }
  210. set fix [trivia_sqlite_escape $fix]
  211. set sql "UPDATE questions SET $thing = '$fix' WHERE question_id = '$question_id'"
  212. putloglev d * $sql
  213. trivia_db_handle eval $sql
  214. putserv "PRIVMSG $nick :Updated!"
  215. return 0
  216. } else {
  217. puthelp "PRIVMSG $nick :Use: report fix <id> (question|answer) <new text>"
  218. return 0
  219. }
  220. }
  221. if {$func == "done"} {
  222. set arg [string trim $arg]
  223. set arg [trivia_sqlite_escape $arg]
  224. putserv "PRIVMSG $nick :Marking report$trivia_c(purple) $arg $trivia_c(off)as done."
  225. set sql "UPDATE reports SET resolved = 'Y' WHERE report_id = '$arg'"
  226. putloglev d * $sql
  227. trivia_db_handle eval $sql
  228. return 0
  229. }
  230. if {$func == "delete"} {
  231. set arg [string trim $arg]
  232. set arg [trivia_sqlite_escape $arg]
  233. putserv "PRIVMSG $nick :Deleting question from report$trivia_c(purple) $arg $trivia_c(off) and marking report as done."
  234. set sql "DELETE FROM questions WHERE question_id IN (SELECT question_id FROM reports WHERE report_id = '$arg')"
  235. putloglev d * $sql
  236. trivia_db_handle eval $sql
  237. set sql "UPDATE reports SET resolved = 'Y' WHERE report_id = '$arg'"
  238. putloglev d * $sql
  239. trivia_db_handle eval $sql
  240. return 0
  241. }
  242. }
  243. #>>>
  244. }
  245. }
  246. #>>>
  247. # Handle input coming from a channel
  248. proc trivia_input { nick host handle channel arg } {
  249. #<<<
  250. global trivia_channel trivia_status trivia_q_answer
  251. global trivia_guesser_count trivia_guesser trivia_c
  252. if {[string tolower $trivia_channel] != [string tolower $channel]} {
  253. return 0
  254. }
  255. if [regexp -nocase "^!t(rivia)? (.+)" $arg matches cmd param] {
  256. trivia_command $nick $host $handle $channel $param
  257. return 0
  258. }
  259. if [regexp -nocase "^!t(rivia)?$" $arg] {
  260. if {$trivia_status == 1} {
  261. puthelp "PRIVMSG $trivia_channel :!trivia ... ?"
  262. return 0
  263. } else {
  264. puthelp "PRIVMSG $trivia_channel :Perhaps you want $trivia_c(purple)!trivia start"
  265. return 0
  266. }
  267. }
  268. # need to be running below here
  269. if {$trivia_status == -1} {
  270. return 0
  271. }
  272. if {$trivia_q_answer == ""} {
  273. return 0
  274. }
  275. #see if someone else is playing
  276. if {($nick == $trivia_guesser) && ($nick != "NoTopic")} {
  277. incr trivia_guesser_count
  278. putloglev d * "$nick has talked $trivia_guesser_count times in a row..."
  279. } else {
  280. set trivia_guesser_count 0
  281. set trivia_guesser $nick
  282. }
  283. #tidy the string up
  284. set arg [string tolower $arg]
  285. set arg [string trim $arg]
  286. if {$arg == [string tolower $trivia_q_answer]} {
  287. # try to stop any other output
  288. clearqueue server
  289. trivia_correct $nick
  290. return 0
  291. }
  292. #if they didn't get it right, and it's been more than 20 lines, taunt!
  293. if {($trivia_guesser_count > 0) && (($trivia_guesser_count % 20)) == 0} {
  294. putserv "PRIVMSG $trivia_channel :Playing with yourself, $nick? ;)"
  295. putlog "Is $nick playing with themselves?"
  296. }
  297. }
  298. #>>>
  299. # Someone got it right!
  300. proc trivia_correct { nick } {
  301. #<<<
  302. global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status
  303. global trivia_timer trivia_delay trivia_db_handle trivia_unanswered trivia_run_qty trivia_run_last trivia_run_nick trivia_c
  304. global trivia_score_time trivia_time_left_warning trivia_asking_question trivia_current_leader
  305. if {$trivia_status != 1} {
  306. #something strange going on
  307. return 0
  308. }
  309. trivia_killtimer
  310. set answer $trivia_q_answer
  311. set trivia_q_answer ""
  312. set newuser 0
  313. set trivia_asking_question 0
  314. set uid [trivia_get_uid $nick]
  315. if {$uid == 0} {
  316. putlog "$nick does not have entry in database... creating"
  317. set uid [trivia_create_user $nick]
  318. set newuser 1
  319. }
  320. putlog "$nick has uid $uid"
  321. set old_leader [trivia_leader]
  322. trivia_incr_score $uid
  323. putquick "PRIVMSG $trivia_channel :Congratulations $trivia_c(purple)$nick$trivia_c(off)! The answer was$trivia_c(purple) $answer$trivia_c(off)."
  324. trivia_bmotion_send "winner" "$nick $answer"
  325. if {$newuser == 1} {
  326. putquick "PRIVMSG $trivia_channel :Welcome to our newest player, $trivia_c(purple)$nick$trivia_c(off) :)"
  327. }
  328. putquick "PRIVMSG $trivia_channel :Rankings: [trivia_near_five2 $uid]"
  329. set leader [trivia_leader]
  330. if {($leader != $old_leader) && ($leader != -1) && ($old_leader != -1)} {
  331. putquick "PRIVMSG $trivia_channel :$trivia_c(purple) $nick $trivia_c(off) has taken the lead!"
  332. }
  333. #set score [trivia_get_score $uid]
  334. #putserv "PRIVMSG $trivia_channel :$nick now has $score points."
  335. set trivia_timer [utimer $trivia_delay trivia_start_round]
  336. set trivia_unanswered 0
  337. if {$trivia_run_last == $uid} {
  338. incr trivia_run_qty
  339. if {$trivia_run_qty > 2} {
  340. putquick "PRIVMSG $trivia_channel :$nick [trivia_get_run $trivia_run_qty] $trivia_run_qty in a row!"
  341. }
  342. } else {
  343. #end of streak
  344. if {$trivia_run_qty > 2} {
  345. putquick "PRIVMSG $trivia_channel :$nick ended $trivia_run_nick's winning spree."
  346. }
  347. set trivia_run_qty 1
  348. set trivia_run_last $uid
  349. set trivia_run_nick $nick
  350. }
  351. if {[rand 100] > 95} {
  352. putserv "PRIVMSG $trivia_channel :Remember, to report a problem with a question or answer, type $trivia_c(purple)!trivia report <description of problem>"
  353. }
  354. if {$trivia_score_time <= [clock seconds]} {
  355. trivia_end_week
  356. } else {
  357. if {[expr $trivia_score_time - [clock seconds]] < $trivia_time_left_warning} {
  358. set diff [expr $trivia_score_time - [clock seconds]]
  359. set diff $diff.0
  360. set nearness [expr $diff / $trivia_time_left_warning * 100]
  361. set chance [rand 100]
  362. putlog "diff = $diff, nearness = $nearness, chance = $chance"
  363. if {$chance < $nearness} {
  364. putserv "PRIVMSG $trivia_channel :[trivia_score_time_left] until the end of this game!"
  365. }
  366. }
  367. }
  368. trivia_check_rehash
  369. }
  370. #>>>
  371. #See if we need to rehash
  372. proc trivia_check_rehash { } {
  373. #<<<
  374. global trivia_must_rehash trivia_channel trivia_c
  375. if {$trivia_must_rehash == 1} {
  376. putquick "PRIVMSG $trivia_channel :$trivia_c(red)### Please wait, reloading trivia script ###"
  377. trivia_killtimer
  378. rehash
  379. }
  380. }
  381. #>>>
  382. # Turn a winning spree into text
  383. proc trivia_get_run { row } {
  384. #<<<
  385. global trivia_channel trivia_c
  386. switch $row {
  387. 3 {
  388. return "is on a winning spree!"
  389. }
  390. 4 {
  391. puthelp "PRIVMSG $trivia_channel :$trivia_c(blue)QUAD DAMAGE!"
  392. return "is on a roll ..."
  393. }
  394. 5 {
  395. return "is on a rampage!"
  396. }
  397. 6 {
  398. return "is unstoppable!"
  399. }
  400. 8 {
  401. return "is on a Google spree!"
  402. }
  403. 10 {
  404. return "is GODLIKE!"
  405. }
  406. 12 {
  407. return "needs to get out more."
  408. }
  409. }
  410. return "is on a roll ..."
  411. }
  412. #>>>
  413. # Escape stuff for SQL
  414. proc trivia_sqlite_escape { text } {
  415. #<<<
  416. return [string map {' ''} $text]
  417. }
  418. #>>>
  419. # Get someone's user ID from their nick
  420. proc trivia_get_uid { nick } {
  421. #<<<
  422. global trivia_db_handle
  423. putloglev 4 * "trivia_get_uid ($nick)"
  424. set nick [trivia_sqlite_escape $nick]
  425. set sql "SELECT user_id FROM users WHERE user_name = '$nick'"
  426. trivia_db_handle eval $sql {
  427. return $user_id
  428. }
  429. return 0
  430. }
  431. #>>>
  432. # Get a period
  433. proc trivia_get_period { } {
  434. #<<<
  435. return [expr [clock scan "last friday 23:59" -gmt true] + 60]
  436. }
  437. #>>>
  438. # Get a score from a UID
  439. proc trivia_get_score { user_id } {
  440. #<<<
  441. global trivia_db_handle
  442. putloglev 4 * "trivia_get_score ($user_id)"
  443. set dt [trivia_get_period]
  444. set sql "SELECT COUNT(*) AS yarr FROM scores WHERE dt > '$dt' AND user_id='$user_id'"
  445. trivia_db_handle eval $sql {
  446. return $yarr
  447. }
  448. return 0
  449. }
  450. #>>>
  451. # Turn a timestamp into a date
  452. proc trivia_ts_to_date { ts } {
  453. #<<<
  454. return [clock format $ts -format "%Y/%m/%d %H:%M"]
  455. }
  456. #>>>
  457. # Get someone's userinfo from their UID
  458. proc trivia_get_userinfo { user_id } {
  459. #<<<
  460. global trivia_db_handle trivia_channel
  461. putloglev 4 * "trivia_get_userinfo ($user_id)"
  462. set donestats 0
  463. set sql "SELECT user_name, user_score, user_last, user_reg FROM users WHERE user_id = '$user_id'"
  464. trivia_db_handle eval $sql {
  465. if {$user_last == ""} {
  466. set last "Unknown"
  467. } else {
  468. set last [trivia_ts_to_date $user_last]
  469. }
  470. if {$user_reg == ""} {
  471. set reg "Unkown"
  472. } else {
  473. set reg [trivia_ts_to_date $user_reg]
  474. }
  475. putserv "PRIVMSG $trivia_channel :Trivia stats for $user_name:"
  476. putserv "PRIVMSG $trivia_channel : Current score: $user_score"
  477. putserv "PRIVMSG $trivia_channel : First seen: $reg"
  478. putserv "PRIVMSG $trivia_channel : Last scored: $last"
  479. set donestats 1
  480. }
  481. set sql "SELECT COUNT(dt) AS score FROM scores WHERE dt > [trivia_get_period] AND user_id = '$user_id'"
  482. if {$donestats} {
  483. trivia_db_handle eval $sql {
  484. putserv "PRIVMSG $trivia_channel :Points this week: $score"
  485. putserv "PRIVMSG $trivia_channel : Weekly Ranking: [trivia_get_rank $user_id]"
  486. }
  487. } else {
  488. putserv "PRIVMSG $trivia_channel :No stats found"
  489. }
  490. }
  491. #>>>
  492. # Get someone's stats from their username
  493. proc trivia_user_stats { user_name } {
  494. #<<<
  495. global trivia_channel
  496. set uid [trivia_get_uid $user_name]
  497. if {$uid == 0} {
  498. putserv "PRIVMSG $trivia_channel :Unknown user '$user_name' (is the case right?)"
  499. return
  500. }
  501. trivia_get_userinfo $uid
  502. }
  503. #>>>
  504. # Increase a UID's score
  505. proc trivia_incr_score { id { howmuch 1 } } {
  506. #<<<
  507. global trivia_db_handle
  508. putloglev 4 * "trivia_incr_score ($id, $howmuch)"
  509. set sql "UPDATE users SET user_last = [clock seconds], user_points = user_points + 1 WHERE user_id = '$id'"
  510. trivia_db_handle eval $sql
  511. set sql "INSERT INTO scores VALUES ('$id', '[clock seconds]')"
  512. trivia_db_handle eval $sql
  513. }
  514. #>>>
  515. # Create a new user
  516. proc trivia_create_user { nick } {
  517. #<<<
  518. global trivia_db_handle
  519. putloglev 4 * "trivia_create_user ($nick)"
  520. set nick [trivia_sqlite_escape $nick]
  521. set sql "INSERT INTO users VALUES (null, '$nick', '', 0, [clock seconds], [clock seconds], 0)"
  522. trivia_db_handle eval $sql
  523. set uid [trivia_db_handle last_insert_rowid]
  524. return $uid
  525. }
  526. #>>>
  527. # Handle a !trivia command
  528. proc trivia_command { nick host handle channel param } {
  529. #<<<
  530. global trivia_channel trivia_status trivia_flag trivia_admin trivia_c
  531. set arg ""
  532. regexp {^([^ ]+)( .+)?$} $param matches cmd arg
  533. set param [string tolower $cmd]
  534. set arg [string trim $arg]
  535. putloglev d * "trivia command: $param from: $nick ($arg)"
  536. if {[matchattr $handle |$trivia_admin $trivia_channel] && ($param != "enable") && ($trivia_status == -1)} {
  537. return 0
  538. }
  539. if [regexp -nocase "^(score|place)( .+)?" $param] {
  540. putserv "PRIVMSG $trivia_channel :[trivia_score $arg $nick]"
  541. if [string match -nocase $nick $arg] {
  542. putserv "PRIVMSG $trivia_channel :(You know putting your own nick is optional, right? :)"
  543. }
  544. return 0
  545. }
  546. if {$param == "top10"} {
  547. putserv "PRIVMSG $trivia_channel :[trivia_get_top10]"
  548. return 0
  549. }
  550. if {$param == "info"} {
  551. trivia_user_stats $arg
  552. return 0
  553. }
  554. if {$param == "start"} {
  555. trivia_start
  556. return 0
  557. }
  558. if {$param == "report"} {
  559. trivia_report $nick $arg
  560. return 0
  561. }
  562. if {$param == "countdown"} {
  563. trivia_countdown
  564. return 0
  565. }
  566. if {![matchattr $handle |$trivia_flag $channel]} {
  567. putserv "PRIVMSG $nick :use: !trivia \[score|top10|start|report\]"
  568. return 0
  569. }
  570. if {$param == "stop"} {
  571. trivia_stop
  572. return 0
  573. }
  574. if {$param == "skip"} {
  575. trivia_skip $nick
  576. return 0
  577. }
  578. if {$param == "stats"} {
  579. trivia_stats
  580. return 0
  581. }
  582. if {![matchattr $handle |$trivia_admin $channel]} {
  583. putserv "PRIVMSG $nick :use: !trivia \[score|top10|start|stop|skip|stats\]"
  584. return 0
  585. }
  586. if {$param == "disable"} {
  587. trivia_disable
  588. return 0
  589. }
  590. if {$param == "enable"} {
  591. trivia_enable
  592. return 0
  593. }
  594. if {$param == "merge"} {
  595. trivia_merge $nick $arg
  596. #puthelp "PRIVMSG $trivia_channel :Score merging is disabled"
  597. return 0
  598. }
  599. if {$param == "rehash"} {
  600. trivia_rehash
  601. return 0
  602. }
  603. putserv "PRIVMSG $nick :use: !trivia \[start|stop|score|top10|skip|stats|enable|disable|merge\]"
  604. return 0
  605. }
  606. #>>>
  607. # Rehash
  608. proc trivia_rehash { } {
  609. #<<<
  610. global trivia_status trivia_channel trivia_must_rehash
  611. if {$trivia_must_rehash == 1} {
  612. putserv "PRIVMSG $trivia_channel :! Reloading trivia now..."
  613. rehash
  614. return 0
  615. }
  616. if {$trivia_status != 1} {
  617. putserv "PRIVMSG $trivia_channel :! Reloading trivia now..."
  618. rehash
  619. return 0
  620. }
  621. set trivia_must_rehash 1
  622. putserv "PRIVMSG $trivia_channel :Trivia will reload after current round"
  623. }
  624. #>>>
  625. # Disable the script
  626. proc trivia_disable { } {
  627. #<<<
  628. global trivia_status trivia_channel
  629. if {$trivia_status == 1} {
  630. #stop the current game
  631. trivia_stop
  632. }
  633. set trivia_status -1
  634. putserv "PRIVMSG $trivia_channel :Trivia disabled."
  635. return 0
  636. }
  637. #>>>
  638. proc trivia_ping { } {
  639. return
  640. }
  641. # Enable the script
  642. proc trivia_enable {} {
  643. #<<<
  644. global trivia_status trivia_channel trivia_db_handle
  645. set trivia_status 0
  646. putserv "PRIVMSG $trivia_channel :Trivia enabled."
  647. trivia_ping
  648. return 0
  649. }
  650. #>>>
  651. #Make a hint
  652. proc trivia_make_hint { hint answer } {
  653. #<<<
  654. global trivia_debug_mode
  655. set hint [string toupper $hint]
  656. set answer [string toupper $answer]
  657. set answer_words [split $answer { }]
  658. set final_hint ""
  659. #are we making the very first hint?
  660. if {$hint == ""} {
  661. foreach word $answer_words {
  662. append final_hint [string repeat "_" [string length $word]]
  663. append final_hint " "
  664. }
  665. set final_hint [string trim $final_hint]
  666. #now put the punctuation in
  667. set letters [split $answer {}]
  668. set i 0
  669. foreach letter $letters {
  670. if {![regexp -nocase {[A-Z0-9 ]} $letter]} {
  671. set final_hint [string replace $final_hint $i $i $letter]
  672. }
  673. incr i
  674. }
  675. return [string trim $final_hint]
  676. }
  677. # explode the into words
  678. set hint_words [split $hint { }]
  679. set i 0
  680. foreach word $hint_words {
  681. set answer_word [lindex $answer_words $i]
  682. putloglev 1 * "considering $answer_word ($word)"
  683. #are we on the first iteration?
  684. if [regexp "^_+$" $word] {
  685. #use the first letter
  686. set letters [string length $word]
  687. #don't hint for single-letter words
  688. if {$letters > 1} {
  689. set word [string index $answer_word 0]
  690. append word [string repeat "_" [expr $letters - 1]]
  691. } else {
  692. set word "_"
  693. }
  694. append final_hint "$word "
  695. } else {
  696. #not the first iteration so figure out where the gaps are
  697. set gaps [list]
  698. set letters [split $word {}]
  699. set j 0
  700. foreach letter $letters {
  701. if {$letter == "_"} {
  702. lappend gaps $j
  703. }
  704. incr j
  705. }
  706. if {[llength $gaps] <= 1} {
  707. #eek no letters to replace, or only one gap
  708. putloglev 1 * " insufficient gaps, using $word"
  709. append final_hint "$word "
  710. } else {
  711. #now we pick a random letter position
  712. set pos [trivia_random_element $gaps]
  713. putloglev 1 * " filling in $pos"
  714. set word [string replace $word $pos $pos [string index $answer_word $pos]]
  715. putloglev 1 * " --> $word"
  716. append final_hint "$word "
  717. }
  718. }
  719. incr i
  720. }
  721. set final_hint [string trim $final_hint]
  722. if {$trivia_debug_mode == 1} {
  723. return $answer
  724. }
  725. return $final_hint
  726. }
  727. #>>>
  728. # Fetch a question
  729. proc trivia_get_question { } {
  730. #<<<
  731. global trivia_db_handle trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_channel
  732. set trivia_q_id ""
  733. 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"
  734. trivia_db_handle eval $sql {
  735. set trivia_q_id $question_id
  736. set trivia_q_cat $cat_name
  737. set trivia_q_question $question
  738. set trivia_q_answer [string toupper $answer]
  739. set trivia_q_hint ""
  740. #tidy up question and answer
  741. regsub -all " +" $trivia_q_question " " trivia_q_question
  742. regsub -all " +" $trivia_q_answer " " trivia_q_answer
  743. set trivia_q_question [string trim $trivia_q_question]
  744. set trivia_q_answer [string trim $trivia_q_answer]
  745. }
  746. if {$trivia_q_id == ""} {
  747. return
  748. }
  749. #update the times used
  750. set sql "UPDATE questions SET count = count + 1 WHERE question_id = '$trivia_q_id'"
  751. trivia_db_handle eval $sql
  752. }
  753. #>>>
  754. # Start a round
  755. proc trivia_start_round { } {
  756. #<<<
  757. 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
  758. global trivia_asking_question trivia_delay botnick
  759. if {$trivia_status != 1} {
  760. #we're switched off, abort
  761. return 0
  762. }
  763. #init variables
  764. set trivia_q_id ""
  765. set trivia_q_cat ""
  766. set trivia_q_question ""
  767. set trivia_q_answer ""
  768. set trivia_q_hint ""
  769. putlog "Fetching next question..."
  770. trivia_get_question
  771. if {$trivia_q_id == ""} {
  772. putlog "Couldn't init trivia round, aborting."
  773. return
  774. }
  775. putlog "Successfully fetched question $trivia_q_id from database"
  776. putloglev 4 * "question = $trivia_q_question"
  777. putloglev 4 * "answer = $trivia_q_answer"
  778. set trivia_q_attempts 1
  779. set trivia_last_qid $trivia_q_id
  780. set trivia_asking_question 1
  781. trivia_bmotion_send "start" "$trivia_channel $trivia_delay $botnick"
  782. trivia_round
  783. }
  784. #>>>
  785. # Run a round
  786. proc trivia_round { } {
  787. #<<<
  788. global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status
  789. global trivia_timer trivia_speed trivia_c trivia_last_ts
  790. if {$trivia_status != 1} {
  791. #we're switched off, abort
  792. return 0
  793. }
  794. if {$trivia_q_attempts == 5} {
  795. trivia_end_round
  796. return 0
  797. }
  798. if {$trivia_q_answer == ""} {
  799. return 0
  800. }
  801. #update the hint
  802. set trivia_q_hint [trivia_make_hint $trivia_q_hint $trivia_q_answer]
  803. putlog "hint is $trivia_q_hint"
  804. #say our stuff
  805. if {$trivia_q_attempts > 1} {
  806. set hint " \[[expr $trivia_q_attempts - 1] of 3\]"
  807. } else {
  808. set hint ""
  809. }
  810. putserv "PRIVMSG $trivia_channel :$trivia_c(red)--== Trivia ==--$trivia_c(off) \[category: \002$trivia_q_cat\002\]"
  811. #\[question id: \002$trivia_q_id\002\]"
  812. set split_question [trivia_question_split $trivia_q_question]
  813. foreach q $split_question {
  814. if {$q != ""} {
  815. putserv "PRIVMSG $trivia_channel :$trivia_c(blue) [trivia_question_inject $q]"
  816. }
  817. }
  818. #set new_question [trivia_question_inject $trivia_q_question]
  819. #putserv "PRIVMSG $trivia_channel :$trivia_c(blue) $new_question"
  820. putserv "PRIVMSG $trivia_channel :Hint$hint: [trivia_explode $trivia_q_hint]"
  821. incr trivia_q_attempts
  822. trivia_bmotion_send "hint" [string map {_ .} $trivia_q_hint]
  823. set trivia_last_ts [clock seconds]
  824. global trivia_must_rehash
  825. if {$trivia_must_rehash != 2} {
  826. set trivia_timer [utimer $trivia_speed trivia_round]
  827. }
  828. }
  829. #>>>
  830. # Make it harder for things to break questions (inject bold codes)
  831. proc trivia_question_inject { question } {
  832. #<<<
  833. putlog "trivia_question_inject $question"
  834. #inject random bold/underline/colour codes into question
  835. global trivia_c
  836. set l [string length $question]
  837. putlog "length: $l"
  838. if {$l < 1} {
  839. return $question
  840. }
  841. set pos [rand $l]
  842. set first [string range $question 0 $pos]
  843. incr pos
  844. set second [string range $question $pos end]
  845. switch [rand 1] {
  846. 0 {
  847. putlog "question_inject: using bold at pos $pos"
  848. return $first$trivia_c(bold)$trivia_c(bold)$second
  849. }
  850. 1 {
  851. putlog "question_inject: using purple at pos $pos"
  852. return $first$trivia_c(purple)$trivia_c(off)$second
  853. }
  854. }
  855. return $question
  856. }
  857. #>>>
  858. # Make it harder for things to break questions (inject line breaks)
  859. proc trivia_question_split { question } {
  860. #<<<
  861. putlog "trivia_question_split $question"
  862. #explodes a question into two lines at word boundaries, if it's long enough
  863. #don't split unscrambles incorrectly
  864. if [regexp -nocase "(unscramble .+:) (\[A-Z \]+)" $question matches first second] {
  865. return [list $first $second]
  866. }
  867. set words [split $question " "]
  868. set wordcount [llength $words]
  869. if {$wordcount < 4} {
  870. putlog "aborting, too short"
  871. return [list $question]
  872. }
  873. #enough words to split
  874. #we want at least two on the first line
  875. putlog "wordcount: $wordcount"
  876. incr wordcount -2
  877. if {$wordcount < 0} {
  878. return [list $question]
  879. }
  880. set pos [rand $wordcount]
  881. incr pos 2
  882. putlog "picked pos $pos"
  883. set wordlist [list]
  884. set i 0
  885. set line ""
  886. foreach word $words {
  887. #putlog "word = $word, i = $i"
  888. append line "$word "
  889. if {$i == $pos} {
  890. #putlog "splitting here, line = $line"
  891. lappend wordlist [string trim $line]
  892. set line ""
  893. }
  894. incr i
  895. }
  896. #putlog "done, appending $line to list"
  897. if {$line != ""} {
  898. lappend wordlist [string trim $line]
  899. }
  900. #putlog "split question list is: $wordlist"
  901. return $wordlist
  902. }
  903. #>>>
  904. # Finish a round (without a winner)
  905. proc trivia_end_round { } {
  906. #<<<
  907. global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status
  908. global trivia_timer trivia_delay trivia_db_handle trivia_unanswered
  909. global trivia_run_last trivia_run_nick trivia_run_qty trivia_c trivia_score_time trivia_asking_question trivia_time_left_warning
  910. if {$trivia_status != 1} {
  911. #we're switched off, abort
  912. return 0
  913. }
  914. set trivia_asking_question 0
  915. trivia_bmotion_send "winner" "* $trivia_q_answer"
  916. trivia_bmotion_send "stop" ""
  917. set trivia_q_answer [string toupper $trivia_q_answer]
  918. putquick "PRIVMSG $trivia_channel :Time's up! Nobody got it right. The answer was$trivia_c(purple) $trivia_q_answer"
  919. set trivia_q_answer ""
  920. incr trivia_unanswered
  921. if {$trivia_run_qty > 2} {
  922. putserv "PRIVMSG $trivia_channel :So much for $trivia_run_nick's winning spree."
  923. }
  924. set trivia_run_last 0
  925. set trivia_run_qty 0
  926. set trivia_run_nick ""
  927. if {[rand 100] > 90} {
  928. putserv "PRIVMSG $trivia_channel :Remember, to report a problem with a question or answer, type $trivia_c(purple)!trivia report <description of problem>"
  929. }
  930. if {$trivia_unanswered > 3} {
  931. putserv "PRIVMSG $trivia_channel :Four unanswered in a row, stopping the game."
  932. putserv "PRIVMSG $trivia_channel :You can restart it with $trivia_c(purple)!t start"
  933. set trivia_status 0
  934. } else {
  935. set trivia_timer [utimer $trivia_delay trivia_start_round]
  936. if {$trivia_score_time <= [clock seconds]} {
  937. trivia_end_week
  938. } else {
  939. if {[expr $trivia_score_time - [clock seconds]] < $trivia_time_left_warning} {
  940. set diff [expr $trivia_score_time - [clock seconds]]
  941. set diff $diff.0
  942. set nearness [expr $diff / $trivia_time_left_warning * 100]
  943. set chance [rand 100]
  944. putlog "diff = $diff, nearness = $nearness, chance = $chance"
  945. if {$chance < $nearness} {
  946. putserv "PRIVMSG $trivia_channel :[trivia_score_time_left] until the end of this game!"
  947. }
  948. }
  949. }
  950. trivia_check_rehash
  951. }
  952. }
  953. #>>>
  954. # Skip the rest of this question
  955. proc trivia_skip { nick } {
  956. #<<<
  957. global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status
  958. global trivia_timer trivia_delay trivia_db_handle trivia_unanswered trivia_score_time
  959. if {$trivia_status != 1} {
  960. #we're switched off, abort
  961. return 0
  962. }
  963. if {$trivia_q_answer == ""} {
  964. #no round in progress
  965. return 0
  966. }
  967. set trivia_q_question ""
  968. set trivia_q_answer ""
  969. trivia_killtimer
  970. trivia_bmotion_send "stop" ""
  971. putserv "PRIVMSG $trivia_channel :Skipping this question by $nick's request."
  972. set trivia_timer [utimer $trivia_delay trivia_start_round]
  973. if {$trivia_score_time <= [clock seconds]} {
  974. trivia_end_week
  975. }
  976. }
  977. #>>>
  978. # Utility function to fetch a random item from a list
  979. proc trivia_random_element { l } {
  980. #<<<
  981. return [lindex $l [rand [llength $l]]]
  982. }
  983. #>>>
  984. # Start the game
  985. proc trivia_start { } {
  986. #<<<
  987. global trivia_status trivia_channel trivia_unanswered trivia_run_last trivia_run_qty trivia_c trivia_must_rehash trivia_db_handle
  988. if {$trivia_status == 1} {
  989. putserv "PRIVMSG $trivia_channel :Trivia is already running."
  990. return 0
  991. }
  992. if {$trivia_status == -1} {
  993. putserv "PRIVMSG $trivia_channel :Trivia is currently disabled."
  994. return 0
  995. }
  996. trivia_ping
  997. # go go go
  998. set trivia_status 1
  999. if {$trivia_must_rehash == 2 } {
  1000. putquick "PRIVMSG $trivia_channel :$trivia_c(blue)### Resuming trivia game ###" -next
  1001. } else {
  1002. putquick "PRIVMSG $trivia_channel :Trivia game started!" -next
  1003. }
  1004. #trivia_stats
  1005. putquick "PRIVMSG $trivia_channel :[trivia_score_time_left] left until end of this game!"
  1006. #try to find last week's winner
  1007. set sql "SELECT winners.user_id, users.user_name FROM winners LEFT JOIN users USING(user_id) ORDER BY dt DESC, score DESC LIMIT 1"
  1008. putloglev d * $sql
  1009. trivia_db_handle eval $sql {
  1010. putquick "PRIVMSG $trivia_channel :Last week's winner was$trivia_c(purple) [string toupper $user_name]$trivia_c(off)!"
  1011. }
  1012. set trivia_unanswered 0
  1013. set trivia_run_last 0
  1014. set trivia_run_qty 0
  1015. trivia_start_round
  1016. return 0
  1017. }
  1018. #>>>
  1019. # Stop the game
  1020. proc trivia_stop { } {
  1021. #<<<
  1022. global trivia_channel trivia_status
  1023. if {$trivia_status == 1} {
  1024. clearqueue server
  1025. putserv "PRIVMSG $trivia_channel :Trivia game stopped."
  1026. set trivia_status 0
  1027. trivia_bmotion_send "stop" ""
  1028. trivia_killtimer
  1029. }
  1030. return 0
  1031. }
  1032. #>>>
  1033. # Kill our timer
  1034. proc trivia_killtimer { } {
  1035. #<<<
  1036. global trivia_timer
  1037. if {$trivia_timer != ""} {
  1038. killutimer $trivia_timer
  1039. }
  1040. }
  1041. #>>>
  1042. # Utility function to explode line into letters
  1043. proc trivia_explode { line } {
  1044. #<<<
  1045. set letters [split $line {}]
  1046. set newline ""
  1047. foreach letter $letters {
  1048. append newline "$letter "
  1049. }
  1050. return [string trim $newline]
  1051. }
  1052. #>>>
  1053. # Get a UID's ranking
  1054. proc trivia_get_rank { uid } {
  1055. #<<<
  1056. global trivia_db_handle
  1057. putloglev 4 * "trivia_get_rank ($uid)"
  1058. set sql "SELECT user_id FROM users ORDER by user_score DESC"
  1059. set dt [trivia_get_period]
  1060. set sql "select user_id, count(dt) as d from scores where dt > $dt group by user_id order by d desc"
  1061. set pos 0
  1062. trivia_db_handle eval $sql {
  1063. incr pos
  1064. putloglev d * "considering user_id $user_id, score $d"
  1065. if {$user_id == $uid} {
  1066. return $pos
  1067. }
  1068. }
  1069. return 0
  1070. }
  1071. #>>>
  1072. # Get the top 10 users
  1073. proc trivia_get_top10 { } {
  1074. #<<<
  1075. global trivia_db_handle
  1076. set dt [trivia_get_period]
  1077. 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"
  1078. set output ""
  1079. set index 0
  1080. trivia_db_handle eval $sql {
  1081. incr index
  1082. append output "\002$index:\002 "
  1083. append output $u
  1084. append output " ("
  1085. append output $d
  1086. append output ") "
  1087. }
  1088. if {$index == 0} {
  1089. set output "No scores so far, everyone is equal first! \\o/"
  1090. } else {
  1091. set output "The top $index players are... $output"
  1092. }
  1093. return $output
  1094. }
  1095. #>>>
  1096. # Fix a number into text
  1097. proc trivia_ordinal { number } {
  1098. #<<<
  1099. if [regexp {1[0-9]$} $number] {
  1100. return "th"
  1101. }
  1102. set last [string range $number end end]
  1103. if {$last == "1"} {
  1104. return "st"
  1105. }
  1106. if {$last == "2"} {
  1107. return "nd"
  1108. }
  1109. if {$last == "3"} {
  1110. return "rd"
  1111. }
  1112. return "th"
  1113. }
  1114. #>>>
  1115. # Get a score
  1116. proc trivia_score { n { nick "" } } {
  1117. #<<<
  1118. set n [string trim $n]
  1119. putloglev 4 * "trivia_score($n, $nick)"
  1120. if {$n == ""} {
  1121. set ni $nick
  1122. set o "You are"
  1123. } else {
  1124. set ni $n
  1125. set o "$n is"
  1126. }
  1127. set uid [trivia_get_uid $ni]
  1128. set score [trivia_get_score $uid]
  1129. set pos [trivia_get_rank $uid]
  1130. if {$score == 0} {
  1131. return "No score found for $n."
  1132. }
  1133. return "$o ranked $pos[trivia_ordinal $pos] with $score points."
  1134. }
  1135. #>>>
  1136. # Turn a list into a stats list
  1137. proc trivia_list_to_stats { pos user_id l } {
  1138. #<<<
  1139. putloglev 4 * "trivia_list_to_stats ($pos, $user_id, $l)"
  1140. global trivia_c
  1141. set uid [lindex $l 0]
  1142. set nick [lindex $l 1]
  1143. set score [lindex $l 2]
  1144. incr pos
  1145. set result ""
  1146. if {$user_id == $uid} {
  1147. set result "$trivia_c(purple)$trivia_c(bold)"
  1148. }
  1149. append result "$pos[trivia_ordinal $pos]: $nick ($score) "
  1150. if {$user_id == $uid} {
  1151. append result "$trivia_c(off)$trivia_c(bold)"
  1152. }
  1153. return $result
  1154. }
  1155. #>>>
  1156. # Another function to get the nearest users to your score
  1157. proc trivia_near_five2 { uid } {
  1158. #<<<
  1159. global trivia_db_handle trivia_c
  1160. set sql "SELECT user_id, user_name, user_score FROM users ORDER BY user_score DESC"
  1161. set dt [trivia_get_period]
  1162. 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"
  1163. putlog $sql
  1164. set result [list]
  1165. trivia_db_handle eval $sql {
  1166. set line [list]
  1167. lappend line $user_id
  1168. lappend line [trivia_question_inject $user_name]
  1169. lappend line $user_score
  1170. putlog "adding line $line"
  1171. lappend result $line
  1172. }
  1173. putlog "results list: $result"
  1174. set position 0
  1175. foreach item $result {
  1176. if {[lindex $item 0] == $uid} {
  1177. putlog "found at position $position"
  1178. break
  1179. }
  1180. incr position
  1181. }
  1182. set line ""
  1183. if {$position > 2} {
  1184. append line [trivia_list_to_stats [expr $position - 2] $uid [lindex $result [expr $position - 2]]]
  1185. }
  1186. if {$position > 1} {
  1187. append line [trivia_list_to_stats [expr $position - 1] $uid [lindex $result [expr $position - 1]]]
  1188. }
  1189. append line [trivia_list_to_stats $position $uid [lindex $result $position]]
  1190. if {$position < [expr [llength $result] - 1]} {
  1191. append line [trivia_list_to_stats [expr $position + 1] $uid [lindex $result [expr $position + 1]]]
  1192. }
  1193. if {$position < [expr [llength $result] - 2]} {
  1194. append line [trivia_list_to_stats [expr $position + 2] $uid [lindex $result [expr $position + 2]]]
  1195. }
  1196. return $line
  1197. }
  1198. #>>>
  1199. # Get the current leader's UID
  1200. proc trivia_leader { } {
  1201. global trivia_db_handle
  1202. set dt [trivia_get_period]
  1203. set sql "SELECT user_id, count(dt) AS user_score FROM scores WHERE dt > $dt GROUP BY user_id ORDER BY COUNT(dt) DESC LIMIT 1"
  1204. set uid -1
  1205. trivia_db_handle eval $sql {
  1206. set uid $user_id
  1207. }
  1208. return $uid
  1209. }
  1210. # Get some stats from the DB
  1211. proc trivia_stats { } {
  1212. #<<<
  1213. global trivia_db_handle trivia_channel
  1214. set sql "SELECT COUNT(*) AS total FROM questions LEFT JOIN categories USING (cat_id) WHERE categories.cat_enabled = 1;"
  1215. set stat_total_questions [trivia_db_handle eval $sql]
  1216. set sql "SELECT COUNT(*) AS total FROM categories WHERE cat_enabled = 1"
  1217. set stat_total_cats [trivia_db_handle eval $sql]
  1218. putserv "PRIVMSG $trivia_channel :Using\002 $stat_total_questions\002 questions in\002 $stat_total_cats\002 categories."
  1219. }
  1220. #>>>
  1221. # Merge two users
  1222. proc trivia_merge { nick param } {
  1223. #<<<
  1224. global trivia_db_handle
  1225. putloglev 4 * "trivia_merge ($nick, $param)"
  1226. if [regexp {^([^ ]+) (.+)$} $param matches nick1 nick2] {
  1227. set olduid [trivia_get_uid [trivia_sqlite_escape $nick1]]
  1228. set newuid [trivia_get_uid [trivia_sqlite_escape $nick2]]
  1229. if {$olduid == 0} {
  1230. puthelp "PRIVMSG $nick :Can't find user '$nick1'"
  1231. return 0
  1232. }
  1233. if {$newuid == 0} {
  1234. puthelp "PRIVMSG $nick :Can't find user '$nick2'"
  1235. return 0
  1236. }
  1237. puthelp "PRIVMSG $nick :Meging score for $nick1 into score for $nick2"
  1238. set sql "UPDATE scores SET user_id=$newuid WHERE user_id=$olduid"
  1239. trivia_db_handle eval $sql
  1240. set newscore [trivia_get_score $newuid]
  1241. puthelp "PRIVMSG $nick :$nick2 now has $newscore points."
  1242. set sql "DELETE FROM users WHERE user_id = $olduid"
  1243. trivia_db_handle eval $sql
  1244. puthelp "PRIVMSG $nick :User $nick1 has been deleted."
  1245. return 0
  1246. } else {
  1247. puthelp "PRIVMSG $nick :Use: !trivia merge <olduser> <newuser>"
  1248. puthelp "PRIVMSG $nick : olduser's score is merged with newuser's and olduser is deleted."
  1249. }
  1250. }
  1251. #>>>
  1252. # Report a broken question
  1253. proc trivia_report { nick msg } {
  1254. #<<<
  1255. global trivia_channel trivia_db_handle trivia_last_qid trivia_c
  1256. if {$trivia_last_qid == 0} {
  1257. puthelp "PRIVMSG $trivia_channel :Sorry, unable to work out which question to report on :("
  1258. return 0
  1259. }
  1260. set nick [trivia_sqlite_escape $nick]
  1261. set msg [trivia_sqlite_escape $msg]
  1262. set sql "INSERT INTO reports VALUES (null, [clock seconds], '$nick', '$trivia_last_qid', '$msg', 'N')"
  1263. trivia_db_handle eval $sql
  1264. 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)."
  1265. set trivia_last_qid 0
  1266. }
  1267. #>>>
  1268. ### WATCHDOG STUFF <<<
  1269. # Watchdog timer to make sure we're not ruined
  1270. proc trivia_watchdog { } {
  1271. #<<<
  1272. global trivia_last_ts trivia_watchdog_timer trivia_status trivia_channel trivia_delay trivia_speed
  1273. putloglev d * "trivia watchdog: tick"
  1274. # slow down if we've never asked a question or we're stopped
  1275. if {($trivia_last_ts == 0) || ($trivia_status == 0)} {
  1276. putloglev 1 * "trivia is not running, slowing down watchdog"
  1277. set trivia_watchdog_timer [utimer 45 trivia_watchdog]
  1278. return 0
  1279. }
  1280. set current_ts [clock seconds]
  1281. set difference [expr $current_ts - $trivia_last_ts]
  1282. set trivia_limit [expr $trivia_delay + $trivia_speed + 5]
  1283. putloglev 1 * "trivia watchdog: current: $current_ts, last: $trivia_last_ts, difference: $difference, max: $trivia_limit"
  1284. if {$difference > $trivia_limit} {
  1285. if {$trivia_status == 1} {
  1286. putlog "watchdog: trivia is broken"
  1287. putquick "PRIVMSG $trivia_channel :Oops, I think I'm broken. Attempting to recover..."
  1288. set trivia_status 0
  1289. trivia_start
  1290. }
  1291. }
  1292. set timer_interval [expr $trivia_delay * 2]
  1293. set trivia_watchdog_timer [utimer 10 trivia_watchdog]
  1294. return 0
  1295. }
  1296. #>>>
  1297. # Kill the watchdog timeer
  1298. proc trivia_killwatchdog { } {
  1299. #<<<
  1300. global trivia_watchdog_timer
  1301. set alltimers [utimers]
  1302. foreach t $alltimers {
  1303. putloglev 1 * "checking timer $t"
  1304. set t_function [lindex $t 1]
  1305. set t_name [lindex $t 2]
  1306. set t_function [string tolower $t_function]
  1307. if {$t_function == "trivia_watchdog"} {
  1308. putloglev d * "killing timer $t_name"
  1309. killutimer $t_name
  1310. }
  1311. if {$t_function == "trivia_score_rot_timer"} {
  1312. putloglev d * "killing timer $t_name"
  1313. killutimer $t_name
  1314. }
  1315. }
  1316. unset trivia_watchdog_timer
  1317. }
  1318. #>>>
  1319. trivia_killwatchdog
  1320. set trivia_watchdog_timer [utimer 45 trivia_watchdog]
  1321. #>>>
  1322. #<<< SCORE ROTATION STUFF
  1323. #take this week's scores and figure out this week's winners
  1324. proc trivia_score_winners { } {
  1325. #<<<
  1326. global trivia_db_handle trivia_channel
  1327. set winners [list]
  1328. set now [clock seconds]
  1329. set updates [list]
  1330. #knock off a week
  1331. set cutoff [expr $now - 604800]
  1332. set sql "SELECT user_id, COUNT(*) AS score FROM scores WHERE dt > $cutoff GROUP BY user_id ORDER BY score DESC LIMIT 5"
  1333. putloglev d * $sql
  1334. trivia_db_handle eval $sql {
  1335. lappend winners [list $user_id $score]
  1336. }
  1337. set winner_name [list]
  1338. foreach winner $winners {
  1339. set sql "SELECT user_name FROM users WHERE user_id = '[trivia_sqlite_escape [lindex $winner 0]]'"
  1340. putloglev d * $sql
  1341. trivia_db_handle eval $sql {
  1342. lappend winner_name [list $user_name [lindex $winner 0] [lindex $winner 1]]
  1343. }
  1344. }
  1345. putlog $winners
  1346. putlog $winner_name
  1347. putserv "PRIVMSG $trivia_channel :This week's winners are:"
  1348. set position 1
  1349. foreach winner $winner_name {
  1350. #smudge woz ere
  1351. putserv "PRIVMSG $trivia_channel :$position[trivia_ordinal $position] place: [lindex $winner 0] with [lindex $winner 2] points"
  1352. set sql "INSERT INTO winners VALUES ([lindex $winner 1], $now, [expr 6 - $position])"
  1353. putloglev d * $sql
  1354. trivia_db_handle eval $sql
  1355. set sql "UPDATE users SET user_score = user_score + [expr 6 - $position]"
  1356. putloglev d * $sql
  1357. trivia_db_handle eval $sql
  1358. incr position
  1359. }
  1360. set sql "DELETE FROM scores"
  1361. putloglev d * $sql
  1362. trivia_db_handle eval $sql
  1363. }
  1364. #>>>
  1365. #get the timestamp for next cutoff
  1366. proc trivia_score_get_time { } {
  1367. #<<<
  1368. global trivia_score_time
  1369. set trivia_score_time [clock scan "saturday"]
  1370. if {$trivia_score_time < [clock seconds]} {
  1371. set trivia_score_time [clock scan "next saturday"]
  1372. }
  1373. set trivia_score_time [expr $trivia_score_time - ( 5 * 3600)]
  1374. putloglev d * "setting next score rotation to [clock format $trivia_score_time]"
  1375. }
  1376. #>>>
  1377. #figure out how long is left until the score rotation
  1378. proc trivia_score_time_left { } {
  1379. #<<<
  1380. global trivia_score_time
  1381. set now [clock seconds]
  1382. if {$now > $trivia_score_time} {
  1383. #erk
  1384. putloglev d * "trivia_score_time_left: oops!"
  1385. putloglev d * "now: $now, time: $trivia_score_time"
  1386. putloglev d * [clock format $now]
  1387. putloglev d * [clock format $trivia_score_time]
  1388. return
  1389. }
  1390. set diff [expr $trivia_score_time - $now]
  1391. putloglev d * "trivia_score_time_left: difference is $diff seconds"
  1392. if {$diff > 60} {
  1393. return [trivia_time_to_words $diff]
  1394. } else {
  1395. return "less than a minute"
  1396. }
  1397. }
  1398. #>>>
  1399. proc trivia_time_to_words { time } {
  1400. #<<<
  1401. putloglev 2 * "trivia_time_to_words $time"
  1402. set output ""
  1403. if {$time > 86400} {
  1404. set days [expr $time / 86400]
  1405. append output $days
  1406. append output " day"
  1407. if {$days > 1} {
  1408. append output "s"
  1409. }
  1410. set time [expr $time - [expr $days * 86400]]
  1411. append output " "
  1412. }
  1413. if {$time > 3600} {
  1414. set hours [expr $time / 3600]
  1415. append output $hours
  1416. append output " hour"
  1417. if {$hours > 1} {
  1418. append output "s"
  1419. }
  1420. set time [expr $time - [expr $hours * 3600]]
  1421. append output " "
  1422. }
  1423. if {$time > 60} {
  1424. set mins [expr $time / 60]
  1425. append output $mins
  1426. append output " minute"
  1427. if {$mins > 1} {
  1428. append output "s"
  1429. }
  1430. }
  1431. set output [string trim $output]
  1432. putloglev d * "trivia_time_to_words returing $output"
  1433. return $output
  1434. }
  1435. #>>>
  1436. proc trivia_score_rot_timer { } {
  1437. global trivia_score_time trivia_channel trivia_c trivia_asking_question trivia_warned
  1438. putloglev 1 * "score_rot tick"
  1439. utimer 10 trivia_score_rot_timer
  1440. if {[clock seconds] > $trivia_score_time} {
  1441. #end of week!
  1442. putlog "trivia: end of week"
  1443. if {$trivia_asking_question == 1} {
  1444. putlog "trivia: game is running"
  1445. if {$trivia_warned == 0} {
  1446. putlog "trivia: need to warn"
  1447. putquick "PRIVMSG $trivia_channel :$trivia_c(red)### BING BONG ###"
  1448. putquick "PRIVMSG $trivia_channel :I've started so I'll finish."
  1449. set trivia_warned 1
  1450. }
  1451. return 0
  1452. } else {
  1453. putlog "trivia: not running game, ending week now"
  1454. trivia_end_week
  1455. return 0
  1456. }
  1457. }
  1458. set trivia_warned 0
  1459. }
  1460. #>>>
  1461. # handle end of week
  1462. proc trivia_end_week { } {
  1463. global trivia_channel trivia_score_time
  1464. # set next week end
  1465. trivia_score_get_time
  1466. # move scores around
  1467. trivia_score_winners
  1468. }
  1469. # Announce the time remaining
  1470. proc trivia_countdown { } {
  1471. #<<<
  1472. global trivia_channel
  1473. putserv "PRIVMSG $trivia_channel :[trivia_score_time_left] left until end of this game."
  1474. }
  1475. #>>>
  1476. trivia_connect
  1477. trivia_score_get_time
  1478. utimer 10 trivia_score_rot_timer
  1479. putlog {TriviaEngine ENGAGED(*$£&($}
  1480. if {$trivia_must_rehash == 2} {
  1481. putlog "Auto-restarting trivia..."
  1482. trivia_start
  1483. set trivia_must_rehash 0
  1484. }