Advertisement
Guest User

asdfBot

a guest
Oct 2nd, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This code is probably 80% MrInanimated's doing.
  2. //All credit to him. Hail Inny!
  3.  
  4. //Init for variables
  5. var globals = {
  6.     activePlayer : {},
  7.     activePlayerHasChanged : false,
  8.     playerNames : {},
  9.     firstRun : true,
  10.     playerLives : {},
  11.     flipCounter : {},
  12.     uFlipCounter : {},
  13.     buffer : channel.data.activePlayerIndex,
  14.     lockedLettersLen : {},
  15.     prevPlayerBuffer : {},
  16.     lostLives : {},
  17.     legitWordCount : {},
  18.     date : {},
  19.     howMany : {}
  20. };
  21.  
  22.  
  23. //Print message to chat.
  24. var sendChat = function(string) {
  25.     channel.socket.emit('chatMessage', string);
  26. };
  27.  
  28.  
  29. //when the active player changes, this function will set activePlayerHasChanged to true.
  30. var updateListen = function() {
  31.         globals.activePlayer = channel.data.activePlayerIndex; //get current Player
  32.        
  33.     if(globals.buffer != globals.activePlayer) {
  34.                     console.log("New Turn:");
  35.                     globals.activePlayerHasChanged = true;
  36.                     globals.buffer = globals.activePlayer;    //buffer the buffer (buffalo buffalo buffalo buffalo buffalo)
  37.     }
  38. };
  39.  
  40. //generates a nonDynamic actors info thingamabob (for stepwise updates)
  41. var generateActorConditions = function() {
  42.  
  43.     console.log("Generated things");
  44.  
  45.     var actors = channel.data.actors;
  46.         for (i = 0; i < actors.length; i++) {
  47.             globals.playerNames[i] = actors[i].displayName;
  48.             globals.lockedLettersLen[i] = actors[i].lockedLetters.length;
  49.             globals.flipCounter[i] = 0;
  50.             globals.uFlipCounter[i] = 0;
  51.             globals.prevPlayerBuffer = channel.data.activePlayerIndex;
  52.             globals.playerLives[i] = channel.data.actors[i].lives;
  53.             globals.lostLives[i] = 0;
  54.             globals.legitWordCount = 0;
  55.             globals.howMany = actors.length;
  56.     }
  57.    
  58.     //Prepare you anus.... I mean, timer.
  59.     var d = new Date();
  60.     globals.date = d.getTime();
  61.  
  62. };
  63.  
  64. //Check if player flipped. This counts unnecessary flips to the side.
  65. //That is... unnecessary flips, still count as flips. Use it how you like.
  66. //I don't print it to the chat but it can easily be done with "globals.uFlipCounter"
  67. var checkForFlip = function(index) {
  68.     //Note that the playerLives receives an update after the flipChecking in main.
  69.     if(globals.lockedLettersLen[index] < channel.data.actors[index].lockedLetters.length) {
  70.             //Flip occurs => Determine if flip is unnecessary or not
  71.             if(globals.playerLives[index] == 3) {
  72.                 console.log("Unnecessary flip for " + globals.playerNames[index] + "!");
  73.                 //flip overflow!
  74.                 globals.uFlipCounter[index] += 1;
  75.                 globals.flipCounter[index] += 1;    //flip is a flip even if it is unnecessary.
  76.                                                     //this can be removed to gain a normal lifecounter
  77.             } else {
  78.                 console.log("Good flip for " + globals.playerNames[index] + "!");
  79.                 globals.flipCounter[index] += 1;
  80.             }
  81.     }
  82.    
  83.     //Update lockedLetters.
  84.     globals.lockedLettersLen[index] = channel.data.actors[index].lockedLetters.length;
  85.  
  86. };
  87.  
  88. var updateLives = function(index) {
  89.  
  90.     //if(life is lost) <- can be thought of that way
  91.     //also decrement the word++. Because if a player loses a life after a turn,
  92.     //it means that a legit word wasn't reached. This is a lazy solution, but w/e. It doesn't cost that much extra.
  93.     if(channel.data.actors[index].lives < globals.playerLives[index]) {
  94.         console.log(globals.playerNames[index] + " just lost a life.");
  95.         globals.lostLives[index] += 1;
  96.         globals.legitWordCount -= 1;
  97.     }
  98.  
  99.     globals.playerLives[index] = channel.data.actors[index].lives;
  100.    
  101. };
  102.  
  103. //Note: EventListener implemented into main for an easier code to maintain.
  104. var mainCycle = function() {
  105.  
  106.     if(channel.data.state === 'playing') {
  107.             //We do this in a firstRun because players are still joining up until 'state:playing'
  108.             //Also less load when working from a generated perspective instead of refreshing in every step.
  109.             //Benchmarking shows that generation takes around 20ms which is a very short time.
  110.             if(globals.firstRun) {
  111.                 console.log("FirstRun initiated!");
  112.                 generateActorConditions();
  113.                 globals.firstRun = !globals.firstRun;   //The ole flippity floo T-Flip-Flop-Oldé-School-Bebop Switch for Bools.
  114.                
  115.             } else {
  116.            
  117.                 updateListen();     //Run the pseudoEventListener for PlayerChange.
  118.                
  119.                 if(globals.activePlayerHasChanged) {
  120.                    
  121.                     //Deals with one player at a time, hence the prevPlayerBuffer.
  122.                     //Very efficient.
  123.                    
  124.                     //The way this system works, is that we put together the player information after he ends his turn.
  125.                     //May have to compensate for gameOver moment. Haven't looked into it too much.
  126.                    
  127.                     globals.legitWordCount += 1;    //A checker with -= 1 compensates for faulty words in updateLives();
  128.                     checkForFlip(globals.prevPlayerBuffer);
  129.                     updateLives(globals.prevPlayerBuffer);
  130.                    
  131.                     console.log("There are " + globals.legitWordCount + " legit words so far.");
  132.                    
  133.                     globals.activePlayerHasChanged = false;
  134.                     globals.prevPlayerBuffer = globals.activePlayer;
  135.                
  136.                 }
  137.                
  138.            
  139.             }
  140.            
  141.     } else {
  142.    
  143.         //Do this once while game is halted. (See T-Flip-Flop-Oldé-School-Bebop)
  144.         if(!globals.firstRun) {
  145.        
  146.             //compensate for playing:count bug
  147.             //UPDATE: compensated for ze compensation of the fix of the playing:count bug
  148.             //Another update: Things are mysterious yo. Uncompensated because bug came round again.
  149.             globals.legitWordCount -= 1;
  150.             var flipResults = "Last round's flips: |";
  151.        
  152.             for(i = 0; i < globals.howMany; i++) {
  153.                 flipResults += globals.playerNames[i] + ": " + globals.flipCounter[i] + " | ";
  154.             }
  155.            
  156.             setTimeout(function() { sendChat(flipResults); }, 2000);
  157.             setTimeout(function() { sendChat("There were " + globals.legitWordCount + " words used in the round"); }, 9000);
  158.            
  159.             var d = new Date();
  160.             var seconds = Math.floor((d.getTime() - globals.date) / 1000);
  161.             setTimeout(function() { sendChat("The last round took " + Math.floor(seconds / 60) + " minutes and " + (seconds % 60) + " seconds.");}, 4000)
  162.            
  163.             globals.firstRun = !globals.firstRun; //The T-Flip-Flop-Oldé-School-Bebop
  164.            
  165.             //clean shit up. May result in duplicates otherwise. (it seems).
  166.             //Whatever. What do I know?
  167.             //Don't judge me fella, this is done in between rounds. WE DON'T NEED EFFICIENT HERE! :D :D :D
  168.             for(i = 0; i < 20; i++) {
  169.                 globals.playerNames[i] = "";
  170.             }
  171.            
  172.         }
  173.     }
  174. };
  175.  
  176. setInterval(mainCycle, 50);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement