Add state to gatherers and track when a gatherer has left or is away

This commit is contained in:
jamal 2014-10-10 20:50:17 -04:00
parent 73ac7d589b
commit d6b501ac46
9 changed files with 111 additions and 16 deletions

View file

@ -202,3 +202,11 @@ table.gathers {
ul.gatherers { ul.gatherers {
@include span-columns(12); @include span-columns(12);
} }
ul#gatherers {
li.away a {
font-style: italic;
font-weight: normal;
color: #5b5b5b;
}
}

View file

@ -32,6 +32,22 @@ class GatherersController < ApplicationController
redirect_to_back redirect_to_back
end end
def status
raise AccessError unless @gatherer.can_destroy? cuser
states = {
"leaving" => Gatherer::STATE_LEAVING,
"away" => Gatherer::STATE_AWAY,
"active" => Gatherer::STATE_ACTIVE,
}
if states.has_key?(params[:status])
@gatherer.update_attribute(:status, states[params[:status]])
end
render :nothing => true, :status => 200
end
def destroy def destroy
raise AccessError unless @gatherer.can_destroy? cuser raise AccessError unless @gatherer.can_destroy? cuser

View file

@ -72,5 +72,24 @@ class GathersController < ApplicationController
end end
@gatherer = @gather.gatherers.of_user(cuser).first if cuser @gatherer = @gather.gatherers.of_user(cuser).first if cuser
update_gatherers
end
def update_gatherers
# Update user that has left and came back
if @gatherer and @gatherer.status == Gatherer::STATE_LEAVING
@gatherer.update_attribute(:status, Gatherer::STATE_ACTIVE)
end
# Remove any users that left over 30 seconds ago
removed_users = false
@gather.gatherers.each do |gatherer|
if gatherer.status == Gatherer::STATE_LEAVING and gatherer.updated_at < Time.now - 30
removed_users = true
gatherer.destroy
end
end
@gather.reload if removed_users
end end
end end

View file

@ -15,6 +15,10 @@ class Gatherer < ActiveRecord::Base
IDLE_TIME = 600 IDLE_TIME = 600
EJECT_VOTES = 4 EJECT_VOTES = 4
STATE_ACTIVE = 0
STATE_AWAY = 1
STATE_LEAVING = 2
include Extra include Extra
attr_protected :id attr_protected :id

View file

@ -6,7 +6,7 @@
<ul id="gatherers"> <ul id="gatherers">
<% @gather.gatherers.each do |gatherer| %> <% @gather.gatherers.each do |gatherer| %>
<li> <li<% if gatherer.status > 0 %> class="away"<% end %>>
<%= flag gatherer.user.country %> <%= flag gatherer.user.country %>
<%= namelink gatherer.user %> <%= namelink gatherer.user %>
<% if cuser and cuser.admin? %> <% if cuser and cuser.admin? %>

View file

@ -1,6 +1,25 @@
<div id="jplayer"></div> <div id="jplayer"></div>
<script type="text/javascript"> <script type="text/javascript">
var played = false; var played = false;
var leaving = true;
<% if @gatherer and @gatherer.can_destroy? cuser %>
var gatherer_id = <%= @gatherer.id %>;
<% else %>
var gatherer_id = 0;
<% end %>
function updateGathererStatus(status) {
$.ajax({
url: '/gatherers/' + gatherer_id + '/status',
type: 'POST',
data: {'status': status},
async: false,
cache: false,
success: function() {},
complete: function() {},
error: function() {}
});
}
$(document).ready(function() { $(document).ready(function() {
$.PeriodicalUpdater("/gathers/" + <%= @gather.id %> + ".js", { $.PeriodicalUpdater("/gathers/" + <%= @gather.id %> + ".js", {
@ -38,6 +57,39 @@
} }
} }
}); });
$(window).bind('beforeunload', function(e) {
if (gatherer_id > 0) {
return "You will be removed from the Gather if you leave this page.";
}
});
$(window).bind('unload', function() {
if (gatherer_id > 0) {
updateGathererStatus('leaving');
}
});
var afk = false;
var afk_timeout;
var afk_time = 1000 * 60 * 15; // 15 minutes
$(window).blur(function() {
afk_timeout = setTimeout(function() {
if (gatherer_id > 0) {
afk = true;
updateGathererStatus('away');
}
}, afk_time);
});
$(window).focus(function() {
if (afk) {
updateGathererStatus('active');
$.get("/gathers/" + <%= @gather.id %> + ".js");
}
clearTimeout(afk_timeout);
});
}); });
</script> </script>

View file

@ -102,6 +102,8 @@ Ensl::Application.routes.draw do
match 'gathers/latest/:game', to: "gathers#latest", via: :get match 'gathers/latest/:game', to: "gathers#latest", via: :get
match 'gather', to: "gathers#latest", game: "ns2", via: :get match 'gather', to: "gathers#latest", game: "ns2", via: :get
match 'gatherers/:id/status', to: "gatherers#status", via: :post
match 'groups/addUser' match 'groups/addUser'
match 'groups/delUser' match 'groups/delUser'

View file

@ -0,0 +1,5 @@
class AddStatusToGatherer < ActiveRecord::Migration
def change
add_column :gatherers, :status, :int, null: false, default: 0
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20140810224606) do ActiveRecord::Schema.define(:version => 20141010193221) do
create_table "admin_requests", :force => true do |t| create_table "admin_requests", :force => true do |t|
t.string "addr" t.string "addr"
@ -294,6 +294,7 @@ ActiveRecord::Schema.define(:version => 20140810224606) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.integer "votes", :default => 0, :null => false t.integer "votes", :default => 0, :null => false
t.integer "status", :default => 0, :null => false
end end
add_index "gatherers", ["gather_id"], :name => "index_gatherers_on_gather_id" add_index "gatherers", ["gather_id"], :name => "index_gatherers_on_gather_id"
@ -758,18 +759,6 @@ ActiveRecord::Schema.define(:version => 20140810224606) do
add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id" add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at" add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"
create_table "shoutmsg_archive", :force => true do |t|
t.integer "user_id"
t.string "text"
t.datetime "created_at"
t.datetime "updated_at"
t.string "shoutable_type"
t.integer "shoutable_id"
end
add_index "shoutmsg_archive", ["shoutable_type", "shoutable_id"], :name => "index_shoutmsgs_on_shoutable_type_and_shoutable_id"
add_index "shoutmsg_archive", ["user_id"], :name => "index_shoutmsgs_on_user_id"
create_table "shoutmsgs", :force => true do |t| create_table "shoutmsgs", :force => true do |t|
t.integer "user_id" t.integer "user_id"
t.string "text" t.string "text"