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

Made a runnable bot, adding the easily implementable functions

everywhere needed...
parent c9366bbf
......@@ -8,38 +8,52 @@
public class Bot
{
private MainLoop loop = null;
private OVCCClient.Player player = null;
private OVCC.Player player = null;
private OVCCClient.Client client = null;
public bool join (OVCCClient.Server server)
throws Error;
throws Error
{
uint player_suffix = 7;
OVCCClient.Game game = null;
/* Create our player instance */
player = new OVCC.Player ("""Clever bot %3u""".printf (player_suffix));
/* connect to the server, choosing an available nickname */
while (true) {
try {
player = new OVCCClient.Player ("Clever bot %3u", player_suffix);
server.connect (player); /* may throw a rejection */
client = new OVCCClient.Client (player);
server.connect_to (player); /* may throw a rejection */
break;
} catch OVCCClient.ServerError.NICKNAME_IN_USE {
} catch OVCCClient.ServerError.MISSING_AUTHENTICATION {
} catch (OVCCClient.ServerError.NICKNAME_IN_USE e) {
} catch (OVCCClient.ServerError.MISSING_AUTHENTICATION e) {
}
}
/* join an open table */
if (server.open_tables.length > 0) {
player.join (server.open_tables[0]); /* may throw a rejection */
} else {
throw new OVCCClient.ServerError.NO_TABLE_OPEN ("failed to join a game");
}
/* join any open table */
game = server.join_table (-1);
// if (server.open_tables.length > 0) {
// player.join (server.open_tables[0]); /* may throw a rejection */
// } else {
// throw new OVCCClient.ServerError.NO_TABLE_OPEN ("failed to join a game");
// }
/* when game is finished, just stop */
game.finished.connect (() => {
loop.quit ();
debug ("We're done, bye!");
/* react on game state changes */
game.notify["state"].connect ((s, p) => {
switch (((OVCC.Game)s).state)
{
case OVCC.GameState.FINISHED:
loop.quit ();
debug ("We're done, bye!");
break;
case OVCC.GameState.ABORTED:
loop.quit ();
debug ("Abnormal game termination!");
break;
}
});
/* we should place a tile? OK, why not */
game.place_tile.connect ((game, tile) => {
client.place_tile.connect ((tile) => {
debug ("Seems we should place a tile...");
game.board.foreach ((b, p, t) => {
for (var i = 0; i < 4; i++) {
......@@ -64,17 +78,19 @@ public class Bot
});
});
/* we should place a pawn? let's do so */
game.place_pawn.connect ((game, tile) => {
client.place_pawn.connect ((game, 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
* using some extensions, but it doesn't matter that much. */
/* FIXME: this should probably also be asynchronous... */
if (game.place_pawn (player.pawns.nth_data (0), o)) {
break;
}
// if (game.place_pawn (player.pawns.nth_data (0), o)) {
// break;
// }
}
});
return true;
}
public Bot (MainLoop l)
......@@ -83,7 +99,7 @@ public class Bot
}
}
public void main (string args)
public int main (string[] args)
{
var loop = new MainLoop ();
var bot = new Bot (loop);
......@@ -95,4 +111,6 @@ public void main (string args)
} catch (Error e) {
warning ("Failed to join on the server: %s", e.message);
}
return 0;
}
......@@ -61,6 +61,7 @@ namespace OVCC
{
STOPPED,
STARTED,
FINISHED,
ABORTED
}
......@@ -95,7 +96,7 @@ namespace OVCC
stack.item_removed.connect (t => {
this.notify_property ("current-tile");
if (stack.is_empty ()) {
state = GameState.STOPPED;
state = GameState.FINISHED;
}
});
this.notify["current-tile"].connect (() => {
......
......@@ -13,6 +13,10 @@ namespace OVCCClient
public Server server {get; private set; default = null;}
public Player player {get; construct;}
public Game game {get; private set; default = null;}
public signal void place_tile (Tile t);
public signal void place_pawn (Tile t);
public Client (Player p)
{
......
......@@ -38,7 +38,6 @@ namespace OVCCClient
~Server ()
{
connection.close ();
}
public /*async*/ bool connect_to (Player player)
......@@ -61,10 +60,10 @@ 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)
throws ServerError
{
return null;
throw new ServerError.NO_OPEN_TABLE ("Not implemented");
}
}
}
......
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