1
0

games.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import random
  2. from common import font, queries
  3. from commands.common import CommandHelpers as CH
  4. from commands.common import GameHelpers
  5. def do_command(self, connection, event, user, channel):
  6. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  7. # Do nothing if it's not a type of command.
  8. if not cmdtype:
  9. return
  10. # Do nothing if there is no command.
  11. if not command:
  12. return
  13. # The first word of the command sting with arguments, is just the command without arguments.
  14. try:
  15. one = command.split()[0] # Get raw command.
  16. except:
  17. return
  18. # Do noting if the games channel function is off and it's a channel message.
  19. if event.target != connection.get_nickname(): # Command issued to channel.
  20. if not queries.get_channel_setting_games(self, channel.id): # Games are turned off in channel settigns.
  21. return # Do nothing.
  22. # Command list.
  23. if command == 'cmd' or command == 'cmds' or command == 'commands':
  24. if cmdtype == 'cmd':
  25. connection.privmsg(replyto, '%sGames: %s%s%s%s%s%s' % (font.grey, CH.ccc(self, "player"), CH.ccc(self, "players"), CH.ccc(self, "levelup") ,CH.ccc(self, "givecoin"), CH.ccc(self, "8ball"), CH.ccc(self, "dice")[:-2]))
  26. connection.privmsg(replyto, font.grey + "Game help: " + font.blue + self.network.help_character + "level" + font.grey + ", " + font.blue + self.network.help_character + "xp" + font.grey + ", " + font.blue + self.network.help_character + "ap" + font.grey + ", " + font.blue + self.network.help_character + "coin" + font.grey + ", " + font.blue + self.network.help_character + "karma" + font.grey + ".")
  27. elif one == 'player':
  28. if len(command.split()) == 1:
  29. if cmdtype == 'help': # Display help text.
  30. connection.privmsg(replyto, 'Displays a users game statistics. User optional.')
  31. connection.privmsg(replyto, 'Usage: %s%s%s%s %snickname' % (font.blue, self.network.command_character, one, font.reset, font.italic))
  32. return
  33. message = '%sYour player stats: ' % font.grey
  34. player_name = event.source.nick
  35. player = queries.get_user(self, player_name)
  36. elif len(command.split()) == 2:
  37. player_name = command.split()[1]
  38. if cmdtype == 'help':
  39. connection.privmsg(replyto, 'Displays game stats for player: %s%s' % (font.red, player_name))
  40. return
  41. player = queries.get_user(self, player_name)
  42. if not player:
  43. connection.action(replyto, 'does not know of a %s%s%s.' % (font.red, trigger.split()[1], font.reset))
  44. return
  45. if player_name.lower() == connection.get_nickname().lower():
  46. connection.privmsg(replyto, "The game does not play the master.")
  47. return
  48. message = '%sPlayer stats for %s%s%s: ' % (font.grey, font.red, player.name, font.reset)
  49. else: # Too many arguments.
  50. connection.privmsg(replyto, 'Too many arguments, For help type: %s%s%s' % (font.blue, self.network.help_character, one))
  51. return
  52. info = GameHelpers.player_info(self, player)
  53. connection.privmsg(replyto, message + info)
  54. elif one == 'levelup':
  55. level, xp, xp_spent, total_xp, karma, coin, coin_spent, coin_given, ap, ap_spent = GameHelpers.get_info(self, user)
  56. if cmdtype == 'help': #Display help text.
  57. connection.privmsg(replyto, 'Spend %s XP to gain your next level. Levelup multiple levels by adding an amount.' % str(int(level * 2.7)))
  58. connection.privmsg(replyto, 'Example: %s%s%s 3' % (font.blue, self.network.command_character, one))
  59. elif cmdtype == "cmd":
  60. upgrade_count = 1
  61. if xp < int(level * 2.7):
  62. connection.privmsg(replyto, "Insufficient XP, you need at least " + str(int(level * 2.7)) + ".")
  63. return
  64. elif ap < 1:
  65. connection.privmsg(replyto, "Insufficient AP, you need at least 1.")
  66. return
  67. elif len(command.split()) == 2: # Command with one argument.
  68. try:
  69. levels = int(command.split()[1])
  70. except:
  71. connection.privmsg(replyto, "Invalid amount.")
  72. return
  73. if levels < 1:
  74. connection.privmsg(replyto, "Invalid amount.")
  75. return
  76. upgrade_count = levels # Valid amount.
  77. elif len(command.split()) >= 3:
  78. connection.privmsg(replyto, "Too many arguments. For help type " + font.blue + self.network.help_character + "levelup" + font.reset + ".")
  79. return
  80. while xp > int(level * 2.7) and ap > 0 and upgrade_count > 0:
  81. print(upgrade_count)
  82. queries.levelup_user(self, user.id, int((level + 1) * 2.7))
  83. level, xp, xp_spent, total_xp, karma, coin, coin_spent, coin_given, ap, ap_spent = GameHelpers.get_info(self, user)
  84. if upgrade_count < 0:
  85. connection.privmsg(replyto, 'Exhausted XP or AP before levelup was complete. You are only partially levelled up.')
  86. info = GameHelpers.player_info(self, user)
  87. connection.privmsg(replyto, "Your new statistics: " + info)
  88. elif one == 'givecoin':
  89. if cmdtype == 'help': #Display help text.
  90. connection.privmsg(replyto, "Give coins to another player. Amount optional.")
  91. connection.privmsg(replyto, font.grey + "Usage: " + font.blue + self.network.command_character + "givecoin " + font.reset + font.italic + "user amount")
  92. elif cmdtype == "cmd":
  93. if len(command.split()) == 1:
  94. connection.privmsg(replyto, "Insufficient arguments. For help type " + font.blue + self.network.help_character + "givecoin" + font.reset + ".")
  95. return
  96. elif len(command.split()) > 3:
  97. connection.privmsg(replyto, "Too many arguments. For help type " + font.blue + self.network.help_character + "givecoin" + font.reset + ".")
  98. return
  99. elif command.split()[1] == event.source.nick.lower():
  100. connection.privmsg(replyto, "You already have your own coin. For help type " + font.blue + self.network.help_character + "givecoin" + font.reset + ".")
  101. return
  102. elif command.split()[1] == connection.get_nickname() or command.split()[1] == self.network.nickname:
  103. connection.privmsg(replyto, "I don't need that. If I wanted I could take all your coin and there would be nothing for you to do to stop me....")
  104. return
  105. elif len(command.split()) == 3:
  106. try:
  107. if float(command.split()[2]) < 0:
  108. connection.privmsg(replyto, "You clever abuser! The " + font.red + self.network.command_character + font.bold + "give" + font.bold + "coin" + font.reset + " command is not designed for robbing. There will be consequences...")
  109. queries.punish_user(self, user_id, -3, -1)
  110. return
  111. except TypeError:
  112. connection.privmsg(replyto, "Invalid amount. For help type " + font.blue + self.network.help_character + "givecoin" + font.reset + ".")
  113. return
  114. elif event.target == connection.get_nickname(): # Private message
  115. for channel in self.channels:
  116. user_present = False
  117. if command.split()[1] in self.channels[channel].users(): # User is on a channel the bot inhabits.
  118. user_present = True
  119. if not user_present:
  120. connection.privmsg(replyto, font.red + trigger.split()[1] + font.reset + " is not present on any channel I inhabit.")
  121. return
  122. else: # Channel message
  123. if not command.split()[1] in self.channels[event.target].users():
  124. print('!!!!!!!!!!!!! people with weird characters (^[) are not seen as on the channel')
  125. print(command.split()[1])
  126. print(self.channels[event.target].users())
  127. connection.privmsg(replyto, font.red + trigger.split()[1] + font.reset + " is not present on this channel.")
  128. return
  129. level, xp, xpspent, totalxp, karma, coin, coinspent, coingiven, ap, apspent = GameHelpers.get_info(self, user)
  130. receiver = queries.get_user(self, command.split()[1])
  131. if level < 1:
  132. connection.privmsg(replyto, "You need to " + font.blue + self.network.command_character + "levelup " + font.reset + "to be able to give coin.")
  133. return
  134. elif coin < 1:
  135. connection.privmsg(replyto, "You have no coin to give.")
  136. return
  137. elif not receiver:
  138. connection.action(replyto, "does not know of any \"" + font.red + trigger.split()[1] + font.reset + "\".")
  139. return
  140. elif receiver.level <= 0:
  141. connection.privmsg(replyto, font.red + trigger.split()[1] + font.reset + " is not playing the game.")
  142. return
  143. #elif receiver.away == True:
  144. # connection.privmsg(replyto, font.red + trigger.split()[1] + font.reset + "is not here right now.")
  145. # return
  146. elif ap < 1:
  147. connection.privmsg(replyto, "You have no action points.")
  148. return
  149. if len(command.split()) == 2:
  150. queires.cointransfer(self, user_id, command.split()[1])
  151. elif len(command.split()) == 3:
  152. if coin < command.split()[2]:
  153. connection.privmsg(replyto, "You do not have enough coin.")
  154. return
  155. queires.cointransfer(self, sender.id, receiver.id)
  156. info = GameHelpers.player_info(self, sender)
  157. connection.notice(event.source.nick, "Your new statistics: " + info)
  158. elif one == 'players':
  159. if cmdtype == 'help': #Display help text.
  160. connection.privmsg(replyto, 'Display top player rankings.')
  161. elif cmdtype == "cmd":
  162. toplevel = GameHelpers.list_top_players(self, "level")
  163. topxp = GameHelpers.list_top_players(self, "xp_spent")
  164. topcoin = GameHelpers.list_top_players(self, "coin")
  165. if toplevel:
  166. connection.notice(event.source.nick, "Ranking level: " + toplevel)
  167. elif topxp:
  168. connection.notice(event.source.nick, "Ranking experience: " + topxp)
  169. elif topcoin:
  170. connection.notice(event.source.nick, "Ranking wealth: " + topcoin)
  171. else:
  172. connection.privmsg(replyto, "Nobody is playing the game.")
  173. elif command.split()[0] == "level":
  174. if cmdtype == "help": #Display help text.
  175. connection.privmsg(replyto, "For increasing amounts of XP you can use !levelup to gain a level. As your level increases, things become more difficult, while more capabilities and options are attained.")
  176. elif command.split()[0] == "xp":
  177. if cmdtype == "help": #Display help text.
  178. connection.privmsg(replyto, "XP is earned by using IRC and playing the game, in channels with " + font.red + connection.get_nickname() + font.reset + ". Ask any operator in " + font.red + self.network.home_channel + font.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.")
  179. elif command.split()[0] == "ap":
  180. if cmdtype == "help": #Display help text.
  181. connection.privmsg(replyto, "AP is earned by using IRC and playing the game in channels with " + font.red + connection.get_nickname() + font.reset + ". Ask any operator in " + font.red + self.network.home_channel + font.reset + " to add a channel. AP is expended for every action you take in the game.")
  182. elif command.split()[0] == "coin":
  183. if cmdtype == "help": #Display help text.
  184. connection.privmsg(replyto, "Coin is earned when certain events occur, mostly when other players perform actions. Coin is used to buy items and classes. To give a player coin use " + font.blue + self.network.command_character + "givecoin" + font.reset + ". Coin affects karma.")
  185. elif command.split()[0] == "karma":
  186. if cmdtype == "help": #Display help text.
  187. 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.")
  188. elif command.split()[0] == "8ball":
  189. if cmdtype == "help": #Display help text.
  190. connection.privmsg(replyto, "Ask a question of the mighty and illusive 8-Ball.")
  191. connection.privmsg(replyto, font.grey + "Usage: " + font.blue + self.network.command_character + "8ball " + font.reset + font.italic + "question")
  192. elif cmdtype == "cmd":
  193. if len(command.split()) < 2: # Command contains only !8ball.
  194. messages = [
  195. "Don't forget to ask a question...",
  196. "Hey, that's not a question!",
  197. "What would you like to know?",
  198. "You want me to predict nothing?",
  199. "Are you intentionally not asking a question?",
  200. "Ask a question you tease!",
  201. "You do not seem to grasp this, for help type: " + font.blue + self.network.help_character + "8ball",
  202. "You will die alone.",
  203. ]
  204. connection.privmsg(replyto, random.choice(messages))
  205. else:
  206. messages = [
  207. "Yes.",
  208. "No.",
  209. "Affirmative.",
  210. "No way!",
  211. "Negative.",
  212. "Positive.",
  213. "Correct.",
  214. "Incorrect.",
  215. "Likely",
  216. "Unlikely",
  217. "Maybe.",
  218. "Definately!",
  219. "Perhaps?",
  220. "Most indubitably.",
  221. "Does the pope shit in the woods?",
  222. "When hell freezes over.",
  223. "Only between 9 and 5.",
  224. "Only just before you die.",
  225. font.red + font.bold + "ERROR: " + font.bold + "probability failure.",
  226. "Ask again later.",
  227. "I don't know.",
  228. "Unpredictable.",
  229. ]
  230. connection.privmsg(replyto, random.choice(messages))
  231. elif command.split()[0] == "dice":
  232. if cmdtype == "help": #Display help text.
  233. if len(command.split()) != 1:
  234. return
  235. connection.privmsg(replyto, "Rolls multiple dices of chosen type. Specifying type or amount is optional.")
  236. connection.privmsg(replyto, font.grey + "Usage: " + font.blue + self.network.command_character + "dice " + font.reset + font.italic + "amount type")
  237. connection.privmsg(replyto, font.grey + "Example: " + font.blue + self.network.command_character + "dice " + font.reset + font.italic + "2 d10")
  238. connection.privmsg(replyto, font.grey + "Note: " + font.reset + "The dice type is specified as a " + font.italic + "d " + font.reset + "followed by the amount of sides.")
  239. elif cmdtype == "cmd":
  240. arguments = len(command.split()) - 1
  241. if arguments == 0:
  242. connection.privmsg(replyto, str(random.randint(1, 6)) + " & " + str(random.randint(1, 6)) + ".")
  243. elif arguments == 1:
  244. try:
  245. diceamount = int(command.split()[1])
  246. except ValueError: # Argument is not an integer.
  247. if command.split()[1].startswith("d"): # Specific dice type.
  248. try:
  249. dicetype = int(command.split()[1][1:])
  250. except ValueError: # "d" not followd by interger.
  251. connection.privmsg(replyto, "Invalid number or type of dice. For help type: " + font.blue + self.network.help_character + "dice" + font.reset + ".")
  252. return
  253. if dicetype < 1:
  254. connection.action(replyto, "can not create objects with less then one side.")
  255. else:
  256. connection.privmsg(replyto, str(GameHelpers.roll_dice(1, dicetype)[0]))
  257. else: # Argument does not start with "d" and is not an integer.
  258. connection.privmsg(replyto, "Invalid number or type of dice. For help type: " + font.blue + self.network.help_character + "dice" + font.reset + ".")
  259. return
  260. if diceamount < 1:
  261. connection.privmsg(replyto, "Rolling " + font.bold + "no " + font.reset + "dice.")
  262. elif diceamount > 10:
  263. connection.action(replyto, "can not fit that many dice into it's robot hands.")
  264. else:
  265. connection.privmsg(replyto, ", ".join(str(rolls) for rolls in GameHelpers.roll_dice(diceamount, 6)) + ".") # Roll x amount of dice.
  266. elif arguments == 2:
  267. try:
  268. diceamount = int(command.split()[1])
  269. except ValueError: # First argument not an integer.
  270. connection.privmsg(replyto, "Invalid number of dice. For help type: " + font.blue + self.network.help_character + "dice" + font.reset + ".")
  271. return
  272. try:
  273. dicetype = int(command.split()[2][1:])
  274. except ValueError: # Second argument not a dice type.
  275. connection.privmsg(replyto, "Invalid type of dice. For help type: " + font.blue + self.network.help_character + "dice" + font.reset + ".")
  276. return
  277. if diceamount < 1:
  278. connection.privmsg(replyto, "Rolling " + font.bold + "no " + font.reset + "dice.")
  279. elif diceamount > 10:
  280. connection.action(replyto, "can not fit that many dice into it's robot hands.")
  281. elif dicetype < 1:
  282. connection.action(replyto, "can not create objects with less then one side.")
  283. elif dicetype > 9999999999 and diceamount > 1:
  284. connection.action(replyto, "Those dices are so large i can only roll one at a time.")
  285. else:
  286. connection.privmsg(replyto, ", ".join(str(rolls) for rolls in GameHelpers.roll_dice(diceamount, dicetype)) + ".") # Roll x amount of x type dice.
  287. else: # Invalid amount of arguments.
  288. connection.privmsg(replyto, "Too many arguments. For help type: " + font.blue + self.network.help_character + "dice" + font.reset + ".")