Commit 5e98d9ee authored by Jonathan Michalon's avatar Jonathan Michalon

clients/bot: use new table listing/selection mechanism

Use the new table listing/selection mechanism from previous commits in bot.
This adds also an optional interactive mode accessible through -i.
parent 080f399f
...@@ -5,13 +5,15 @@ ...@@ -5,13 +5,15 @@
public class Bot : OVCCClient.Client public class Bot : OVCCClient.Client
{ {
private MainLoop loop = null; private MainLoop loop = null;
private bool interactive = false;
private OVCC.SigQueue sigqueue = new OVCC.SigQueue (); private OVCC.SigQueue sigqueue = new OVCC.SigQueue ();
public async bool join (OVCCClient.Server server) public async bool join (OVCCClient.Server server)
throws Error throws Error
{ {
uint player_suffix = 7; uint player_suffix = 7;
int table_to_join = -1;
/* connect to the server */ /* connect to the server */
yield bind_to (server); yield bind_to (server);
...@@ -34,10 +36,29 @@ public class Bot : OVCCClient.Client ...@@ -34,10 +36,29 @@ public class Bot : OVCCClient.Client
} }
debug ("Logged in with player_suffix = %u", player_suffix); debug ("Logged in with player_suffix = %u", player_suffix);
/* join any open table */ /* join an open table */
debug ("Trying to join a table...");
var list = yield server.enumerate_tables(OVCCClient.TablesFilter.OPEN);
print ("List of open tables:\n");
var idx = 0;
foreach (var d in list) {
print (" - %02i: %s\n", idx, d.to_string());
idx++;
}
if (list.length == 0) {
print ("(currently no table)\n");
}
print ("What table index to join? (-1 or empty for any open table) ");
string? line = interactive ? stdin.read_line () : "";
if (line != null && line != "") {
table_to_join = int.parse(line);
}
debug ("Trying to join table %i...", table_to_join);
try { try {
yield join_table (-1); yield join_table (table_to_join);
} catch (Error e4) { } catch (Error e4) {
leave (); leave ();
throw e4; throw e4;
...@@ -144,9 +165,10 @@ public class Bot : OVCCClient.Client ...@@ -144,9 +165,10 @@ public class Bot : OVCCClient.Client
loop.quit (); loop.quit ();
} }
public Bot (MainLoop l) public Bot (MainLoop l, bool i = false)
{ {
loop = l; loop = l;
interactive = i;
} }
} }
...@@ -179,21 +201,34 @@ private static void setup_signal_handlers () ...@@ -179,21 +201,34 @@ private static void setup_signal_handlers ()
Bot bot = null; Bot bot = null;
// has to be outside of main to be const ?!
unowned string host = "localhost";
uint16 port = 0xDEAD;
bool interactive = false;
public int main (string[] args) public int main (string[] args)
{ {
var host = "localhost"; const OptionEntry[] options = {
uint16 port = 0xdead; { "server", 's', 0, OptionArg.STRING, ref host, "Use this server", "SERVER" },
{ "port", 'p', 0, OptionArg.INT, ref port, "Use this port number", "PORT" },
if (args.length > 1) { { "interactive", 'i', 0, OptionArg.NONE, ref interactive, "Be interactive", null },
host = args[1]; { null }
} };
if (args.length > 2) {
port = (uint16)int.parse (args[2]); try {
var opt_context = new OptionContext ("- OVCC Bot");
opt_context.set_help_enabled (true);
opt_context.add_main_entries (options, null);
opt_context.parse (ref args);
} catch (OptionError e) {
print ("error: %s\n", e.message);
print ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
return 0;
} }
var loop = new MainLoop (); var loop = new MainLoop ();
var server = new OVCCClient.Server (host, port); var server = new OVCCClient.Server (host, port);
bot = new Bot (loop); bot = new Bot (loop, interactive);
#if HAVE_POSIX #if HAVE_POSIX
setup_signal_handlers (); setup_signal_handlers ();
......
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