viewing of others code working

This commit is contained in:
Ryan Peters 2021-07-22 20:41:12 -04:00
parent 59e5bf2585
commit 2d36f0d2f6
6 changed files with 75 additions and 24 deletions

View File

@ -12,5 +12,18 @@ namespace BinaryDad.Coding.Controllers
{ {
return View(); return View();
} }
public bool IsBadWord(string word)
{
return false;
}
[Route("view/{connectionId}")]
public IActionResult ViewHtml(string connectionId)
{
var user = Users.Sessions[connectionId];
return View(user);
}
} }
} }

View File

@ -7,38 +7,51 @@ namespace BinaryDad.Coding.Hubs
{ {
public class CodeHub : Hub public class CodeHub : Hub
{ {
private static readonly IDictionary<string, User> users = new Dictionary<string, User>(StringComparer.OrdinalIgnoreCase); public Task UpdateCode(string user, int index, string code, bool isTeacher)
public Task UpdateCode(string user, int index, string code)
{ {
return Clients.All.SendAsync("ReceiveCode", user, index, code); if (index == 0)
{
Users.Sessions[Context.ConnectionId].Html = code;
}
else if (index == 1)
{
Users.Sessions[Context.ConnectionId].Css = code;
}
if (isTeacher)
{
return Clients.All.SendAsync("ReceiveCode", user, index, code);
}
return null;
} }
public async Task SaveName(string name, string color) public async Task SaveName(string name, string color)
{ {
users[Context.ConnectionId].Name = name; Users.Sessions[Context.ConnectionId].Id = Context.ConnectionId;
users[Context.ConnectionId].Color = color; Users.Sessions[Context.ConnectionId].Name = name;
Users.Sessions[Context.ConnectionId].Color = color;
await Clients.All.SendAsync("UsersList", users); await Clients.All.SendAsync("UsersList", Users.Sessions);
} }
public override async Task OnConnectedAsync() public override async Task OnConnectedAsync()
{ {
users[Context.ConnectionId] = new User Users.Sessions[Context.ConnectionId] = new User
{ {
Name = "" Name = ""
}; };
await Clients.All.SendAsync("UsersList", users); await Clients.All.SendAsync("UsersList", Users.Sessions);
await base.OnConnectedAsync(); await base.OnConnectedAsync();
} }
public override async Task OnDisconnectedAsync(Exception exception) public override async Task OnDisconnectedAsync(Exception exception)
{ {
users.Remove(Context.ConnectionId); Users.Sessions.Remove(Context.ConnectionId);
await Clients.All.SendAsync("UsersList", users); await Clients.All.SendAsync("UsersList", Users.Sessions);
await base.OnDisconnectedAsync(exception); await base.OnDisconnectedAsync(exception);
} }

View File

@ -2,7 +2,10 @@
{ {
public class User public class User
{ {
public string Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Color { get; set; } public string Color { get; set; }
public string Html { get; set; }
public string Css { get; set; }
} }
} }

10
BinaryDad.Coding/Users.cs Normal file
View File

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
namespace BinaryDad.Coding
{
public static class Users
{
public static readonly IDictionary<string, User> Sessions = new Dictionary<string, User>(StringComparer.OrdinalIgnoreCase);
}
}

View File

@ -0,0 +1,14 @@
@model User
@{
Layout = null;
var html = Model.Html;
var htmlTagIndex = Model.Html.IndexOf("<body>");
if (htmlTagIndex > 0)
{
html = html.Insert(htmlTagIndex, $"<head><style type=\"text/css\">{Model.Css}</style></head>");
}
}
@Html.Raw(html)

View File

@ -44,34 +44,32 @@
isLiveMode(); isLiveMode();
setHeight(); setHeight();
window.location.hash = '#live'; // set live mode on by default
let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9'; let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9';
if (isTeacher) { if (isTeacher) {
$liveModeLink.hide(); $liveModeLink.hide();
$liveModeStatus.hide(); $liveModeStatus.hide();
} else {
window.location.hash = '#live'; // set live mode on by default
} }
let $editors = $('.ace_editor'); let $editors = $('.ace_editor');
if (isTeacher) { $editors.each(function (i, e) {
$editors.each(function (i, e) { let editor = ace.edit(e);
let editor = ace.edit(e); editor.session.on('change', function () {
editor.session.on('change', function () { let user = $yourNameText.val();
let code = editor.getValue();
let user = 'ryan'; connection.invoke('UpdateCode', user, i, code, isTeacher).catch(function (err) {
let code = editor.getValue(); return console.error(err.toString());
connection.invoke('UpdateCode', user, i, code).catch(function (err) {
return console.error(err.toString());
});
}); });
}); });
} });
connection.on('ReceiveCode', function (user, index, code) { connection.on('ReceiveCode', function (user, index, code) {
@ -87,7 +85,7 @@
let usersList = Object.values(users) let usersList = Object.values(users)
.filter(u => u.name !== '') .filter(u => u.name !== '')
.map(u => `<span style="color: ${u.color};" title="${u.color}">${u.name}</span>`); .map(u => `<a href="/view/${u.id}" style="color: ${u.color};" title="${u.color}">${u.name}</a>`);
$usersList.html(`<strong>${usersList.length} users online:</strong> ${usersList.join(', ')}`); $usersList.html(`<strong>${usersList.length} users online:</strong> ${usersList.join(', ')}`);