-
-
Save iszak/166e1c61211ef59d97e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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