Commit 926953d6 authored by Jonathan Michalon's avatar Jonathan Michalon
Browse files

Some refactor and API "enhancement"

Working generic code moved from bot to client lib, more use of formerly
almost empty classes (client & player).
parent 8ab1068d
......@@ -8,38 +8,31 @@
public class Bot
{
private MainLoop loop = null;
private OVCC.Player player = null;
private OVCCClient.Client client = null;
private OVCCClient.Game game = null;
public OVCC.Network.SignalHandle signal_handle = null;
public bool join (OVCCClient.Server server)
throws Error
{
uint player_suffix = 7;
client = new OVCCClient.Client ();
/* connect to the server */
debug ("Trying to connect to server...");
server.connect_to (); /* may throw a rejection */
debug ("Connected");
client.bind_to (server);
/* login to the server, choosing an available nickname */
debug ("Trying to login...");
do {
/* Create our player instance */
player = new OVCC.Player ("""Clever bot %03u""".printf (player_suffix));
try {
client = new OVCCClient.Client (player);
server.login_to (player);
client.login ("""Clever bot %03u""".printf (player_suffix));
break;
} catch (OVCCClient.ServerError.NICKNAME_IN_USE e) {
player_suffix++;
} catch (OVCCClient.ServerError.MISSING_AUTHENTICATION e) {
server.disconnect_from ();
leave ();
throw e; /* avoid infinite loop */
} catch (Error e) {
server.disconnect_from ();
leave ();
throw e;
}
}
......@@ -49,18 +42,18 @@ public class Bot
/* join any open table */
debug ("Trying to join a table...");
try {
game = server.join_table (-1);
client.join_table (-1);
} catch (Error e) {
server.disconnect_from ();
leave ();
throw e;
}
debug ("Joined");
/* start listening to the server */
server.start_listening (game, player);
client.start_listening ();
/* react on game state changes */
game.notify["state"].connect ((s, p) => {
client.game.notify["state"].connect ((s, p) => {
switch (((OVCC.Game)s).state)
{
case OVCC.GameState.FINISHED:
......@@ -76,7 +69,7 @@ public class Bot
/* we should place a tile? OK, why not */
client.place_tile.connect ((tile) => {
debug ("Seems we should place a tile...");
game.board.foreach ((b, p, t) => {
client.game.board.foreach ((b, p, t) => {
for (var i = 0; i < 4; i++) {
var npos = p;
......@@ -88,7 +81,7 @@ public class Bot
}
for (var j = 0; j < 4; j++) {
/* FIXME: this should probably be asynchronous... */
if (game.place_tile (player, npos)) {
if (client.player.place_tile (npos)) {
return false;
} else {
tile.rotate (1);
......@@ -113,6 +106,13 @@ public class Bot
return true;
}
public void leave ()
{
client.leave ();
client = null;
loop.quit ();
}
public Bot (MainLoop l)
{
......@@ -126,11 +126,10 @@ private static void signal_handler (int sig)
{
debug ("Caught signal %d, exiting", sig);
try {
server.disconnect_from ();
bot.leave ();
} catch (Error e) {
warning ("Error disconnecting from server: %s", e.message);
warning ("Error leaving from server: %s", e.message);
}
loop.quit ();
Posix.exit (2);
}
......@@ -148,12 +147,12 @@ private static void setup_signal_handlers ()
#endif
MainLoop loop = null;
Bot bot = null;
OVCCClient.Server server = null;
public int main (string[] args)
{
MainLoop loop = null;
OVCCClient.Server server = null;
var host = "localhost";
uint16 port = 0xdead;
......
......@@ -27,19 +27,23 @@ namespace OVCC
public PlayerID id { get; private set; }
public uint score { get; private set; }
[Description (nick = "Nick", blurb = "Player's nickname")]
public string nick { get; construct; default = "Anonymous"; }
public string nick { get; construct;}
public Game game { get; private set; default = null; }
public Player (string nick)
{
Object (nick: nick);
}
public void joined (Game g)
{
game = g;
}
/* FIXME: WTF if this method? */
public bool add_tile (Board board,
Tile tile,
Position pos)
public bool place_tile (Position pos)
requires (game != null)
{
return board.add_tile (tile, pos);
return game.place_tile (this, pos);
}
}
}
......@@ -11,16 +11,16 @@ namespace OVCCClient
public class Client : Object
{
public Server server {get; private set; default = null;}
public Player player {get; construct;}
public Player player {get; set;}
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)
public Client ()
{
Object (player: p);
Object ();
}
public bool bind_to (Server s)
......@@ -32,19 +32,45 @@ namespace OVCCClient
}
server = s;
try {
// server.connect_to (player);
} catch {}
debug ("Trying to connect to server...");
server.connect_to (); /* may throw a rejection */
debug ("Connected");
return false;
return true;
}
public bool leave ()
{
server.disconnect_from ();
/* destroy server */
server = null;
game = null;
return false;
}
public bool login (string nick)
throws ServerError
{
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);
player.joined (game);
return game;
}
}
}
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