Added support for GPSShogi engine
[kaya.git] / lib / plugins / engines / gpsshogi.rb
blobd56c9b178eb8162f046a763817a77ed6c8c24d0e
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'plugins/plugin'
9 require 'interaction/match'
10 require_bundle 'engines', 'engine'
12 class GPSShogiEngine < Engine
13   include Plugin
14   
15   plugin :name => 'GPSShogi Engine Protocol',
16          :protocol => 'GPSShogi',
17          :interface => :engine
18   
19   def on_move(data)
20     text = @serializer.serialize(data[:move], data[:old_state])
21     send_command text
22   end
23   
24   def on_engine_start
25     send_command "new"
26     if @color == :black
27       send_command "black"
28       send_command "go"
29     else
30       send_command "white"
31     end
32   end
33   
34   def extra_command(text)
35     if text =~ /^[0-9]*\. (\.\.\.) (\S+)/
36       move = @serializer.deserialize($2, @match.state)
37       if move
38         @match.move(self, move)
39       end
40     end
41   end
42   
43   def on_close(data)
44     send_command "quit"
45     @engine.kill
46   end
47   
48   def allow_undo?(player, manager)
49     # gpsshogi does not wait when you tell it to undo
50     # so we stop it from playing before calling undo
51     send_command "force"
52     send_command "undo"
53     send_command "undo"
54     if @color == :black
55       send_command "black"
56     else
57       send_command "white"
58     end
59     manager.undo(self, 2)
60   end
61 end