Commit fe649419 authored by Jonathan Michalon's avatar Jonathan Michalon
Browse files

Handle game start, triggered by bot after 5 sec.

parent 93e0c3e3
......@@ -56,6 +56,9 @@ public class Bot
client.game.notify["state"].connect ((s, p) => {
switch (((OVCC.Game)s).state)
{
case OVCC.GameState.STARTED:
debug ("Game started!");
break;
case OVCC.GameState.FINISHED:
loop.quit ();
debug ("We're done, bye!");
......@@ -92,7 +95,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
......@@ -104,9 +107,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 ();
......@@ -173,9 +188,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,16 @@ 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;
/* set starting player as the first in the list */
current_player = _players.nth_data(0);
state = GameState.STARTED;
return true;
}
/**
......
......@@ -68,6 +68,9 @@ 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) => {
send_to_peer (SignalType.GAME_STATE, "i", ((OVCC.Game)s).state);
});
}
private bool send_to_peer (SignalType s, string format, ...)
......@@ -112,6 +115,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;
}
......
......@@ -41,6 +41,7 @@ namespace OVCCClient
public bool leave ()
{
game.abort();
server.disconnect_from ();
/* destroy server */
server = null;
......
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