InnominateOne

Quick and Dirty CYOA Rewrite

Mar 18th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //this line creates a new object called "purchases"
  2. purchases = {};
  3. //purchases will keep track of tableName and rowNum values
  4.  
  5. //we create a variable for points, instead of making
  6. //temporary ones every time we need them
  7. points = 100;
  8.  
  9. //we call this after we've changed the points total, to keep te counter
  10. //up to date
  11. function refreshCounter() {
  12.     document.getElementById("counter").innerHTML = points.toString();
  13. }
  14.  
  15. //this is a new function, since with the current design we need to find the row a lot
  16. function getRow(tableName, rowNum) {
  17.     return document.getElementById(tableName).getElementsByTagName("tr")[rowNum];
  18. }
  19.  
  20. //getCost and getBonus were both identical except for the class name we wanted
  21. //now they're much simpler; most of the common functionality is in getCellData
  22. function getCellData(row, className) {
  23.     //a try { <A> } catch(error) { <B> } block basically means
  24.     //"do <A>, but if something goes wrong there do <B>"
  25.     try
  26.     {
  27.         //we got rid of the loop by using the built-in html function
  28.         //getElementsByClassName
  29.         return row.getElementsByClassName(className)[0].innerHTML;
  30.     }
  31.     catch(error)
  32.     {
  33.         return undefined;
  34.     }
  35. }
  36.  
  37. function getCost(row) {
  38.     return parseInt(getCellData(row, "cost_col")) || 0;
  39. }
  40.  
  41. function getBonus(row) {
  42.     return parseInt(getCellData(row, "bonus_col")) || 0;
  43. }
  44.  
  45. function buy(tableName, rowNum, method, noclear) { 
  46.     var row = getRow(tableName, rowNum);
  47.     var cost = getCost(row);
  48.     var bonus = getBonus(row);
  49.    
  50.     if(points >= cost) {
  51.         //used to be "noclear != 1", but this is much
  52.         //simpler and means the same thing in this case
  53.         if(!noclear) {
  54.             //we pass no argument to refund so that it refunds every match
  55.             refund(tableName);
  56.         }    
  57.        
  58.         //purchase option
  59.         if(method == "roll") {
  60.             row.className = "rolled";
  61.             points += bonus; //"x += y" is a shorthand for "x = x + y"
  62.         } else {
  63.             if(tableName == "artifact") {
  64.                 points += bonus; //"x += y" is a shorthand for "x = x + y"
  65.             }
  66.             row.className = "purchased";
  67.         }
  68.         points -= cost; //"x -= y" is a shorthand for "x = x - y"
  69.        
  70.         //get the existing set or make a new one
  71.         var rowSet = purchases[tableName] || {};
  72.         //add our rowNum to it
  73.         rowSet[rowNum] = true;
  74.         //update our purchases with the new set
  75.         purchases[tableName] = rowSet;
  76.     }
  77.    
  78.     refreshCounter();
  79. }
  80.  
  81. function refund(tableName, rowNum) {
  82.     var refundRow = function(rowNum) {
  83.             var row = getRow(tableName, rowNum);
  84.             row.className = "";
  85.             points += getCost(row);
  86.         };
  87.    
  88.     var rowSet = purchases[tableName];
  89.     if(typeof rowSet === "undefined") {
  90.         //no need to refund, since it isn't even purchased
  91.         return;
  92.     }
  93.    
  94.     if(typeof rowNum === "undefined") {
  95.         //we need to refund the whole table
  96.        
  97.         //iterate over all the rowNums
  98.         var rowNums = Object.keys(rowSet);
  99.         for (var i=0; i<rowNums.length; i++) {
  100.             refundRow(rowNums[i]);
  101.         }
  102.  
  103.         //remove the tableName from purchases
  104.         delete purchases[tableName];
  105.     }
  106.     else
  107.     {
  108.         refundRow(rowNum);
  109.         delete rowSet[rowNum];
  110.     }
  111.     refreshCounter();
  112. }
  113.  
  114. function slidePanel(id) {
  115.     var panel = document.getElementById(id);
  116.     if(panel.className == "infocus") {
  117.         panel.className = "nodisplay";
  118.     } else {
  119.         panel.className = "infocus";
  120.     }
  121. }
  122.  
  123. function roll(dice, sides) {
  124.     var total = 0;
  125.     for (var i=0; i<dice; i++) {
  126.         total = total + Math.floor(Math.random()*sides)+1;
  127.     }
  128.     return total;
  129. }
  130.  
  131. function roll_choice(tableName) {
  132.     var confirmed = window.confirm("Are you sure you want to take a random option? This may not be reversible.");
  133.     if(confirmed === true) {
  134.         var choice = roll(1,0) - 1;
  135.         //changed the 1 for the noclear argument to true
  136.         //which is better than 1 when all you want is true or false
  137.         buy(tableName,choice,"roll",true);
  138.     }
  139. }
  140.  
  141. //this function is radically different from the original version
  142. //hopefully doesn't have the bug where things don't get removed properly
  143. //it may look more complex, but (apart from the sort) it's actually really simple
  144. function gen_summary() {
  145.     //this gives us an array containing all the tableName entries (in a random order)
  146.     var tableNames = Object.keys(purchases);
  147.    
  148.     //this will hold our temporary objects
  149.     var allPurchased = [];
  150.    
  151.     //we iterate over our tableNames
  152.     for (var i=0; i<tableNames.length; i++) {
  153.         var tableName = tableNames[i];
  154.         var rowSet = purchases[tableName];
  155.        
  156.         var rowNums = Object.keys(rowSet);
  157.         for (var j=0; j<rowNums.length; j++) {
  158.             var rowNum = rowNums[j];
  159.             var row = getRow(tableName, rowNum);
  160.            
  161.             var name = getCellData(row, "name_col");
  162.             var cost = getCost(row);
  163.             var className = row.className;
  164.            
  165.             var bonus = 0;
  166.             if(className == "rolled") {
  167.                 bonus = getBonus(row);
  168.             }
  169.            
  170.             var obj = { name:name, cost:cost, className:className, bonus:bonus };
  171.             allPurchased.push(obj);
  172.         }
  173.     }
  174.    
  175.     //this is tricky, so don't worry if you don't get it yet
  176.     //we're defining a new function with a different syntax
  177.     //this one is called tableNameCompare and it is only visible
  178.     //inside this function (see the "var" on the left)
  179.     //we use it later to sort our output so that the cheapest
  180.     //purchases come first (bonuses subtracted from costs),
  181.     //alphabetically otherwise
  182.     var tableNameCompare = function(a, b) {
  183.             aCost = a.cost - a.bonus;
  184.             bCost = b.cost - b.bonus;
  185.             costDifference = aCost - bCost;
  186.             if(costDifference == 0) {
  187.                 //they have the same price, so go alphabetically
  188.                
  189.                 //make sure they both have the same case, otherwise
  190.                 //javascript puts capitals first
  191.                 aSortName = a.name.toUpperCase();
  192.                 bSortName = b.name.toUpperCase();
  193.                
  194.                 //this is like the "costDifference" line above but for strings
  195.                 return aSortName.localeCompare(bSortName);
  196.             }
  197.             return costDifference;
  198.         };
  199.    
  200.     //now we sort of names by increasing cost and alphabetically
  201.     allPurchased.sort(tableNameCompare);
  202.    
  203.     //setup our variables for the summary
  204.     var baubles = 100;
  205.     var baubleString, prefixString, nameString, fullString;
  206.    
  207.     //create an empty array to hold our strings
  208.     var outStrings = [];
  209.    
  210.     //loop over our tableNames again, this time in sorted order
  211.     for (var i=0; i<allPurchased.length; i++) {
  212.         //get back tableName, rowNum, and row
  213.         var purchased = allPurchased[i];
  214.        
  215.         //update bauble count in one go
  216.         baubles += purchased.bonus - purchased.cost;
  217.         baubleString = "[" + baubles + "]";
  218.        
  219.         //setup prefixString
  220.         if(purchased.className == "rolled") {
  221.             prefixString = " Rolled: ";
  222.         } else {
  223.            prefixString = " ";
  224.         }
  225.        
  226.         //combine the substrings we made earlier
  227.         //it's very easy to tell what each part does now
  228.         //by looking at where we defined each variable
  229.         //baubleString is "[<baubles>]"
  230.         //prefixString is either " Rolled: " or " "
  231.         //nameString is just "<name>"
  232.         fullString = baubleString + prefixString + purchased.name;
  233.        
  234.         //now we put our string at the end of the array we made
  235.         outStrings.push(fullString);
  236.     }
  237.    
  238.     //this turns our array of strings into one big string, putting
  239.     //"<br>\n" in between all of the smaller strings
  240.     //the "<br>" is the HTML break tag
  241.     //"\n" means "new line", which makes it so it isn't just one
  242.     //big clump of HTML
  243.     var result = outStrings.join("<br>\n");
  244.     document.getElementById("p_summary").innerHTML = result;
  245. }
  246.  
  247. function is_taken(tableName, rowNum) {
  248.     //new function means we don't have to mess with the DOM at all
  249.     //in fact this function never even gets called now
  250.     return purchases[tableName][rowNum] !== undefined;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment