main.vala 4.53 KB
Newer Older
1 2 3 4 5
/*
 * A sample bot.
 */


Colomban Wendling's avatar
Colomban Wendling committed
6
/* maybe inherit from Client? */
7
public class Bot
8
{
9 10 11
  private MainLoop          loop      = null;
  private OVCCClient.Client client    = null;
  private OVCC.SigQueue     sigqueue  = new OVCC.SigQueue ();
12 13
  
  public bool join (OVCCClient.Server server)
14
    throws Error
15
  {
Colomban Wendling's avatar
Colomban Wendling committed
16
    uint player_suffix = 7;
17

18 19
    client = new OVCCClient.Client ();

20
    /* connect to the server */
21
    client.bind_to (server);
22 23 24

    /* login to the server, choosing an available nickname */
    debug ("Trying to login...");
Colomban Wendling's avatar
Colomban Wendling committed
25
    while (true) {
26
      try {
27
        client.login ("""Clever bot %03u""".printf (player_suffix));
28
        break;
29
      } catch (OVCCClient.ServerError.NICKNAME_IN_USE e) {
30
        player_suffix++;
31
      } catch (OVCCClient.ServerError.MISSING_AUTHENTICATION e) {
32
        leave ();
33
        throw e; /* avoid infinite loop */
34
      } catch (Error e) {
35
        leave ();
36
        throw e;
37 38
      }
    }
39 40
    debug ("Logged in with player_suffix = %u", player_suffix);

41
    /* join any open table */
42
    debug ("Trying to join a table...");
43
    try {
44
      client.join_table (-1);
45
    } catch (Error e) {
46
      leave ();
47
      throw e;
48 49
    }
    debug ("Joined");
50

51
    /* react on game state changes */
52
    sigqueue.add (client.game, client.game.notify["state"].connect ((s, p) => {
Colomban Wendling's avatar
Colomban Wendling committed
53
      switch (((OVCC.Game)s).state) {
54 55 56
        case OVCC.GameState.STARTED:
          debug ("Game started!");
          break;
57
        case OVCC.GameState.FINISHED:
Colomban Wendling's avatar
Colomban Wendling committed
58
          leave ();
59 60 61
          debug ("We're done, bye!");
          break;
        case OVCC.GameState.ABORTED:
Colomban Wendling's avatar
Colomban Wendling committed
62
          leave ();
63 64 65
          debug ("Abnormal game termination!");
          break;
      }
66
    }));
67
    /* we should place a tile? OK, why not */
68
    sigqueue.add (client, client.place_tile.connect ((tile) => {
69
      debug ("Seems we should place a tile...");
70
      client.game.board.foreach ((b, p, t) => {
71 72 73 74 75 76 77 78 79 80 81
        for (var i = 0; i < 4; i++) {
          var npos = p;
          
          switch (i) {
            case 0: npos.y--; break; /* top */
            case 1: npos.x++; break; /* right */
            case 2: npos.y++; break; /* bottom */
            case 3: npos.x--; break; /* left */
          }
          for (var j = 0; j < 4; j++) {
            /* FIXME: this should probably be asynchronous... */
82
            if (client.player.place_tile (npos)) {
83 84 85 86 87 88 89 90
              return false;
            } else {
              tile.rotate (1);
            }
          }
        }
        return true;
      });
91
    }));
92
    /* we should place a pawn? let's do so */
93
    sigqueue.add (client, client.place_pawn.connect ((tile) => {
94 95 96 97 98
      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... */
99 100 101
//         if (game.place_pawn (player.pawns.nth_data (0), o)) {
//           break;
//         }
102
      }
103
    }));
104

105

106
    return true;
107
  }
108

109 110 111 112 113 114 115
  public void play ()
    throws OVCC.GameError
  {
    debug ("Requesting game start");
    try {
      client.game.start();
    } catch (OVCC.GameError.STARTED e) {
116
      warning ("Cannot start game: %s", e.message);
117 118 119
    }
  }

120 121
  public void leave ()
  {
122 123
    sigqueue.remove_all ();
    
124 125 126 127
    client.leave ();
    client = null;
    loop.quit ();
  }
128 129 130 131 132
  
  public Bot (MainLoop l)
  {
    loop = l;
  }
133 134
}

135 136 137 138 139 140

#if HAVE_POSIX
private static void signal_handler (int sig)
{
  debug ("Caught signal %d, exiting", sig);
  try {
141
    bot.leave ();
142
  } catch (Error e) {
143
    warning ("Error leaving from server: %s", e.message);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  }
  Posix.exit (2);
}

private static void setup_signal_handlers ()
{
  Posix.sigaction_t s = {};
  s.sa_handler = signal_handler;
  Posix.sigemptyset (s.sa_mask);
  s.sa_flags = 0;
  
  Posix.sigaction (Posix.SIGQUIT, s, null);
  Posix.sigaction (Posix.SIGTERM, s, null);
  Posix.sigaction (Posix.SIGINT, s, null);
}
#endif


Colomban Wendling's avatar
Colomban Wendling committed
162
Bot bot = null;
163

164
public int main (string[] args)
165
{
166 167 168 169 170 171 172 173 174 175
  var     host = "localhost";
  uint16  port = 0xdead;
  
  if (args.length > 1) {
    host = args[1];
  }
  if (args.length > 2) {
    port = (uint16)int.parse (args[2]);
  }
  
Colomban Wendling's avatar
Colomban Wendling committed
176 177
  var loop = new MainLoop ();
  var server = new OVCCClient.Server (host, port);
178 179 180 181 182
  bot = new Bot (loop);
  
#if HAVE_POSIX
  setup_signal_handlers ();
#endif
183
  
184
  try {
185
    bot.join (server);
186 187 188
    /* wait to let others join */
    Thread.usleep (5000000);
    bot.play();
189
    loop.run ();
190
  } catch (Error e) {
191
    warning ("Failed to join & play on the server: %s", e.message);
192
  }
193 194

  return 0;
195
}