main.vala 4.36 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
  private MainLoop          loop   = null;
10
  private OVCCClient.Client client = null;
11 12
  
  public bool join (OVCCClient.Server server)
13
    throws Error
14
  {
Colomban Wendling's avatar
Colomban Wendling committed
15
    uint player_suffix = 7;
16

17 18
    client = new OVCCClient.Client ();

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

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

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

50
    
51
    /* react on game state changes */
52
    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
    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 91 92
              return false;
            } else {
              tile.rotate (1);
            }
          }
        }
        return true;
      });
    });
    /* we should place a pawn? let's do so */
93
    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 122 123 124 125
  public void leave ()
  {
    client.leave ();
    client = null;
    loop.quit ();
  }
126 127 128 129 130
  
  public Bot (MainLoop l)
  {
    loop = l;
  }
131 132
}

133 134 135 136 137 138

#if HAVE_POSIX
private static void signal_handler (int sig)
{
  debug ("Caught signal %d, exiting", sig);
  try {
139
    bot.leave ();
140
  } catch (Error e) {
141
    warning ("Error leaving from server: %s", e.message);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  }
  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
160
Bot bot = null;
161

162
public int main (string[] args)
163
{
164 165 166 167 168 169 170 171 172 173
  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
174 175
  var loop = new MainLoop ();
  var server = new OVCCClient.Server (host, port);
176 177 178 179 180
  bot = new Bot (loop);
  
#if HAVE_POSIX
  setup_signal_handlers ();
#endif
181
  
182
  try {
183
    bot.join (server);
184 185 186
    /* wait to let others join */
    Thread.usleep (5000000);
    bot.play();
187
    loop.run ();
188
  } catch (Error e) {
189
    warning ("Failed to join & play on the server: %s", e.message);
190
  }
191 192

  return 0;
193
}