js/GameManager.js
/** Game object containing the data about a game. */
Game object containing the data about a game.
Game object containing the data about a game .
function Game(dirname, basepath) {
  this.dirname = dirname;
  this.basepath = basepath;
  if (this.basepath[this.basepath.length-1] != "/") this.basepath += "/"; 
  this.fullpath = this.basepath + this.dirname + "/";
  this.hasinfofile = false;
  this.icon = (fs.existsSync(this.fullpath+"icon.png")) ? this.fullpath+"icon.png" : "resources/sphere.svg";
  this.screenshot = (fs.existsSync(this.fullpath+"screenshot.png")) ? this.fullpath+"screenshot.png" : undefined;
  this.video = (fs.existsSync(this.fullpath+"video.webm")) ? this.fullpath+"video.webm" : undefined;
  ///
  var sgmdata = fs.readFileSync(this.fullpath+"game.sgm").toString();
  var lines = sgmdata.split("\n");
  var keys = [];
  var values = [];
  for (var i = 0; i < lines.length; i++) {
    var splitline = lines[i].split("=");
    keys[i] = splitline[0];
    values[i] = splitline[1];
  }
  this.name = values[keys.indexOf("name")];
  this.shortdescription = (values[keys.indexOf("description")] == undefined || values[keys.indexOf("description")] == "") ? "No description" : values[keys.indexOf("description")];
  this.author = values[keys.indexOf("author")];
  this.screen_width = values[keys.indexOf("screen_width")];
  this.screen_height = values[keys.indexOf("screen_height")];
  this.script = values[keys.indexOf("script")];
  ////
  this.description = "<h1>Description</h1><p>" + this.shortdescription + "</p>";
  this.controls = "<h1>Controls</h1>No control info found in info.txt."
  if (fs.existsSync(this.fullpath+"info.txt")) {
    console.log("Found info.txt for " + this.name);
    this.hasinfofile = true;
    this.otherinfo = "";
    var txtdata = fs.readFileSync(this.fullpath+"info.txt").toString();
    var endpos = txtdata.indexOf("\n\n");
    this.info = marked(txtdata.substr(0, endpos));
    this.info = this.info.split("\n").slice(0, this.info.split("\n").length-1).join("<br>");  
    this.info = this.info.split("<a").join('<a target="_blank"');
    var detailedinfo = marked(txtdata.substr(endpos));
    var infos = detailedinfo.split("<h1>");
    for (var i = 0; i < infos.length; i++) {
      switch($("<h1>"+infos[i]).html()) {
        case "Description":
          this.description = "<h1>"+infos[i];
          break;
        case "Credits":
          this.credits = "<h1>"+infos[i];
          break;
        case "Controls":
          this.controls = "<h1>"+infos[i];
          break;
        case "":
          break;
        default: 
          this.otherinfo += "<h1>"+infos[i];
      }
    }
  }
  this.getHTML = function() {
    var screenshotarea = "";
    if (this.video) screenshotarea = '<video src="' + this.video + '" controls></video>'
    else if (this.screenshot) screenshotarea = '<img src="' + this.screenshot + '" alt="screenshot">'
    else screenshotarea = '<br>No screenshot available';
    var html =
      '<li id="'+this.dirname+'">' +
      '<div class="gameheader">' +
        '<a class="expand" href="javascript:void(0)">' +
        '<img src="'+this.icon+'" alt="game icon">' +
        '<p class="gametitle">'+this.name+'</p>' +
        '<p class="shortdescription">'+this.shortdescription+'</p>' +
        '</a>' +
      '</div>'+ 
      '<a class="launchbutton" href="#" data-path="'+this.fullpath+'">Play game</a>' +
      '<p class="clear"></p>';
      if (this.hasinfofile) {
        html +=
        '<div class="gamedetails">' +
          '<p class="directory">Path: '+this.fullpath+'</p>' +
          '<div class="col">' +
            '<div class="description">'+this.description+'</div>' +
            '<div class="controls">'+this.controls+'</div>' +
          '</div>' + 
          '<div class="col">' + 
            '<div class="screenshotarea">'+screenshotarea+'</div>' +
            '<div class="gameinfo">' +
              this.info +
            '</div>' + 
          '</div>' + 
        '</div>' + 
        '<p class="clear"></p>' +
        '</li>';
      }
      else {
        html +=
        '<div class="gamedetails">' +
        '<p class="directory">Path: '+this.fullpath+'</p>' +
        '</div>' +
        '<p class="clear"></p>';
      }
    return html;
  }
}
function Game ( dirname , basepath ) { this . dirname = dirname ; this . basepath = basepath ; if ( this . basepath [ this . basepath . length - 1 ] != "/" ) this . basepath += "/" ; this . fullpath = this . basepath + this . dirname + "/" ; this . hasinfofile = false ; this . icon = ( fs . existsSync ( this . fullpath + "icon.png" ) ) ? this . fullpath + "icon.png" : "resources/sphere.svg" ; this . screenshot = ( fs . existsSync ( this . fullpath + "screenshot.png" ) ) ? this . fullpath + "screenshot.png" : undefined ; this . video = ( fs . existsSync ( this . fullpath + "video.webm" ) ) ? this . fullpath + "video.webm" : undefined ; var sgmdata = fs . readFileSync ( this . fullpath + "game.sgm" ) . toString ( ) ; var lines = sgmdata . split ( "\n" ) ; var keys = [ ] ; var values = [ ] ; for ( var i = 0 ; i < lines . length ; i ++ ) { var splitline = lines [ i ] . split ( "=" ) ; keys [ i ] = splitline [ 0 ] ; values [ i ] = splitline [ 1 ] ; } this . name = values [ keys . indexOf ( "name" ) ] ; this . shortdescription = ( values [ keys . indexOf ( "description" ) ] == undefined || values [ keys . indexOf ( "description" ) ] == "" ) ? "No description" : values [ keys . indexOf ( "description" ) ] ; this . author = values [ keys . indexOf ( "author" ) ] ; this . screen_width = values [ keys . indexOf ( "screen_width" ) ] ; this . screen_height = values [ keys . indexOf ( "screen_height" ) ] ; this . script = values [ keys . indexOf ( "script" ) ] ; this . description = "<h1>Description</h1><p>" + this . shortdescription + "</p>" ; this . controls = "<h1>Controls</h1>No control info found in info.txt." if ( fs . existsSync ( this . fullpath + "info.txt" ) ) { console . log ( "Found info.txt for " + this . name ) ; this . hasinfofile = true ; this . otherinfo = "" ; var txtdata = fs . readFileSync ( this . fullpath + "info.txt" ) . toString ( ) ; var endpos = txtdata . indexOf ( "\n\n" ) ; this . info = marked ( txtdata . substr ( 0 , endpos ) ) ; this . info = this . info . split ( "\n" ) . slice ( 0 , this . info . split ( "\n" ) . length - 1 ) . join ( "<br>" ) ; this . info = this . info . split ( "<a" ) . join ( '<a target="_blank"' ) ; var detailedinfo = marked ( txtdata . substr ( endpos ) ) ; var infos = detailedinfo . split ( "<h1>" ) ; for ( var i = 0 ; i < infos . length ; i ++ ) { switch ( $ ( "<h1>" + infos [ i ] ) . html ( ) ) { case "Description" : this . description = "<h1>" + infos [ i ] ; break ; case "Credits" : this . credits = "<h1>" + infos [ i ] ; break ; case "Controls" : this . controls = "<h1>" + infos [ i ] ; break ; case "" : break ; default : this . otherinfo += "<h1>" + infos [ i ] ; } } } this . getHTML = function ( ) { var screenshotarea = "" ; if ( this . video ) screenshotarea = '<video src="' + this . video + '" controls></video>' else if ( this . screenshot ) screenshotarea = '<img src="' + this . screenshot + '" alt="screenshot">' else screenshotarea = '<br>No screenshot available' ; var html = '<li id="' + this . dirname + '">' + '<div class="gameheader">' + '<a class="expand" href="javascript:void(0)">' + '<img src="' + this . icon + '" alt="game icon">' + '<p class="gametitle">' + this . name + '</p>' + '<p class="shortdescription">' + this . shortdescription + '</p>' + '</a>' + '</div>' + '<a class="launchbutton" href="#" data-path="' + this . fullpath + '">Play game</a>' + '<p class="clear"></p>' ; if ( this . hasinfofile ) { html += '<div class="gamedetails">' + '<p class="directory">Path: ' + this . fullpath + '</p>' + '<div class="col">' + '<div class="description">' + this . description + '</div>' + '<div class="controls">' + this . controls + '</div>' + '</div>' + '<div class="col">' + '<div class="screenshotarea">' + screenshotarea + '</div>' + '<div class="gameinfo">' + this . info + '</div>' + '</div>' + '</div>' + '<p class="clear"></p>' + '</li>' ; } else { html += '<div class="gamedetails">' + '<p class="directory">Path: ' + this . fullpath + '</p>' + '</div>' + '<p class="clear"></p>' ; } return html ; } }
Game object containing the data about a game.
Game object containing the data about a game .
//TODO: clean stuff up //Add trailing slash //////////////////////// //Load images and video /////////////////////////// //Load and process game.sgm //////////////////////////// //Load and process about.txt //Get the first few lines up to the first heading, convert them to markdown and display them. //Remove the last \n and replace the other ones with <br> //Get and split the other lines. //Dump the data of any custom headers here //alert(this.otherinfo); //end process about.txt /** Get the game's info in HTML form. */ //Header bit //end gameheader //Big description bit //end col //col //end gameinfo //end col //end gamedetails