Permalink
Browse files

Added "Random Based On Era" for occupations. Added option to restrict…

… occupations to Lovecraftian occupations.
  • Loading branch information...
1 parent fcefd4c commit b409a0fe17c010565a40fc9aaa6dcebe6e0ea31f @b-turchyn committed Mar 16, 2014
Showing with 94 additions and 16 deletions.
  1. +38 −4 src/classes/sql.php
  2. +21 −0 src/classes/ui.php
  3. +20 −5 src/controllers/character_sheet.php
  4. +7 −0 src/models/occupation.php
  5. +0 −1 src/models/roll_type.php
  6. +8 −6 src/ui/char_config.php
View
@@ -27,6 +27,7 @@ class MySQLQueries extends mysqli {
// List of all eras
const getEras = "SELECT era_id, era_name FROM %seras WHERE deleted_dt IS NULL ORDER BY era_name ASC";
const getOccupations = "SELECT occupation_id, occupation_name FROM %soccupations ORDER BY occupation_name ASC";
+ const getOccupationsFromEra = "select o.occupation_id, o.occupation_name FROM %1\$soccupations o INNER JOIN %1\$soccupation_eras oe ON o.occupation_id = oe.occupation_id WHERE oe.era_id = ? AND CASE 1 WHEN ? THEN o.lovecraftian_sw = 1 ELSE 1=1 END ORDER BY o.occupation_name";
const getEthnicityCount =
"";
@@ -111,7 +112,7 @@ public function getEras( ) {
$stmt->execute();
// Retrieve the result, and return false on failure.
$stmt->bind_result($key, $name);
- $result = [];
+ $result = array();
while ( $stmt->fetch() ) {
$result[] = array( 'key' => $key, 'value' => $name );
}
@@ -138,7 +139,40 @@ public function getOccupations( ) {
$stmt->execute();
// Retrieve the result, and return false on failure.
$stmt->bind_result($key, $name);
- $result = [];
+ $result = array();
+ while ( $stmt->fetch() ) {
+ $result[] = array( 'key' => $key, 'value' => $name );
+ }
+ } else echo $this->error;
+
+ if ( $stmt != NULL ) {
+ $stmt->close();
+ }
+
+ return $result;
+ }
+
+
+ /**
+ * Retrieves all occupations in a given era from the database
+ *
+ * @param $era the ID of the era to search in
+ * @param $lovecraftian Whether to select only Lovecraft-style occupations
+ * @return array of arrays in key => value layout on success, NULL on failure
+ * @author Brian Turchyn
+ */
+ public function getOccupationsFromEra($era, $lovecraftian) {
+ $result = null;
+ $stmt = null;
+
+ echo $lovecraftian;
+
+ if( $stmt = $this->prepare($this->preparePrefix(self::getOccupationsFromEra) ) ) {
+ $stmt->bind_param("ii", $era, $lovecraftian);
+ $stmt->execute();
+ // Retrieve the result, and return false on failure.
+ $stmt->bind_result($key, $name);
+ $result = array();
while ( $stmt->fetch() ) {
$result[] = array( 'key' => $key, 'value' => $name );
}
@@ -166,7 +200,7 @@ public function getEthnicities( ) {
$stmt->execute();
// Retrieve the result, and return false on failure.
$stmt->bind_result($key, $name);
- $result = [];
+ $result = array();
while ( $stmt->fetch() ) {
$result[] = array( 'key' => $key, 'value' => $name );
}
@@ -311,7 +345,7 @@ public function getFullName($gender, $first = null, $last = null) {
* @author Brian Turchyn
*/
private function preparePrefix($query) {
- return preg_replace("/%s/", $this->prefix, $query);
+ return sprintf($query, $this->prefix);
}
}
?>
View
@@ -36,6 +36,27 @@ static function buildControlGroup( $label_id, $label_value, $contents ) {
EOF;
}
+ static function buildCheckboxInput( $id, $name, $value, $checked = false, $klass = NULL ) {
+ $result = "<input type=\"checkbox\" id=\"" . htmlspecialchars( $id ) . "\"";
+
+ if ( $name != NULL ) {
+ $result .= " name=\"" . htmlspecialchars( $name ) . "\"";
+ }
+ if( $value !== NULL ) {
+ $result .= " value=\"" . htmlspecialchars( $value ) . "\"";
+ }
+ if ( $klass != NULL ) {
+ $result .= " class=\"" . htmlspecialchars ( $klass ) . "\"";
+ }
+ if( $checked == true ) {
+ $result .= " checked=\"checked\"";
+ }
+
+ $result .= ">";
+
+ return $result;
+ }
+
static function buildTextInput( $id, $name = NULL, $value = NULL, $klass = NULL, $placeholder = NULL ) {
$result = "<input type=\"text\" id=\"" . htmlspecialchars( $id ) . "\"";
@@ -40,10 +40,7 @@
$viewVars['gender'] = $gender['value'];
// Generate Occupation information
- $occupations = Occupation::getOccupations( );
- $occupation = ( $params['occupation'] === 'R' ?
- $occupations[mt_rand(1, count($occupations) - 1 )] :
- Occupation::get( $params['occupation'] ) );
+ $occupation = getOccupation( $params );
$viewVars['occupation'] = $occupation['value'];
$viewVars['nationality'] = 'TODO';
@@ -63,7 +60,8 @@
'era' => $params['era'],
'stat_type' => $params['stat_type'],
'gender' => $params['gender'],
- 'occupation' => $params['occupation'] );
+ 'occupation' => $params['occupation'],
+ 'lovecraftian' => $params['lovecraftian'] );
}
// Prepare output
$_SESSION['messages'] = $msgs;
@@ -79,6 +77,7 @@ function updateParams( ) {
$params['stat_type'] = getPostVar( 'stat_type' );
$params['gender'] = getPostVar( 'gender' );
$params['occupation'] = getPostVar( 'occupation' );
+ $params['lovecraftian'] = ( isset( $_POST['lovecraftian'] ) ? true : false );
return $params;
}
@@ -113,4 +112,20 @@ function isValid( $params, $msgs ) {
return !$msgs->hasErrors( );
}
+function getOccupation( $params ) {
+ $result = NULL;
+ $occupations = Occupation::getOccupations( );
+
+ if( $params['occupation'] === 'R' ) {
+ $result = $occupations[mt_rand(2, count($occupations) - 1 )];
+ } elseif ( $params['occupation'] === 'E' ) {
+ $occupations = Occupation::getFromEra( $params['era'], $params['lovecraftian'] );
+ $result = $occupations[mt_rand(0, count($occupations) - 1 )];
+ } else {
+ $result = Occupation::get( $params['occupation'] );
+ }
+
+ return $result;
+}
+
?>
@@ -28,11 +28,18 @@ static function getOccupations( ) {
if( self::$occupations === NULL ) {
self::$occupations = $sql->getOccupations( );
array_unshift( self::$occupations, array( 'key' => 'R', 'value' => 'Random' ) );
+ array_unshift( self::$occupations, array( 'key' => 'E', 'value' => 'Random Based On Era' ) );
}
return self::$occupations;
}
+ static function getFromEra( $era, $lovecraftian ) {
+ global $sql;
+
+ return $sql->getOccupationsFromEra( $era, ( $lovecraftian ? true : false ) );
+ }
+
static function getValue( $occupation ) {
$result = get( $occupation );
@@ -28,4 +28,3 @@ class RollType
}
?>
-
@@ -47,12 +47,14 @@
echo UI::buildControlGroup( 'gender', 'Gender',
UI::BuildSelect( 'gender', Gender::$GENDERS, 'gender', NULL, $viewVars['gender'] ) )
?>
- <div class="control-group">
- <label class="control-label" for="occupation">Occupation</label>
- <div class="controls">
- <?php echo UI::buildSelect( 'occupation', Occupation::getOccupations( ), 'occupation', NULL, $viewVars['occupation'] ) ?>
- </div>
- </div>
+ <?php
+ echo UI::buildControlGroup( 'occupation', 'Occupation',
+ UI::buildSelect( 'occupation', Occupation::getOccupations( ), 'occupation', NULL, $viewVars['occupation'] ) )
+ ?>
+ <?php
+ echo UI::buildControlGroup( 'lovecraftian', 'Lovecraft-based Occupations Only',
+ UI::buildCheckboxInput( 'lovecraftian', 'lovecraftian', 'true', $viewVars['lovecraftian'] ) )
+ ?>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-primary" name="generate" value="Generate">Generate</button>

0 comments on commit b409a0f

Please sign in to comment.