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


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

16
    /* connect to the server */
17
    bind_to (server);
18 19 20

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

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

47
    /* react on game state changes */
48
    sigqueue.add (game, game.notify["state"].connect ((s, p) => {
Colomban Wendling's avatar
Colomban Wendling committed
49
      switch (((OVCC.Game)s).state) {
50 51 52
        case OVCC.GameState.STARTED:
          debug ("Game started!");
          break;
53
        case OVCC.GameState.FINISHED:
Colomban Wendling's avatar
Colomban Wendling committed
54
          leave ();
55 56 57
          debug ("We're done, bye!");
          break;
        case OVCC.GameState.ABORTED:
Colomban Wendling's avatar
Colomban Wendling committed
58
          leave ();
59 60 61
          debug ("Abnormal game termination!");
          break;
      }
62
    }));
63
    /* we should place a tile? OK, why not */
64
    sigqueue.add (this, place_tile.connect ((tile) => {
65
      debug ("Seems we should place a tile...");
66
      game.board.foreach ((b, p, t) => {
67 68 69 70 71 72 73 74 75 76 77
        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... */
78
            if (player.place_tile (npos)) {
79 80 81 82 83 84 85 86
              return false;
            } else {
              tile.rotate (1);
            }
          }
        }
        return true;
      });
87
    }));
88
    /* we should place a pawn? let's do so */
89
    sigqueue.add (this, place_pawn.connect ((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

102
    return true;
103
  }
104

105 106 107 108 109
  public void play ()
    throws OVCC.GameError
  {
    debug ("Requesting game start");
    try {
110
      game.start();
111
    } catch (OVCC.GameError.STARTED e) {
112
      warning ("Cannot start game: %s", e.message);
113 114 115
    }
  }

116
  public new void leave ()
117
  {
118 119
    sigqueue.remove_all ();
    
120
    base.leave ();
121 122
    loop.quit ();
  }
123 124 125 126 127
  
  public Bot (MainLoop l)
  {
    loop = l;
  }
128 129
}

130 131 132 133 134 135

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

159
public int main (string[] args)
160
{
161 162 163 164 165 166 167 168 169 170
  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
171 172
  var loop = new MainLoop ();
  var server = new OVCCClient.Server (host, port);
173 174 175 176 177
  bot = new Bot (loop);
  
#if HAVE_POSIX
  setup_signal_handlers ();
#endif
178
  
179
  try {
180
    bot.join (server);
181
    /* wait to let others join */
182 183 184 185 186 187 188 189 190 191 192
    Timeout.add_seconds (5, () => {
      if (bot.game.state != OVCC.GameState.STARTED) {
        debug ("let the show begin!");
        try {
          bot.play ();
        } catch (Error e) {
          warning ("Failed to start playing: %s", e.message);
        }
      }
      return false;
    });
193
    loop.run ();
194
  } catch (Error e) {
195
    warning ("Failed to join on the server: %s", e.message);
196
  }
197 198

  return 0;
199
}