Code Monkey home page Code Monkey logo

snake.js's Introduction

Serpent.io, a.k.a. Snake Code Climate

Tech Inventory

  • Backbone.js to manage game and scoreboard as a single page app
  • Ruby on Rails backend to deliver the SPA and provide a JSON api to persist high scores.
  • jQuery for keyboard interactions.

When I first wrote this game I was still learning the "javascript way" of doing object-oriented programming with prototypical inheritance. I more or less succeeded in that aspect, but the code responsible for rendering the view and managing user interactions was a huge ball of mud. At that time I knew just enough jQuery to be dangerous, and while the basic mechanics seemed to work I had all my truth in the DOM and ran into a wall while trying to extend it to have more features. I wasn't unbinding events properly, so chaos ensued when I tried to reset the game for another play without a hard refresh. It was a mess, but I had other things I wanted to work on.

Backbone reboot

I recently revisted the project for a couple of days to apply what I'd learned in the intervening months about keeping a clean seperation between the DOM and my data with the help of a client-side framework called Backbone.js. I did an almost complete rewrite, and instead of iterating over all the cells for every frame the game is now primarily driven by cascading events. The cell is the fundamental model of the game, which may belong to any of several different collections (snakeSegments, apples, obstacles) depending on the current state of that cell; these collections publish appropriate events when cells are added or removed, which trigger changes elsewhere in the game. For example, when an apple is eaten the game view hears the "appleEaten" event and knows to update the score and make a new apple.

Each square is a div element to a backbone cell view. The view listens to change events on the cell model and updates the div's css class when the underlying data model changes. The game state is now maintained completely seperate from the DOM's appearance thanks to views that listen to data models.

Rails backend

This was primarily a javascript project, but as a finishing touch I whipped up a simple Ruby on Rails backend to allow users to save scores to a scoreboard. A HighScore model is created when the Backbone app POSTs one to the /high_scores url. I originally fetched the scores with a GET request to /high_scores, but eventually moved to bootstrapping the high score data with the page delivering the Backbone app to avoid an unnecessary XHR request.

snake.js's People

Contributors

jhamon avatar jeffnv avatar

Stargazers

Carlos Marin avatar

Watchers

James Cloos avatar

Forkers

jeffnv

snake.js's Issues

Leaderboard can be easily vandalised

A simple POST request to

http://www.serpent.io/high_scores

with a body payload of

{
"username":"USERNAME",
"score":1337
}

will be accepted multiple times with the exact same _snakejs_session and X-CSRF-Token used for the initial high-score submission. This leaves your high-score leaderboard open to vandals.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.