Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function(){
- var roomid = getParameterFromURL("roomid");
- var role = window.langu_role;//getParameterByName('role');
- if(role ==undefined || role == "" || role == null){
- role = "student"
- }
- // var socket = new WebSocket("ws://localhost:4500/websocket/" + roomid);
- // var socket = new WebSocket("wss://electrocatstudios.com/websocket");
- //var socket = new WebSocket("ws://localhost:4600/websocket");
- // socket.onopen = function () {
- // console.log("Status: Connected");
- // send();
- // };
- // socket.onclose = function(){
- // console.log("Socket closing");
- // }
- // if(window.ws.socket === undefined){
- // console.log("the socket does not exist!");
- // }else{
- // console.log("Huzzah our websocket does exist");
- // }
- var socket = null;
- if(window.ws != undefined && window.ws.socket != undefined){
- socket = window.ws.socket;
- }else{
- console.log("We don't have a websocket - are we debugging?");
- }
- // var socket = window.ws.socket;
- function get_asset_from_json(ass){
- // Read in details of asset and return the relevant asset type
- if(ass.type == "ripple" || ass.type == "imageitem"){
- // We don't care about ripples or imageitems here
- return;
- }else if(ass.type=="line"){
- // console.log("Found a line")
- var ln = new Line();
- ln.points = ass.points;
- ln.linesize = ass.linesize;
- ln.color = ass.color;
- return ln;
- }else if(ass.type=="templine"){
- var ln = new TempLine();
- ln.points = ass.points;
- ln.linesize = ass.linesize;
- ln.color = ass.color;
- return ln;
- }else if(ass.type=="imageitem"){
- var ii = new ImageItem(null,ass.loc.x, ass.loc.y,ass.file_data);
- ii.id = ass.id; // Override the one generated in the ctor
- return ii;
- }else if(ass.type=="textbox"){
- // console.log("Making a textbox");
- var tb = new TextBox(ass.loc.x, ass.loc.y, ass.text);
- tb.guid = ass.id;
- // console.log(ass);
- tb.size.x = ass.loc.w;
- tb.size.y = ass.loc.h;
- tb.setup();
- return tb;
- }else if(ass.type == "videoitem"){
- // console.log("Making a video item")
- var vi = new VideoItem(ass.loc.x, ass.loc.y);
- vi.size.x = ass.size.x;
- vi.size.y = ass.size.y;
- ass.sent = true;
- vi.url = ass.url;
- vi.div_id = ass.id;
- return vi;
- }else{
- console.log("Unfulfilled asset type: " + ass.type);
- console.log(ass);
- }
- }
- if(socket != null){
- socket.on('asset update', function (e) {
- // var dstr = e.data.replace(/'/g, '"');
- if(typeof e !== 'object'){
- var data = JSON.parse(e);
- }else{
- var data = e;
- }
- // console.log(data);
- if(window.othercursor == null){
- // Flip the colours as we are the other role
- if(role == "teacher"){
- window.othercursor = new Cursor('#0000ff', false);
- }else{
- // We are not the main cursor so not owned by the teacher
- window.othercursor = new Cursor('#ff0000', false);
- }
- }
- var assets = [];
- window.othercursor.loc.x = data.cursor.x;
- window.othercursor.loc.y = data.cursor.y;
- if(data.cursor.screen_width !== undefined && data.cursor.screen_width !== 0){
- window.othercursor.screen.width = data.cursor.screen_width;
- }
- if(data.cursor.screen_height !== undefined && data.cursor.screen_height !== 0){
- window.othercursor.screen.height = data.cursor.screen_height;
- }
- if(data.cursor.offset !== undefined){
- window.othercursor.offset = data.cursor.offset;
- }
- assets = data.assets;
- // If we are refreshing assets (on initial page reload for instance) then
- // do that now
- for(var i=0; data.refresh_assets !== undefined && data.refresh_assets != null && i<data.refresh_assets.length; i++){
- // Add the asset to the cursor - will return undefined for ripples and other
- // bits we want to ignore for now.
- if(data.refresh_assets[i].type == "textboxchange"){
- for(var j=0;j<window.cursor.assets.length;j++){
- if(window.cursor.assets[j].guid == data.refresh_assets[i].id ){
- window.cursor.assets[j].text = data.refresh_assets[i].text;
- // window.cursor.assets[j].display_text_array = data.refresh_assets[i].data
- }
- }
- }else if(data.refresh_assets[i].type == "imageitemmove"){
- // TODO: Make sure we move the correct image to an appropriate location
- console.log("We got a refresh item for text box moving");
- }else if(data.refresh_assets[i].type == "videoitemmove"){
- // console.log("we have a refreshed videoitemmove");
- var bFound = false;
- for(var j=0;j<cursor.assets.length && bFound == false;j++){
- var a = cursor.assets[j];
- if(a.div_id == data.refresh_assets[i].id){
- cursor.assets[j].loc.x = data.refresh_assets[i].loc.x;
- cursor.assets[j].loc.y = data.refresh_assets[i].loc.y;
- bFound = true;
- }
- }
- }else if(data.refresh_assets[i].type == "videoitemevent"){
- console.log("we have a refreshed videoitemevent");
- // Not sure what to do with this - for now ignore.
- }else if(data.refresh_assets[i].type == "textboxmove"){
- var bFound = false;
- for(var j=0;j<cursor.assets.length && bFound == false;j++){
- var a = cursor.assets[j];
- if(a.guid == data.refresh_assets[i].id){
- cursor.assets[j].loc.x = data.refresh_assets[i].loc.x;
- cursor.assets[j].loc.y = data.refresh_assets[i].loc.y;
- cursor.assets[j].update_location();
- bFound = true;
- }
- }
- }else{
- var ass = get_asset_from_json(data.refresh_assets[i]);
- if(ass !== undefined && (ass.type == "textbox" || ass.type == "videoitem")){
- // Check that we don't have one with the same id alread
- var bFound = false;
- for(var j=0;j<window.cursor.assets.length;j++){
- if(ass.type == "textbox" && window.cursor.assets[j].guid == ass.guid){
- console.log("we have this asset already (during refresh)")
- bFound = true;
- break;
- }else if(ass.type == "videoitem" && window.cursor.assets[j].div_id == ass.div_id){
- bFound = true;
- break;
- }
- }
- if(!bFound){
- if(ass.type == "videoitem"){
- ass.load_video();
- ass.is_master = true; // We have been given our own video back - make sure we know we are master
- }
- ass.sent = true; // Otherwise we'll just send it again
- ass.never_send = true; // We must never send this - it was obtained on a refresh
- window.cursor.assets.push(ass);
- }
- }else if(ass !== undefined && ass !== null){
- ass.sent = true; // Otherwise we'll just send it again
- ass.never_send = true; // We must never send this - it was obtained on a refresh
- window.cursor.assets.push(ass);
- }
- }
- }
- if(data.refresh_assets !== undefined && data.refresh_assets != null && data.refresh_assets.length > 0){
- window.cursor.refresh_lowest_point();
- }
- // Update the assets that have been sent from the other user(s) in the room
- // NOTE: If we need an asset that we don't add for a users refresh of their own assets
- // then they need to be added separately into this if statment - otherwise deal with it
- // in the get_asset_from_json() function.
- for(var i=0; i<assets.length;i++){
- var ass = assets[i];
- if(ass.type == "ripple"){
- // Add a ripple
- window.othercursor.ripples.push(new Ripple(ass.loc.x, ass.loc.y, window.othercursor.color));
- }else if(ass.type == "imageitemmove"){
- // Update the placement of the item by id
- for(var j=0; j<window.othercursor.assets.length;j++){
- var a = window.othercursor.assets[j];
- if(a.id == ass.id){
- window.othercursor.assets[j].loc.x = ass.loc.x;
- window.othercursor.assets[j].loc.y = ass.loc.y;
- }
- }
- }else if(ass.type == "textboxchange"){
- // console.log("Updating text in textbox")
- // console.log(ass)
- for(var j=0;j<window.cursor.assets.length;j++){
- // var a = window.othercursor.assets[j];
- var a = window.cursor.assets[j];
- if(a.type == "textbox" && ass.id == a.guid){
- // Create a new array for the text
- // window.othercursor.assets[j].display_text_array = []
- // window.cursor.assets[j].display_text_array = []
- window.cursor.assets[j].text = ass.text;
- window.cursor.assets[j].update_text();
- // for(var y=0;y<ass.data.length;y++){
- // // window.othercursor.assets[j].display_text_array[y] = [];
- // window.cursor.assets[j].display_text_array[y] = [];
- // for(var x=0;x<ass.data[y].length;x++){
- // // window.othercursor.assets[j].display_text_array[y][x] = ass.data[y][x];
- // window.cursor.assets[j].display_text_array[y][x] = ass.data[y][x];
- // }
- // }
- }
- }
- }else if(ass.type == "textboxmove"){
- var bFound = false;
- for(var j=0; j<window.cursor.assets.length;j++){
- var a = window.cursor.assets[j];
- if(a.type == "textbox" && ass.id == a.guid){
- window.cursor.assets[j].loc.x = ass.loc.x;
- window.cursor.assets[j].loc.y = ass.loc.y;
- window.cursor.assets[j].moved = false; // Just in case we were about to send a new position
- bFound = true;
- }
- }
- if(!bFound){
- console.log("Failed to find the textbox with id: " + ass.id);
- }
- }else if(ass.type == "videoitemmove"){
- var bFound = false;
- for(var j=0;j<window.cursor.assets.length && !bFound;j++){
- var a = window.cursor.assets[j];
- if(a.type == "videoitem" && a.div_id == ass.id){
- window.cursor.assets[j].loc.x = ass.loc.x;
- window.cursor.assets[j].loc.y = ass.loc.y;
- window.cursor.assets[j].moved = false;
- bFound = true;
- }
- }
- if(!bFound){
- console.log(ass)
- console.log("failed to find video item with div_id " + ass.id);
- }
- }else if(ass.type == "videoitemevent"){
- var bFound = false;
- for(var j=0;j<window.cursor.assets.length && !bFound;j++){
- var a = window.cursor.assets[j];
- if(a.type == "videoitem" && a.div_id == ass.id){
- bFound = true;
- window.cursor.assets[j].handle_event(ass);
- }
- }
- // if(ass.event_type == "play"){
- // var bFound = false;
- // for(var j=0;j<window.cursor.assets.length && !bFound;j++){
- // var a = window.cursor.assets[j];
- // if(a.type == "videoitem" && a.div_id == ass.id){
- // bFound = true;
- // window.cursor.assets[j].play_video(ass.seekTo);
- // }
- // }
- // }
- // if(ass.event_type == "stop"){
- // var bFound = false;
- // for(var j=0;j<window.cursor.assets.length && !bFound;j++){
- // var a = window.cursor.assets[j];
- // if(a.type == "videoitem" && a.div_id == ass.id){
- // bFound = true;
- // window.cursor.assets[j].stop_video();
- // }
- // }
- // }
- }else{
- var a = get_asset_from_json(ass);
- if(a !== undefined){
- // console.log("Getting a new assets " + ass.type);
- // console.log(a);
- if(a.type == "textbox"){
- a.sent = true;
- a.never_send = true;
- var bFound = false;
- for(var j=0;j<window.cursor.assets.length;j++){
- if(window.cursor.assets[j].guid == a.guid){
- bFound = true;
- console.log("we already have this " + a.type);
- break;
- }
- }
- // Don't push if we already have one with the same guid
- if(!bFound){
- if(a.name == "Messaging"){
- // Push messaging so it's the first one
- window.cursor.assets.unshift(a);
- }else{
- window.cursor.assets.push(a);
- }
- }
- }else if(a.type == "videoitem"){
- a.sent = true;
- a.never_send = true;
- var bFound = false;
- for(var j=0;j<window.cursor.assets.length;j++){
- if(window.cursor.assets[j].div_id == a.div_id){
- bFound = true;
- console.log("We already have this " + a.type + " , div_id: " + a.id);
- break;
- }
- }
- // Don't push if we already have one with the same guid
- if(!bFound){
- // Must remember to load video and create div
- a.load_video();
- window.cursor.assets.push(a);
- }
- }else{
- window.othercursor.assets.push(a);
- }
- }
- }
- }
- if(assets.length > 0){
- window.othercursor.refresh_lowest_point();
- }
- return false;
- });
- }
- if(socket != null){
- socket.on('status', function(e){
- var data = JSON.parse(e);
- if(data.status !== undefined){
- if(data.status == "fail"){
- console.log(data.message);
- }
- }
- });
- }
- var screen_width = $(window).width();
- var screen_height = $(window).height();
- function send() {
- // console.log("about to send......");
- var assets = [];
- for(var i=0;i<cursor.ripples.length;i++){
- var asset = cursor.ripples[i];
- if(asset.sent == false){
- var next_obj = {type: 'ripple', loc: {'x': asset.loc.x, 'y': asset.loc.y}};
- assets.push(next_obj);
- cursor.ripples[i].sent = true;
- // console.log(next_obj);
- }
- }
- for(var i=0;i<cursor.assets.length;i++){
- var asset = cursor.assets[i];
- // TODO: Add in asset
- if(asset.type == "line" && asset.sent == false){
- // console.log(cursor.assets);
- var next_obj = {
- type: "line",
- points: asset.points,
- size: asset.linesize,
- color: asset.color
- }
- // console.log("Sending line");
- assets.push(next_obj);
- cursor.assets[i].sent = true;
- }else if(asset.type == "templine" && asset.sent == false){
- var next_obj = {
- type: "templine",
- points: asset.points,
- size: asset.linesize,
- color: asset.color
- }
- assets.push(next_obj);
- cursor.assets[i].sent = true;
- }else if(asset.type == 'imageitem' && asset.sent == false && asset.uploaded == true){ // Wait until uploaded before sending
- var next_obj = {
- type: "imageitem",
- loc: {
- x: asset.loc.x,
- y: asset.loc.y
- },
- id: asset.id,
- filename: asset.server_filename
- }
- // TEMP DEBUG
- console.log("Sending image");
- console.log(next_obj);
- // TODO: push file_data
- assets.push(next_obj);
- cursor.assets[i].sent = true;
- }else if(asset.type === 'imageitem' && asset.moved === true){
- var next_obj = {
- type: "imageitemmove",
- loc: {
- x: asset.loc.x,
- y: asset.loc.y
- },
- id: asset.id
- }
- cursor.assets[i].moved = false;
- assets.push(next_obj);
- }else if(asset.type == "textbox" && asset.sent == false && asset.never_send == false){
- // The initial creation of the text box
- var next_obj = {
- type: "textbox",
- loc: {
- x: asset.loc.x,
- y: asset.loc.y,
- w: asset.size.x,
- h: asset.size.y
- },
- text: asset.name,
- id: asset.guid
- }
- // TODO: Figure out why this isn't actually working
- // IRL
- if(next_obj.loc.w == undefined){
- next_obj.loc.w = 400;
- }
- if(next_obj.loc.h == undefined){
- next_obj.loc.h = 200;
- }
- cursor.assets[i].sent = true;
- assets.push(next_obj);
- // console.log("Added textbox")
- // console.log(next_obj);
- }else if(asset.type == "textbox" && asset.sent == true && asset.changed == true){
- var next_obj = {
- type: "textboxchange",
- text: asset.text,
- id: asset.guid
- }
- cursor.assets[i].changed = false;
- assets.push(next_obj);
- }else if(asset.type == "textbox" && asset.sent == true && asset.moved == true){
- var next_obj = {
- type: "textboxmove",
- loc: {
- x: asset.loc.x,
- y: asset.loc.y
- },
- id: asset.guid
- }
- cursor.assets[i].moved = false;
- assets.push(next_obj);
- }else if(asset.type == "textbox" && asset.assets_dirty == true){
- for(var j=0;j<asset.assets.length;j++){
- var next_asset = asset.assets[j];
- if(next_asset.sent == false){
- var next_obj = {
- type: "textboxasset",
- id: asset.guid,
- asset: next_asset
- }
- }
- }
- }else if(asset.type == "textbox" && asset.assets_deleted == true){
- var next_obj = {
- type: "textboxassetclear",
- id: asset.guid
- }
- cursor.assets[i].assets_deleted = false;
- assets.push(next_obj);
- }else if(asset.type == "videoitem" && asset.sent == false && asset.url_set){
- var next_obj = {
- type: "videoitem",
- url: asset.url,
- loc: {x: asset.loc.x, y: asset.loc.y},
- size: {x: asset.size.x, y: asset.size.y},
- id: asset.div_id
- }
- cursor.assets[i].sent = true;
- // console.log("Sending video item");
- // console.log(next_obj);
- assets.push(next_obj);
- }else if(asset.type == "videoitem" && asset.sent == true && asset.moved == true){
- var next_obj = {
- type: "videoitemmove",
- loc: {
- x: asset.loc.x,
- y: asset.loc.y
- },
- id: asset.div_id
- }
- cursor.assets[i].moved = false;
- assets.push(next_obj);
- }
- if(asset.type == "videoitem" && asset.event_to_send != null){
- var next_obj = {
- type: "videoitemevent",
- event_type: asset.event_to_send.type,
- id: asset.div_id
- }
- assets.push(next_obj);
- cursor.assets[i].event_to_send = null; // Don't send twice
- }
- }
- // Prep the data object to send to the websocket
- var data_str = { cursor: {x: cursor.loc.x, y: cursor.loc.y, offset: cursor.offset}, screen: {width: screen_width, height: screen_height} };
- var data = {
- id: roomid,
- data: data_str,
- role: role,
- assets: assets
- }
- var bDebug = false;
- for(var i=0; i < data.assets.length; i++){
- if(data.assets[i].type !== "ripple"){
- bDebug = true;
- }
- }
- if(bDebug && data.assets.length > 0){
- console.log(data.assets);
- }
- if(socket !== null){
- // Send the updated information to the websocket
- socket.emit('classroom update', JSON.stringify(data));
- }
- setTimeout(send, 100);
- }
- // This will give us all of the assets - ours and theirs for initial setup
- // On return from that we will then start sending updates
- setTimeout(function(){
- if(socket === undefined || socket === null){
- console.log("The socket does not exist - are we debugging?");
- return;
- }
- socket.emit("refresh", {id: roomid, role:role});
- }, 1000);
- // Delay before we start the process of sending updates
- setTimeout(send, 3000);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement