main.vala 3.34 KB
Newer Older
1 2 3 4 5 6 7
/*
 * A sample bot.
 * Currently it is dummy code only to develop the API. It doesn't compile and
 * it probably don't use the better API ever. Let's see.
 */


8
public class Bot
9
{
10
  private MainLoop          loop   = null;
11 12
  private OVCC.Player       player = null;
  private OVCCClient.Client client = null;
13 14
  
  public bool join (OVCCClient.Server server)
15
    throws Error
16
  {
17 18
    uint            player_suffix = 7;
    OVCCClient.Game game = null;
19

20
    /* connect to the server, choosing an available nickname */
21 22 23 24 25
    debug ("Trying to connect to server...");
    do {
      /* Create our player instance */
      player = new OVCC.Player ("""Clever bot %3u""".printf (player_suffix));
      
26
      try {
27 28
        client = new OVCCClient.Client (player);
        server.connect_to (player); /* may throw a rejection */
29
        break;
30
      } catch (OVCCClient.ServerError.NICKNAME_IN_USE e) {
31
        player_suffix++;
32
      } catch (OVCCClient.ServerError.MISSING_AUTHENTICATION e) {
33
        throw e; /* avoid infinite loop */
34 35
      }
    }
36 37 38
    while (true);
    debug ("Connected with player_suffix = %u", player_suffix);

39
    /* join any open table */
40
    debug ("Trying to join a table...");
41 42
    try {
      game = server.join_table (-1);
43
    } catch (Error e) {
44
      server.disconnect_from ();
45
      throw e;
46 47
    }
    debug ("Joined");
48
    
49 50 51 52 53 54 55 56 57 58 59 60 61
    /* react on game state changes */
    game.notify["state"].connect ((s, p) => {
      switch (((OVCC.Game)s).state)
      {
        case OVCC.GameState.FINISHED:
          loop.quit ();
          debug ("We're done, bye!");
          break;
        case OVCC.GameState.ABORTED:
          loop.quit ();
          debug ("Abnormal game termination!");
          break;
      }
62 63
    });
    /* we should place a tile? OK, why not */
64
    client.place_tile.connect ((tile) => {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
      debug ("Seems we should place a tile...");
      game.board.foreach ((b, p, t) => {
        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... */
            if (game.place_tile (player, npos)) {
              return false;
            } else {
              tile.rotate (1);
            }
          }
        }
        return true;
      });
    });
    /* we should place a pawn? let's do so */
89
    client.place_pawn.connect ((game, tile) => {
90 91 92 93 94
      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... */
95 96 97
//         if (game.place_pawn (player.pawns.nth_data (0), o)) {
//           break;
//         }
98 99
      }
    });
100 101

    return true;
102
  }
103 104 105 106 107
  
  public Bot (MainLoop l)
  {
    loop = l;
  }
108 109
}

110
public int main (string[] args)
111
{
112 113
  var loop = new MainLoop ();
  var bot = new Bot (loop);
114
  var server = new OVCCClient.Server ("localhost", 0xdead);
115
  
116
  try {
117 118
    bot.join (server);
    loop.run ();
119 120 121
  } catch (Error e) {
    warning ("Failed to join on the server: %s", e.message);
  }
122 123

  return 0;
124
}