Commit e34c9445 authored by Colomban Wendling's avatar Colomban Wendling
Browse files

Merge branch 'composed-tiles' of ssh://git.tuxfamily.org/gitroot/chalmion/ovcc into composed-tiles

Conflicts:
	clients/bot/src/main.vala
parents 5ec3714c 888d6a4c
......@@ -47,12 +47,13 @@ public class Bot
}
debug ("Joined");
/* start listening to the server */
client.start_listening ();
/* react on game state changes */
client.game.notify["state"].connect ((s, p) => {
switch (((OVCC.Game)s).state) {
case OVCC.GameState.STARTED:
debug ("Game started!");
break;
case OVCC.GameState.FINISHED:
leave ();
debug ("We're done, bye!");
......@@ -89,7 +90,7 @@ public class Bot
});
});
/* we should place a pawn? let's do so */
client.place_pawn.connect ((game, tile) => {
client.place_pawn.connect ((tile) => {
debug ("We're asked to place a pawn!");
foreach (var o in tile.objects) {
/* we guess all pawns can go anywhere another can -- which isn't true
......@@ -101,9 +102,21 @@ public class Bot
}
});
return true;
}
public void play ()
throws OVCC.GameError
{
debug ("Requesting game start");
try {
client.game.start();
} catch (OVCC.GameError.STARTED e) {
warning ("Cannot start game: %s", e.message);
}
}
public void leave ()
{
client.leave ();
......@@ -168,9 +181,12 @@ public int main (string[] args)
try {
bot.join (server);
/* wait to let others join */
Thread.usleep (5000000);
bot.play();
loop.run ();
} catch (Error e) {
warning ("Failed to join on the server: %s", e.message);
warning ("Failed to join & play on the server: %s", e.message);
}
return 0;
......
......@@ -122,16 +122,18 @@ namespace OVCC
* Returns: Whether game is started (and not whether game just started)
*/
public bool start ()
throws GameError
{
if (n_players >= 2 && state != GameState.STARTED) {
/* set starting player as the first in the list */
current_player = _players.nth_data(0);
state = GameState.STARTED;
return true;
if (state == GameState.STARTED) {
throw new GameError.STARTED ("Game already started");
}
return false;
state = GameState.STARTED;
/* set starting player as the first in the list */
current_player = _players.nth_data(0);
return true;
}
/**
......
......@@ -68,6 +68,14 @@ namespace OVCC.Network
game_object.player_removed.connect ((p) => {
send_to_peer (SignalType.PLAYER_REMOVED, "s", p.nick);
});
game_object.notify["state"].connect ((s, p) => {
OVCC.GameState state = ((OVCC.Game)s).state;
/* other states should be automatic (useful until full communication) */
// if (state == OVCC.GameState.STARTED ||
// state == OVCC.GameState.ABORTED ) {
send_to_peer (SignalType.GAME_STATE, "i", state);
// }
});
}
private bool send_to_peer (SignalType s, string format, ...)
......@@ -112,6 +120,23 @@ namespace OVCC.Network
}
}
break;
case SignalType.GAME_STATE:
OVCC.GameState s;
msg.parse_data ("i", out s);
debug ("Game state changed from %s to %s",
game_object.state.to_string(), s.to_string());
switch (s) {
case OVCC.GameState.STARTED:
game_object.start();
break;
case OVCC.GameState.FINISHED:
case OVCC.GameState.STOPPED:
break; /* should be automatic */
case OVCC.GameState.ABORTED:
game_object.abort();
break;
}
break;
}
return true;
}
......
......@@ -11,7 +11,7 @@ namespace OVCCClient
public class Client : Object
{
public Server server {get; private set; default = null;}
public Player player {get; set;}
public Player player {get; private set;}
public Game game {get; private set; default = null;}
public signal void place_tile (Tile t);
......@@ -24,7 +24,7 @@ namespace OVCCClient
}
public bool bind_to (Server s)
throws ClientError
throws Error
{
if (server != null)
{
......@@ -41,6 +41,7 @@ namespace OVCCClient
public bool leave ()
{
game.abort();
server.disconnect_from ();
/* destroy server */
server = null;
......@@ -52,24 +53,34 @@ namespace OVCCClient
public bool login (string nick)
throws ServerError
{
server.login_to (nick, "patate");
player = new Player (nick);
server.login_to (player);
return true;
}
/* start listening to the server */
public void start_listening ()
{
server.start_listening (game, player);
}
public Game join_table (int index)
throws ServerError
{
game = server.join_table (index);
game = server.join_table (index, player);
player.joined (game);
game.notify["current-player"].connect (() => {
debug ("Current player changed");
if (game.current_player == player) {
this.place_tile (game.current_tile);
}
});
game.board.tile_added.connect ((t, p) => {
if (game.current_player == player) {
// this.place_pawn (t);
}
});
return game;
}
}
......
......@@ -78,12 +78,12 @@ namespace OVCCClient
return true;
}
public /*async*/ bool login_to (Player player)
public /*async*/ bool login_to (string login, string password)
throws ServerError
{
Message msg;
send_message (new LoginMessage (player.nick, "patate"));
send_message (new LoginMessage (login, password));
try {
msg = Message.receive (input);
} catch (Error e) {
......@@ -116,9 +116,10 @@ namespace OVCCClient
} catch {
/* FIXME ? */
}
connection = null;
input = null;
output = null;
signal_handle = null;
connection = null;
input = null;
output = null;
}
public bool is_connected ()
......@@ -146,7 +147,7 @@ namespace OVCCClient
/* join a table, index -1 means any open table
* The index is typically within the list returned by enumerate_tables() */
public Game join_table (int index)
public Game join_table (int index, Player player)
throws ServerError
requires (connection != null)
{
......@@ -157,9 +158,14 @@ namespace OVCCClient
tileset.load (tiles, File.new_for_path (TILESET_FILE));
Game game = new Game.from_tileset(tileset);
return game;
/* start communication NOW: we have the game and the player and must
* send the PLAYER_ADDED triggered by next instruction... */
start_communication (game, player);
game.add_player (player);
throw new ServerError.NO_OPEN_TABLE ("Not implemented");
return game;
}
private void listen_loop ()
......@@ -191,7 +197,7 @@ namespace OVCCClient
signal_handle = null;
}
public void start_listening (Game game, Player player)
public void start_communication (Game game, Player player)
{
signal_handle = new SignalHandle (game, player, input, output);
Thread.create<void> (listen_loop, false);
......
......@@ -34,7 +34,7 @@ public class Client: Object
public Player player {get; set; default = null;}
public Game game {get; set; default = null;}
private SignalHandle signal_handle = null;
public SignalHandle signal_handle {get; private set; default = null; }
public Client ()
{
......@@ -82,7 +82,8 @@ public class Client: Object
game = game_p;
signal_handle = new SignalHandle (game, player, input, output);
game.add_player (player);
/* the client adds itselves */
// game.add_player (player);
return true;
}
......
......@@ -141,7 +141,7 @@ public class Server: ThreadedSocketService
}
break;
case MessageType.SIGNAL:
client.signal_handle.signal_received ((msg as SignalMessage));
break;
case MessageType.DISCONNECT: running = false; break;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment