init
This commit is contained in:
4
app/controllers/application_controller.rb
Normal file
4
app/controllers/application_controller.rb
Normal file
@ -0,0 +1,4 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
|
||||
allow_browser versions: :modern
|
||||
end
|
0
app/controllers/concerns/.keep
Normal file
0
app/controllers/concerns/.keep
Normal file
52
app/controllers/game_controller.rb
Normal file
52
app/controllers/game_controller.rb
Normal file
@ -0,0 +1,52 @@
|
||||
class GameController < ApplicationController
|
||||
respond_to? :json
|
||||
def index
|
||||
@recent_games = Game.order(timestamp: :desc).limit(20)
|
||||
end
|
||||
|
||||
def show
|
||||
@game = Game.find(params[:id])
|
||||
end
|
||||
|
||||
def create
|
||||
game_params[:timestamp] = Time.at(params[:timestamp].to_i).utc if params[:timestamp].present?
|
||||
if Game.find_by(timestamp: game_params[:timestamp])
|
||||
return head 208 # HTTP 208: Already reported
|
||||
end
|
||||
|
||||
players = []
|
||||
params[:players].each do | name, _pos |
|
||||
player = Player.find_by(name: name)
|
||||
if player.nil?
|
||||
player = Player.new(name: name, wins: 0, losses: 0, purse: 0)
|
||||
player.save
|
||||
end
|
||||
players << player
|
||||
end
|
||||
|
||||
@game = Game.create(game_params)
|
||||
|
||||
if @game.save
|
||||
players.each do | player |
|
||||
if @game.winner == player.name
|
||||
player.purse += @game.payout
|
||||
player.wins += 1
|
||||
player.save
|
||||
elsif @game.loser == player.name
|
||||
player.purse -= @game.payout
|
||||
player.losses += 1
|
||||
player.save
|
||||
end
|
||||
end
|
||||
head :ok
|
||||
else
|
||||
head :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
def game_params
|
||||
params.require(:game).permit(:timestamp, :gametype, :wager, :winner, :loser, :high_roll, :low_roll, :payout)
|
||||
end
|
||||
end
|
5
app/controllers/leaderboard_controller.rb
Normal file
5
app/controllers/leaderboard_controller.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class LeaderboardController < ApplicationController
|
||||
def index
|
||||
@players = Player.order(purse: :desc)
|
||||
end
|
||||
end
|
5
app/controllers/player_controller.rb
Normal file
5
app/controllers/player_controller.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class PlayerController < ApplicationController
|
||||
def show
|
||||
@player = Player.find(params[:id])
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user