Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Songlist webserver #911

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ macosx-app: all
# Must be done BEFORE info.plist is created.
$(MKDIR) $(macosx_bundle_path)/resources
$(INSTALL_DATA) icons/ultrastardx.icns $(macosx_bundle_path)/resources/
$(MKDIR) $(macosx_bundle_path)/resources/web
$(INSTALL_DATA) game/resources/web/index.html $(macosx_bundle_path)/resources/
$(INSTALL_DATA) game/resources/web/datatables.min.js $(macosx_bundle_path)/resources/web/
$(INSTALL_DATA) game/resources/web/datatables.min.css $(macosx_bundle_path)/resources/web/
$(INSTALL_DATA) game/resources/web/jquery.min.js $(macosx_bundle_path)/resources/web/
$(INSTALL_DATA) game/resources/web/jquery.min.map $(macosx_bundle_path)/resources/web/

# the info.plist file
$(INSTALL_DATA) $(USDX_SRC_DIR)/macosx/Info.plist $(macosx_bundle_path)/
Expand Down
15 changes: 15 additions & 0 deletions game/resources/web/datatables.min.css

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions game/resources/web/datatables.min.js

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions game/resources/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Song List</title>

<script src="/theme.js" fetchpriority="high" blocking="render"></script>
<link rel="stylesheet" href="/style.css" />

<script src="/jquery.min.js"></script>
<script src="/datatables.min.js"></script>
<link rel="stylesheet" href="/datatables.min.css" />
</head>
<body>
<h1>Song List</h1>

<div class="controls">
<div class="toggle-switch">
<label>
<input type="checkbox" id="themeToggle">
<span class="slider"></span>
</label>
</div>
</div>

<table id="songTable" class="hover stripe">
<thead>
<tr>
<th>Artist</th>
<th>Title</th>
<th>Edition</th>
<th>Genre</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<!--SONG_ROWS-->
</tbody>
</table>

<script>
const defaultPageLength = 25;
const PAGE_LENGTH = "pageLength";

let pageLength = parseInt(localStorage.getItem(PAGE_LENGTH), 10);
if (!pageLength || pageLength < 10 || pageLength > 100) {
pageLength = defaultPageLength;
}

let table = new DataTable('#songTable', {
// default entries per page
pageLength: pageLength,
layout: {
top: {
search: {
text: '',
placeholder: 'Search for songs…'
}
},
topStart: null,
topEnd: null,
bottomStart: [
{
pageLength: {
menu: [ 10, 25, 50, 100 ]
}
},
],
bottom2: {
info: {
text: 'Showing _START_ to _END_ of _TOTAL_ songs'
}
},
bottomEnd: {
paging: {
// numbers: 6
}
}
}
});
table.on('length.dt', function (e, settings, len) {
localStorage.setItem(PAGE_LENGTH, len);
});
</script>
</body>
</html>
2 changes: 2 additions & 0 deletions game/resources/web/jquery.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions game/resources/web/jquery.min.map

Large diffs are not rendered by default.

107 changes: 107 additions & 0 deletions game/resources/web/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* Default colors */
:root {
--text-color: #121212;
--bkg-color: #fff;
--accent-color: #add8e6;
}
/* Dark theme colors */
[data-theme="dark"] {
--text-color: #fff;
--bkg-color: #181a1b;
--accent-color: rgb(27, 73, 88);
}

body {
font-family: Arial, sans-serif;
background: var(--bkg-color);
color: var(--text-color);
}

table {
width: 100% !important;
}

th, td {
padding: 8px;
text-align: left;
}

th {
background-color: var(--accent-color);
}

input {
margin-bottom: 12px;
padding: 8px;
width: 100%;
}

.controls {
position: absolute;
top: 12px;
right: 10px;
}


/* Toggle Switch */
/* inspired by:
- https://codepen.io/alvarotrigo/pen/zYPydpB
- https://dribbble.com/shots/14199649-Dark-Light-Mode-Toggle-Switch-Pattern-A11y
*/

:root {
--switch-light: #d8dbe0;
--switch-dark: #28292c;
--switch-width: 60px;
--switch-height: 30px;
}

.toggle-switch {
position: relative;
width: var(--switch-width);

label {
position: absolute;
width: 100%;
height: var(--switch-height);
background-color: var(--switch-dark);
border-radius: calc(var(--switch-height)/2);
cursor: pointer;
}

input {
position: absolute;
display: none;
}

.slider {
position: absolute;
width: 100%;
height: 100%;
border-radius: calc(var(--switch-height)/2);
transition: 0.3s;
}

input:checked ~ .slider {
background-color: var(--switch-light);
}

.slider::before {
content: "";
position: absolute;
top: calc(var(--switch-height)*0.13);
left: calc(var(--switch-height)*0.16);
width: calc(var(--switch-height)*0.75);
height: calc(var(--switch-height)*0.75);
border-radius: 50%;
box-shadow: inset calc(var(--switch-height)*0.28) -4px 0 0 var(--switch-light);
background-color: var(--switch-dark);
transition: 0.3s;
}

input:checked ~ .slider::before {
transform: translateX(calc(var(--switch-height)*0.95));
background-color: var(--switch-dark);
box-shadow: none;
}
}
56 changes: 56 additions & 0 deletions game/resources/web/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const LIGHT_THEME = "light", DARK_THEME = "dark";

function isValidTheme(theme) {
return theme === LIGHT_THEME || theme === DARK_THEME
}

function getTheme() {
let theme = localStorage.getItem("theme");
if (isValidTheme(theme)) {
return theme;
}

if (window.matchMedia("(prefers-color-scheme: dark)")) {
return DARK_THEME;
}

return LIGHT_THEME;
}

function isLightTheme() {
return getTheme() === LIGHT_THEME
}

function setTheme(theme) {
if (!isValidTheme(theme)) return;

document.documentElement.setAttribute("data-theme", theme);
localStorage.setItem("theme", theme);
}

function toggleTheme() {
if (isLightTheme()) {
setTheme(DARK_THEME);
} else {
setTheme(LIGHT_THEME);
}
}

// initialize theme
setTheme(getTheme());

function updateToggle(toggle) {
toggle.checked = isLightTheme()
}

window.onload = function() {
let toggle = document.querySelector("#themeToggle");

toggle.addEventListener("change", () => {
toggleTheme();
updateToggle(toggle);
});

// initialize toggle
updateToggle(toggle);
}
18 changes: 17 additions & 1 deletion src/base/UMain.pas
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ implementation
ULuaParty,
ULuaScreenSing,
UTime,
UWebcam;
UWebcam,
UWebServer;
//UVideoAcinerella;

procedure Main;
var
WindowTitle: string;
BadPlayer: integer;
Server: TWebServer;

begin
{$IFNDEF Debug}
try
Expand Down Expand Up @@ -262,6 +265,14 @@ procedure Main;
Display.CurrentScreen^.FadeTo( @ScreenOptionsRecord );
end;

//------------------------------
// Start Webserver
//------------------------------
Log.LogStatus('Webserver', 'Initialization');
// Create and start the web server
Server := TWebServer.Create(8091);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make the port configurable, but set it to strictly only listen on localhost. If people want to have it reachable over network, this forces them to put at the very least a tiny bit of effort into setting up a reverse proxy, which then hopefully has much less attack surface than the old UWebServer.

Server.Start;

//------------------------------
// Start Mainloop
//------------------------------
Expand Down Expand Up @@ -296,6 +307,11 @@ procedure Main;
Log.LogStatus('Finalize SDL', 'Finalization');
SDL_Quit();

Log.LogStatus('Finalize Webserver', 'Finalization');
//Server.Terminate; // Terminate the thread
//Server.WaitFor;
//Server.Free;

Log.LogStatus('Finalize Log', 'Finalization');
{$IFNDEF Debug}
end;
Expand Down
Loading
Loading