Skip to content

[wip] kick player out of game #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ en:

game_status: "There's [a game](%{game_url}) with *%{players} players*. Type /join to *join*."
joined_game: "@%{username} joined the game. There are now %{players} players."
kick_user: "This user was kicked from the game."
left_game: "@%{username} left the game. There are now *%{players}* players."
location_set: "The location for the current game has been set"
not_attending: "You're not attending the current game, @%{username}"
2 changes: 2 additions & 0 deletions lib/pickup_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def handle_text(message)
PickupBot::Commands::Join.run(telegram_bot, message)
when /leave/
PickupBot::Commands::Leave.run(telegram_bot, message)
when /kick/
PickupBot::Commands::Kick.run(telegram_bot, message)
else
PickupBot::Commands::Error.run(telegram_bot, message, exception)
end
Expand Down
1 change: 1 addition & 0 deletions lib/pickup_bot/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Commands
require 'pickup_bot/commands/error'
require 'pickup_bot/commands/help'
require 'pickup_bot/commands/join'
require 'pickup_bot/commands/kick'
require 'pickup_bot/commands/leave'
require 'pickup_bot/commands/set_location'
require 'pickup_bot/commands/status'
3 changes: 3 additions & 0 deletions lib/pickup_bot/commands/join.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def initialize(telegram_bot, message)
def run
if game_exists?
current_player = Player.find_or_create_by(telegram_user_id: message.from.id)
unless current_player.username
current_player.update(username: username)
end
attendence = Attendance.new(game: current_game, player: current_player)
attendence.save
telegram_bot.api.send_message(
Expand Down
44 changes: 44 additions & 0 deletions lib/pickup_bot/commands/kick.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Commands
class Kick
def self.run(telegram_bot, message)
new(telegram_bot, message).run
end

def initialize(telegram_bot, message)
@telegram_bot = telegram_bot
@message = message
end

def run
return destroy_user_attendance if user_attendance
end

private

def kick_username
@message.text.split(" ").second
end

def current_game
Game.active.find_by_chat_id(@message.chat.id)
end

def destroy_user_attendance
user_attendance.destroy

@telegram_bot.api.send_message(
chat_id: @message.chat.id,
text: I18n.t("bot.kick_user")
)
end

def user_attendance
Attendance.find_by(game: current_game, player: kicked_player)
end

def kicked_player
Player.find_by(username: kick_username)
end

end
end
34 changes: 34 additions & 0 deletions spec/lib/pickup_bot/commands/kick_game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "spec_helper"
require 'telegram/bot'
require 'pickup_bot'

feature "kicking a user" do
let(:bot) { Telegram::Bot::Client.new('fake-token') }
let(:pickup_bot) { PickupBot.new(bot) }
let(:telegram_user) { Telegram::Bot::Types::User.new(user_params) }
let(:telegram_chat) { Telegram::Bot::Types::Chat.new(chat_params) }
let(:message) { Telegram::Bot::Types::Message.new(message_params("/kick OneTrickPony")) }

before :each do
stub_request(:any, /api.telegram.org/).to_return(status: 200, body:"[]", :headers => {})
end

scenario "kick another user from a game" do
game = Game.create(chat_id: fake_chat_id, required_players: 4)
kicked_player = Player.create(telegram_user_id: 12, username: "OneTrickPony")
Attendance.create(game: game, player: kicked_player)

pickup_bot.run(message)

expect(a_request(:post, "https://api.telegram.org/botfake-token/sendMessage").
with(body: {
"chat_id" => "123",
"text" => I18n.t(
"bot.kick_user"
)
}
)).to have_been_made.times(1)

expect(Attendance.count).to eq(0)
end
end