Skip to content

Instantly share code, notes, and snippets.

@Ravenclaw968
Last active October 17, 2017 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ravenclaw968/32ee273bec49736c01bfa21a3b754740 to your computer and use it in GitHub Desktop.
Save Ravenclaw968/32ee273bec49736c01bfa21a3b754740 to your computer and use it in GitHub Desktop.
App.competition = App.cable.subscriptions.create "CompetitionChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
# Decision that the message that comes is a message that is sent during the competition
if data["type"] == "roast"
# Adds a message to the chat room and scrolls down as the message comes to make the viewing experience easier
$.ajax "/summarize",
data :
id: data["message"]
success : (res, status, xhr) ->
$("#chat_" + data["competition"]).append(res)
$('#cht_' + data["competition"]).scrollTop($('#cht_' + data["competition"]).get(0).scrollHeight)
error : (request, status, error) ->
$("#chat_" + data["competition"]).append(request.responseText)
$('#cht_' + data["competition"]).scrollTop($('#cht_' + data["competition"]).get(0).scrollHeight)
# Decision that the message that comes is an edit to an already existing message during the competition
else if data["type"] == "edit"
$.ajax "/summarize",
data :
id: data["message"]
success : (res, status, xhr) ->
$("#message_total_" + data["message"]).replaceWith(res)
error : (request, status, error) ->
$("#message_total_" + data["message"]).replaceWith(request.responseText)
# Decision the the message that comes is a destruction of an already existing message during the competition
else if data["type"] == "destroy"
$("#message_total_" + data["message"]).remove()
# Other unimportant decision for the question.
else if data["type"] == "start"
setInterval(->
time = parseInt($("#time-span_" + data["competition"]).html(), 10)
if time - 1 >= 0
$("#time-span_" + data["competition"]).html(time - 1)
return
, 1000)
setTimeout(->
window.location.replace("/voting/" + data["competition"])
return
, parseInt(data["duration"], 10) * 1000)
# Decision that the user has voted in the competition
else if data["type"] == "vote"
# Changes the number of votes for the challenger and challenged and the percentage of votes
# the challenger and the challenged has.
if data["win_votes"] == 1
$("#winner_votes_" + data["competition"]).html(data["win_votes"] + " vote")
else
$("#winner_votes_" + data["competition"]).html(data["win_votes"] + " votes")
if data["lose_votes"] == 1
$("#loser_votes_" + data["competition"]).html(data["lose_votes"] + " vote")
else
$("#loser_votes_" + data["competition"]).html(data["lose_votes"] + " vote")
$("#winner_percentage_" + data["competition"]).html(data["win_percentage"] + "%")
$("#loser_percentage_" + data["competition"]).html(data["lose_percentage"] + "%")
# Faulty Javascript version
(function() {
App.competition = App.cable.subscriptions.create("CompetitionChannel", {
connected: function() {},
disconnected: function() {},
received: function(data) {
if (data["type"] === "roast") {
return $.ajax("/summarize", {
data: {
id: data["message"]
},
success: function(res, status, xhr) {
$("#chat_" + data["competition"]).append(res);
return $('#cht_' + data["competition"]).scrollTop($('#cht_' + data["competition"]).get(0).scrollHeight);
},
error: function(request, status, error) {
$("#chat_" + data["competition"]).append(request.responseText);
return $('#cht_' + data["competition"]).scrollTop($('#cht_' + data["competition"]).get(0).scrollHeight);
}
});
} else if (data["type"] === "start") {
setInterval(function() {
var time;
time = parseInt($("#time-span_" + data["competition"]).html(), 10);
if (time - 1 >= 0) {
$("#time-span_" + data["competition"]).html(time - 1);
}
}, 1000);
return setTimeout(function() {
window.location.replace("/voting/" + data["competition"]);
}, parseInt(data["duration"], 10) * 1000);
} else if (data["type"] === "vote") {
if (data["win_votes"] === 1) {
$("#winner_votes_" + data["competition"]).html(data["win_votes"] + " vote");
} else {
$("#winner_votes_" + data["competition"]).html(data["win_votes"] + " votes");
}
if (data["lose_votes"] === 1) {
$("#loser_votes_" + data["competition"]).html(data["lose_votes"] + " vote");
} else {
$("#loser_votes_" + data["competition"]).html(data["lose_votes"] + " vote");
}
$("#winner_percentage_" + data["competition"]).html(data["win_percentage"] + "%");
return $("#loser_percentage_" + data["competition"]).html(data["lose_percentage"] + "%");
}
}
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment