diff --git a/game/src/game/main.js b/game/src/game/main.js index d45f32f..82f1e56 100644 --- a/game/src/game/main.js +++ b/game/src/game/main.js @@ -118,7 +118,8 @@ Menu.prototype.show = function() { html += '
' + difficulty + '
'; } html += ''; - // html += ''; + var s = stats.getStatsForLevel(item.levelname); + html += ''; html += item.title + ''; } html += ''; @@ -326,6 +327,7 @@ Hash.getLevelHash = function(username, levelname) { return '#/' + username + '/' // module Main //////////////////////////////////////////////////////////////////////////////// +var stats = null; var hash = null; var menu = null; var level = null; @@ -348,6 +350,12 @@ $(document).ready(function() { hash = new Hash(); menu = new Menu(); level = new Level(); + stats = new PlayerStats(username, function() { + // if we're in the menu, reload the menu so the icons show up + if (hash.levelname == null) { + menu.show(); + } + }); tick(); setInterval(tick, 1000 / 60); diff --git a/game/src/game/playerstats.js b/game/src/game/playerstats.js new file mode 100644 index 0000000..e47ca25 --- /dev/null +++ b/game/src/game/playerstats.js @@ -0,0 +1,70 @@ +function loadStats(username, callback) { + $.ajax({ + 'url': '/stats/' + username, + 'type': 'GET', + 'cache': false, + 'dataType': 'json', + 'success': callback + }); +} + +function saveStats(username, levelname, stats) { + $.ajax({ + 'url': '/stats/' + username + '/' + levelname, + 'type': 'PUT', + 'dataType': 'json', + 'data': JSON.stringify({ + 'statistic': { + 'complete': stats.complete, + 'got_all_cogs': stats.gotAllCogs + } + }), + 'contentType': 'application/json; charset=utf-8' + }); +} + +function PlayerStats(username, callback) { + this.username = username; + this.stats = {}; + + if (this.username != null) { + var this_ = this; + loadStats(username, function(stats) { + for (var i = 0; i < stats.length; i++) { + var s = stats[i]['statistic']; + this_.stats[s.levelname] = { + complete: s['complete'], + gotAllCogs: s['got_all_cogs'] + }; + } + callback(); + }); + } +} + +PlayerStats.prototype.getStatsForLevel = function(levelname) { + if (this.username != null) { + if (levelname in this.stats) { + return this.stats[levelname]; + } + } else { + // TODO: cookies + } + return { + complete: false, + gotAllCogs: false + }; +}; + +PlayerStats.prototype.setStatsForLevel = function(levelname, complete, gotAllCogs) { + if (this.username != null) { + var stats = { + complete: complete, + gotAllCogs: gotAllCogs + }; + this.stats[levelname] = stats; + saveStats(this.username, levelname, stats); + } else { + // TODO: cookies + } +}; diff --git a/game/www/style.css b/game/www/style.css index 25d23a4..cfd398e 100644 --- a/game/www/style.css +++ b/game/www/style.css @@ -20,7 +20,7 @@ div#levelScreen { #levels { overflow: hidden; - width: 250px; + width: 275px; margin-left: auto; margin-right: auto; background: #C7C7C7 !important; diff --git a/rails/app/controllers/users_controller.rb b/rails/app/controllers/users_controller.rb index 58108c5..d0cb6d4 100644 --- a/rails/app/controllers/users_controller.rb +++ b/rails/app/controllers/users_controller.rb @@ -35,6 +35,24 @@ def level_data render :json => @level, :methods => [:html_title] end + def get_stats + @user = User.find_by_username(params[:username]) + render :json => @user.statistics, :methods => [:levelname] + end + + def set_stats + @user = User.find_by_username(params[:username]) + @level = @user.levels.select { |l| l.html_title == params[:levelname]}.first + @statistic = @user.statistics.select { |s| s.level.html_title == params[:levelname]}.first + if @statistic.nil? + @statistic = Statistic.new + @statistic.user = @user + @statistic.level = @level + end + @statistic.update_attributes(params[:statistic]) + render :text => '' + end + # update the level def update_level @level = current_user.levels.select { |l| l.html_title == params[:levelname]}.first diff --git a/rails/app/models/statistic.rb b/rails/app/models/statistic.rb index a426359..f83d304 100644 --- a/rails/app/models/statistic.rb +++ b/rails/app/models/statistic.rb @@ -1,4 +1,10 @@ class Statistic < ActiveRecord::Base belongs_to :level belongs_to :user + + attr_accessible :complete, :got_all_cogs + + def levelname + self.level.html_title + end end diff --git a/rails/app/models/user.rb b/rails/app/models/user.rb index 5da75df..3278151 100644 --- a/rails/app/models/user.rb +++ b/rails/app/models/user.rb @@ -9,6 +9,7 @@ class User < ActiveRecord::Base attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :login has_many :levels + has_many :statistics validates_uniqueness_of :username, :on => :create, :message => "must be unique" diff --git a/rails/app/views/game/play.html.erb b/rails/app/views/game/play.html.erb index fc3be01..60fa18a 100644 --- a/rails/app/views/game/play.html.erb +++ b/rails/app/views/game/play.html.erb @@ -1,5 +1,9 @@ <%= content_for :title, "Play" %> + +
Your browser does not support the HTML5 canvas element.
diff --git a/rails/config/routes.rb b/rails/config/routes.rb index 3ad4fe6..3ff105e 100644 --- a/rails/config/routes.rb +++ b/rails/config/routes.rb @@ -9,6 +9,9 @@ match 'data/:username' => 'users#menu_data' match 'data/:username/:levelname' => 'users#level_data', :via => :get + match 'stats/:username' => 'users#get_stats', :via => :get + match 'stats/:username/:levelname' => 'users#set_stats', :via => :put + # resources :users do # resources :levels # end diff --git a/rails/db/migrate/20120101002338_statistics.rb b/rails/db/migrate/20120101002338_statistics.rb new file mode 100644 index 0000000..fd38876 --- /dev/null +++ b/rails/db/migrate/20120101002338_statistics.rb @@ -0,0 +1,11 @@ +class Statistics < ActiveRecord::Migration + def self.up + add_column :statistics, :complete, :boolean, :default => false + add_column :statistics, :got_all_cogs, :boolean, :default => false + end + + def self.down + remove_column :statistics, :complete + remove_column :statistics, :got_all_cogs + end +end diff --git a/rails/db/schema.rb b/rails/db/schema.rb index e1ef4eb..513c414 100644 --- a/rails/db/schema.rb +++ b/rails/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120101002337) do +ActiveRecord::Schema.define(:version => 20120101002338) do create_table "levels", :force => true do |t| t.string "title" @@ -27,6 +27,8 @@ t.integer "user_id" t.datetime "created_at" t.datetime "updated_at" + t.boolean "complete", :default => false + t.boolean "got_all_cogs", :default => false end create_table "users", :force => true do |t|