Skip to content

Instantly share code, notes, and snippets.

@orlax
Created January 13, 2024 15:54
Show Gist options
  • Save orlax/f54b5a1705de77debe967b23f8a00136 to your computer and use it in GitHub Desktop.
Save orlax/f54b5a1705de77debe967b23f8a00136 to your computer and use it in GitHub Desktop.
Que Pelicula Ver / What move should I Watch?
const fs = require('fs');
const filename = 'movies.json';
function readData() {
try {
// Check if the file exists
if (!fs.existsSync(filename)) {
// If not, create an empty file
fs.writeFileSync(filename, '{"movies":[]}', 'utf8');
}
const data = fs.readFileSync(filename, 'utf8');
return JSON.parse(data);
} catch (error) {
return { movies: [] };
}
}
function writeData(data){
const json = JSON.stringify(data, null, "\t");
fs.writeFileSync(filename, json, "utf8");
}
function list(){
const movies = readData();
console.log("Got this: ", movies);
console.log("Peliculas por ver");
movies.movies.forEach(movie=>{
if(!movie.vista){
console.log(" - "+movie.name);
}
});
console.log("---------------------------");
console.log("Peliculas Vistas");
movies.movies.forEach(movie=>{
if(movie.vista){
console.log(" - "+movie.name);
}
});
}
function add(movie_name){
const movies = readData();
const all = movie_name.split(",");
all.forEach(name=>{
const movie = {
name: name,
vista: false
};
movies.movies.push(movie);
});
writeData(movies);
}
function pick(){
const movies = readData();
const random = Math.floor(Math.random() * movies.movies.length);
console.log("MIRA ESTA PELICULA");
console.log(movies.movies[random].name);
movies.movies[random].vista = true;
writeData(movies);
}
const command = process.argv[2];
switch (command) {
case 'list':
list();
break;
case 'pick':
pick();
break;
case 'add':
add(process.argv[3]);
break;
default:
console.log('Invalid command. Available commands: list, pick, add');
}
@orlax
Copy link
Author

orlax commented Jan 13, 2024

Este Script es un programa sencillo para guardar una lista de peliculas en un archivo de JSON, seleccionar una al azar y marcarla como "vista" Tiene tres comandos:

list - muestra la lista de peliculas separadas por vistas y no vistas
add - permite agregar nuevas peliculas a la vista separadas por , ejemplo: "Barbie, Oppenheimer"
pick - selecciona una pelicula al azar, muestra su nombre y la marca como vista: True en el JSON

Cree este script para acabar con mi problema al tratar de seleccionar una Pelicula.

Pueden leer mas aqui: https://orla.games/empece-a-usar-el-azar-en-mi-vida

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment