<commit_before>/**
 * Game.js
 * --------
 * Author & Copyright (c) 2013: <NAME>, www.nathandavies.co.uk
 */

var Game = function() {
    Trace.Information('New Game()');
    self = this;
    
    // Stop the loader
    Loader.Stop();
    
    // Required resources
    self.gridController = new GridController();
    self.localMediaStream = new LocalMediaStream(self);
    self.shooter = new Shooter();
    
    // Responsive grid
    $(window).bind('resize', function() { self.gridController.Resize(); });
    
    // Show the local player's game board
    self.LocalGameBoard();
    
    // Show an invite screen, unless already in a lobby
    if (!PeerHandler.connection) {
        $('body').append(Mustache.render(HTML.InviteScreen(), {
            session: Location.url + '?session=' + PeerHandler.peer.id
        }));
        
        // Copy link
        $('#invitelink').zclip({
            path: './js/lib/zclip.swf',
            copy: $('#invitelink p').html(),
            afterCopy: function() {
                $('#invitelink .copy').html('Copied');
            }
        });
    }
    
    // Peer connection
    if (PeerHandler.connection) {
        self.PeerHandlers();
        Shooter.prototype.SetUsersTurn(false);
    }
    PeerHandler.peer.on('connection', function(connection) {
        if (!PeerHandler.connection) {
            // Store the connection
            PeerHandler.connection = connection;
            
            // Open the connection and show the remote game board
            PeerHandler.connection.on('open', function() {
                Trace.Information('Peer has connected: '+PeerHandler.connection.peer);
                connection.send({state:'initiation'});
                
                self.PeerHandlers();
            });
        }
        else {
            // Notify the peer that this game is full
            Trace.Information(connection.peer+' was rejected as game is full');
            connection.on('open', function() {
                connection.send({state:'gameisfull'});
            });
        }
    });
    
    // Are you sure you want to quit?
    $(window).bind('beforeunload', function() {
        return 'Navigating away from this page will destroy the game session.';
    });
}

Game.prototype = {
    gridController: null,
    localGameScore: 0,
    localMediaStream: null,
    plotter: null,
    plottingNotification: '#remote .plotting',
    remoteGameScore: 0,
    remoteMediaStream: null,
    shooter: null,
    
    // Handle data
    HandleData: function(data) {
        // State change
        if (data.state) {
            PeerHandler.Remote.UpdateState(data.state);
            
            // If the remote peer is ready
            if (PeerHandler.Remote.IsReady()) {
                // Remove plotting notification and show the grid
                $(this.plottingNotification).remove();
                $('#remote .grid').fadeIn('slow');
                
                // Initiate the shooter
                Shooter.prototype.Initiate();
            }
        }
        
        // Missile
        if (data.missile && !Shooter.prototype.IsUsersTurn()) {
            // Toggle user's turn
            Shooter.prototype.ToggleUsersTurn();
        }
    },
    
    // Show local game board
    LocalGameBoard: function() {
        // Hide the heading
        $('h1').hide();
        
        // Stream properties
        var properties;
        if (this.localMediaStream)
            properties = {stream: this.localMediaStream.ObjectURL()};
        
        new GameBoard($.extend({
            id: 'local',
            muted: true,
            score: this.localGameScore
        }, properties));
        this.gridController.Resize();
        
        // Plot ships
        this.plotter = new Plotter();
        
        // Update the local state
        PeerHandler.Local.UpdateState('plotting');
    },
    
    // Peer handlers
    PeerHandlers: function() {        
        self = this;
        
        // Let remote peer know what state the local peer is in
        if (PeerHandler.Local.state)
            PeerHandler.Local.UpdateState(PeerHandler.Local.state);
        
        // Accept video call
        this.VideoCall();
        PeerHandler.peer.on('call', function(call) {
            PeerHandler.call = call;
            PeerHandler.call.answer();
            PeerHandler.call.on('stream', function(stream) {
                self.remoteMediaStream = new RemoteMediaStream(stream);
            });
        });
        
        // Handle received data, lost connection or error
        PeerHandler.connection.on('data', function(data) { self.HandleData(data); });
        PeerHandler.connection.on('close', function() { PeerHandler.Disconnected(); });
        PeerHandler.connection.on('error', function(error) { PeerHandler.Error(error); });
        
        // Show the game board?
        this.RemoteGameBoard();
    },
    
    // Show remote game board
    RemoteGameBoard: function() {
        // Stream properties
        var properties;
        if (this.remoteMediaStream)
            properties = {stream: this.remoteMediaStream.ObjectURL()}
        
        new GameBoard($.extend({
            id: 'remote',
            score: this.remoteGameScore,
            ready: PeerHandler.Remote.IsReady()
        }, properties));
        this.gridController.Resize();
        
        // If the user is ready, initiate the shooter
        if (PeerHandler.Remote.IsReady()) Shooter.prototype.Initiate();
    },
    
    // Video call
    VideoCall: function() {
        self = this;
        if (this.localMediaStream && PeerHandler.connection) {
            Trace.Information('Sending media stream to remote peer...');
            PeerHandler.peer.call(PeerHandler.connection.peer, self.localMediaStream.stream);
        }
    }
}<commit_msg>self = this<commit_after>/**
 * Game.js
 * --------
 * Author & Copyright (c) 2013: <NAME>, www.nathandavies.co.uk
 */

var Game = function() {
    Trace.Information('New Game()');
    var self = this;
    
    // Stop the loader
    Loader.Stop();
    
    // Required resources
    self.gridController = new GridController();
    self.localMediaStream = new LocalMediaStream(self);
    self.shooter = new Shooter();
    
    // Responsive grid
    $(window).bind('resize', function() { self.gridController.Resize(); });
    
    // Show the local player's game board
    self.LocalGameBoard();
    
    // Show an invite screen, unless already in a lobby
    if (!PeerHandler.connection) {
        $('body').append(Mustache.render(HTML.InviteScreen(), {
            session: Location.url + '?session=' + PeerHandler.peer.id
        }));
        
        // Copy link
        $('#invitelink').zclip({
            path: './js/lib/zclip.swf',
            copy: $('#invitelink p').html(),
            afterCopy: function() {
                $('#invitelink .copy').html('Copied');
            }
        });
    }
    
    // Peer connection
    if (PeerHandler.connection) {
        self.PeerHandlers();
        Shooter.prototype.SetUsersTurn(false);
    }
    PeerHandler.peer.on('connection', function(connection) {
        if (!PeerHandler.connection) {
            // Store the connection
            PeerHandler.connection = connection;
            
            // Open the connection and show the remote game board
            PeerHandler.connection.on('open', function() {
                Trace.Information('Peer has connected: '+PeerHandler.connection.peer);
                connection.send({state:'initiation'});
                
                self.PeerHandlers();
            });
        }
        else {
            // Notify the peer that this game is full
            Trace.Information(connection.peer+' was rejected as game is full');
            connection.on('open', function() {
                connection.send({state:'gameisfull'});
            });
        }
    });
    
    // Are you sure you want to quit?
    $(window).bind('beforeunload', function() {
        return 'Navigating away from this page will destroy the game session.';
    });
}

Game.prototype = {
    gridController: null,
    localGameScore: 0,
    localMediaStream: null,
    plotter: null,
    plottingNotification: '#remote .plotting',
    remoteGameScore: 0,
    remoteMediaStream: null,
    shooter: null,
    
    // Handle data
    HandleData: function(data) {
        // State change
        if (data.state) {
            PeerHandler.Remote.UpdateState(data.state);
            
            // If the remote peer is ready
            if (PeerHandler.Remote.IsReady()) {
                // Remove plotting notification and show the grid
                $(this.plottingNotification).remove();
                $('#remote .grid').fadeIn('slow');
                
                // Initiate the shooter
                Shooter.prototype.Initiate();
            }
        }
        
        // Missile
        if (data.missile && !Shooter.prototype.IsUsersTurn()) {
            // Toggle user's turn
            Shooter.prototype.ToggleUsersTurn();
        }
    },
    
    // Show local game board
    LocalGameBoard: function() {
        // Hide the heading
        $('h1').hide();
        
        // Stream properties
        var properties;
        if (this.localMediaStream)
            properties = {stream: this.localMediaStream.ObjectURL()};
        
        new GameBoard($.extend({
            id: 'local',
            muted: true,
            score: this.localGameScore
        }, properties));
        this.gridController.Resize();
        
        // Plot ships
        this.plotter = new Plotter();
        
        // Update the local state
        PeerHandler.Local.UpdateState('plotting');
    },
    
    // Peer handlers
    PeerHandlers: function() {
        var self = this;
        
        // Let remote peer know what state the local peer is in
        if (PeerHandler.Local.state)
            PeerHandler.Local.UpdateState(PeerHandler.Local.state);
        
        // Accept video call
        self.VideoCall();
        PeerHandler.peer.on('call', function(call) {
            PeerHandler.call = call;
            PeerHandler.call.answer();
            PeerHandler.call.on('stream', function(stream) {
                self.remoteMediaStream = new RemoteMediaStream(stream);
            });
        });
        
        // Handle received data, lost connection or error
        PeerHandler.connection.on('data', function(data) { self.HandleData(data); });
        PeerHandler.connection.on('close', function() { PeerHandler.Disconnected(); });
        PeerHandler.connection.on('error', function(error) { PeerHandler.Error(error); });
        
        // Show the game board?
        self.RemoteGameBoard();
    },
    
    // Show remote game board
    RemoteGameBoard: function() {
        // Stream properties
        var properties;
        if (this.remoteMediaStream)
            properties = {stream: this.remoteMediaStream.ObjectURL()}
        
        new GameBoard($.extend({
            id: 'remote',
            score: this.remoteGameScore,
            ready: PeerHandler.Remote.IsReady()
        }, properties));
        this.gridController.Resize();
        
        // If the user is ready, initiate the shooter
        if (PeerHandler.Remote.IsReady()) Shooter.prototype.Initiate();
    },
    
    // Video call
    VideoCall: function() {
        var self = this;
        if (self.localMediaStream && PeerHandler.connection) {
            Trace.Information('Sending media stream to remote peer...');
            PeerHandler.peer.call(PeerHandler.connection.peer, self.localMediaStream.stream);
        }
    }
}
{'fraction_non_alphanumeric': 0.12273349298665755, 'fraction_numerical': 0.0011973999315771468, 'mean_word_length': 1.7656102175969726, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1955449', 'n_tokens_mistral': 3097, 'n_tokens_neox': 2991, 'n_words': 694}