-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jordan Fowler
committed
Dec 15, 2009
1 parent
48db271
commit 157dd8c
Showing
2 changed files
with
91 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,62 +1,128 @@ | |||
// Author: Jordan Fowler | // Author: Jordan Fowler (TheBreeze) | ||
|
|||
var currentGame = null; | |||
|
|
||
var TicTacToe = new Class({ | var TicTacToe = new Class({ | ||
wins: 0, | |||
loses: 0, | |||
draws: 0, | |||
methodMap: { | methodMap: { | ||
'queued': 'onQueued', | 'queued': 'onQueued', | ||
'start': 'onStart', | 'start': 'onStart', | ||
'turn': 'onTurn', | 'turn': 'onTurn', | ||
'move': 'onMove', | 'move': 'onMove', | ||
'game_over': 'onGameOver' | 'game_over': 'onGameOver', | ||
'win': 'onWin', | |||
'loss': 'onLoss', | |||
'draw': 'onDraw', | |||
'user_count': 'onUserCount' | |||
}, | }, | ||
|
|
||
initialize: function() { | initialize: function() { | ||
if (TicTacToe.connection == null) { | if (TicTacToe.connection == null) { | ||
TicTacToe.connection = new WebSocket('ws://192.168.0.2:8000/tictactoe'); | TicTacToe.connection = new WebSocket('ws://192.168.0.2:8000/tictactoe'); | ||
}; | }; | ||
|
|
||
TicTacToe.connection.onopen = this.onJoin.bind(this); | TicTacToe.connection.onopen = this.join.bind(this); | ||
TicTacToe.connection.onmessage = this.onMessage.bind(this); | TicTacToe.connection.onmessage = this.onMessage.bind(this); | ||
TicTacToe.connection.onclose = this.onGameOver.bind(this); | TicTacToe.connection.onclose = this.onGameOver.bind(this); | ||
|
|||
this.setGameStats(); | |||
}, | }, | ||
|
|
||
onMessage: function(event) { | onMessage: function(event) { | ||
var command = JSON.decode(event.data); | var command = JSON.decode(event.data); | ||
|
|
||
console.log('[RCV] ' + command.msg); | console.log('[RCV] ' + command.msg); | ||
|
|
||
this[this.methodMap[command.msg]].call(this, [command]); | this[this.methodMap[command.msg]].call(this, command); | ||
}, | }, | ||
|
|
||
onJoin: function(event) { | message: function(msg, options) { | ||
console.log('[SENT] join game'); | var command = JSON.encode({msg: msg, data: options}); | ||
|
|
||
TicTacToe.connection.send(JSON.encode({msg: 'join'})); | console.log('[SENT] ' + msg); | ||
|
|||
TicTacToe.connection.send(command); | |||
}, | }, | ||
|
|
||
onQueued: function(command) { | setStatus: function(status) { | ||
$('status').set('text', status); | |||
}, | |||
|
|||
setGameStats: function() { | |||
$('game-stats').set('text', 'Wins: '+this.wins+' / Losses: '+this.wins+' / Draws: '+this.draws); | |||
}, | |||
|
|||
setUserCount: function(userCount) { | |||
$('user-count').set('text', 'Number of players: ' + userCount); | |||
}, | |||
|
|||
join: function(event) { | |||
this.message('join'); | |||
|
|||
this.setStatus('Connecting you to a game...'); | |||
}, | |||
|
|||
reset: function() { | |||
|
|
||
}, | }, | ||
|
|
||
onQueued: function(command) { | |||
this.setStatus('Waiting for another player...'); | |||
}, | |||
|
|||
onStart: function(command) { | onStart: function(command) { | ||
|
this.setStatus('Game found! Their turn first...'); | ||
}, | }, | ||
|
|
||
onTurn: function(command) { | onTurn: function(command) { | ||
|
this.setStatus('Your turn...'); | ||
}, | }, | ||
|
|
||
onMove: function(command) { | onMove: function(command) { | ||
|
$('cell-'+command.data.x+'-'+command.data.y).set('text', command.key); | ||
|
|||
this.setStatus('Their turn...'); | |||
}, | |||
|
|||
move: function(x, y) { | |||
this.message('move', {x: x, y: y}); | |||
}, | }, | ||
|
|
||
onGameOver: function(command) { | onGameOver: function(command) { | ||
|
this.setStatus('Game over.'); | ||
this.setGameStats(); | |||
this.reset(); | |||
}, | |||
|
|||
onWin: function(command) { | |||
this.wins += 1; | |||
this.setStatus('Game over. You win!'); | |||
this.setGameStats(); | |||
}, | |||
|
|||
onLoss: function(command) { | |||
this.losses += 1; | |||
this.setStatus('Game over. You lose!'); | |||
this.setGameStats(); | |||
}, | |||
|
|||
onDraw: function(command) { | |||
this.draws += 1; | |||
this.setStatus('Game over. It was a draw!'); | |||
this.setGameStats(); | |||
}, | |||
|
|||
onUserCount: function(command) { | |||
this.setUserCount(command.data); | |||
} | } | ||
}); | }); | ||
|
|
||
$('join-game').addEvent('click', function(event) { | $$('.cell').addEvent('click', function(event) { | ||
currentGame = new TicTacToe(); | try { | ||
currentGame.move($(this).get('id').split('-')[1], $(this).get('id').split('-')[2]); | |||
} catch(error) { | |||
alert('Please wait while we connect you to a game...'); | |||
} | |||
}); | }); | ||
|
|
||
currentGame = new TicTacToe(); |