From 5e98d9ee93a7af73c99427bc77c1df7d35fba2cf Mon Sep 17 00:00:00 2001 From: Jonathan Michalon Date: Wed, 15 May 2019 13:32:59 +0200 Subject: [PATCH] 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. --- clients/bot/src/main.vala | 63 ++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/clients/bot/src/main.vala b/clients/bot/src/main.vala index 2c99bf7..f171af6 100644 --- a/clients/bot/src/main.vala +++ b/clients/bot/src/main.vala @@ -5,13 +5,15 @@ public class Bot : OVCCClient.Client { - private MainLoop loop = null; + private MainLoop loop = null; + private bool interactive = false; private OVCC.SigQueue sigqueue = new OVCC.SigQueue (); public async bool join (OVCCClient.Server server) throws Error { uint player_suffix = 7; + int table_to_join = -1; /* connect to the server */ yield bind_to (server); @@ -34,10 +36,29 @@ public class Bot : OVCCClient.Client } debug ("Logged in with player_suffix = %u", player_suffix); - /* join any open table */ - debug ("Trying to join a table..."); + /* join an open 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 { - yield join_table (-1); + yield join_table (table_to_join); } catch (Error e4) { leave (); throw e4; @@ -144,9 +165,10 @@ public class Bot : OVCCClient.Client loop.quit (); } - public Bot (MainLoop l) + public Bot (MainLoop l, bool i = false) { loop = l; + interactive = i; } } @@ -179,21 +201,34 @@ private static void setup_signal_handlers () 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) { - var host = "localhost"; - uint16 port = 0xdead; - - if (args.length > 1) { - host = args[1]; - } - if (args.length > 2) { - port = (uint16)int.parse (args[2]); + const OptionEntry[] options = { + { "server", 's', 0, OptionArg.STRING, ref host, "Use this server", "SERVER" }, + { "port", 'p', 0, OptionArg.INT, ref port, "Use this port number", "PORT" }, + { "interactive", 'i', 0, OptionArg.NONE, ref interactive, "Be interactive", null }, + { null } + }; + + 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 server = new OVCCClient.Server (host, port); - bot = new Bot (loop); + bot = new Bot (loop, interactive); #if HAVE_POSIX setup_signal_handlers (); -- GitLab