Translate status bar messages.
[kaya.git] / lib / plugins / ics / lib / style12.rb
blobe63a618b5cc4aff44d7f43f1aa752831684fbb8c
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_bundle 'ics', 'icsapi'
10 module ICS
12 # Style 12 was designed by Daniel Sleator 
13 # (sleator+@cs.cmu.edu) Darooha@ICC
14 class Style12
15   PATTERN = %r{
16     ^<12>\s+                      # header
17     ([qkbnrpQKBNRP-]{8})\s+       # chessboard
18     ([qkbnrpQKBNRP-]{8})\s+       # chessboard
19     ([qkbnrpQKBNRP-]{8})\s+       # chessboard
20     ([qkbnrpQKBNRP-]{8})\s+       # chessboard
21     ([qkbnrpQKBNRP-]{8})\s+       # chessboard
22     ([qkbnrpQKBNRP-]{8})\s+       # chessboard
23     ([qkbnrpQKBNRP-]{8})\s+       # chessboard
24     ([qkbnrpQKBNRP-]{8})\s+       # chessboard
25     ([BW])\s+                     # black/white
26     (-1|[0-7])\s+                 # passing pawn
27     ([01])\s+                     # castle rights
28     ([01])\s+                     # castle rights
29     ([01])\s+                     # castle rights
30     ([01])\s+                     # castle rights
31     (-?\d+)\s+                    # 50 moves made
32     (\d+)\s+                      # game number
33     (\S+)\s+                      # white name
34     (\S+)\s+                      # black name
35     (-[1-4]|[0-2])\s+             # status
36     (\d+)\s+                      # time
37     (\d+)\s+                      # inc
38     (\d+)\s+                      # w material
39     (\d+)\s+                      # b material
40     (-?\d+)\s+                    # w time
41     (-?\d+)\s+                    # b time
42     (\d+)\s+                      # move made
43     (\S+)\s+                      # coordmove
44     (\S+)\s+                      # time used
45     (\S+)\s+                      # algmove
46     ([0-1])                       # flip }x
48   CHESSBOARD = 1
49   TURN = 9
50   EN_PASSANT = 10
51   WHITE_KING_CASTLING = 11
52   WHITE_QUEEN_CASTLING = 12
53   BLACK_KING_CASTLING = 13
54   BLACK_QUEEN_CASTLING = 14
55   REVERSIBLE_MOVES = 15
56   GAME_NUMBER = 16
57   WHITE_PLAYER = 17
58   BLACK_PLAYER = 18
59   RELATION = 19
60   STARTING_TIME = 20
61   STARTING_INCREMENT = 21
62   WHITE_TIME = 24
63   BLACK_TIME = 25
64   MOVE_ORDINAL = 26
65   LAST_MOVE_VERBOSE = 27
66   TIME_USED = 28
67   LAST_MOVE = 29
68   FLIP = 30
69   
70   class Relation
71     MOVE_LIST_START = -4
72     ISOLATED_POSITION = -3
73     OBSERVING_EXAMINED = -2
74     NOT_MY_MOVE = -1
75     OBSERVING_PLAYED = 0
76     MY_MOVE = 1
77     EXAMINING = 2
78   end
80   def self.from_match(match, games)
81     game_number = match[GAME_NUMBER].to_i
82     current_game = games[game_number]
83     if current_game.nil?
84       game = Game.dummy
85       current_game = {
86         :white => { :name => match[WHITE_PLAYER] },
87         :black => { :name => match[BLACK_PLAYER] },
88         :number => game_number,
89         :game => game,
90         :icsapi => ICSApi.new(game)
91       }
92       games[game_number] = current_game
93     end
94     icsapi = current_game[:icsapi]
95     state = 
96       icsapi.new_state(:turn => match[TURN] == 'W' ? :white : :black,
97                        :en_passant => match[EN_PASSANT].to_i,
98                        :wk_castling => match[WHITE_KING_CASTLING].to_i == 1,
99                        :wq_castling => match[WHITE_QUEEN_CASTLING].to_i == 1,
100                        :bk_castling => match[BLACK_KING_CASTLING].to_i == 1,
101                        :bq_castling => match[BLACK_QUEEN_CASTLING].to_i == 1)
102     match[CHESSBOARD..CHESSBOARD+8].each_with_index do |row, i|
103       row.split(//).each_with_index do |p, j|
104         piece = icsapi.new_piece(p)
105         state.board[Point.new(j, i)] = piece
106       end
107     end
109     style12 = new(:state => state,
110                   :game_number => match[GAME_NUMBER].to_i,
111                   :move_index => match[MOVE_ORDINAL].to_i,
112                   :white_player => match[WHITE_PLAYER],
113                   :black_player => match[BLACK_PLAYER],
114                   :relation => match[RELATION].to_i,
115                   :last_move => match[LAST_MOVE_VERBOSE],
116                   :last_move_san => match[LAST_MOVE],
117                   :time => {
118                     :white => match[WHITE_TIME].to_i,
119                     :black => match[BLACK_TIME].to_i },
120                   :match_info => current_game
121                   )
122   end
124   def initialize(opts)
125     @opts = opts
126   end
127   
128   def method_missing(field)
129     if @opts.has_key?(field)
130       @opts[field]
131     else
132       super(field)
133     end
134   end
135   
136   def move_index
137     (@opts[:move_index] - 1) * 2 + 
138       (state.turn == :white ? 0 : 1)
139   end