Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This code is probably 80% MrInanimated's doing.
- //All credit to him. Hail Inny!
- //Init for variables
- var globals = {
- activePlayer : {},
- activePlayerHasChanged : false,
- playerNames : {},
- firstRun : true,
- playerLives : {},
- flipCounter : {},
- uFlipCounter : {},
- buffer : channel.data.activePlayerIndex,
- lockedLettersLen : {},
- prevPlayerBuffer : {},
- lostLives : {},
- legitWordCount : {},
- date : {},
- howMany : {}
- };
- //Print message to chat.
- var sendChat = function(string) {
- channel.socket.emit('chatMessage', string);
- };
- //when the active player changes, this function will set activePlayerHasChanged to true.
- var updateListen = function() {
- globals.activePlayer = channel.data.activePlayerIndex; //get current Player
- if(globals.buffer != globals.activePlayer) {
- console.log("New Turn:");
- globals.activePlayerHasChanged = true;
- globals.buffer = globals.activePlayer; //buffer the buffer (buffalo buffalo buffalo buffalo buffalo)
- }
- };
- //generates a nonDynamic actors info thingamabob (for stepwise updates)
- var generateActorConditions = function() {
- console.log("Generated things");
- var actors = channel.data.actors;
- for (i = 0; i < actors.length; i++) {
- globals.playerNames[i] = actors[i].displayName;
- globals.lockedLettersLen[i] = actors[i].lockedLetters.length;
- globals.flipCounter[i] = 0;
- globals.uFlipCounter[i] = 0;
- globals.prevPlayerBuffer = channel.data.activePlayerIndex;
- globals.playerLives[i] = channel.data.actors[i].lives;
- globals.lostLives[i] = 0;
- globals.legitWordCount = 0;
- globals.howMany = actors.length;
- }
- //Prepare you anus.... I mean, timer.
- var d = new Date();
- globals.date = d.getTime();
- };
- //Check if player flipped. This counts unnecessary flips to the side.
- //That is... unnecessary flips, still count as flips. Use it how you like.
- //I don't print it to the chat but it can easily be done with "globals.uFlipCounter"
- var checkForFlip = function(index) {
- //Note that the playerLives receives an update after the flipChecking in main.
- if(globals.lockedLettersLen[index] < channel.data.actors[index].lockedLetters.length) {
- //Flip occurs => Determine if flip is unnecessary or not
- if(globals.playerLives[index] == 3) {
- console.log("Unnecessary flip for " + globals.playerNames[index] + "!");
- //flip overflow!
- globals.uFlipCounter[index] += 1;
- globals.flipCounter[index] += 1; //flip is a flip even if it is unnecessary.
- //this can be removed to gain a normal lifecounter
- } else {
- console.log("Good flip for " + globals.playerNames[index] + "!");
- globals.flipCounter[index] += 1;
- }
- }
- //Update lockedLetters.
- globals.lockedLettersLen[index] = channel.data.actors[index].lockedLetters.length;
- };
- var updateLives = function(index) {
- //if(life is lost) <- can be thought of that way
- //also decrement the word++. Because if a player loses a life after a turn,
- //it means that a legit word wasn't reached. This is a lazy solution, but w/e. It doesn't cost that much extra.
- if(channel.data.actors[index].lives < globals.playerLives[index]) {
- console.log(globals.playerNames[index] + " just lost a life.");
- globals.lostLives[index] += 1;
- globals.legitWordCount -= 1;
- }
- globals.playerLives[index] = channel.data.actors[index].lives;
- };
- //Note: EventListener implemented into main for an easier code to maintain.
- var mainCycle = function() {
- if(channel.data.state === 'playing') {
- //We do this in a firstRun because players are still joining up until 'state:playing'
- //Also less load when working from a generated perspective instead of refreshing in every step.
- //Benchmarking shows that generation takes around 20ms which is a very short time.
- if(globals.firstRun) {
- console.log("FirstRun initiated!");
- generateActorConditions();
- globals.firstRun = !globals.firstRun; //The ole flippity floo T-Flip-Flop-Oldé-School-Bebop Switch for Bools.
- } else {
- updateListen(); //Run the pseudoEventListener for PlayerChange.
- if(globals.activePlayerHasChanged) {
- //Deals with one player at a time, hence the prevPlayerBuffer.
- //Very efficient.
- //The way this system works, is that we put together the player information after he ends his turn.
- //May have to compensate for gameOver moment. Haven't looked into it too much.
- globals.legitWordCount += 1; //A checker with -= 1 compensates for faulty words in updateLives();
- checkForFlip(globals.prevPlayerBuffer);
- updateLives(globals.prevPlayerBuffer);
- console.log("There are " + globals.legitWordCount + " legit words so far.");
- globals.activePlayerHasChanged = false;
- globals.prevPlayerBuffer = globals.activePlayer;
- }
- }
- } else {
- //Do this once while game is halted. (See T-Flip-Flop-Oldé-School-Bebop)
- if(!globals.firstRun) {
- //compensate for playing:count bug
- //UPDATE: compensated for ze compensation of the fix of the playing:count bug
- //Another update: Things are mysterious yo. Uncompensated because bug came round again.
- globals.legitWordCount -= 1;
- var flipResults = "Last round's flips: |";
- for(i = 0; i < globals.howMany; i++) {
- flipResults += globals.playerNames[i] + ": " + globals.flipCounter[i] + " | ";
- }
- setTimeout(function() { sendChat(flipResults); }, 2000);
- setTimeout(function() { sendChat("There were " + globals.legitWordCount + " words used in the round"); }, 9000);
- var d = new Date();
- var seconds = Math.floor((d.getTime() - globals.date) / 1000);
- setTimeout(function() { sendChat("The last round took " + Math.floor(seconds / 60) + " minutes and " + (seconds % 60) + " seconds.");}, 4000)
- globals.firstRun = !globals.firstRun; //The T-Flip-Flop-Oldé-School-Bebop
- //clean shit up. May result in duplicates otherwise. (it seems).
- //Whatever. What do I know?
- //Don't judge me fella, this is done in between rounds. WE DON'T NEED EFFICIENT HERE! :D :D :D
- for(i = 0; i < 20; i++) {
- globals.playerNames[i] = "";
- }
- }
- }
- };
- setInterval(mainCycle, 50);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement