Skip to content

Instantly share code, notes, and snippets.

@iszak
Forked from anonymous/-
Created August 1, 2014 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iszak/166e1c61211ef59d97e3 to your computer and use it in GitHub Desktop.
Save iszak/166e1c61211ef59d97e3 to your computer and use it in GitHub Desktop.
class GamesController < ApplicationController
def new
@game = Game.new
@game.field = Array.new(64).map{ rand(1..8) }.join
unless session[:client].nil? or session[:client].length != 32
@game.vplayer = session[:client]
else
@game.vplayer = SecureRandom.hex(16)
session[:client] = @game.vplayer
end
@game.vscore = 0 # default scores
@game.hscore = 0
@game.status = 'v' # v - vertical, h - horizontal
@game.range = 0
if @game.valid? and @game.save
redirect_to game_path(@game)
else
redirect_to "404.html"
end
end
def show
@game = Game.find(params[:id])
@length = Math.sqrt(@game.field.length).to_i
@links = []
if @game.hplayer.nil?
if session[:client].nil?
session[:client] = SecureRandom.hex(16)
elsif session[:client] != @game.vplayer
@game.hplayer = session[:client]
@game.save
end
end
if session[:client] == @game.vplayer and @game.status == 'v'
@length.times do |i|
@links.push( i*@length + @game.range )
end
elsif session[:client] == @game.hplayer
i = @length*@game.range
(i..(i+@length)).each do |i|
@links.push(i)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment