Skip to content

Commit

Permalink
added difficulty rating to levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Wallace committed Feb 19, 2011
1 parent ed18bce commit 2ed93ed
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 28 deletions.
17 changes: 12 additions & 5 deletions game/src/game/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ function ajaxGet(what, url, onSuccess) {
// class MenuItem
////////////////////////////////////////////////////////////////////////////////

function MenuItem(levelname, title) {
function MenuItem(levelname, title, difficulty) {
this.levelname = levelname;
this.title = title;
this.difficulty = difficulty;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -74,7 +75,7 @@ Menu.prototype.load = function(username, onSuccess) {
var levels = json['user']['levels'];
for (var i = 0; i < levels.length; i++) {
var level = levels[i];
this_.items.push(new MenuItem(level['html_title'], level['title']));
this_.items.push(new MenuItem(level['html_title'], level['title'], level['difficulty']));
}
this_.isLoading = false;
this_.selectedIndex = 0;
Expand All @@ -88,8 +89,8 @@ Menu.prototype.updateSelectedIndex = function() {
$('.level').blur();
$(selectedLevel).focus();

// no idea why 450 is the magic number that centers the selected level, but not going to worry about it
var scrollTop = $('#levelScreen').scrollTop() + $(selectedLevel).offset().top - 450;
// no idea why 475 is the magic number that centers the selected level, but not going to worry about it
var scrollTop = $('#levelScreen').scrollTop() + $(selectedLevel).offset().top - 475;
$('#levelScreen').scrollTop(scrollTop);
}
};
Expand All @@ -108,9 +109,15 @@ Menu.prototype.show = function() {
var html = '<h2>';
html += (this.username == 'rapt') ? 'Official Levels' : 'Levels made by ' + this.username;
html += '</h2><div id="levels">';
var prevDifficulty = null;
for (var i = 0; i < this.items.length; i++) {
var item = this.items[i];
html += '<a class="level" id="level' + i + '" href="' + Hash.getLevelHash(this.username, item.levelname) + '">' + item.title;
var difficulty = ['Easy', 'Medium', 'Hard', 'Impossible', 'Demoralizing'][item.difficulty];
if (difficulty != prevDifficulty) {
prevDifficulty = difficulty;
html += '<div class="difficulty">' + difficulty + '</div>';
}
html += '<a class="level" id="level' + i + '" href="' + Hash.getLevelHash(this.username, item.levelname) + '">' + item.title + '</a>';
}
html += '</div>';
$('#levelScreen').html(html);
Expand Down
7 changes: 7 additions & 0 deletions game/www/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ div#levelScreen {
-webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.5);
}

.difficulty {
font: bold 14px/24px "Lucida Grande", Arial, sans-serif;
text-align: center;
color: black;
background: rgba(0, 0, 0, 0.2) !important;
}

.level {
font: 14px/24px "Lucida Grande", Arial, sans-serif;
padding: 0 5px;
Expand Down
4 changes: 2 additions & 2 deletions rails/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def show
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"

respond_with(@user, {:only => :username, :include => {:levels => {:methods => [:html_title], :only => [:title]}}})
respond_with(@user, {:only => :username, :include => {:levels => {:methods => [:html_title], :only => [:title, :difficulty]}}})
rescue
redirect_to "/", :flash => {:error => "Couldn't find user '#{params[:username]}'"}
return
Expand Down Expand Up @@ -65,7 +65,7 @@ def edit_level
def update_level
@level = current_user.levels.select { |l| l.html_title == params[:levelname]}.first
@level.update_attributes(params[:level])
respond_with(@level, :methods => [:html_title])
render :text => ''
end

end
8 changes: 0 additions & 8 deletions rails/app/helpers/layout_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,4 @@ def stylesheet(*args)
def javascript(*args)
content_for(:head) { javascript_include_tag(*args) }
end

def edit_links_for_level(level)
if current_user.present? and level.user == current_user
"<span class='links'><a href='/edit/#{current_user.username}/#{level.html_title}/'>Edit</a> <a href='/levels/#{level.id}/' data-confirm='Deleted levels cannot be recovered! Are you sure you want to delete #{level.html_title}?' data-method='delete' rel='nofollow'>Delete</a></span>"
else
""
end
end
end
2 changes: 1 addition & 1 deletion rails/app/models/level.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Level < ActiveRecord::Base
validates_presence_of :title, :on => :create, :message => "can't be blank"
validates_uniqueness_of :title, :scope => :user_id

attr_accessible :data, :position, :title
attr_accessible :data, :position, :title, :difficulty

before_create :determine_position, :set_default_level

Expand Down
40 changes: 29 additions & 11 deletions rails/app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@
<% # no link to play a specific level, must click the play button above %>
<%= level.title %>
<% end %>
<%== edit_links_for_level level %>
<span class='links'>
<select>
<% ['Easy', 'Medium', 'Hard', 'Impossible', 'Demoralizing'].each_with_index do |name, index| %>
<option value="<%= index %>" <%= level.difficulty == index ? 'selected' : '' %>><%= name %></option>
<% end %>
</select>
&nbsp; &nbsp; &nbsp;
<a href='/edit/<%= @user.username %>/<%= level.html_title %>/'>Edit</a>
<a href='/levels/<%= level.id %>/' data-confirm='Deleted levels cannot be recovered! Are you sure you want to delete #{level.html_title}?' data-method='delete' rel='nofollow'>Delete</a>
</span>
</li>
<% end %>
</ul>
Expand All @@ -39,14 +48,24 @@
<% end %>
<% content_for :javascript do %>
function savePosition(element){
$.ajax({
url: '/edit/<%= @user.username %>/'+element.attr('html_title'),
type: 'PUT',
data: ({ levelname: element.attr('html_title'), level: { position: element.attr('position') }})
});
}
function saveLevelByElement(element){
$.ajax({
url: '/edit/<%= @user.username %>/'+$(element).attr('html_title'),
type: 'PUT',
data: ({
levelname: $(element).attr('html_title'),
level: {
position: $(element).attr('position'),
difficulty: parseInt($(element).find('select')[0].value, 10)
}
})
});
}
$('.links select').change(function(e) {
saveLevelByElement(e.target.parentNode.parentNode);
});
$( "#levels" ).sortable({
axis: 'y',
update: function(e, ui){
Expand All @@ -65,10 +84,9 @@
pos = parseFloat($("#levels li:nth-child("+idx+")").attr('position')) + 1.0;
}
$(ui.item).attr('position', pos);
savePosition(ui.item);
saveLevelByElement(ui.item);
}
});
$( "#levels" ).disableSelection();
<% end %>
<div id="new_level">
Expand Down
9 changes: 9 additions & 0 deletions rails/db/migrate/20120101002337_level_type_migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class LevelTypeMigration < ActiveRecord::Migration
def self.up
add_column :levels, :difficulty, :integer, :default => 0
end

def self.down
remove_column :levels, :difficulty
end
end
3 changes: 2 additions & 1 deletion rails/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110107171803) do
ActiveRecord::Schema.define(:version => 20120101002337) do

create_table "levels", :force => true do |t|
t.string "title"
Expand All @@ -19,6 +19,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.float "position", :default => 0.0
t.integer "difficulty", :default => 0
end

create_table "statistics", :force => true do |t|
Expand Down

0 comments on commit 2ed93ed

Please sign in to comment.