Skip to content

Instantly share code, notes, and snippets.

@jim-clark
jim-clark / guide-key-concepts.md
Last active March 28, 2024 13:23
Key Concepts of The Guide To Building a Browser Game

Key Concepts of The Guide To Building a Browser Game

Coders!

Before this morning's Rock-Paper-Scissors code-along and before you begin to code your amazing games for project 1, it's worthwhile to refresh the KEY CONCEPTS of the Guide on How to Build a Browser Game...

The Guide on How to Build a Browser Game is a proven PROCESS that helps students be successful by producing:

  • Well structured code that closely mimics the popular MVC (Model-View-Controller) pattern
  • More concise code (often, less is more)
  • Less buggy code

Django Class-Based View Customization Tips

ListView Filtering Example

class CatList(ListView):
  model = Cat
  
  # Only return the cats for the logged in user
 def get_queryset(self):

Rolling Back Django Migrations

Django migrations can be unapplied to a database in the reverse order that they were applied using the following commands...

Listing the Migrations for a Specific Django App

Run the following to list the existing migrations for the main_app Django app:

python3 manage.py showmigrations main_app
# This is a list of rules for ignoring files in every Git repository on your
# computer. See https://help.github.com/articles/ignoring-files

# GitHub maintains a repository of items that can generally be gitignored
# globally safely at: https://github.com/github/gitignore/tree/main/Global

# GitHub also maintains a repository of language-specific items that should
# typically be ignored on a per-project basis with a .gitignore in that
# project's root directory.

Accepting Multi-Line Input and Rendering in EJS

If you'd like a user to be able to enter multi-line text and have that text render the line-breaks within HTML, read on...

Mongoose Schema

As usual, use a String property on a schema to hold the text content, for example:

const logEntrySchema = new Schema({

Accessing Mongoose enum Validator Arrays

If you are using an enum validator in a schema and would like to access and iterate over the enum's strings to, for example, render the <option> tags for a <select> element, you can access the array of strings in your controller function and pass them to your template as follows:

// models/movie.js

const movieSchema = new Schema({
  ...
  mpaaRating: {

Using Neon for PostgreSQL Hosting

Creating a Database

  1. Sign up at https://neon.tech/.

  2. You get one free "project" which can contain multiple "branches" which can contain multiple databases. Name your project something like "sei" and stick with the default "main" branch.

  3. Click the Database dropdown.

Displaying Error Detail in the MERN-Stack

Scenario

When errors happen in the controller, typically due to validation errors when CUD'ing MongoDB/Mongoose documents, you might want to display specific error information to the user instead of a generic error message.

Let's assume that the schema for a User model has two properties, username & email, that need to be unique. When a user signs up, if either property is not unique, we want to let them know which property, username or email, caused the error.

The demonstration code we're going to use was created by cloning mern-infrastructure and updating the userSchema as follows:

@jim-clark
jim-clark / py-pac-poe-solution-1.py
Created October 21, 2022 18:37
Solution to Optional Py-Pac-Poe Lab
# Py-Pac-Poe
print("----------------------")
print("Let's play Py-Pac-Poe!")
print("----------------------\n\n")
score = {'X': 0, 'O': 0, 'T': 0}
num_wins = int(input("How many wins to play to? "))
board = {}
turn = 'X'
winner = None
require('dotenv').config();
require('./config/database');
const Movie = require('./models/movie');
const Performer = require('./models/performer');
(async function() {
/*-- Write the code for each exercise below --*/