DoSomething/ds-mdata-responder — pull request #340 by tongxiang (2014-11-25 21:31:19+00:00)
Title: :fire: SGCompetitiveStoryController refactor: creating 1) gameUtilities.js 2) gameMessageHelpers.js 3) gameRecordHelpers.js

#### What's this PR do?
Whew! It's been a battle with the :japanese_ogre:. `SGCompetitiveStoryController` has been cut from over 1500 lines to 740, with many more lines to go. It's a start. What's happened?

From a high level, I extracted functions from the :japanese_ogre: into module files, each organized along a function-related theme. Within the `gameMessageHelpers.js` file, I also created objects within the module.exports object, which further allowed for some meaningful semantic classification of functions (as opposed to ridiculous function names like `getUniqueIndivEndGameMessage()`.

These modules were then required within the main controller file, and referred to with variable names that (I hope) were semantically meaningful, like `report` and `message`.

**OPPORTUNISTIC REFACTORING**
1. Many functions were needlessly attached to the `this` scope of the controller, or else needlessly referenced properties attached to that scope. Whenever I could, I took these out. :fire:
2. Many functions were declared in the local namespace of the controller when in fact they were simply closed over by another function. Within their respective modules, I specified these functions as private with the `_` prefix.

**MODULES**
For a full list of the original names of functions and what they've been refactored to, and some more notes on refactoring strategy, check out [this Evernote file](https://www.evernote.com/l/AHI8sMHZ0xVHdbG0uG0KIZGeN8evT71ersQ).
1. `smsHelpers.js` - I've changed the name of the file previously called `messageHelper.js`. Since this file contains a lot of simple string manipulation functions, I changed its name for clarity and differentiation from `gameMessageHelpers.js`.
2. `gameMessageHelpers.js` - this is the biggest module I created. It contains a LOT of renamed functions, all related to sending messages, calculating end-level or end-game messages, sending the message alerting other users of a player's exit and ending their game, and sending wait messages before the game has begun.
3. `gameRecordHelpers.js` - three functions dealing with recording game play actions in the gameDoc-- `.updatePlayerCurrentStatus()`, `.addPathToStoryResults()`, and `.updateStoryResults()`--have been turned into two functions.
1. `gameUtilities.js` - this module contains functions which don't deal with game content, but rather facilitate other game logic.

**WHAT'S LEFT ON THE :japanese_ogre: CONTROLLER?**
Wow! Only six top-level functions left!
1. `.createGame()`
2. `.betaJoinGame()`
3. `.alphaStartGame()`
4. `.userAction()`
5. `.findUserGame()`
6. `.startGame()`

#### Where should the reviewer start?
Whew! This PR is a doozy, with many changes. I'd first go through `SGCompetitiveStoryController`, seeing what functions have been removed, and then check out the module files I've created one-by-one.

#### How should this be manually tested?
`npm test`! Has also been tested by creating a game manually and playing through it.

#### Any background context you want to provide?
:bomb: the :japanese_ogre: with :fire:!!!!!

#### What are the relevant tickets?
Closes #323

#### Questions:
Please let me know if there are better design patterns, or better names I can use!