Commit 654f8472 authored by Colomban Wendling's avatar Colomban Wendling
Browse files

Bot: inherit from OVCCClient.Client

parent 27fec2da
......@@ -3,28 +3,24 @@
*/
/* maybe inherit from Client? */
public class Bot
public class Bot : OVCCClient.Client
{
private MainLoop loop = null;
private OVCCClient.Client client = null;
private OVCC.SigQueue sigqueue = new OVCC.SigQueue ();
private MainLoop loop = null;
private OVCC.SigQueue sigqueue = new OVCC.SigQueue ();
public bool join (OVCCClient.Server server)
throws Error
{
uint player_suffix = 7;
client = new OVCCClient.Client ();
/* connect to the server */
client.bind_to (server);
bind_to (server);
/* login to the server, choosing an available nickname */
debug ("Trying to login...");
while (true) {
try {
client.login ("""Clever bot %03u""".printf (player_suffix));
login ("""Clever bot %03u""".printf (player_suffix));
break;
} catch (OVCCClient.ServerError.NICKNAME_IN_USE e) {
player_suffix++;
......@@ -41,7 +37,7 @@ public class Bot
/* join any open table */
debug ("Trying to join a table...");
try {
client.join_table (-1);
join_table (-1);
} catch (Error e) {
leave ();
throw e;
......@@ -49,7 +45,7 @@ public class Bot
debug ("Joined");
/* react on game state changes */
sigqueue.add (client.game, client.game.notify["state"].connect ((s, p) => {
sigqueue.add (game, game.notify["state"].connect ((s, p) => {
switch (((OVCC.Game)s).state) {
case OVCC.GameState.STARTED:
debug ("Game started!");
......@@ -65,9 +61,9 @@ public class Bot
}
}));
/* we should place a tile? OK, why not */
sigqueue.add (client, client.place_tile.connect ((tile) => {
sigqueue.add (this, place_tile.connect ((tile) => {
debug ("Seems we should place a tile...");
client.game.board.foreach ((b, p, t) => {
game.board.foreach ((b, p, t) => {
for (var i = 0; i < 4; i++) {
var npos = p;
......@@ -79,7 +75,7 @@ public class Bot
}
for (var j = 0; j < 4; j++) {
/* FIXME: this should probably be asynchronous... */
if (client.player.place_tile (npos)) {
if (player.place_tile (npos)) {
return false;
} else {
tile.rotate (1);
......@@ -90,7 +86,7 @@ public class Bot
});
}));
/* we should place a pawn? let's do so */
sigqueue.add (client, client.place_pawn.connect ((tile) => {
sigqueue.add (this, 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
......@@ -111,18 +107,17 @@ public class Bot
{
debug ("Requesting game start");
try {
client.game.start();
game.start();
} catch (OVCC.GameError.STARTED e) {
warning ("Cannot start game: %s", e.message);
}
}
public void leave ()
public new void leave ()
{
sigqueue.remove_all ();
client.leave ();
client = null;
base.leave ();
loop.quit ();
}
......
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