Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ===COMMAND===
- when "gofish", "gf"
- user = @users[packet["u"]]
- sub, message = message == nil ? ["", nil] : message.split(' ', 2)
- @games["GoFish::Game"] ||= GoFish::Game.new self
- case sub.downcase
- when "join", "j"
- @games["GoFish::Game"].running && sendMessage("A game of go fish is currently in progress. Please wait until it's done to join.") && return
- if @games["GoFish::Game"].players.include?(user)
- sendMessage("You are already in the game of GoFish. To start it, use !#{cmd} start.") && return
- end
- user.getAttachment() != nil && sendMessage("You are already in a game of #{user.getAttachment().class.name}. Please leave it before joining Go Fish.") && return
- user.setAttachment(@games["GoFish::Game"]) && sendMessage("You have joined the game of #{user.getAttachment().class.name}.")
- user.getAttachment().join user
- when "start", "s"
- user.getAttachment == nil && sendMessage("You are not currently playing a game of Go Fish") && return
- user.getAttachment().players.length < 2 && sendMessage("There must be at least two players to start a game of Go Fish.") && return
- sendMessage("The game of GoFish has been started.")
- user.getAttachment().start
- when "end", "e"
- user.getAttachment == nil && sendMessage("You are not currently playing a game of Go Fish") && return
- @games["GoFish::Game"] = nil
- user.setAttachment(nil)
- sendMessage("The game of GoFish has been ended.")
- when "load"
- return
- @games["GoFish::Game"] = GoFish::Game.new self
- @games["GoFish::Game"].loadFromState YAML::load_file('./.data/GoFish.data')
- @games["GoFish::Game"].players.each do |u|
- if @users.has_key?(u.id.to_s)
- @users[u.id.to_s].setAttachment(@games["GoFish::Game"])
- else
- @games["GoFish::Game"].players.delete(u)
- end
- end
- if @games["GoFish::Game"].players.length < 2
- sendMessage("There aren't enough players to continue the game so it has been forcefully ended.")
- @games["GoFish::Game"].handlewin()
- @games["GoFish::Game"] = nil
- @users.each do |user|
- if (att = user.getAttachment()) != nil
- puts att.class.name
- if att.class.name == "GoFish::Game"
- user.setAttachment(nil)
- end
- end
- end
- end
- else
- (att = user.getAttachment) == nil && sendMessage("You are not currently playing a game of Go Fish. Use !#{cmd} join to join one.") && return
- att.handlecmd sub, message == nil ? [] : message.split(' '), user
- if !att.running
- @games["GoFish::Game"].players.each do |user|
- if (att = user.getAttachment()) != nil
- user.setAttachment(nil)
- end
- end
- @games["GoFish::Game"] = nil
- else
- #File.open('./.data/GoFish.data', 'w+') {|f| f.write(YAML::dump(@games["GoFish::Game"].getState())) }
- end
- end
- #if @games["GoFish::Game"] == nil
- # File.delete('./.data/GoFish.data')
- #end
- ===CLASS FILE===
- module GoFish
- class Game
- attr_accessor :players, :running
- @@cardletters = ["", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
- @@cardletterlookup = {"A" => 1, "J" => 11, "Q" => 12, "K" => 13}
- @@cardstrs = ["", "ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king"]
- @@numstrs = ["zero", "one", "two", "three", "four"]
- def initialize bot
- @players = []
- @bot = bot
- @deck = ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] * 4).shuffle
- @hands = []
- @pairs = []
- @currplayer = 0
- @running = false
- end
- def getState
- [@players, @deck, @hands, @pairs, @currplayer, @running]
- end
- def loadFromState state
- @players, @deck, @hands, @pairs, @currplayer, @running = state
- end
- def join user
- @players.push(user)
- @hands.push([])
- @pairs.push([])
- end
- def start
- @running = true
- @numplayers = @players.length
- numcards = @numplayers < 4 ? 9 : 7
- numcards.times do |x|
- @numplayers.times do |y|
- @hands[y].push(@deck.pop())
- end
- end
- @numplayers.times do |x|
- @hands[x].sort!
- end
- doturn(0)
- end
- def stop
- handlewin
- end
- def doturn player
- oldplayer = player
- while true
- if @hands[player].length == 0
- out = "You are out of cards"
- begin
- card = @deck.pop()
- card == nil && raise("Empty deck")
- @hands[player].push(card)
- out += ", so you picked up a " + card2str(card)
- rescue
- player += 1
- if player == @players.length
- player = 0
- end
- if oldplayer == player
- handlewin()
- return true
- end
- redo
- end
- @bot.sendPM @players[player].id, out+"!"
- break
- else
- break
- end
- end
- @currplayer = player
- nick = @players[player].regname
- @bot.sendMessage(nick+", your turn! Use !GoFish ask <player> <card>, to ask somebody for cards! There are #{@deck.length} cards left in the deck.")
- cards = []
- @pairs[player].each do |card|
- cards << @@cardletters[card]
- end
- cards.length > 0 && @bot.sendMessage(nick+"'s pairs: "+ cards.join(", "))
- cards = []
- @hands[player].each do |card|
- puts "[#{card.to_s}]"
- cards << @@cardletters[card]
- end
- @bot.sendPM @players[player].id, "Cards: "+ cards.join(", ")
- end
- def handlewin
- @bot.sendMessage("Everybody is out of cards!")
- pairlens = []
- @pairs.each_with_index do |x, index|
- pairlens << [x.length, index]
- end
- pairlens.sort!
- pairlens.reverse!
- @bot.sendMessage("#{@players[pairlens[0][1]].regname} is the winner with #{pairlens[0][0]} pairs! Congratulations!")
- @running = false
- end
- def card2str card, plural=false
- name = @@cardstrs[card]
- if plural == true
- if card == 6
- name += "es"
- else
- name += "s"
- end
- end
- return name
- end
- def handlecmd cmd, args, user
- cmd = cmd.downcase
- nick = user.regname
- id = user.id
- if @players[@currplayer] != user
- @bot.sendPM(id, "Error! It's not your turn!")
- return
- end
- if cmd == "a" || cmd == "ask"
- if args.length < 2
- @bot.sendPM(id, "Please specify a card to ask for. Usage: !GoFish ask <palyer> <card>")
- return
- end
- askee = args[0]
- card = args[1].upcase
- if card.is_i?
- card = card.to_i
- else
- begin
- card = @@cardletterlookup[card]
- rescue
- @bot.sendPM(id, "Invalid card!")
- return
- end
- end
- found = false
- for x in @players
- if askee == x.id || askee.downcase == x.regname.downcase
- askeehm = x
- found = true
- break
- end
- end
- if found == false
- @bot.sendPM(id, "No such player!")
- return
- end
- if askee.downcase == nick.downcase
- @bot.sendPM(id, "You can't ask yourself!")
- return
- end
- currhand = @hands[@currplayer]
- if !currhand.include?(card)
- @bot.sendPM(id, "You don't have any of that card!")
- return
- end
- askeenum = @players.index(askeehm)
- numcards = @hands[askeenum].count(card)
- if @hands[askeenum].length == 0
- @bot.sendMessage(askee+" has no cards!")
- numcards = 1
- elsif numcards == 0
- @bot.sendMessage(askee+" has no "+card2str(card, true)+"! Go fish!")
- if @deck.length == 0
- @bot.sendMessage("There are no more cards in the deck.")
- else
- pickedupcard = @deck.pop()
- @bot.sendPM(id, "You got a "+card2str(pickedupcard))
- currhand.push(pickedupcard)
- end
- else
- @bot.sendMessage(askee+" has "+@@numstrs[numcards]+" "+card2str(card, numcards>1)+"!")
- numcards.times do |x|
- @hands[askeenum].delete_at(@hands[askeenum].index(card) || @hands[askeenum].length)
- currhand.push(card)
- end
- if @hands[askeenum].length == 0
- out = "You are out of cards"
- begin
- card = @deck.pop()
- card == nil && raise("Empty deck")
- @hands[askeenum].push(card)
- out += ", so you picked up a "+card2str(card)
- rescue
- #
- end
- @bot.sendPM(askee, out+"!")
- end
- end
- cards = currhand
- pairs = []
- for x in cards
- cardcount = currhand.count(x)
- if cardcount > 1
- pairs.push(x)
- currhand.delete_at(currhand.index(x) || currhand.length)
- currhand.delete_at(currhand.index(x) || currhand.length)
- end
- if cardcount == 4
- pairs.push(x)
- currhand.delete_at(currhand.index(x) || currhand.length)
- currhand.delete_at(currhand.index(x) || currhand.length)
- end
- end
- @pairs[@currplayer] += pairs
- if pairs.length > 0
- out = ""
- for x in pairs.first(pairs.length - 1)
- out += "a pair of "+card2str(x, true)+", "
- end
- if pairs.length > 1
- out = ""
- for x in pairs.first(pairs.length - 2)
- out += "a pair of "+card2str(x, true)+", "
- end
- out += " and "
- end
- out += "a pair of "+card2str(pairs.last, true)+"!"
- @bot.sendMessage(user.regname+" has "+out)
- end
- if currhand.length == 0
- out = "You are out of cards"
- begin
- card = @deck.pop()
- card == nil && raise("Empty deck")
- currhand.push(card)
- out += ", so you picked up a "+card2str(card)
- rescue
- #
- end
- @bot.sendPM(id, out+"!")
- end
- if numcards == 0
- @currplayer = askeenum
- end
- return doturn(@currplayer)
- elsif cmd == "c" or cmd == "cards"
- cards = []
- @hands[@currplayer].each do |card|
- cards << @@cardletters[card]
- end
- @bot.sendPM(id, "Cards: "+ cards.join(", "))
- elsif cmd == "s" or cmd == "status"
- nick = @players[@currplayer].regname
- @bot.sendMessage("It is currently #{nick}'s turn! There are #{@deck.length} cards left in the deck.")
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement