Skip to content

Instantly share code, notes, and snippets.

@brlafreniere
Created December 7, 2020 21:09
Show Gist options
  • Save brlafreniere/03aecd64c656edc7cad732bc70c911ce to your computer and use it in GitHub Desktop.
Save brlafreniere/03aecd64c656edc7cad732bc70c911ce to your computer and use it in GitHub Desktop.
# Below is the controller code from a sort of "group/party game" webapp I had worked on at one point.
#
# It's from a Ruby on Rails application, which provides a lot of functionality out of the box
# So it might not look like "that much code" because Rails eliminates the need for writing a ton of boilerplate.
#
# I believe it at least demonstrates some understanding of model relationships and high level application design.
#
# information is broadcasted in real-time using websockets via ActionCable
class GamesController < ApplicationController
def current_game
if current_user.game
render json: current_user.game
else
render status: :not_found
end
end
def start_game
game = current_user.game
game.started_at = Time.now
game.save
GameChannel.broadcast_to(game, {game: game})
end
def join_game
@game = Game.where(handle: params[:handle]).first
begin
@game.users << current_user
rescue ActiveRecord::RecordNotUnique
# don't need to do anything, user is already joined in the game
end
end
def create_game
@game = Game.new
@game.game_type = params[:type]
@game.owner = current_user
@game.save
current_user.game = @game
current_user.save
render json: @game.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment