games.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import random
  2. from commands.common import CommandHelpers as CH
  3. from commands.common import GameHelpers
  4. bold = "\x02"
  5. italic = "\x1D"
  6. underline = "\x1F"
  7. reverse = "\x16" # swap background and foreground colors ("reverse video")
  8. reset = "\x0F"
  9. blue = "\x0302"
  10. green = "\x0303"
  11. red = "\x0304"
  12. grey = "\x0314"
  13. def do_command(self, connection, event):
  14. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  15. # Do nothing if there is no command.
  16. if not command:
  17. return
  18. try:
  19. command.split()[0]
  20. except:
  21. return
  22. # Do noting if the games channel function is off and it's a channel message.
  23. if not self.db.one("SELECT games FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'") and not event.target == connection.get_nickname():
  24. return
  25. if command == "cmd" or command == "cmds" or command == "commands":
  26. if cmdtype == "cmd":
  27. connection.privmsg(replyto, grey + "Games: " + CH.ccc(self, "8ball") + CH.ccc(self, "dice") + CH.ccc(self, "player") + CH.ccc(self, "players") + CH.ccc(self, "levelup") + CH.ccc(self, "givecoin")[:-2] + ".")
  28. connection.privmsg(replyto, grey + "Game help: " + blue + self.helpchar + "level" + grey + ", " + blue + self.helpchar + "xp" + grey + ", " + blue + self.helpchar + "ap" + grey + ", " + blue + self.helpchar + "coin" + grey + ", " + blue + self.helpchar + "karma" + grey + ".")
  29. elif command.split()[0] == "8ball":
  30. if cmdtype == "help": #Display help text.
  31. connection.privmsg(replyto, "Ask a question of the mighty and illusive 8-Ball.")
  32. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "8ball " + reset + italic + "question")
  33. elif cmdtype == "cmd":
  34. if len(command.split()) < 2: # Command contains only !8ball.
  35. messages = [
  36. "Don't forget to ask a question...",
  37. "Hey, that's not a question!",
  38. "What would you like to know?",
  39. "You want me to predict nothing?",
  40. "Are you intentionally not asking a question?",
  41. "Ask a question you tease!",
  42. "You do not seem to grasp this, for help type: " + blue + self.helpchar + "8ball",
  43. "You will die alone.",
  44. ]
  45. connection.privmsg(replyto, random.choice(messages))
  46. else:
  47. messages = [
  48. "Yes.",
  49. "No.",
  50. "Affirmative.",
  51. "No way!",
  52. "Negative.",
  53. "Positive.",
  54. "Correct.",
  55. "Incorrect.",
  56. "Likely",
  57. "Unlikely",
  58. "Maybe.",
  59. "Definately!",
  60. "Perhaps?",
  61. "Most indubitably.",
  62. "Does the pope shit in the woods?",
  63. "When hell freezes over.",
  64. "Only between 9 and 5.",
  65. "Only just before you die.",
  66. red + bold + "ERROR: " + bold + "probability failure.",
  67. "Ask again later.",
  68. "I don't know.",
  69. "Unpredictable.",
  70. ]
  71. connection.privmsg(replyto, random.choice(messages))
  72. elif command.split()[0] == "dice":
  73. if cmdtype == "help": #Display help text.
  74. if len(command.split()) is not 1:
  75. return
  76. connection.privmsg(replyto, "Rolls multiple dices of chosen type. Specifying type or amount is optional.")
  77. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "dice " + reset + italic + "amount type")
  78. connection.privmsg(replyto, grey + "Example: " + blue + self.cmdchar + "dice " + reset + italic + "2 d10")
  79. connection.privmsg(replyto, grey + "Note: " + reset + "The dice type is specified as a " + italic + "d " + reset + "followed by the amount of sides.")
  80. elif cmdtype == "cmd":
  81. arguments = len(command.split()) - 1
  82. if arguments == 0:
  83. connection.privmsg(replyto, str(random.randint(1, 6)) + " & " + str(random.randint(1, 6)) + ".")
  84. elif arguments == 1:
  85. try:
  86. diceamount = int(command.split()[1])
  87. except ValueError: # Argument is not an integer.
  88. if command.split()[1].startswith("d"): # Specific dice type.
  89. try:
  90. dicetype = int(command.split()[1][1:])
  91. except ValueError: # "d" not followd by interger.
  92. connection.privmsg(replyto, "Invalid number or type of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
  93. return
  94. if dicetype < 1:
  95. connection.action(replyto, "can not create objects with less then one side.")
  96. else:
  97. connection.privmsg(replyto, str(GameHelpers.roll_dice(1, dicetype)[0]))
  98. else: # Argument does not start with "d" and is not an integer.
  99. connection.privmsg(replyto, "Invalid number or type of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
  100. return
  101. if diceamount < 1:
  102. connection.privmsg(replyto, "Rolling " + bold + "no " + reset + "dice.")
  103. elif diceamount > 10:
  104. connection.action(replyto, "can not fit that many dice into it's robot hands.")
  105. else:
  106. connection.privmsg(replyto, ", ".join(str(rolls) for rolls in GameHelpers.roll_dice(diceamount, 6)) + ".") # Roll x amount of dice.
  107. elif arguments == 2:
  108. try:
  109. diceamount = int(command.split()[1])
  110. except ValueError: # First argument not an integer.
  111. connection.privmsg(replyto, "Invalid number of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
  112. return
  113. try:
  114. dicetype = int(command.split()[2][1:])
  115. except ValueError: # Second argument not a dice type.
  116. connection.privmsg(replyto, "Invalid type of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
  117. return
  118. if diceamount < 1:
  119. connection.privmsg(replyto, "Rolling " + bold + "no " + reset + "dice.")
  120. elif diceamount > 10:
  121. connection.action(replyto, "can not fit that many dice into it's robot hands.")
  122. elif dicetype < 1:
  123. connection.action(replyto, "can not create objects with less then one side.")
  124. elif dicetype > 9999999999 and diceamount > 1:
  125. connection.action(replyto, "Those dices are so large i can only roll one at a time.")
  126. else:
  127. connection.privmsg(replyto, ", ".join(str(rolls) for rolls in GameHelpers.roll_dice(diceamount, dicetype)) + ".") # Roll x amount of x type dice.
  128. else: # Invalid amount of arguments.
  129. connection.privmsg(replyto, "Too many arguments. For help type: " + blue + self.helpchar + "dice" + reset + ".")
  130. elif command.split()[0] == "player":
  131. if cmdtype == "help": #Display help text.
  132. connection.privmsg(replyto, "Displays a users game info. User optional.")
  133. elif cmdtype == "cmd":
  134. if len(command.split()) == 1:
  135. user = event.source.nick.lower()
  136. message = grey + "Your info. " + reset
  137. elif len(command.split()) == 2:
  138. user = command.split()[1]
  139. if not self.db.one("SELECT id FROM users WHERE LOWER(name)=%s AND network='" + self.network + "'", (user, )):
  140. connection.action(replyto, "does not know of a " + red + trigger.split()[1] + reset + ".")
  141. return
  142. if user == connection.get_nickname().lower():
  143. connection.privmsg(replyto, "The game does not play the master.")
  144. return
  145. if user == event.source.nick.lower():
  146. message = grey + "Your info. " + reset
  147. else:
  148. message = grey + "Info for " + red + trigger.split()[1] + reset + ". "
  149. else:
  150. connection.privmsg(replyto, "Too many arguments, For help type " + blue + self.helpchar + "players " + reset + ".")
  151. return
  152. level, xp, xpspent, totalxp, karma, coin, coinspent, coingiven, apspent = GameHelpers.get_info(self, user)
  153. ap = xp + xpspent
  154. if ap < 0:
  155. ap = 0
  156. connection.privmsg(replyto, message + "Level: " + str(level) + grey + ", " + reset + "XP: " + grey + str(xpspent) + "/" + reset + str(round(xp + xpspent, 4)) + grey + ", " + reset + "AP: " + str(int(ap - float(apspent))) + grey + ", " + reset + "coin: " + str(coin) + grey + "[S " + str(coinspent) + ", G " + str(coingiven) + "], " + reset + "karma: " + str(round(karma, 4)))
  157. elif command.split()[0] == "levelup":
  158. if cmdtype == "help": #Display help text.
  159. connection.privmsg(replyto, "Spend 10 XP per level gained. Gain multiple levels by specifying the amount.")
  160. elif cmdtype == "cmd":
  161. user = event.source.nick.lower()
  162. level, xp, xpspent, totalxp, karma, coin, coinspent, coingiven, apspent = GameHelpers.get_info(self, user)
  163. if len(command.split()) == 1:
  164. print("XP < 10: " + str(xp < 10))
  165. if xp < 10:
  166. connection.privmsg(replyto, "Insuficcient XP, you need at least 10.")
  167. else:
  168. self.db.run("UPDATE users SET level=level+1, xp_spent=xp_spent+10 WHERE LOWER(name)=%s AND network=%s", (user, self.network, ))
  169. self.db.run("UPDATE users SET coin=coin+0.3 WHERE level>0")
  170. elif len(command.split()) == 2:
  171. try:
  172. levels = int(command.split()[1])
  173. except:
  174. connection.privmsg(replyto, "Invalid amount.")
  175. return
  176. print("XP < level * 10: " + str(xp < levels * 10))
  177. if levels < 1:
  178. connection.privmsg(replyto, "Invalid amount.")
  179. elif xp < levels * 10:
  180. connection.privmsg(replyto, "Insuficcient XP, you need at least " + str(levels * 10) + ".")
  181. if xp - apspent < 1:
  182. connection.privmsg(replyto, "Insufficient AP, you need at least 1.")
  183. return
  184. else:
  185. self.db.run("UPDATE users SET level=level+%s, xp_spent=xp_spent+%s, ap_spent=ap_spent-1 WHERE LOWER(name)=LOWER(%s) AND network=%s", (levels, levels * 10, user, self.network, ))
  186. self.db.run("UPDATE users SET coin=coin+%s WHERE level>0", (self.network, ))
  187. else:
  188. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "levelup " + reset + ".")
  189. elif command.split()[0] == "givecoin":
  190. if cmdtype == "help": #Display help text.
  191. connection.privmsg(replyto, "Give coins to another player. Amount optional.")
  192. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "givecoin " + reset + italic + "user amount")
  193. elif cmdtype == "cmd":
  194. if len(command.split()) == 1:
  195. connection.privmsg(replyto, "Insufficient arguments. For help type " + blue + self.helpchar + "givecoin" + reset + ".")
  196. return
  197. elif len(command.split()) > 3:
  198. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "givecoin" + reset + ".")
  199. return
  200. elif command.split()[1] == event.source.nick.lower():
  201. connection.privmsg(replyto, "You already have your own coin. For help type " + blue + self.helpchar + "givecoin" + reset + ".")
  202. return
  203. level, xp, xpspent, karma, coin, coinspent, coingiven, apspent = GameHelpers.get_info(self, event.source.nick)
  204. receivingrecord = self.db.one("SELECT level, away FROM users WHERE LOWER(name)=%s AND network=%s", (command.split()[1], self.network, ))
  205. if level < 1:
  206. connection.privmsg(replyto, "You need to " + blue + self.cmdchar + "levelup " + reset + "to be able to give coin.")
  207. return
  208. elif coin < 1:
  209. connection.privmsg(replyto, "You have no coin to give.")
  210. return
  211. elif receivingrecord:
  212. connection.action(replyto, "does not know of any \"" + red + trigger.split()[1] + reset + "\".")
  213. return
  214. elif receivingrecord[0] == 0:
  215. connection.privmsg(replyto, red + trigger.split()[1] + reset + " is not playing the game.")
  216. return
  217. elif receivingrecord[1] == True:
  218. connection.privmsg(replyto, red + trigger.split()[1] + reset + "is not here right now.")
  219. return
  220. elif apspent >= xp:
  221. connection.privmsg(replyto, "You have no action points, go use IRC some more...")
  222. return
  223. if len(command.split()) == 2:
  224. self.db.run("UPDATE users SET coin=coin-1, coin_spent=coin_spent+1, coin_given=coin_given+1 ap_spent=ap_spent+1 WHERE name=%s AND network=%s", (event.source.nick, self.network, ))
  225. self.db.run("UPDATE users SET coin=coin+1 WHERE LOWER(name)=%s AND network=%s", (command.split()[1], self.network, ))
  226. elif len(command.split()) == 3:
  227. self.db.run("UPDATE users SET coin=coin-%s, coin_spent=coint_spent+%s coin_given=coin_given+%s ap_spent=ap_spent+%s WHERE name=%s AND network=%s", (command.split()[2], command.split()[2], command.split()[2], command.split()[2], event.source.nick, self.network, ))
  228. self.db.run("UPDATE users SET coin=coin+%s WHERE LOWER(name)=%s AND network=%s", (command.split()[2], command.split()[1], self.network))
  229. elif command.split()[0] == "players":
  230. if cmdtype == "help": #Display help text.
  231. connection.privmsg(replyto, "Display top player rankings.")
  232. elif cmdtype == "cmd":
  233. toplevel = GameHelpers.list_top_players(self, "level")
  234. if toplevel:
  235. connection.notice(event.source.nick, "Ranking level: " + toplevel)
  236. topxp = GameHelpers.list_top_players(self, "xp_spent")
  237. if topxp:
  238. connection.notice(event.source.nick, "Ranking experience: " + topxp)
  239. topcoin = GameHelpers.list_top_players(self, "coin")
  240. if topcoin:
  241. connection.notice(event.source.nick, "Ranking wealth: " + topcoin)
  242. if not toplevel and not topxp and not topcoin:
  243. connection.privmsg(replyto, "Nobody is playing the game.")
  244. elif command.split()[0] == "level":
  245. if cmdtype == "help": #Display help text.
  246. connection.privmsg(replyto, "For 10 XP you can use !levelup to gain a level. As your level increases, things become more difficult, while more capabilities and options are unlocked.")
  247. elif command.split()[0] == "xp":
  248. if cmdtype == "help": #Display help text.
  249. connection.privmsg(replyto, "XP is earned by using IRC and playing the game, in channels with " + red + connection.get_nickname() + reset + ". Ask any operator in " + red + self.homechannel + reset + " to add a channel. XP is used to level up, advance classes, as a limit and as a multiplier. Once XP is expended it keeps counting towards your total.")
  250. elif command.split()[0] == "ap":
  251. if cmdtype == "help": #Display help text.
  252. connection.privmsg(replyto, "XP is earned by using IRC and playing the game in channels with " + red + connection.get_nickname() + reset + ". Ask any operator in " + red + self.homechannel + reset + " to add a channel. AP is expended for every action you take in the game.")
  253. elif command.split()[0] == "coin":
  254. if cmdtype == "help": #Display help text.
  255. connection.privmsg(replyto, "Coin is earned when certain events occur, for instance when a player expends XP. Coin is used to buy items and classes. To give a player coin use " + blue + self.cmdchar + "givecoin" + reset + ". Coin affects karma.")
  256. elif command.split()[0] == "karma":
  257. if cmdtype == "help": #Display help text.
  258. connection.privmsg(replyto, "Karma is a secret formula, based upon on your IRC events and how you play the game. It does not increase like XP. Some events, skills and items are karma based.")
  259. # elif command.split()[0] == "classup":
  260. # if cmdtype == "help": #Display help text.
  261. # connection.privmsg(replyto, "Spend 10 XP to gain a class in your current level. List available classes with " + blue + self.helpchar + "classup available " + reset + ".")
  262. # connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "classup " + reset + italic + "class")
  263. # elif cmdtype == "cmd":
  264. #
  265. # if len(command.split()) == 1:
  266. # connection.privmsg(replyto, "Insufficient arguments. For help type " + blue + self.helpchar + "classup" + reset + ".")
  267. # if len(command.split()) == 2:
  268. # if command.split()[1] == "available":
  269. # level, xp, xpspent, karma = GameHelpers.get_info(self, event.source.nick)
  270. # if level == 0:
  271. # connection.privmsg(replyto, "There are no level 0 classes.")
  272. # if level == 1:
  273. # connection.privmsg(replyto, "Level 1 classes: Maggot, nubcake, ")
  274. # if level == 2:
  275. # connection.privmsg(replyto, "Level 2 classes: Retard, ")
  276. # if level == 3:
  277. # connection.privmsg(replyto, "Level 3 classes: ")
  278. # if level == 4:
  279. # connection.privmsg(replyto, "Level 3 classes: ")
  280. # if level == 5:
  281. # connection.privmsg(replyto, "Level 4 classes: ")
  282. # else:
  283. # else:
  284. # connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "classup" + reset + ".")