mirror of
https://github.com/brachiel/Androtafl.git
synced 2026-03-16 22:50:23 +00:00
First version with minimal network capabilities.
This commit is contained in:
parent
b3506db50b
commit
d6ec8c877e
7 changed files with 142 additions and 1 deletions
|
|
@ -8,10 +8,13 @@
|
|||
<!-- Libraries -->
|
||||
<link rel="stylesheet" type="text/css" href="css/jo/jo.css" />
|
||||
<script type="text/javascript" charset="utf-8" src="js/cordova-1.5.0.js"></script>
|
||||
<script type="text/javascript" charset="utf-8" src="js/socket.io.min.js"></script>
|
||||
|
||||
<!-- Application Code -->
|
||||
<link rel="stylesheet" type="text/css" href="css/ui.css" />
|
||||
<script type="text/javascript" charset="utf-8" src="js/tafl.js"></script>
|
||||
<script type="text/javascript" charset="utf-8" src="js/net.js"></script>
|
||||
|
||||
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
|||
41
assets/www/js/net.js
Normal file
41
assets/www/js/net.js
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
|
||||
|
||||
var TaflNet = {
|
||||
connect: function(address) {
|
||||
if (!address) address = "chrummibei.ch:47413";
|
||||
|
||||
var s = io.connect("http://"+address);
|
||||
this.socket = s;
|
||||
|
||||
s.on('connect', function() {
|
||||
console.log("Connected");
|
||||
s.emit('client.hello');
|
||||
});
|
||||
|
||||
s.on('server.hello', function(data) {
|
||||
console.log("Got hello");
|
||||
console.log(s.emit('game.find_opponent'));
|
||||
});
|
||||
|
||||
s.on('game.start', function(data) {
|
||||
TaflNet.on_game_start(data);
|
||||
});
|
||||
|
||||
s.on('move.new', function(move) {
|
||||
TaflNet.on_move(move);
|
||||
});
|
||||
|
||||
s.on('game.end', function(data) {
|
||||
TaflNet.on_game_end(data);
|
||||
});
|
||||
},
|
||||
|
||||
send_move: function(move) {
|
||||
var s = this.socket;
|
||||
s.emit('move.send', move);
|
||||
},
|
||||
|
||||
on_game_start: function(data) {},
|
||||
on_move: function(move) {},
|
||||
on_game_end: function(data) {}
|
||||
};
|
||||
2
assets/www/js/socket.io.min.js
vendored
Normal file
2
assets/www/js/socket.io.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -290,3 +290,8 @@ var TaflUnitTest = function() {
|
|||
|
||||
test("capture", state.board[10], "...WWWW.W..");
|
||||
};
|
||||
|
||||
var exports = {};
|
||||
exports.tafl = Tafl;
|
||||
exports.taflstate = TaflState;
|
||||
exports.taflunittest = TaflUnitTest;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ tafl_game.to_move = Tafl.initialStates.Hnefatafl.to_move();
|
|||
|
||||
var N = tafl_game.board.length;
|
||||
var uiBoard;
|
||||
var myColor;
|
||||
|
||||
function prepareBoard(board) {
|
||||
var ret_board = [];
|
||||
|
|
@ -77,6 +78,12 @@ var uiBoard = new joTaflBoard(uiBoardData);
|
|||
|
||||
var moveFrom = null;
|
||||
uiBoard.selectEvent.subscribe(function(index, table){
|
||||
if (tafl_game.to_move !== myColor) {
|
||||
// not my turn
|
||||
table.deselect();
|
||||
return;
|
||||
};
|
||||
|
||||
var i = table.getRow();
|
||||
var j = table.getCol();
|
||||
|
||||
|
|
@ -103,7 +110,9 @@ uiBoard.selectEvent.subscribe(function(index, table){
|
|||
}
|
||||
|
||||
try {
|
||||
makeMove([moveFrom, [i, j]]);
|
||||
var move = [moveFrom, [i, j]];
|
||||
makeMove(move);
|
||||
TaflNet.send_move(move);
|
||||
|
||||
moveFrom = null;
|
||||
table.deselect();
|
||||
|
|
@ -155,3 +164,18 @@ var uiStack = new joStack();
|
|||
var uiScreen = new joScreen(uiStack);
|
||||
|
||||
uiStack.push(uiCard);
|
||||
|
||||
// Connect Network
|
||||
|
||||
TaflNet.connect();
|
||||
|
||||
TaflNet.on_game_start = function(data) {
|
||||
myColor = data.your_color;
|
||||
var colorName = (myColor == 'W')?'White':'Black';
|
||||
|
||||
uiScreen.alert("Game started. You're " + colorName);
|
||||
};
|
||||
|
||||
TaflNet.on_move = function(move) {
|
||||
makeMove(move);
|
||||
};
|
||||
|
|
|
|||
62
server/tafl_server.js
Normal file
62
server/tafl_server.js
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/* This is a minimalistic tafl server using socket.io */
|
||||
|
||||
// TODO: Rewrite the initialisation to be used behind apache
|
||||
var io = require('socket.io').listen(47413);
|
||||
var tafllib = require('../assets/www/js/tafl');
|
||||
var tafl = tafllib.tafl;
|
||||
|
||||
var games = {};
|
||||
var sockets_waiting = [];
|
||||
|
||||
var ready = function(socket) { socket.emit('ready'); };
|
||||
|
||||
io.sockets.on('connection', function(socket) {
|
||||
socket.emit('server.hello');
|
||||
|
||||
socket.on('game.join', function(game_id) {
|
||||
if (! games[game_id]) {
|
||||
socket.emit('error');
|
||||
return;
|
||||
}
|
||||
|
||||
socket.set('game.id', game_id, ready(socket));
|
||||
});
|
||||
|
||||
socket.on('client.hello', function() {
|
||||
console.log("Got hello");
|
||||
});
|
||||
|
||||
socket.on('game.find_opponent', function() {
|
||||
console.log("Looking for opponent...");
|
||||
if (sockets_waiting.length == 0) {
|
||||
sockets_waiting.push(socket);
|
||||
return;
|
||||
}
|
||||
|
||||
var socket2 = sockets_waiting.pop();
|
||||
|
||||
// TODO: Create TaflState and assign it to the game
|
||||
|
||||
socket.opponent = socket2;
|
||||
socket2.opponent = socket;
|
||||
|
||||
socket2.emit('game.start', {your_color: 'W'});
|
||||
socket.emit('game.start', {your_color: 'B'});
|
||||
});
|
||||
|
||||
socket.on('board.get', function() {
|
||||
socket.get('game.id', function(err, game_id) {
|
||||
if (err) { socket.emit('error', err); return; }
|
||||
if (! games[game_id]) { socket.emit('error', 'no such game'); return; };
|
||||
|
||||
socket.emit('board', game[game_id].board);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('move.send', function(move) {
|
||||
// TODO: Check if move is valid
|
||||
socket.opponent.emit('move.new', move);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
@ -4,6 +4,8 @@ package ch.chrummibei.androtafl;
|
|||
import android.os.Bundle;
|
||||
import org.apache.cordova.*;
|
||||
|
||||
//import com.strumsoft.websocket.phonegap.WebSocketFactory;
|
||||
|
||||
public class AndrotaflActivity extends DroidGap {
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
|
|
@ -11,5 +13,7 @@ public class AndrotaflActivity extends DroidGap {
|
|||
super.onCreate(savedInstanceState);
|
||||
//setContentView(R.layout.main);
|
||||
super.loadUrl("file:///android_asset/www/index.html");
|
||||
|
||||
//appView.addJavascriptInterface(new WebSocketFactory(appView), "WebSocketFactory");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue