Advertisement
austinh115

[RUBY] GoFish Game (Works as far as I remember)

Sep 25th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.85 KB | None | 0 0
  1. ===COMMAND===
  2.             when "gofish", "gf"
  3.                 user = @users[packet["u"]]
  4.                 sub, message = message == nil ? ["", nil] : message.split(' ', 2)
  5.                 @games["GoFish::Game"] ||= GoFish::Game.new self
  6.                
  7.                 case sub.downcase
  8.                     when "join", "j"
  9.                         @games["GoFish::Game"].running && sendMessage("A game of go fish is currently in progress. Please wait until it's done to join.") && return
  10.                         if @games["GoFish::Game"].players.include?(user)
  11.                             sendMessage("You are already in the game of GoFish. To start it, use !#{cmd} start.") && return
  12.                         end
  13.                         user.getAttachment() != nil && sendMessage("You are already in a game of #{user.getAttachment().class.name}. Please leave it before joining Go Fish.") && return
  14.                         user.setAttachment(@games["GoFish::Game"]) && sendMessage("You have joined the game of #{user.getAttachment().class.name}.")
  15.                         user.getAttachment().join user
  16.                     when "start", "s"
  17.                         user.getAttachment == nil && sendMessage("You are not currently playing a game of Go Fish") && return
  18.                         user.getAttachment().players.length < 2 && sendMessage("There must be at least two players to start a game of Go Fish.") && return
  19.                         sendMessage("The game of GoFish has been started.")
  20.                         user.getAttachment().start
  21.                     when "end", "e"
  22.                         user.getAttachment == nil && sendMessage("You are not currently playing a game of Go Fish") && return
  23.                         @games["GoFish::Game"] = nil
  24.                         user.setAttachment(nil)
  25.                         sendMessage("The game of GoFish has been ended.")
  26.                     when "load"
  27.                         return
  28.                         @games["GoFish::Game"] = GoFish::Game.new self
  29.                         @games["GoFish::Game"].loadFromState YAML::load_file('./.data/GoFish.data')
  30.                         @games["GoFish::Game"].players.each do |u|
  31.                             if @users.has_key?(u.id.to_s)
  32.                                 @users[u.id.to_s].setAttachment(@games["GoFish::Game"])
  33.                             else
  34.                                 @games["GoFish::Game"].players.delete(u)
  35.                             end
  36.                         end
  37.                         if @games["GoFish::Game"].players.length < 2
  38.                             sendMessage("There aren't enough players to continue the game so it has been forcefully ended.")
  39.                             @games["GoFish::Game"].handlewin()
  40.                             @games["GoFish::Game"] = nil
  41.                             @users.each do |user|
  42.                                 if (att = user.getAttachment()) != nil
  43.                                     puts att.class.name
  44.                                     if att.class.name == "GoFish::Game"
  45.                                         user.setAttachment(nil)
  46.                                     end
  47.                                 end
  48.                             end
  49.                         end
  50.                     else
  51.                         (att = user.getAttachment) == nil && sendMessage("You are not currently playing a game of Go Fish. Use !#{cmd} join to join one.") && return
  52.                         att.handlecmd sub, message == nil ? [] : message.split(' '), user
  53.                         if !att.running
  54.                             @games["GoFish::Game"].players.each do |user|
  55.                                 if (att = user.getAttachment()) != nil
  56.                                     user.setAttachment(nil)
  57.                                 end
  58.                             end
  59.                             @games["GoFish::Game"] = nil
  60.                         else
  61.                             #File.open('./.data/GoFish.data', 'w+') {|f| f.write(YAML::dump(@games["GoFish::Game"].getState())) }
  62.                         end
  63.                 end
  64.                 #if @games["GoFish::Game"] == nil
  65.                 #   File.delete('./.data/GoFish.data')
  66.                 #end
  67. ===CLASS FILE===
  68. module GoFish
  69.  
  70.     class Game
  71.    
  72.         attr_accessor :players, :running
  73.  
  74.         @@cardletters       = ["", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
  75.         @@cardletterlookup  = {"A" => 1, "J" => 11, "Q" => 12, "K" => 13}
  76.         @@cardstrs          = ["", "ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king"]
  77.         @@numstrs           = ["zero", "one", "two", "three", "four"]
  78.    
  79.         def initialize bot
  80.             @players    = []
  81.             @bot        = bot
  82.             @deck       = ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] * 4).shuffle
  83.             @hands      = []
  84.             @pairs      = []
  85.             @currplayer = 0
  86.             @running = false
  87.         end
  88.        
  89.         def getState
  90.             [@players, @deck, @hands, @pairs, @currplayer, @running]
  91.         end
  92.        
  93.         def loadFromState state
  94.             @players, @deck, @hands, @pairs, @currplayer, @running = state
  95.         end
  96.        
  97.         def join user
  98.             @players.push(user)
  99.             @hands.push([])
  100.             @pairs.push([])
  101.         end
  102.        
  103.         def start
  104.             @running = true
  105.             @numplayers = @players.length
  106.             numcards = @numplayers < 4 ? 9 : 7
  107.             numcards.times do |x|
  108.                 @numplayers.times do |y|
  109.                     @hands[y].push(@deck.pop())
  110.                 end
  111.             end
  112.             @numplayers.times do |x|
  113.                 @hands[x].sort!
  114.             end
  115.             doturn(0)
  116.         end
  117.        
  118.         def stop
  119.             handlewin
  120.         end
  121.        
  122.         def doturn player
  123.             oldplayer = player
  124.             while true
  125.                 if @hands[player].length == 0
  126.                     out = "You are out of cards"
  127.                     begin
  128.                         card = @deck.pop()
  129.                         card == nil && raise("Empty deck")
  130.                         @hands[player].push(card)
  131.                         out += ", so you picked up a " + card2str(card)
  132.                     rescue
  133.                         player += 1
  134.                         if player == @players.length
  135.                             player = 0
  136.                         end
  137.                         if oldplayer == player
  138.                             handlewin()
  139.                             return true
  140.                         end
  141.                         redo
  142.                     end
  143.                     @bot.sendPM @players[player].id, out+"!"
  144.                     break
  145.                 else
  146.                     break
  147.                 end
  148.             end
  149.             @currplayer = player
  150.             nick = @players[player].regname
  151.             @bot.sendMessage(nick+", your turn! Use !GoFish ask <player> <card>, to ask somebody for cards! There are #{@deck.length} cards left in the deck.")
  152.             cards = []
  153.             @pairs[player].each do |card|
  154.                 cards << @@cardletters[card]
  155.             end
  156.             cards.length > 0 && @bot.sendMessage(nick+"'s pairs: "+ cards.join(", "))
  157.             cards = []
  158.             @hands[player].each do |card|
  159.                 puts "[#{card.to_s}]"
  160.                 cards << @@cardletters[card]
  161.             end
  162.             @bot.sendPM @players[player].id, "Cards: "+ cards.join(", ")
  163.         end
  164.        
  165.         def handlewin
  166.             @bot.sendMessage("Everybody is out of cards!")
  167.             pairlens = []
  168.             @pairs.each_with_index do |x, index|
  169.                 pairlens << [x.length, index]
  170.             end
  171.             pairlens.sort!
  172.             pairlens.reverse!
  173.             @bot.sendMessage("#{@players[pairlens[0][1]].regname} is the winner with #{pairlens[0][0]} pairs! Congratulations!")
  174.             @running = false
  175.         end
  176.        
  177.         def card2str card, plural=false
  178.             name = @@cardstrs[card]
  179.             if plural == true
  180.                 if card == 6
  181.                     name += "es"
  182.                 else
  183.                     name += "s"
  184.                 end
  185.             end
  186.             return name
  187.         end
  188.        
  189.         def handlecmd cmd, args, user
  190.             cmd = cmd.downcase
  191.             nick = user.regname
  192.             id = user.id
  193.             if @players[@currplayer] != user
  194.                 @bot.sendPM(id, "Error! It's not your turn!")
  195.                 return
  196.             end
  197.             if cmd == "a" || cmd == "ask"
  198.                 if args.length < 2
  199.                     @bot.sendPM(id, "Please specify a card to ask for. Usage: !GoFish ask <palyer> <card>")
  200.                     return
  201.                 end
  202.                 askee = args[0]
  203.                 card = args[1].upcase
  204.                 if card.is_i?
  205.                     card = card.to_i
  206.                 else
  207.                     begin
  208.                         card = @@cardletterlookup[card]
  209.                     rescue
  210.                         @bot.sendPM(id, "Invalid card!")
  211.                         return
  212.                     end
  213.                 end
  214.                 found = false
  215.                 for x in @players
  216.                     if askee == x.id || askee.downcase == x.regname.downcase
  217.                         askeehm = x
  218.                         found = true
  219.                         break
  220.                     end
  221.                 end
  222.                 if found == false
  223.                     @bot.sendPM(id, "No such player!")
  224.                     return
  225.                 end
  226.                 if askee.downcase == nick.downcase
  227.                     @bot.sendPM(id, "You can't ask yourself!")
  228.                     return
  229.                 end
  230.                 currhand = @hands[@currplayer]
  231.                 if !currhand.include?(card)
  232.                     @bot.sendPM(id, "You don't have any of that card!")
  233.                     return
  234.                 end
  235.                 askeenum = @players.index(askeehm)
  236.                 numcards = @hands[askeenum].count(card)
  237.                 if @hands[askeenum].length == 0
  238.                     @bot.sendMessage(askee+" has no cards!")
  239.                     numcards = 1
  240.                 elsif numcards == 0
  241.                     @bot.sendMessage(askee+" has no "+card2str(card, true)+"! Go fish!")
  242.                     if @deck.length == 0
  243.                         @bot.sendMessage("There are no more cards in the deck.")
  244.                     else
  245.                         pickedupcard = @deck.pop()
  246.                         @bot.sendPM(id, "You got a "+card2str(pickedupcard))
  247.                         currhand.push(pickedupcard)
  248.                     end
  249.                 else
  250.                     @bot.sendMessage(askee+" has "+@@numstrs[numcards]+" "+card2str(card, numcards>1)+"!")
  251.                     numcards.times do |x|
  252.                         @hands[askeenum].delete_at(@hands[askeenum].index(card) || @hands[askeenum].length)
  253.                         currhand.push(card)
  254.                     end
  255.                     if @hands[askeenum].length == 0
  256.                         out = "You are out of cards"
  257.                         begin
  258.                             card = @deck.pop()
  259.                             card == nil && raise("Empty deck")
  260.                             @hands[askeenum].push(card)
  261.                             out += ", so you picked up a "+card2str(card)
  262.                         rescue
  263.                             #
  264.                         end
  265.                         @bot.sendPM(askee, out+"!")
  266.                     end
  267.                 end
  268.                 cards = currhand
  269.                 pairs = []
  270.                 for x in cards
  271.                     cardcount = currhand.count(x)
  272.                     if cardcount > 1
  273.                         pairs.push(x)
  274.                         currhand.delete_at(currhand.index(x) || currhand.length)
  275.                         currhand.delete_at(currhand.index(x) || currhand.length)
  276.                     end
  277.                     if cardcount == 4
  278.                         pairs.push(x)
  279.                         currhand.delete_at(currhand.index(x) || currhand.length)
  280.                         currhand.delete_at(currhand.index(x) || currhand.length)
  281.                     end
  282.                 end
  283.                 @pairs[@currplayer] += pairs
  284.                 if pairs.length > 0
  285.                     out = ""
  286.                     for x in pairs.first(pairs.length - 1)
  287.                         out += "a pair of "+card2str(x, true)+", "
  288.                     end
  289.                     if pairs.length > 1
  290.                         out = ""
  291.                         for x in pairs.first(pairs.length - 2)
  292.                             out += "a pair of "+card2str(x, true)+", "
  293.                         end
  294.                         out += " and "
  295.                     end
  296.                     out += "a pair of "+card2str(pairs.last, true)+"!"
  297.                     @bot.sendMessage(user.regname+" has "+out)
  298.                 end
  299.                 if currhand.length == 0
  300.                     out = "You are out of cards"
  301.                     begin
  302.                         card = @deck.pop()
  303.                         card == nil && raise("Empty deck")
  304.                         currhand.push(card)
  305.                         out += ", so you picked up a "+card2str(card)
  306.                     rescue
  307.                         #
  308.                     end
  309.                     @bot.sendPM(id, out+"!")
  310.                 end
  311.                 if numcards == 0
  312.                     @currplayer = askeenum
  313.                 end
  314.                 return doturn(@currplayer)
  315.             elsif cmd == "c" or cmd == "cards"
  316.                 cards = []
  317.                 @hands[@currplayer].each do |card|
  318.                     cards << @@cardletters[card]
  319.                 end
  320.                 @bot.sendPM(id, "Cards: "+ cards.join(", "))
  321.             elsif cmd == "s" or cmd == "status"
  322.                 nick = @players[@currplayer].regname
  323.                 @bot.sendMessage("It is currently #{nick}'s turn! There are #{@deck.length} cards left in the deck.")
  324.             end
  325.         end
  326.     end
  327.    
  328. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement