forked from marva/sumorobot-web
		
	add code processing adn block highlight
This commit is contained in:
		@@ -1,5 +1,5 @@
 | 
				
			|||||||
// The local/remote server URL
 | 
					// The local/remote server URL
 | 
				
			||||||
//var ROBOT_SERVER = '10.42.0.1';
 | 
					//var ROBOT_SERVER = '192.168.2.1:80';
 | 
				
			||||||
var ROBOT_SERVER = 'ws.achex.ca:4010';
 | 
					var ROBOT_SERVER = 'ws.achex.ca:4010';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var workspace;
 | 
					var workspace;
 | 
				
			||||||
@@ -113,6 +113,50 @@ window.addEventListener('load', function() {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var lines = [];
 | 
				
			||||||
 | 
					    var foundTrue = false;
 | 
				
			||||||
 | 
					    // Function to process code and highlight blocks and lines
 | 
				
			||||||
 | 
					    function processCode(index, highlight) {
 | 
				
			||||||
 | 
					        // When all lines are already processed
 | 
				
			||||||
 | 
					        if (lines.length == 0) return;
 | 
				
			||||||
 | 
					        // Split into code and block ID
 | 
				
			||||||
 | 
					        var temp = lines[index].split(';;');
 | 
				
			||||||
 | 
					        var code = temp[0];
 | 
				
			||||||
 | 
					        // Default timeout for processing the next line
 | 
				
			||||||
 | 
					        var timeout = 100;
 | 
				
			||||||
 | 
					        var isCondition = /(if|elif|else)/.test(code);
 | 
				
			||||||
 | 
					        // When it is a condition line
 | 
				
			||||||
 | 
					        if (isCondition) {
 | 
				
			||||||
 | 
					            if (foundTrue) {
 | 
				
			||||||
 | 
					                // Start processing the code from the beginning again
 | 
				
			||||||
 | 
					                index = -1;
 | 
				
			||||||
 | 
					                foundTrue = false;
 | 
				
			||||||
 | 
					            } else if (/opponent/.test(code)) {
 | 
				
			||||||
 | 
					                foundTrue = (sumorobot.sensors['opponent'] < 40.0);
 | 
				
			||||||
 | 
					            } else if (/LEFT/.test(code)) {
 | 
				
			||||||
 | 
					                foundTrue = (sumorobot.sensors['line_left'] > 2000);
 | 
				
			||||||
 | 
					            } else if (/RIGHT/.test(code)) {
 | 
				
			||||||
 | 
					                foundTrue = (sumorobot.sensors['line_right'] > 2000);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                foundTrue = true;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        // Some lines don't correspond to any block
 | 
				
			||||||
 | 
					        if (temp[1] && index != -1 && ((!isCondition && foundTrue) || isCondition)) {
 | 
				
			||||||
 | 
					            // When sleep function, we get the timeout value from the function
 | 
				
			||||||
 | 
					            if (/\d/.test(code)) {
 | 
				
			||||||
 | 
					                timeout = parseInt(code.replace(/[a-z\.()]/g, ''));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            // Block ID should always be 20 symbols long
 | 
				
			||||||
 | 
					            var blockId = temp[1].substring(0, 20);
 | 
				
			||||||
 | 
					            // Highlight the block
 | 
				
			||||||
 | 
					            workspace.highlightBlock(blockId);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        // Set an timeout for processing the next line
 | 
				
			||||||
 | 
					        index = (index + 1) % lines.length
 | 
				
			||||||
 | 
					        setTimeout(function() { processCode(index) }, timeout);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Start button listener
 | 
					    // Start button listener
 | 
				
			||||||
    $('.btn-start').click(function() {
 | 
					    $('.btn-start').click(function() {
 | 
				
			||||||
        // When we are in Python coding mode
 | 
					        // When we are in Python coding mode
 | 
				
			||||||
@@ -125,21 +169,19 @@ window.addEventListener('load', function() {
 | 
				
			|||||||
            parsedCode = sumorobot.getBlocklyCode().replace(/;;.{20}/g, '');
 | 
					            parsedCode = sumorobot.getBlocklyCode().replace(/;;.{20}/g, '');
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // Escape the qoutes, replace new lines and send the code
 | 
					        // Escape the qoutes, replace new lines and send the code
 | 
				
			||||||
        sumorobot.send('code', parsedCode.replace(/"/g, '\\"').replace(/\n/g, ';;'));
 | 
					        //sumorobot.send('code', parsedCode.replace(/"/g, '\\"').replace(/\n/g, ';;'));
 | 
				
			||||||
        // TODO: Highlight the blocks and lines
 | 
					        // Split into lines of code and filter empty lines
 | 
				
			||||||
        /*var lines = sumorobot.getBlocklyCode().replace(/ /g, '').split('\n');
 | 
					        lines = sumorobot.getBlocklyCode().split('\n').filter(Boolean);
 | 
				
			||||||
        // Process code lines
 | 
					        // Process the code starting from the first line
 | 
				
			||||||
        for (i in lines) {
 | 
					        processCode(0);
 | 
				
			||||||
            if (lines[i]) {
 | 
					 | 
				
			||||||
                console.log(lines[i].split(';;'));
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }*/
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Stop button listener
 | 
					    // Stop button listener
 | 
				
			||||||
    $('.btn-stop').click(function() {
 | 
					    $('.btn-stop').click(function() {
 | 
				
			||||||
        sumorobot.send('stop');
 | 
					        sumorobot.send('stop');
 | 
				
			||||||
        // TODO: Stop highlighting blocks and lines
 | 
					        // Stop highlighting blocks and lines
 | 
				
			||||||
 | 
					        lines = [];
 | 
				
			||||||
 | 
					        workspace.highlightBlock('');
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Enter (return) keypress listener on robot ID field
 | 
					    // Enter (return) keypress listener on robot ID field
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user