diff --git a/BinaryDad.Coding/Controllers/HomeController.cs b/BinaryDad.Coding/Controllers/HomeController.cs index 07d2164..787b2c6 100644 --- a/BinaryDad.Coding/Controllers/HomeController.cs +++ b/BinaryDad.Coding/Controllers/HomeController.cs @@ -12,5 +12,18 @@ namespace BinaryDad.Coding.Controllers { 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); + } } } diff --git a/BinaryDad.Coding/Hubs/CodeHub.cs b/BinaryDad.Coding/Hubs/CodeHub.cs index 5ab253f..b7b472a 100644 --- a/BinaryDad.Coding/Hubs/CodeHub.cs +++ b/BinaryDad.Coding/Hubs/CodeHub.cs @@ -7,38 +7,51 @@ namespace BinaryDad.Coding.Hubs { public class CodeHub : Hub { - private static readonly IDictionary users = new Dictionary(StringComparer.OrdinalIgnoreCase); - - public Task UpdateCode(string user, int index, string code) + public Task UpdateCode(string user, int index, string code, bool isTeacher) { - 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) { - users[Context.ConnectionId].Name = name; - users[Context.ConnectionId].Color = color; + Users.Sessions[Context.ConnectionId].Id = Context.ConnectionId; + 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() { - users[Context.ConnectionId] = new User + Users.Sessions[Context.ConnectionId] = new User { Name = "" }; - await Clients.All.SendAsync("UsersList", users); + await Clients.All.SendAsync("UsersList", Users.Sessions); await base.OnConnectedAsync(); } 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); } diff --git a/BinaryDad.Coding/User.cs b/BinaryDad.Coding/User.cs index ef13868..4d5ae0e 100644 --- a/BinaryDad.Coding/User.cs +++ b/BinaryDad.Coding/User.cs @@ -2,7 +2,10 @@ { public class User { + public string Id { get; set; } public string Name { get; set; } public string Color { get; set; } + public string Html { get; set; } + public string Css { get; set; } } } diff --git a/BinaryDad.Coding/Users.cs b/BinaryDad.Coding/Users.cs new file mode 100644 index 0000000..f4c97e3 --- /dev/null +++ b/BinaryDad.Coding/Users.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; + +namespace BinaryDad.Coding +{ + public static class Users + { + public static readonly IDictionary Sessions = new Dictionary(StringComparer.OrdinalIgnoreCase); + } +} diff --git a/BinaryDad.Coding/Views/Home/ViewHtml.cshtml b/BinaryDad.Coding/Views/Home/ViewHtml.cshtml new file mode 100644 index 0000000..bc9d5af --- /dev/null +++ b/BinaryDad.Coding/Views/Home/ViewHtml.cshtml @@ -0,0 +1,14 @@ +@model User +@{ + Layout = null; + + var html = Model.Html; + var htmlTagIndex = Model.Html.IndexOf(""); + + if (htmlTagIndex > 0) + { + html = html.Insert(htmlTagIndex, $""); + } +} + +@Html.Raw(html) \ No newline at end of file diff --git a/BinaryDad.Coding/wwwroot/js/hub.js b/BinaryDad.Coding/wwwroot/js/hub.js index cc9ba7a..7445f43 100644 --- a/BinaryDad.Coding/wwwroot/js/hub.js +++ b/BinaryDad.Coding/wwwroot/js/hub.js @@ -44,34 +44,32 @@ isLiveMode(); setHeight(); - window.location.hash = '#live'; // set live mode on by default let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9'; if (isTeacher) { $liveModeLink.hide(); $liveModeStatus.hide(); + } else { + window.location.hash = '#live'; // set live mode on by default } 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'; - let code = editor.getValue(); - - connection.invoke('UpdateCode', user, i, code).catch(function (err) { - return console.error(err.toString()); - }); + connection.invoke('UpdateCode', user, i, code, isTeacher).catch(function (err) { + return console.error(err.toString()); }); }); - } + }); connection.on('ReceiveCode', function (user, index, code) { @@ -87,7 +85,7 @@ let usersList = Object.values(users) .filter(u => u.name !== '') - .map(u => `${u.name}`); + .map(u => `${u.name}`); $usersList.html(`${usersList.length} users online: ${usersList.join(', ')}`);