Skip to content

Commit

Permalink
added cog tracking backend stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Wallace committed Mar 5, 2011
1 parent e554daf commit 6b3c41c
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 3 deletions.
10 changes: 9 additions & 1 deletion game/src/game/main.js
Expand Up @@ -118,7 +118,8 @@ Menu.prototype.show = function() {
html += '<div class="difficulty">' + difficulty + '</div>';
}
html += '<a class="level" id="level' + i + '" href="' + Hash.getLevelHash(this.username, item.levelname) + '">';
// html += '<img src="/images/' + ['empty', 'check', 'checkplus'][Math.floor(Math.random() * 3)] + '.png">';
var s = stats.getStatsForLevel(item.levelname);
html += '<img src="/images/' + (s.gotAllCogs ? 'checkplus' : s.complete ? 'check' : 'empty') + '.png">';
html += item.title + '</a>';
}
html += '</div>';
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
70 changes: 70 additions & 0 deletions 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
}
};
2 changes: 1 addition & 1 deletion game/www/style.css
Expand Up @@ -20,7 +20,7 @@ div#levelScreen {

#levels {
overflow: hidden;
width: 250px;
width: 275px;
margin-left: auto;
margin-right: auto;
background: #C7C7C7 !important;
Expand Down
18 changes: 18 additions & 0 deletions rails/app/controllers/users_controller.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions 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
1 change: 1 addition & 0 deletions rails/app/models/user.rb
Expand Up @@ -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"

Expand Down
4 changes: 4 additions & 0 deletions rails/app/views/game/play.html.erb
@@ -1,5 +1,9 @@
<%= content_for :title, "Play" %>

<script type="text/javascript"><!--
var username = <%=raw current_user.nil? ? 'null' : '"' + current_user.username + '"' %>;
// --></script>

<div id="game">
<canvas id="canvas">Your browser does not support the HTML5 canvas element.</canvas>
<div id="levelScreen"></div>
Expand Down
3 changes: 3 additions & 0 deletions rails/config/routes.rb
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions 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
4 changes: 3 additions & 1 deletion rails/db/schema.rb
Expand Up @@ -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"
Expand All @@ -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|
Expand Down

0 comments on commit 6b3c41c

Please sign in to comment.