Created
February 11, 2014 01:32
-
-
Save benneuman/8927753 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
helpers do | |
def parse_shot(shot) | |
if shot.at_already_shot_coordinate? | |
{success: false, reason: "already shot there"} | |
else | |
{success: true, result: { | |
coordinate: shot.coordinate, | |
hit: shot.result.hit?, | |
sunk: shot.result.sunk_ship?, | |
name: ship.result.sunk_ship_name | |
}} | |
end.to_json | |
end | |
def parse_shot_result(shot_result) | |
if shot_result.hit | |
ship = PlayerShip.find(shot_result.ship_id) | |
{coord: shot_result.coord, | |
hit: shot_result.hit, | |
sunk: ship.sunk?, | |
name: ship.name}.to_json | |
else | |
shot_result.to_json | |
end | |
end | |
def log_shot_result(shot_coord) | |
ship_id = get_enemy.find_ship_id_by_coord(shot_coord) | |
hit = (ship_id == 0 ? false : true) | |
get_player.log_guess(coord: shot_coord, | |
hit: hit, | |
ship_id: ship_id) | |
end | |
end |
This file contains hidden or 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
get '/' do | |
erb :index | |
end | |
class RetrievesBoard | |
def self.for_type(player, which) | |
case which | |
when 'offense' | |
OffenseBoard.for(player) | |
when 'defense' | |
DefenseBoard.new | |
end | |
end | |
end | |
class Item | |
attr_accessor :coordinate, :thing | |
def initialize(coordinate, thing) | |
@coordinate = coordinate | |
@thing = thing | |
end | |
end | |
class DefenseBoard | |
def items | |
[Item.new(10, "cruiser")] | |
end | |
end | |
class OffenseBoard | |
def self.for(player) | |
self.new | |
end | |
def items | |
[Item.new(10, "shot")] | |
end | |
end | |
def parse_board(board) | |
board.items.map {|item| {coord: item.coordinate, thing: item.thing}}.to_json | |
end | |
get '/board/:which' do | |
return {success: false, reason: "Board choice must be 'offense' or 'defense' "}.to_json unless ["offense", "defense"].include? params[:which] | |
player = Object.new #get_player(session[:player_id]) | |
parse_board(RetrievesBoard.for_type(player, params[:which])) | |
end | |
get '/player/:id/create' do | |
session[:player_id] = params[:id].to_i | |
set_game_id | |
set_enemy_id | |
session.inspect | |
end | |
get '/player2' do | |
session[:player_id] = 2 | |
set_game_id | |
set_enemy_id | |
session.inspect | |
end | |
get '/cookie' do | |
session.inspect | |
end | |
get '/check_turn' do | |
my_turn?.to_s | |
end | |
post '/shot/create' do | |
shot_coord = params["coord"].to_i | |
coordinate = Coordinate.at shot_coord | |
shot = Shot.create_at coordinate | |
parse_shot(shot) | |
end | |
get '/opponent_turn_results' do | |
latest_guess = get_latest_guess | |
return "false" if latest_guess.nil? | |
return "false" if latest_guess.player == get_player | |
parse_shot_result(latest_guess) | |
end | |
get '/check_game_over' do | |
if won? | |
"1" | |
elsif lost? | |
"2" | |
else | |
"0" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment