-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.rb
152 lines (137 loc) · 4.5 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require("bundler/setup")
Bundler.require(:default)
Dir[File.dirname(__FILE__) + '/lib/*.rb'].each { |file| require file }
require 'sinatra'
require 'sinatra/activerecord'
require './environments'
get '/' do
erb(:splash_page)
end
get '/start' do
@characters = Card.all.where(card_type: 'Cat')
erb(:player_form)
end
post '/start/new_players' do
Game.full_reset #Take this out when deploying application
player1 = params.fetch('player1')
player2 = params.fetch('player2')
player3 = params.fetch('player3')
player4 = params.fetch('player4')
piece_img = {"Miss Scarlet" => url('../img/pieces/red.png'), "Colonel Mustard" => url('../img/pieces/yellow.png'), "Mr. Green" => url('../img/pieces/green.png'), "Mrs. Peacock" => url('../img/pieces/blue.png'), "Mrs. White" => url('../img/pieces/white.png'), "Professor Plum" => url('../img/pieces/purple.png')}
new_player1 = Player.new({:name => player1, :turn => 't', :image => piece_img[player1]})
new_player2 = Player.new({:name => player2, :turn => 'f', :image => piece_img[player2]})
new_player3 = Player.new({:name => player3, :turn => 'f', :image => piece_img[player3]})
new_player4 = Player.new({:name => player4, :turn => 'f', :image => piece_img[player4]})
if (new_player1.save) && (new_player2.save) && (new_player3.save) && (new_player4.save)
Player.place_player
Card.murder
Card.deal_cards
redirect '/board'
else
erb(:errors)
end
end
get('/board') do
@spaces = Space.all.order(:id)
@rooms = ["A1", "A2", "B1", "B2", "A5", "A6", "B5", "B6", "A9", "A10", "B9", "B10", "E1", "E2", "F1", "F2", "E9", "E10", "F9", "F10", "I1", "I2", "J1", "J2", "I5", "I6", "J5", "J6", "I9", "I10", "J9", "J10"]
@final_room = ["E5", "E6", "F5", "F6"]
@player = Player.all.where(turn: 't').first
@cards = Card.all.where(player_id: @player.id)
erb(:board)
end
get('/board/:coordinates') do
new_coords = params.fetch('coordinates')
current_player = Player.all.where(turn: 't').first
current_player.move(new_coords)
# current_player.move Will update players position every click and redirect back to board
redirect '/board'
end
get '/players/:id/roll' do
id = params[:id].to_i
player = Player.find(id)
player.roll_dice
redirect back
end
get '/players/:id/checkcards' do
id = params[:id].to_i
@player = Player.find(id)
@player_cards = Card.all.where(player_id: @player.id)
erb(:checkcards)
end
get '/players/:id/scorecard' do
id = params[:id].to_i
@player = Player.find(id)
@cards = Card.all
erb(:scorecard)
end
post '/players/:id/scorecard' do
@player = Player.find(params.fetch(:id))
if params.key?('card_ids')
card_ids = params.fetch("card_ids")
card_ids.each do |card_id|
card = Card.find(card_id.to_i)
@player.cards.push(card)
end
redirect back
else
redirect back
end
end
patch '/players/:id/scorecard' do
@player = Player.find(params.fetch(:id))
if params.key?('card_ids')
card_ids = params.fetch("card_ids")
card_ids.each do |card_id|
card = Card.find(card_id.to_i)
@player.cards.destroy(card)
end
redirect back
else
redirect back
end
end
get '/players/:id/make_guess' do
@player = Player.all.where(turn: 't').first
@room = Space.find_by(player_id: @player)
@cats = Card.all.where(card_type: 'Cat')
@weapons = Card.all.where(card_type: 'Weapon')
erb(:make_guess)
end
get '/players/:id/final_guess' do
@current_player = Player.all.where(turn: 't').first
@rooms = Card.all.where(card_type: 'Room')
@cats = Card.all.where(card_type: 'Cat')
@weapons = Card.all.where(card_type: 'Weapon')
erb(:final_guess)
end
post '/players/:id/final_guess' do
@current_player = Player.all.where(turn: 't').first
id = params["id"].to_i
cat = params['killer']
weapon = params['weapon']
room = params['room']
if @current_player.final_guess(cat, weapon, room)
erb(:win)
else
@current_player.end_turn
player_to_destory = Player.find_by(name: @current_player.name)
space_update = Space.find_by(player_id: id)
space_update.update({:player_id => nil})
player_to_destory.destroy
erb(:lose)
end
end
post '/players/:id/make_guess' do
@player = Player.all.where(turn: 't').first
@weapon = params['weapon']
@cat = params['killer']
@room = params['room']
@response_from_user = @player.player_guess_match(@cat, @weapon, @room)
@found_card = Card.find_by(card_name: @response_from_user)
erb(:guess_result)
end
get '/players/next' do
current_player = Player.all.where(turn: 't').first
current_player.end_turn
redirect '/board'
end