4
0

funwar.tcl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # funwar.tcl
  2. #
  3. # This script was created to post fun clan wars/matches for the two multiplayer
  4. # games RTCW and ET in channels periodically and with the commands below. The
  5. # matches are retrieved from tables in postreSQL databases.
  6. #
  7. # Usage:
  8. # !et post ET matches
  9. # !rtcw post RTCW matches
  10. # !funwars post ET and RTCW matches
  11. #
  12. # Enable for a channel with: .chanset #channel +funwar
  13. # Disable for a channel with: .chanset #channel -funwar
  14. #
  15. # See https://github.com/hwipl/eggdrop-scripts for the latest version and
  16. # additional information including the license (MIT).
  17. # tested versions, might run on earlier versions
  18. package require Tcl 8.6
  19. package require eggdrop 1.8.4
  20. # postgres sql
  21. package require tdbc::postgres
  22. namespace eval ::funwar {
  23. # channel flag for enabling/disabling
  24. setudef flag funwar
  25. # sql server address, username and password
  26. variable sqlServer "eggdroppostgres"
  27. variable sqlUser "eggdrop"
  28. variable sqlPassword "eggdropPassword"
  29. # ET database and table on sql server
  30. variable sqlDbnameEt "postgres"
  31. variable sqlTblnameEt "test.funmatch"
  32. # RTCW database and table on sql server
  33. variable sqlDbnameRtcw "postgres"
  34. variable sqlTblnameRtcw "test.funmatch"
  35. # crontab style definition of how often funwars are posted in channels
  36. # format:
  37. # "MIN HOUR DAY MONTH YEAR"
  38. # examples:
  39. # "20,50 * * * *" post at minute 20 and 50 of every hour
  40. # "*/10 * * * *" post every ten minutes
  41. variable autoCron "20,50 * * * *"
  42. # trigger configuration for ET, RTCW and both
  43. variable triggerEt "!et"
  44. variable triggerRtcw "!rtcw"
  45. variable triggerBoth "!funwars"
  46. # output configuration
  47. variable outputHeader "*** Funwars: ***"
  48. variable outputHeader2 "Game: Date/Time: XonX: Clantag: IRC:"
  49. variable outputFooter "*** end of funwars list ***"
  50. }
  51. # post the funwar header in the channel
  52. proc ::funwar::postHeader {chan} {
  53. variable outputHeader
  54. variable outputHeader2
  55. set fmt "%-7s %-20s %-6s %-12s %-12s"
  56. set lineHeader [format $fmt {*}$outputHeader2]
  57. puthelp "PRIVMSG $chan :$outputHeader"
  58. puthelp "PRIVMSG $chan :$lineHeader"
  59. }
  60. # post a funwar line in the channel
  61. proc ::funwar::post {game id date time xonx clantag irc www server org chan } {
  62. set fmt "%-7s %-20s %-6s %-12s %-12s"
  63. set outputBody [format $fmt $game $date/$time $xonx $clantag $irc]
  64. puthelp "PRIVMSG $chan :$outputBody"
  65. }
  66. # post the funwar footer in the channel
  67. proc ::funwar::postFooter {chan} {
  68. variable outputFooter
  69. puthelp "PRIVMSG $chan :$outputFooter"
  70. }
  71. # get rows from sql table
  72. proc ::funwar::sqlGet {server user password dbname tblname order limit} {
  73. # connect to data base
  74. # add -sslmode require if you want to enforce ssl
  75. tdbc::postgres::connection create db -host $server -database $dbname \
  76. -user $user -password $password
  77. # query table in data base
  78. set columns "id,date,time,xonx,clantag,irc,www,server,org"
  79. set top "FETCH FIRST $limit ROWS ONLY"
  80. set queryStr "SELECT $columns FROM $tblname ORDER BY $order DESC $top"
  81. set query [db prepare $queryStr]
  82. # collect all rows and return them
  83. set rows ""
  84. $query foreach row {
  85. lappend rows $row
  86. }
  87. $query close
  88. db close
  89. return $rows
  90. }
  91. # query db, parse everything and post in channels
  92. proc ::funwar::sqlParsedb {server user password dbname tblname order chan \
  93. game limit command} {
  94. # query rows from table in database
  95. set rows [sqlGet $server $user $password $dbname $tblname $order \
  96. $limit]
  97. if {$rows == ""} {
  98. return
  99. }
  100. if { $command != "noheader" } {
  101. # post header in every given channel
  102. foreach channel $chan {
  103. postHeader $channel
  104. }
  105. }
  106. # parse earch row and post them in channels
  107. foreach row $rows {
  108. set id [dict get $row id]
  109. set date [dict get $row date]
  110. set time [dict get $row time]
  111. set xonx [dict get $row xonx]
  112. set clantag [dict get $row clantag]
  113. set irc [dict get $row irc]
  114. set www [dict get $row www]
  115. set server [dict get $row server]
  116. set org [dict get $row org]
  117. foreach channel $chan {
  118. # post row in every given channel
  119. post $game $id $date $time $xonx $clantag $irc $www \
  120. $server $org $channel
  121. }
  122. }
  123. if { $command != "nofooter" } {
  124. # post footer in every given channel
  125. foreach channel $chan {
  126. postFooter $channel
  127. }
  128. }
  129. }
  130. # helper for posting rtcw entries
  131. proc ::funwar::postRtcw {chan limit command} {
  132. variable sqlServer
  133. variable sqlUser
  134. variable sqlPassword
  135. variable sqlDbnameRtcw
  136. variable sqlTblnameRtcw
  137. set order "id"
  138. set game "RTCW"
  139. sqlParsedb $sqlServer $sqlUser $sqlPassword $sqlDbnameRtcw \
  140. $sqlTblnameRtcw $order $chan $game $limit $command
  141. }
  142. # helper for posting et entries
  143. proc ::funwar::postEt {chan limit command} {
  144. variable sqlServer
  145. variable sqlUser
  146. variable sqlPassword
  147. variable sqlDbnameEt
  148. variable sqlTblnameEt
  149. set order "id"
  150. set game "ET"
  151. sqlParsedb $sqlServer $sqlUser $sqlPassword $sqlDbnameEt \
  152. $sqlTblnameEt $order $chan $game $limit $command
  153. }
  154. # handle the !rtcw trigger
  155. proc ::funwar::rtcw { nick host hand chan arg } {
  156. # check channel flag if enabled in this channel
  157. if {![channel get $chan funwar]} {
  158. return 0
  159. }
  160. set limit "10"
  161. set command ""
  162. postRtcw $chan $limit $command
  163. return 1
  164. }
  165. # handle the !et trigger
  166. proc ::funwar::et { nick host hand chan arg } {
  167. # check channel flag if enabled in this channel
  168. if {![channel get $chan funwar]} {
  169. return 0
  170. }
  171. set limit "10"
  172. set command ""
  173. postEt $chan $limit $command
  174. return 1
  175. }
  176. # handle the !funwars trigger
  177. proc ::funwar::both { nick host hand chan arg } {
  178. # check channel flag if enabled in this channel
  179. if {![channel get $chan funwar]} {
  180. return 0
  181. }
  182. set limit "3"
  183. set command "nofooter"
  184. postRtcw $chan $limit $command
  185. set command "noheader"
  186. postEt $chan $limit $command
  187. return 1
  188. }
  189. # handle cron auto posting
  190. proc ::funwar::auto { min hour day month year } {
  191. # determine list of channels to post in based on channel flag
  192. set autoChannels ""
  193. foreach botChan [channels] {
  194. # only use channels the bot is on and have the flag enabled
  195. if {![botonchan $botChan] || ![channel get $botChan funwar]} {
  196. continue
  197. }
  198. lappend autoChannels $botChan
  199. }
  200. if {$autoChannels == ""} {
  201. # no channels to post in
  202. return
  203. }
  204. set limit "3"
  205. set command "nofooter"
  206. postRtcw $autoChannels $limit $command
  207. set command "noheader"
  208. postEt $autoChannels $limit $command
  209. }
  210. namespace eval ::funwar {
  211. bind pub - $triggerEt ::funwar::et
  212. bind pub - $triggerRtcw ::funwar::rtcw
  213. bind pub - $triggerBoth ::funwar::both
  214. bind cron - $autoCron ::funwar::auto
  215. putlog "Loaded funwars.tcl"
  216. }