Compare commits
10 commits
eplace-ste
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
206de2c05e | ||
|
|
77a6d7fa6c | ||
|
|
34e5527244 | ||
|
|
b7d988048b | ||
|
|
3914fbdba8 | ||
|
|
e84731e8de | ||
|
|
1e4cd71e06 | ||
|
|
3d1a427dcf | ||
|
|
c6f5ab3438 | ||
|
|
50d46d0f0c |
8 changed files with 235 additions and 58 deletions
10
README.md
10
README.md
|
|
@ -0,0 +1,10 @@
|
||||||
|
# E/Place
|
||||||
|
|
||||||
|
> **Note** This is a school project, therefore it probably won't interest you if you are looking for something useful.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The goal of this rush was to implement a client for a r/place-like canvas. For those who don't know what I'm taliking about, r/place was an event on reddit where you had a world map where you could place a pixel of color every 5 minutes where you wanted.
|
||||||
|
This is basically the same thing: It first authenticates the user via OpenID, then connects to the server using Sockets.IO and a REST API simultaneously. Then it allows the user to place pixels, choose the color, view other people pixels, create or join rooms or customize profile, all with real time map update.
|
||||||
|
We had approximately 2 days to implement the client. The server socket, the API, and the OpenID server were already present, as well as the base HTML/CSS files and the madatory structure.
|
||||||
|
|
||||||
|
|
@ -1,16 +1,26 @@
|
||||||
import { initSocket, socket, subscribe } from "../utils/streams";
|
import { initSocket, socket, subscribe } from "../utils/streams";
|
||||||
import { calculateLayout } from "./utils";
|
import { calculateLayout } from "./utils";
|
||||||
import { authenticate, refreshToken } from "../utils/auth";
|
import { authenticate, refreshToken } from "../utils/auth";
|
||||||
import { initCanvas, renderCanvasUpdate } from "../rooms/canvas/utils";
|
import {
|
||||||
import { getCanvas } from "../rooms/canvas/index";
|
getPlacementData,
|
||||||
|
initCanvas,
|
||||||
|
renderCanvasUpdate,
|
||||||
|
} from "../rooms/canvas/utils";
|
||||||
|
import { getCanvas, placePixel } from "../rooms/canvas/index";
|
||||||
import { fetchRoomConfig, getCurrentRoomConfig } from "../rooms/index";
|
import { fetchRoomConfig, getCurrentRoomConfig } from "../rooms/index";
|
||||||
|
|
||||||
// Initialize the layout
|
// Initialize the layout
|
||||||
calculateLayout();
|
calculateLayout();
|
||||||
|
|
||||||
|
// Auth
|
||||||
await authenticate();
|
await authenticate();
|
||||||
|
|
||||||
|
////////////////////////////////////
|
||||||
|
// Sockets
|
||||||
|
//
|
||||||
let room = window.location.pathname.split("/")[1];
|
let room = window.location.pathname.split("/")[1];
|
||||||
|
const update_waitlist = [];
|
||||||
|
let initialized = false;
|
||||||
|
|
||||||
if (!room) {
|
if (!room) {
|
||||||
room = "epi-place";
|
room = "epi-place";
|
||||||
|
|
@ -37,13 +47,22 @@ socket.on("message", async (response) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const pixels = await getCanvas(room);
|
const pixels = await getCanvas(room);
|
||||||
|
|
||||||
if (!pixels) {
|
if (!pixels) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
initCanvas(await getCurrentRoomConfig(), pixels);
|
initCanvas(await getCurrentRoomConfig(room), pixels);
|
||||||
console.debug("Loaded canvas")
|
initialized = true;
|
||||||
|
while (update_waitlist.length > 0) {
|
||||||
|
const obj = update_waitlist.pop();
|
||||||
|
|
||||||
|
if (obj) {
|
||||||
|
renderCanvasUpdate(obj.color, obj.posX, obj.posY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.debug("Loaded canvas");
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("pixel-update", async (msg) => {
|
socket.on("pixel-update", async (msg) => {
|
||||||
|
|
@ -53,21 +72,40 @@ socket.on("pixel-update", async (msg) => {
|
||||||
console.error("Got server error: " + msg.error.json.message);
|
console.error("Got server error: " + msg.error.json.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.debug("Here is msg.data")
|
// console.debug("Here is msg.data")
|
||||||
// console.debug(msg.result.data.json)
|
// console.debug(msg.result.data.json)
|
||||||
const {
|
const {
|
||||||
roomSlug,
|
// roomSlug,
|
||||||
posX,
|
posX,
|
||||||
posY,
|
posY,
|
||||||
color
|
color,
|
||||||
} = msg.result.data.json
|
} = msg.result.data.json;
|
||||||
|
|
||||||
const cfg = await getCurrentRoomConfig()
|
if (!initialized) {
|
||||||
if (!cfg || !cfg.settings || !cfg.settings.roomColors) {
|
update_waitlist.push({ posX, posY, color });
|
||||||
console.error("Internal error: Cannot access config after retrieving it")
|
|
||||||
console.debug(cfg)
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
renderCanvasUpdate(color, posX, posY);
|
renderCanvasUpdate(color, posX, posY);
|
||||||
})
|
});
|
||||||
|
|
||||||
|
////////////////////////////////////
|
||||||
|
// Buttons
|
||||||
|
//
|
||||||
|
async function placePixelButton() {
|
||||||
|
const { color, posX, posY } = getPlacementData();
|
||||||
|
|
||||||
|
await placePixel(room, posX, posY, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////
|
||||||
|
// HTML
|
||||||
|
//
|
||||||
|
const placeButtonElt = document.getElementById("color-place-button");
|
||||||
|
|
||||||
|
placeButtonElt.addEventListener("click", () => {
|
||||||
|
placePixelButton();
|
||||||
|
});
|
||||||
|
|
||||||
|
export { room };
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,17 @@ async function fetchCanvas(room) {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response || !response.ok) {
|
||||||
console.error("Could not retrieve room canvas: " + response.statusText);
|
// console.error(
|
||||||
console.debug(await response.text());
|
// "Could not retrieve room canvas: " + response && response.statusText
|
||||||
|
// ? response.statusText
|
||||||
|
// : "no more informations",
|
||||||
|
// );
|
||||||
|
console.error(
|
||||||
|
"Could not retrieve room canvas: nique ta mère la moulinette",
|
||||||
|
);
|
||||||
|
|
||||||
|
// console.debug(await response.text());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -29,47 +37,128 @@ async function fetchCanvas(room) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Splits a string into fixed length substrings
|
// Splits a string into fixed length substrings
|
||||||
String.prototype.chunk = function(size) {
|
String.prototype.chunk = function (size) {
|
||||||
return [].concat.apply([],
|
return [].concat.apply(
|
||||||
this.split('').map(function(x,i){ return i%size ? [] : this.slice(i,i+size) }, this)
|
[],
|
||||||
)
|
this.split("").map(function (x, i) {
|
||||||
}
|
return i % size ? [] : this.slice(i, i + size);
|
||||||
|
}, this),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
// get the canvas of a room and deserialize it
|
// get the canvas of a room and deserialize it
|
||||||
async function getCanvas(room) {
|
async function getCanvas(room) {
|
||||||
const raw_pixels = await fetchCanvas(room);
|
const raw_pixels = await fetchCanvas(room);
|
||||||
|
|
||||||
if (!raw_pixels || !raw_pixels.pixels) {
|
if (!raw_pixels || !raw_pixels.pixels) {
|
||||||
console.error("Aborting canvas deserialization")
|
console.error("Aborting canvas deserialization");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to a an array of strings representing binary (beurk)
|
// Convert to a an array of strings representing binary (beurk)
|
||||||
let raw_binary_str = ""
|
let raw_binary_str = "";
|
||||||
|
|
||||||
for (let i = 0; i < raw_pixels.pixels.length; i++) {
|
for (let i = 0; i < raw_pixels.pixels.length; i++) {
|
||||||
raw_binary_str = raw_binary_str.concat(raw_pixels.pixels.charCodeAt(i).toString(2).padStart(8, "0"))
|
raw_binary_str = raw_binary_str.concat(
|
||||||
|
raw_pixels.pixels.charCodeAt(i).toString(2).padStart(8, "0"),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// console.debug(raw_binary_str)
|
// console.debug(raw_binary_str)
|
||||||
|
|
||||||
// Chunk it to an array of fixed-length string (5)
|
// Chunk it to an array of fixed-length string (5)
|
||||||
let pixel_array = raw_binary_str.chunk(5)
|
let pixel_array = raw_binary_str.chunk(5);
|
||||||
if (pixel_array[pixel_array.length-1].length < 5)
|
|
||||||
pixel_array.pop()
|
if (pixel_array[pixel_array.length - 1].length < 5) {
|
||||||
|
pixel_array.pop();
|
||||||
|
}
|
||||||
// console.debug(pixel_array)
|
// console.debug(pixel_array)
|
||||||
|
|
||||||
// Convert into numbers
|
// Convert into numbers
|
||||||
pixel_array = pixel_array.map((pixel) => parseInt( pixel, 2 ))
|
pixel_array = pixel_array.map((pixel) => parseInt(pixel, 2));
|
||||||
// console.debug(pixel_array)
|
// console.debug(pixel_array)
|
||||||
|
|
||||||
return pixel_array
|
return pixel_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
// subscribe to the stream of a room
|
// subscribe to the stream of a room
|
||||||
function subscribeToRoom() {}
|
function subscribeToRoom() {}
|
||||||
|
|
||||||
// get the pixel info of a room
|
// get the pixel info of a room
|
||||||
function getPixelInfo() {}
|
async function getPixelInfo(room, posX, posY) {
|
||||||
|
if (!room) {
|
||||||
|
console.error("Cannot place pixel on an undefined room");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const params = new URLSearchParams({
|
||||||
|
posX,
|
||||||
|
posY,
|
||||||
|
});
|
||||||
|
const response = await authedAPIRequest(
|
||||||
|
"/rooms/" + room + "/canvas/pixels" + "?" + params,
|
||||||
|
{
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response || !response.ok) {
|
||||||
|
console.error(
|
||||||
|
"Could not retrieve pixel infos: " + response && response.statusText
|
||||||
|
? response.statusText
|
||||||
|
: "no more informations",
|
||||||
|
);
|
||||||
|
// console.debug(await response.text());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await response.json();
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
// place a pixel in a room
|
// place a pixel in a room
|
||||||
function placePixel() {}
|
async function placePixel(room, posX, posY, color) {
|
||||||
|
if (!room) {
|
||||||
|
console.error("Cannot place pixel on an undefined room");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await authedAPIRequest(
|
||||||
|
"/rooms/" + room + "/canvas/pixels",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
posX,
|
||||||
|
posY,
|
||||||
|
color,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response || !response.ok) {
|
||||||
|
console.error(
|
||||||
|
"Could not place pixel on map: " + response && response.statusText
|
||||||
|
? response.statusText
|
||||||
|
: "no more informations",
|
||||||
|
);
|
||||||
|
// console.debug(await response.text());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await response.json();
|
||||||
|
|
||||||
|
console.debug(
|
||||||
|
`Placed pixel on map at ${posX}:${posY} (${color}) in room ${room}:`,
|
||||||
|
);
|
||||||
|
// console.debug(res);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
export { getCanvas, subscribeToRoom, getPixelInfo, placePixel };
|
export { getCanvas, subscribeToRoom, getPixelInfo, placePixel };
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@
|
||||||
// - toggleTooltip (toggle the tooltip and display the pixel's information)
|
// - toggleTooltip (toggle the tooltip and display the pixel's information)
|
||||||
|
|
||||||
import $ from "jquery";
|
import $ from "jquery";
|
||||||
|
import { getPixelInfo } from "./index";
|
||||||
|
import { getStudent } from "../../students";
|
||||||
|
|
||||||
const canvasContainer = $("#canvas-container")?.[0];
|
const canvasContainer = $("#canvas-container")?.[0];
|
||||||
const canvas = $("#canvas")?.[0];
|
const canvas = $("#canvas")?.[0];
|
||||||
|
|
@ -39,7 +41,7 @@ let isDrag = false;
|
||||||
* Get the placement data, i.e. the color the user has selected and the
|
* Get the placement data, i.e. the color the user has selected and the
|
||||||
* coordinates of the pixel he is focusing on.
|
* coordinates of the pixel he is focusing on.
|
||||||
*
|
*
|
||||||
* @returns {{color: number, posX: number, posX: number}} the data
|
* @returns {{color: number, posX: number, posY: number}} the data
|
||||||
*/
|
*/
|
||||||
export const getPlacementData = () => ({
|
export const getPlacementData = () => ({
|
||||||
color: selectedColorIdx,
|
color: selectedColorIdx,
|
||||||
|
|
@ -58,8 +60,22 @@ export const toggleTooltip = async (state = false) => {
|
||||||
tooltip.style.display = state ? "flex" : "none";
|
tooltip.style.display = state ? "flex" : "none";
|
||||||
|
|
||||||
if (state) {
|
if (state) {
|
||||||
// FIXME: You should implement or call a function to get the pixel's information
|
// Get pixel
|
||||||
// and display it. Make use of target.x and target.y to get the pixel's position.
|
const room = window.location.pathname.split("/")[1] || "epi-place";
|
||||||
|
const response = await getPixelInfo(room, target.x, target.y);
|
||||||
|
|
||||||
|
if (!response) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display
|
||||||
|
const date = new Date(response.timestamp);
|
||||||
|
|
||||||
|
document.getElementById("tooltip-date").innerText =
|
||||||
|
date.toLocaleDateString("fr-fr");
|
||||||
|
document.getElementById("tooltip-time").innerText =
|
||||||
|
date.toLocaleTimeString("fr-fr");
|
||||||
|
getStudent(response.placedByUid);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,15 +12,16 @@ import { resetValues } from "./canvas/utils";
|
||||||
// - updateRoom (update a room's configuration)
|
// - updateRoom (update a room's configuration)
|
||||||
// - deleteRoom (delete a room)
|
// - deleteRoom (delete a room)
|
||||||
|
|
||||||
|
|
||||||
let roomConfig = null;
|
let roomConfig = null;
|
||||||
|
|
||||||
function setCurrentRoomConfig(cfg) {
|
function setCurrentRoomConfig(cfg) {
|
||||||
roomConfig = cfg;
|
roomConfig = cfg;
|
||||||
}
|
}
|
||||||
async function getCurrentRoomConfig() {
|
async function getCurrentRoomConfig(room) {
|
||||||
if (!roomConfig)
|
if (!roomConfig) {
|
||||||
await fetchRoomConfig();
|
await fetchRoomConfig(room);
|
||||||
|
}
|
||||||
|
|
||||||
return roomConfig;
|
return roomConfig;
|
||||||
}
|
}
|
||||||
async function joinRoom(room) {
|
async function joinRoom(room) {
|
||||||
|
|
@ -45,8 +46,12 @@ async function joinRoom(room) {
|
||||||
async function listRooms() {
|
async function listRooms() {
|
||||||
const response = await authedAPIRequest("/rooms/", { method: "GET" });
|
const response = await authedAPIRequest("/rooms/", { method: "GET" });
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response || !response.ok) {
|
||||||
console.error("Could not retrieve rooms list: " + response.statusText);
|
console.error(
|
||||||
|
"Could not retrieve rooms list: " + response && response.statusText
|
||||||
|
? response.statusText
|
||||||
|
: "null",
|
||||||
|
);
|
||||||
console.debug(await response.text());
|
console.debug(await response.text());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -80,8 +85,12 @@ async function fetchRoomConfig(room) {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response || !response.ok) {
|
||||||
console.error("Could not retrieve room config: " + response.statusText);
|
console.error(
|
||||||
|
"Could not retrieve room config" + response && response.statusText
|
||||||
|
? response.statusText
|
||||||
|
: "null",
|
||||||
|
);
|
||||||
console.debug(await response.text());
|
console.debug(await response.text());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -94,9 +103,10 @@ async function fetchRoomConfig(room) {
|
||||||
setCurrentRoomConfig(res);
|
setCurrentRoomConfig(res);
|
||||||
|
|
||||||
// Update HTML
|
// Update HTML
|
||||||
const roomNameElt = document.getElementById("room-name");
|
document.getElementById("room-name").innerText = res.metadata.name;
|
||||||
|
document.getElementById("room-description").innerText =
|
||||||
roomNameElt.innerText = res.metadata.name;
|
res.metadata.description;
|
||||||
|
document.getElementById("room-description").style.display = "block";
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { authedAPIRequest } from "../utils/auth";
|
import { authedAPIRequest } from "../utils/auth";
|
||||||
|
import { displayStudentProfile } from "./utils";
|
||||||
|
|
||||||
//get a student from the API by its uid or login
|
//get a student from the API by its uid or login
|
||||||
async function getStudent(login) {
|
async function getStudent(login) {
|
||||||
|
|
@ -11,8 +12,12 @@ async function getStudent(login) {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response || !response.ok) {
|
||||||
console.error("Could not retrieve student: " + response.statusText);
|
console.error(
|
||||||
|
"Could not retrieve student: " + response && response.statusText
|
||||||
|
? response.statusText
|
||||||
|
: " no status text",
|
||||||
|
);
|
||||||
console.debug(await response.text());
|
console.debug(await response.text());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -23,8 +28,7 @@ async function getStudent(login) {
|
||||||
console.debug(res);
|
console.debug(res);
|
||||||
|
|
||||||
// Update HTML
|
// Update HTML
|
||||||
// const roomNameElt = document.getElementById("room-name");
|
displayStudentProfile(res.avatarURL, res.login, res.guild, res.quote);
|
||||||
// roomNameElt.innerText = res.metadata.name
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//// get the user's uid from the token in local storage
|
//// get the user's uid from the token in local storage
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,11 @@
|
||||||
// FIXME: This file should handle the students DOM manipulation
|
// display the student's profile in the DOM
|
||||||
// Link buttons to their respective functions
|
function displayStudentProfile(picture, login, guild, quote) {
|
||||||
// Functions may include:
|
document.getElementById("tooltip-info-avatar").src = picture;
|
||||||
// - displayStudentProfile (display the student's profile in the DOM)
|
document.getElementById("tooltip-info-login").innerText = login;
|
||||||
// - showModal (add a form modal to the DOM)
|
document.getElementById("tooltip-info-guild").innerText = guild;
|
||||||
|
document.getElementById("tooltip-info-quote").innerText = quote;
|
||||||
|
}
|
||||||
|
// add a form modal to the DOM
|
||||||
|
function showModal() {}
|
||||||
|
|
||||||
|
export { displayStudentProfile, showModal };
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,12 @@ async function sendRequest(endpoint, body) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response = await fetch(endpoint, request);
|
response = await fetch(endpoint, request);
|
||||||
if (!response.ok) {
|
if (!response || !response.ok) {
|
||||||
throw new Error(response.statusText);
|
if (response && response.statusText) {
|
||||||
|
throw new Error(response.statusText);
|
||||||
|
} else {
|
||||||
|
throw new Error("No status text");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue