Changeset 23 for TriviaEngine/TriviaEngine-sqlite.tcl
- Timestamp:
- 10/15/06 18:25:40 (6 years ago)
- File:
-
- 1 edited
-
TriviaEngine/TriviaEngine-sqlite.tcl (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
-
TriviaEngine/TriviaEngine-sqlite.tcl
r22 r23 16 16 # allow new categories to be created? 17 17 set trivia_allow_new_cats 0 18 19 # time before end of round to start counting down 20 set trivia_time_left_warning 86400 18 21 19 22 # hold the current question info <<< … … 51 54 set trivia_watchdog_timer "" 52 55 set trivia_last_ts 0 56 set trivia_score_time 0 57 set trivia_debug_mode 0 58 set trivia_warned 0 59 set trivia_asking_question 0 60 set trivia_current_leader -1 53 61 54 62 #colours <<< … … 147 155 #>>> 148 156 157 #<<< debug 158 if [regexp -nocase {^debug (.+)} $cmd matches func] { 159 global trivia_debug_mode 160 161 if {$trivia_debug_mode == 0} { 162 putquick "PRIVMSG $nick :Debug mode is disabled. Cannot use debug commands." 163 return 0 164 } 165 166 if {$func == "endweek"} { 167 putquick "PRIVMSG $nick :Ending this week in 10 seconds..." 168 global trivia_score_time 169 set trivia_score_time [expr [clock seconds] + 10] 170 return 0 171 } 172 } 173 #>>> 174 175 149 176 #<<< handle reports 150 177 if [regexp -nocase {^report (help|list|fix|view|done)?( .+)?} $cmd matches func arg] { … … 309 336 global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status 310 337 global trivia_timer trivia_delay trivia_db_handle trivia_unanswered trivia_run_qty trivia_run_last trivia_run_nick trivia_c 338 global trivia_score_time trivia_time_left_warning trivia_asking_question trivia_current_leader 311 339 312 340 if {$trivia_status != 1} { … … 319 347 set trivia_q_answer "" 320 348 set newuser 0 349 set trivia_asking_question 0 321 350 322 351 set uid [trivia_get_uid $nick] … … 329 358 putlog "$nick has uid $uid" 330 359 360 set old_leader [trivia_leader] 361 331 362 trivia_incr_score $uid 332 363 … … 335 366 putquick "PRIVMSG $trivia_channel :Welcome to our newest player, $trivia_c(purple)$nick$trivia_c(off) :)" 336 367 } 337 putquick "PRIVMSG $trivia_channel :rank0rs: [trivia_near_five2 $uid]" 368 putquick "PRIVMSG $trivia_channel :Rankings: [trivia_near_five2 $uid]" 369 370 set leader [trivia_leader] 371 372 if {($leader != $old_leader) && ($leader != -1) && ($old_leader != -1)} { 373 putquick "PRIVMSG $trivia_channel :$trivia_c(purple) $nick $trivia_c(off) has taken the lead!" 374 } 338 375 339 376 #set score [trivia_get_score $uid] … … 361 398 } 362 399 400 if {$trivia_score_time <= [clock seconds]} { 401 trivia_end_week 402 } else { 403 if {[expr $trivia_score_time - [clock seconds]] < $trivia_time_left_warning} { 404 putserv "PRIVMSG $trivia_channel :[trivia_score_time_left] until the end of this game!" 405 } 406 } 363 407 364 408 trivia_check_rehash … … 385 429 return "is on a winning spree!" 386 430 } 431 4 { 432 puthelp "PRIVMSG $trivia_channel :QUAD DAMAGE!" 433 return "is on a roll ..." 434 } 387 435 5 { 388 436 return "is on a rampage!" … … 407 455 # Escape stuff for SQL 408 456 proc trivia_sqlite_escape { text } { 457 #<<< 409 458 return [string map {' ''} $text] 410 459 } 460 #>>> 411 461 412 462 # Get someone's user ID from their nick … … 460 510 proc trivia_get_userinfo { user_id } { 461 511 #<<< 462 global trivia_db_handle 512 global trivia_db_handle trivia_channel 463 513 464 514 putloglev 4 * "trivia_get_userinfo ($user_id)" 465 466 set sql "SELECT user_score, user_last, user_reg FROM users WHERE user_id = '$user_id'" 515 set donestats 0 516 517 set sql "SELECT user_name, user_score, user_last, user_reg FROM users WHERE user_id = '$user_id'" 518 467 519 trivia_db_handle eval $sql { 468 520 if {$user_last == ""} { … … 478 530 } 479 531 480 return "$user_score|$last|$reg" 481 } 482 return "No stats" 532 putserv "PRIVMSG $trivia_channel :Trivia stats for $user_name:" 533 putserv "PRIVMSG $trivia_channel : Current score: $user_score" 534 putserv "PRIVMSG $trivia_channel : First seen: $reg" 535 putserv "PRIVMSG $trivia_channel : Last scored: $last" 536 set donestats 1 537 } 538 539 set sql "SELECT COUNT(dt) AS score FROM scores WHERE dt > [trivia_get_period] AND user_id = '$user_id'" 540 if {$donestats} { 541 trivia_db_handle eval $sql { 542 putserv "PRIVMSG $trivia_channel :Points this week: $score" 543 putserv "PRIVMSG $trivia_channel : Weekly Ranking: [trivia_get_rank $user_id]" 544 } 545 } else { 546 putserv "PRIVMSG $trivia_channel :No stats found" 547 } 483 548 } 484 549 #>>> … … 491 556 set uid [trivia_get_uid $user_name] 492 557 if {$uid == 0} { 493 putserv "PRIVMSG $trivia_channel :Unknown user '$user_name' "558 putserv "PRIVMSG $trivia_channel :Unknown user '$user_name' (is the case right?)" 494 559 return 495 560 } 496 set stats [trivia_get_userinfo $uid] 497 if {$stats == "No stats"} { 498 putserv "PRIVMSG $trivia_channel :No stats available." 499 return 500 } 501 502 set stats2 [split $stats "|"] 503 putserv "PRIVMSG $trivia_channel :Trivia stats for $user_name:" 504 putserv "PRIVMSG $trivia_channel : Current score: [lindex $stats2 0]" 505 putserv "PRIVMSG $trivia_channel : Ranking: [trivia_get_rank $uid]" 506 putserv "PRIVMSG $trivia_channel : First seen: [lindex $stats2 2]" 507 putserv "PRIVMSG $trivia_channel : Last scored: [lindex $stats2 1]" 561 562 trivia_get_userinfo $uid 508 563 } 509 564 #>>> … … 516 571 putloglev 4 * "trivia_incr_score ($id, $howmuch)" 517 572 518 set sql "UPDATE users SET user_last = [clock seconds] WHERE user_id = '$id'"573 set sql "UPDATE users SET user_last = [clock seconds], user_points = user_points + 1 WHERE user_id = '$id'" 519 574 trivia_db_handle eval $sql 520 575 … … 532 587 533 588 set nick [trivia_sqlite_escape $nick] 534 set sql "INSERT INTO users VALUES (null, '$nick', '', 0, [clock seconds], [clock seconds] )"589 set sql "INSERT INTO users VALUES (null, '$nick', '', 0, [clock seconds], [clock seconds], 0)" 535 590 trivia_db_handle eval $sql 536 591 … … 586 641 } 587 642 643 if {$param == "countdown"} { 644 trivia_countdown 645 return 0 646 } 647 588 648 if {![matchattr $handle |$trivia_flag $channel]} { 589 putserv "PRIVMSG $nick :use: !trivia \[score|top10|start \]"649 putserv "PRIVMSG $nick :use: !trivia \[score|top10|start|report\]" 590 650 return 0 591 651 } … … 695 755 proc trivia_make_hint { hint answer } { 696 756 #<<< 757 global trivia_debug_mode 758 697 759 set hint [string toupper $hint] 698 760 set answer [string toupper $answer] … … 772 834 } 773 835 set final_hint [string trim $final_hint] 836 if {$trivia_debug_mode == 1} { 837 return $answer 838 } 839 774 840 return $final_hint 775 841 } … … 813 879 #<<< 814 880 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 881 global trivia_asking_question 815 882 816 883 if {$trivia_status != 1} { … … 842 909 843 910 set trivia_last_qid $trivia_q_id 911 912 set trivia_asking_question 1 844 913 845 914 trivia_round … … 983 1052 global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status 984 1053 global trivia_timer trivia_delay trivia_db_handle trivia_unanswered 985 global trivia_run_last trivia_run_nick trivia_run_qty trivia_c 1054 global trivia_run_last trivia_run_nick trivia_run_qty trivia_c trivia_score_time trivia_asking_question trivia_time_left_warning 986 1055 987 1056 if {$trivia_status != 1} { … … 989 1058 return 0 990 1059 } 1060 1061 set trivia_asking_question 0 991 1062 992 1063 set trivia_q_answer [string toupper $trivia_q_answer] … … 1007 1078 1008 1079 if {$trivia_unanswered > 3} { 1009 putserv "PRIVMSG $trivia_channel :Three unanswered in a row, stopping the game." 1080 putserv "PRIVMSG $trivia_channel :Four unanswered in a row, stopping the game." 1081 putserv "PRIVMSG $trivia_channel :You can restart it with $trivia_c(purple)!t start" 1010 1082 set trivia_status 0 1011 1083 } else { 1012 1084 set trivia_timer [utimer $trivia_delay trivia_start_round] 1085 if {$trivia_score_time <= [clock seconds]} { 1086 trivia_end_week 1087 } else { 1088 if {[expr $trivia_score_time - [clock seconds]] < $trivia_time_left_warning} { 1089 putserv "PRIVMSG $trivia_channel :[trivia_score_time_left] until the end of this game!" 1090 } 1091 } 1013 1092 trivia_check_rehash 1014 1093 } … … 1020 1099 #<<< 1021 1100 global trivia_q_id trivia_q_cat trivia_q_question trivia_q_answer trivia_q_hint trivia_q_attempts trivia_channel trivia_status 1022 global trivia_timer trivia_delay trivia_db_handle trivia_unanswered 1101 global trivia_timer trivia_delay trivia_db_handle trivia_unanswered trivia_score_time 1023 1102 1024 1103 if {$trivia_status != 1} { … … 1039 1118 putserv "PRIVMSG $trivia_channel :Skipping this question by $nick's request." 1040 1119 set trivia_timer [utimer $trivia_delay trivia_start_round] 1120 if {$trivia_score_time <= [clock seconds]} { 1121 trivia_end_week 1122 } 1041 1123 } 1042 1124 #>>> … … 1076 1158 #trivia_stats 1077 1159 1160 putquick "PRIVMSG $trivia_channel :[trivia_score_time_left] left until end of this game!" 1161 1162 #try to find last week's winner 1163 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" 1164 putloglev d * $sql 1165 trivia_db_handle eval $sql { 1166 putquick "PRIVMSG $trivia_channel :Last week's winner was$trivia_c(purple) [string toupper $user_name]$trivia_c(off)!" 1167 } 1168 1078 1169 set trivia_unanswered 0 1079 1170 set trivia_run_last 0 … … 1091 1182 1092 1183 if {$trivia_status == 1} { 1184 clearqueue server 1093 1185 putserv "PRIVMSG $trivia_channel :Trivia game stopped." 1094 1186 set trivia_status 0 … … 1165 1257 } 1166 1258 1167 set output "The top $index players are... $output" 1259 if {$index == 0} { 1260 set output "No scores so far, everyone is equal first! \\o/" 1261 } else { 1262 set output "The top $index players are... $output" 1263 } 1168 1264 return $output 1169 1265 } … … 1295 1391 } 1296 1392 #>>> 1393 1394 # Get the current leader's UID 1395 proc trivia_leader { } { 1396 global trivia_db_handle 1397 1398 set dt [trivia_get_period] 1399 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" 1400 set uid -1 1401 trivia_db_handle eval $sql { 1402 set uid $user_id 1403 } 1404 1405 return $uid 1406 } 1297 1407 1298 1408 # Get some stats from the DB … … 1423 1533 killutimer $t_name 1424 1534 } 1535 1536 if {$t_function == "trivia_score_rot_timer"} { 1537 putloglev d * "killing timer $t_name" 1538 killutimer $t_name 1539 } 1425 1540 } 1426 1541 … … 1433 1548 #>>> 1434 1549 1550 #<<< SCORE ROTATION STUFF 1551 1552 #take this week's scores and figure out this week's winners 1553 proc trivia_score_winners { } { 1554 #<<< 1555 global trivia_db_handle trivia_channel 1556 1557 set winners [list] 1558 1559 set now [clock seconds] 1560 1561 set updates [list] 1562 1563 #knock off a week 1564 set cutoff [expr $now - 604800] 1565 1566 set sql "SELECT user_id, COUNT(*) AS score FROM scores WHERE dt > $cutoff GROUP BY user_id ORDER BY score DESC LIMIT 5" 1567 putloglev d * $sql 1568 1569 trivia_db_handle eval $sql { 1570 lappend winners [list $user_id $score] 1571 } 1572 1573 set winner_name [list] 1574 1575 foreach winner $winners { 1576 set sql "SELECT user_name FROM users WHERE user_id = '[trivia_sqlite_escape [lindex $winner 0]]'" 1577 putloglev d * $sql 1578 trivia_db_handle eval $sql { 1579 lappend winner_name [list $user_name [lindex $winner 0] [lindex $winner 1]] 1580 } 1581 } 1582 1583 putlog $winners 1584 putlog $winner_name 1585 1586 putserv "PRIVMSG $trivia_channel :This week's winners are:" 1587 set position 1 1588 foreach winner $winner_name { 1589 #smudge woz ere 1590 putserv "PRIVMSG $trivia_channel :$position[trivia_ordinal $position] place: [lindex $winner 0] with [lindex $winner 2] points" 1591 set sql "INSERT INTO winners VALUES ([lindex $winner 1], $now, [expr 6 - $position])" 1592 putloglev d * $sql 1593 trivia_db_handle eval $sql 1594 1595 set sql "UPDATE users SET user_score = user_score + [expr 6 - $position]" 1596 putloglev d * $sql 1597 trivia_db_handle eval $sql 1598 1599 incr position 1600 } 1601 1602 set sql "DELETE FROM scores" 1603 putloglev d * $sql 1604 trivia_db_handle eval $sql 1605 } 1606 1607 #>>> 1608 1609 #get the timestamp for next cutoff 1610 proc trivia_score_get_time { } { 1611 #<<< 1612 global trivia_score_time 1613 1614 set trivia_score_time [clock scan "saturday"] 1615 if {$trivia_score_time < [clock seconds]} { 1616 set trivia_score_time [clock scan "next saturday"] 1617 } 1618 putloglev d * "setting next score rotation to [clock format $trivia_score_time]" 1619 } 1620 #>>> 1621 1622 #figure out how long is left until the score rotation 1623 proc trivia_score_time_left { } { 1624 #<<< 1625 global trivia_score_time 1626 1627 set now [clock seconds] 1628 if {$now > $trivia_score_time} { 1629 #erk 1630 putloglev d * "trivia_score_time_left: oops!" 1631 putloglev d * "now: $now, time: $trivia_score_time" 1632 putloglev d * [clock format $now] 1633 putloglev d * [clock format $trivia_score_time] 1634 return 1635 } 1636 1637 set diff [expr $trivia_score_time - $now] 1638 putloglev d * "trivia_score_time_left: difference is $diff seconds" 1639 1640 if {$diff > 60} { 1641 return [trivia_time_to_words $diff] 1642 } else { 1643 return "less than a minute" 1644 } 1645 } 1646 #>>> 1647 1648 proc trivia_time_to_words { time } { 1649 #<<< 1650 putloglev 2 * "trivia_time_to_words $time" 1651 set output "" 1652 1653 if {$time > 86400} { 1654 set days [expr $time / 86400] 1655 append output $days 1656 append output " day" 1657 if {$days > 1} { 1658 append output "s" 1659 } 1660 set time [expr $time - [expr $days * 86400]] 1661 append output " " 1662 } 1663 1664 if {$time > 3600} { 1665 set hours [expr $time / 3600] 1666 append output $hours 1667 append output " hour" 1668 if {$hours > 1} { 1669 append output "s" 1670 } 1671 set time [expr $time - [expr $hours * 3600]] 1672 append output " " 1673 } 1674 1675 if {$time > 60} { 1676 set mins [expr $time / 60] 1677 append output $mins 1678 append output " minute" 1679 if {$mins > 1} { 1680 append output "s" 1681 } 1682 } 1683 1684 set output [string trim $output] 1685 putloglev d * "trivia_time_to_words returing $output" 1686 return $output 1687 } 1688 #>>> 1689 1690 proc trivia_score_rot_timer { } { 1691 global trivia_score_time trivia_channel trivia_c trivia_asking_question trivia_warned 1692 1693 putloglev 1 * "score_rot tick" 1694 utimer 10 trivia_score_rot_timer 1695 1696 if {[clock seconds] > $trivia_score_time} { 1697 #end of week! 1698 1699 putlog "trivia: end of week" 1700 1701 if {$trivia_asking_question == 1} { 1702 putlog "trivia: game is running" 1703 if {$trivia_warned == 0} { 1704 putlog "trivia: need to warn" 1705 putquick "PRIVMSG $trivia_channel :$trivia_c(red)### BING BONG ###" 1706 putquick "PRIVMSG $trivia_channel :I've started so I'll finish." 1707 set trivia_warned 1 1708 } 1709 return 0 1710 } else { 1711 putlog "trivia: not running game, ending week now" 1712 trivia_end_week 1713 return 0 1714 } 1715 } 1716 set trivia_warned 0 1717 } 1718 #>>> 1719 1720 # handle end of week 1721 proc trivia_end_week { } { 1722 global trivia_channel trivia_score_time 1723 1724 # set next week end 1725 trivia_score_get_time 1726 1727 # move scores around 1728 trivia_score_winners 1729 } 1730 1731 # Announce the time remaining 1732 proc trivia_countdown { } { 1733 #<<< 1734 global trivia_channel 1735 1736 putserv "PRIVMSG $trivia_channel :[trivia_score_time_left] left until end of this game." 1737 } 1738 #>>> 1739 1435 1740 trivia_connect 1741 trivia_score_get_time 1742 utimer 10 trivia_score_rot_timer 1436 1743 1437 1744 putlog {TriviaEngine ENGAGED(*$£&($}
Note: See TracChangeset
for help on using the changeset viewer.
