1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
use std::fmt;
use std::marker::PhantomData;
use std::sync::Mutex;
use std::sync::Arc;
use std::error::Error;
use std::time::{Instant,Duration};
use TryFrom;
use MaxIndex;
use error::EventDispatchError;
use error::EventHandlerError;
use error::TypeConvertError;
use error::PlayerError;
use UsiOutput;
use Logger;
use OnErrorHandler;
use shogi::*;
use selfmatch::*;
use rule::Validate;
pub trait MapEventKind<K> {
fn event_kind(&self) -> K;
}
#[derive(Debug)]
pub enum SystemEvent {
Usi,
IsReady,
SetOption(String,SysEventOption),
UsiNewGame,
Position(Teban,UsiInitialPosition,u32,Vec<Move>),
Go(UsiGo),
Stop,
PonderHit,
Quit,
GameOver(GameEndState),
SendUsiCommand(UsiOutput),
QuitReady,
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum SystemEventKind {
Usi = 0,
IsReady,
SetOption,
UsiNewGame,
Position,
Go,
Stop,
PonderHit,
Quit,
GameOver,
SendUsiCommand,
QuitReady,
}
impl From<SystemEventKind> for usize {
fn from(kind: SystemEventKind) -> usize {
kind as usize
}
}
impl MaxIndex for SystemEventKind {
fn max_index() -> usize {
SystemEventKind::QuitReady as usize
}
}
impl MapEventKind<SystemEventKind> for SystemEvent {
fn event_kind(&self) -> SystemEventKind {
match *self {
SystemEvent::Usi => SystemEventKind::Usi,
SystemEvent::IsReady => SystemEventKind::IsReady,
SystemEvent::SetOption(_,_) => SystemEventKind::SetOption,
SystemEvent::UsiNewGame => SystemEventKind::UsiNewGame,
SystemEvent::Position(_,_,_,_) => SystemEventKind::Position,
SystemEvent::Go(_) => SystemEventKind::Go,
SystemEvent::Stop => SystemEventKind::Stop,
SystemEvent::PonderHit => SystemEventKind::PonderHit,
SystemEvent::Quit => SystemEventKind::Quit,
SystemEvent::GameOver(_) => SystemEventKind::GameOver,
SystemEvent::SendUsiCommand(_) => SystemEventKind::SendUsiCommand,
SystemEvent::QuitReady => SystemEventKind::QuitReady,
}
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum SysEventOption {
Str(String),
Num(i64),
Bool(bool),
Exist,
}
impl Clone for SysEventOption {
fn clone(&self) -> SysEventOption {
match *self {
SysEventOption::Str(ref s) => SysEventOption::Str(s.clone()),
SysEventOption::Num(n) => SysEventOption::Num(n),
SysEventOption::Bool(b) => SysEventOption::Bool(b),
SysEventOption::Exist => SysEventOption::Exist,
}
}
}
#[derive(Debug)]
pub enum SysEventOptionKind {
Str,
Num,
Bool,
Exist,
}
#[derive(Eq,PartialEq,Debug)]
pub enum UsiInitialPosition {
Sfen(Banmen, MochigomaCollections),
Startpos,
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum UsiGo {
Go(UsiGoTimeLimit),
Ponder(UsiGoTimeLimit),
Mate(UsiGoMateTimeLimit),
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum UsiGoTimeLimit {
None,
Limit(Option<(u32,u32)>,Option<UsiGoByoyomiOrInc>),
Infinite,
}
impl UsiGoTimeLimit {
pub fn to_instant(&self,teban:Teban,now:Instant) -> Option<Instant> {
match self {
&UsiGoTimeLimit::None => None,
&UsiGoTimeLimit::Infinite => None,
&UsiGoTimeLimit::Limit(Some((ms,mg)),None) => {
Some(match teban {
Teban::Sente => {
now + Duration::from_millis(ms as u64)
},
Teban::Gote => {
now + Duration::from_millis(mg as u64)
}
})
},
&UsiGoTimeLimit::Limit(Some((ms,mg)),Some(UsiGoByoyomiOrInc::Byoyomi(b))) => {
Some(match teban {
Teban::Sente => {
now + Duration::from_millis(ms as u64 + b as u64)
},
Teban::Gote => {
now + Duration::from_millis(mg as u64 + b as u64)
}
})
}
&UsiGoTimeLimit::Limit(Some((ms,mg)),Some(UsiGoByoyomiOrInc::Inc(bs,bg))) => {
Some(match teban {
Teban::Sente => {
now + Duration::from_millis(ms as u64 + bs as u64)
},
Teban::Gote => {
now + Duration::from_millis(mg as u64 + bg as u64)
}
})
},
&UsiGoTimeLimit::Limit(None,Some(UsiGoByoyomiOrInc::Byoyomi(b))) => {
Some(now + Duration::from_millis(b as u64))
}
&UsiGoTimeLimit::Limit(None,Some(UsiGoByoyomiOrInc::Inc(bs,bg))) => {
Some(match teban {
Teban::Sente => {
now + Duration::from_millis(bs as u64)
},
Teban::Gote => {
now + Duration::from_millis(bg as u64)
}
})
},
&UsiGoTimeLimit::Limit(None,None) => {
Some(now)
}
}
}
pub fn calc_next_limit(&self,teban:Teban,think_start_time:Instant,now:Instant) -> Option<u64> {
let limit = self.to_instant(teban, think_start_time);
limit.and_then(|limit| match self {
&UsiGoTimeLimit::None => None,
&UsiGoTimeLimit::Infinite => None,
&UsiGoTimeLimit::Limit(Some((ms,mg)),None) |
&UsiGoTimeLimit::Limit(Some((ms,mg)),Some(UsiGoByoyomiOrInc::Byoyomi(_))) => {
Some(match teban {
Teban::Sente => ms as u64,
Teban::Gote => mg as u64,
})
},
&UsiGoTimeLimit::Limit(Some((ms,mg)),Some(UsiGoByoyomiOrInc::Inc(_,_))) => {
let elapsed = limit - now;
Some(match teban {
Teban::Sente => {
ms as u64 + elapsed.as_secs() * 1000 + elapsed.subsec_millis() as u64
},
Teban::Gote => {
mg as u64 + elapsed.as_secs() * 1000 + elapsed.subsec_millis() as u64
}
})
},
&UsiGoTimeLimit::Limit(None,None) |
&UsiGoTimeLimit::Limit(None,Some(UsiGoByoyomiOrInc::Byoyomi(_))) => {
Some(0)
}
&UsiGoTimeLimit::Limit(None,Some(UsiGoByoyomiOrInc::Inc(_,_))) => {
let elapsed = limit - now;
Some(elapsed.as_secs() * 1000 + elapsed.subsec_millis() as u64)
},
})
}
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum UsiGoMateTimeLimit {
None,
Limit(u32),
Infinite,
}
impl UsiGoMateTimeLimit {
pub fn to_instant(&self,now:Instant) -> Option<Instant> {
match *self {
UsiGoMateTimeLimit::Infinite | UsiGoMateTimeLimit::None => None,
UsiGoMateTimeLimit::Limit(limit) => {
Some(now + Duration::from_millis(limit as u64))
}
}
}
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum UsiGoByoyomiOrInc {
Byoyomi(u32),
Inc(u32,u32),
}
#[derive(Debug)]
pub enum UserEvent {
Stop,
PonderHit(Instant),
Quit,
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum UserEventKind {
Stop = 0,
PonderHit,
Quit,
}
impl MapEventKind<UserEventKind> for UserEvent {
fn event_kind(&self) -> UserEventKind {
match *self {
UserEvent::Stop => UserEventKind::Stop,
UserEvent::PonderHit(_) => UserEventKind::PonderHit,
UserEvent::Quit => UserEventKind::Quit,
}
}
}
impl From<UserEventKind> for usize {
fn from(kind: UserEventKind) -> usize {
kind as usize
}
}
impl MaxIndex for UserEventKind {
fn max_index() -> usize {
UserEventKind::Quit as usize
}
}
#[derive(Debug)]
pub enum SelfMatchEvent {
GameStart(u32,Teban,String),
Moved(Teban,Moved),
GameEnd(SelfMatchGameEndState),
Abort,
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum Moved {
To(MovedKind,(u32,u32),(u32,u32),bool),
Put(MochigomaKind,(u32,u32)),
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum MovedKind {
Fu = 0,
Kyou,
Kei,
Gin,
Kin,
Kaku,
Hisha,
SOu,
GOu,
FuN,
KyouN,
KeiN,
GinN,
KakuN,
HishaN,
Blank,
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum SelfMatchGameEndState {
Win(Teban),
Resign(Teban),
NyuGyokuWin(Teban),
NyuGyokuLose(Teban),
Draw,
Foul(Teban,FoulKind),
Timeover(Teban),
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum GameEndState {
Win,
Lose,
Draw,
}
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum FoulKind {
InvalidMove,
PutFuAndMate,
Sennichite,
SennichiteOu,
NotRespondedOute,
Suicide,
}
#[derive(Debug)]
pub enum SelfMatchEventKind {
GameStart = 0,
Moved,
GameEnd,
Abort,
}
impl MapEventKind<SelfMatchEventKind> for SelfMatchEvent {
fn event_kind(&self) -> SelfMatchEventKind {
match *self {
SelfMatchEvent::GameStart(_,_,_) => SelfMatchEventKind::GameStart,
SelfMatchEvent::Moved(_,_) => SelfMatchEventKind::Moved,
SelfMatchEvent::GameEnd(_) => SelfMatchEventKind::GameEnd,
SelfMatchEvent::Abort => SelfMatchEventKind::Abort,
}
}
}
impl From<SelfMatchEventKind> for usize {
fn from(kind: SelfMatchEventKind) -> usize {
kind as usize
}
}
impl MaxIndex for SelfMatchEventKind {
fn max_index() -> usize {
SelfMatchEventKind::Abort as usize
}
}
impl fmt::Display for MovedKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
MovedKind::Fu => write!(f,"歩"),
MovedKind::Kyou => write!(f,"香"),
MovedKind::Kei => write!(f,"桂"),
MovedKind::Gin => write!(f,"銀"),
MovedKind::Kin => write!(f,"金"),
MovedKind::Kaku => write!(f,"角"),
MovedKind::Hisha => write!(f,"飛"),
MovedKind::SOu => write!(f,"王"),
MovedKind::GOu => write!(f,"玉"),
MovedKind::FuN => write!(f,"成歩"),
MovedKind::KyouN => write!(f,"成香"),
MovedKind::KeiN => write!(f,"成桂"),
MovedKind::GinN => write!(f,"成銀"),
MovedKind::KakuN => write!(f,"馬"),
MovedKind::HishaN => write!(f,"龍"),
MovedKind::Blank => write!(f,"駒無し"),
}
}
}
impl<'a> TryFrom<(&'a Banmen,&'a Move),TypeConvertError<String>> for Moved {
fn try_from(s:(&'a Banmen,&'a Move)) -> Result<Moved, TypeConvertError<String>> {
Ok(match s {
(&Banmen(ref kinds),&Move::To(KomaSrcPosition(sx,sy),KomaDstToPosition(dx,dy,n))) => {
match kinds[sy as usize - 1][9 - sx as usize] {
KomaKind::SFu => Moved::To(MovedKind::Fu,(sx,sy),(dx,dy),n),
KomaKind::SKyou => Moved::To(MovedKind::Kyou,(sx,sy),(dx,dy),n),
KomaKind::SKei => Moved::To(MovedKind::Kei,(sx,sy),(dx,dy),n),
KomaKind::SGin => Moved::To(MovedKind::Gin,(sx,sy),(dx,dy),n),
KomaKind::SKin => Moved::To(MovedKind::Kin,(sx,sy),(dx,dy),n),
KomaKind::SKaku => Moved::To(MovedKind::Kaku,(sx,sy),(dx,dy),n),
KomaKind::SHisha => Moved::To(MovedKind::Hisha,(sx,sy),(dx,dy),n),
KomaKind::SOu => Moved::To(MovedKind::SOu,(sx,sy),(dx,dy),n),
KomaKind::SFuN => Moved::To(MovedKind::FuN,(sx,sy),(dx,dy),n),
KomaKind::SKyouN => Moved::To(MovedKind::KyouN,(sx,sy),(dx,dy),n),
KomaKind::SKeiN => Moved::To(MovedKind::KeiN,(sx,sy),(dx,dy),n),
KomaKind::SGinN => Moved::To(MovedKind::GinN,(sx,sy),(dx,dy),n),
KomaKind::SKakuN => Moved::To(MovedKind::KakuN,(sx,sy),(dx,dy),n),
KomaKind::SHishaN => Moved::To(MovedKind::HishaN,(sx,sy),(dx,dy),n),
KomaKind::GFu => Moved::To(MovedKind::Fu,(sx,sy),(dx,dy),n),
KomaKind::GKyou => Moved::To(MovedKind::Kyou,(sx,sy),(dx,dy),n),
KomaKind::GKei => Moved::To(MovedKind::Kei,(sx,sy),(dx,dy),n),
KomaKind::GGin => Moved::To(MovedKind::Gin,(sx,sy),(dx,dy),n),
KomaKind::GKin => Moved::To(MovedKind::Kin,(sx,sy),(dx,dy),n),
KomaKind::GKaku => Moved::To(MovedKind::Kaku,(sx,sy),(dx,dy),n),
KomaKind::GHisha => Moved::To(MovedKind::Hisha,(sx,sy),(dx,dy),n),
KomaKind::GOu => Moved::To(MovedKind::GOu,(sx,sy),(dx,dy),n),
KomaKind::GFuN => Moved::To(MovedKind::FuN,(sx,sy),(dx,dy),n),
KomaKind::GKyouN => Moved::To(MovedKind::KyouN,(sx,sy),(dx,dy),n),
KomaKind::GKeiN => Moved::To(MovedKind::KeiN,(sx,sy),(dx,dy),n),
KomaKind::GGinN => Moved::To(MovedKind::GinN,(sx,sy),(dx,dy),n),
KomaKind::GKakuN => Moved::To(MovedKind::KakuN,(sx,sy),(dx,dy),n),
KomaKind::GHishaN => Moved::To(MovedKind::HishaN,(sx,sy),(dx,dy),n),
KomaKind::Blank => Moved::To(MovedKind::Blank,(sx,sy),(dx,dy),n),
}
},
(_,&Move::Put(k,KomaDstPutPosition(x,y))) => {
match k {
MochigomaKind::Fu => Moved::Put(MochigomaKind::Fu,(x,y)),
MochigomaKind::Kyou => Moved::Put(MochigomaKind::Kyou,(x,y)),
MochigomaKind::Kei => Moved::Put(MochigomaKind::Kei,(x,y)),
MochigomaKind::Gin => Moved::Put(MochigomaKind::Gin,(x,y)),
MochigomaKind::Kin => Moved::Put(MochigomaKind::Kin,(x,y)),
MochigomaKind::Hisha => Moved::Put(MochigomaKind::Hisha,(x,y)),
MochigomaKind::Kaku => Moved::Put(MochigomaKind::Kaku,(x,y)),
}
}
})
}
}
const KANSUJI_MAP:[char; 10] = ['零','一','二','三','四','五','六','七','八','九'];
const MOCHIGOMA_DISPLAY_MAP:[char; 7] = ['歩','香','桂','銀','金','角','飛'];
impl fmt::Display for Moved {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if !self.validate() {
match self {
&Moved::To(k,(sx,sy),(dx,dy),true) if sy > 9 || dy > 9 => {
write!(f,"{},{}{} -> {},{}成 (不正な手です)",sx,sy,k,dx,dy)
},
&Moved::To(k,(sx,sy),(dx,dy),false) if sy > 9 || dy > 9 => {
write!(f,"{},{}{} -> {},{} (不正な手です)",sx,sy,k,dx,dy)
},
&Moved::To(k,(sx,sy),(dx,dy),true) => {
write!(f,"{}{}{} -> {}{}成 (不正な手です)",sx,KANSUJI_MAP[sy as usize],k,dx,KANSUJI_MAP[dy as usize])
},
&Moved::To(k,(sx,sy),(dx,dy),false) => {
write!(f,"{}{}{} -> {}{} (不正な手です)",sx,KANSUJI_MAP[sy as usize],k,dx,KANSUJI_MAP[dy as usize])
},
&Moved::Put(k,(x,y)) if y > 9 => {
write!(f,"{},{}{} (不正な手です)",x,y,MOCHIGOMA_DISPLAY_MAP[k as usize])
},
&Moved::Put(k,(x,y)) => {
write!(f,"{}{}{} (不正な手です)",x,KANSUJI_MAP[y as usize],MOCHIGOMA_DISPLAY_MAP[k as usize])
},
}
} else {
match self {
&Moved::To(k,(sx,sy),(dx,dy),true) => {
write!(f,"{}{}{} -> {}{}成",sx,KANSUJI_MAP[sy as usize],k,dx,KANSUJI_MAP[dy as usize])
},
&Moved::To(k,(sx,sy),(dx,dy),false) => {
write!(f,"{}{}{} -> {}{}",sx,KANSUJI_MAP[sy as usize],k,dx,KANSUJI_MAP[dy as usize])
},
&Moved::Put(k,(x,y)) => {
write!(f,"{}{}{}",x,KANSUJI_MAP[y as usize],MOCHIGOMA_DISPLAY_MAP[k as usize])
},
}
}
}
}
#[derive(Debug)]
pub struct EventQueue<E,K> where E: MapEventKind<K> + fmt::Debug, K: fmt::Debug {
event_kind:PhantomData<K>,
events:Vec<E>,
}
impl<E,K> EventQueue<E,K> where E: MapEventKind<K> + fmt::Debug, K: fmt::Debug {
pub fn new() -> EventQueue<E,K> {
EventQueue {
event_kind:PhantomData::<K>,
events: Vec::new()
}
}
pub fn push(&mut self,e:E) {
self.events.push(e);
}
pub fn clear(&mut self) {
self.events.clear();
}
pub fn drain_events(&mut self) -> Vec<E> {
self.events.drain(0..).collect()
}
pub fn has_event(&self) -> bool {
self.events.len() > 0
}
}
pub trait EventDispatcher<'b,K,E,T,UE> where K: MaxIndex + fmt::Debug,
E: MapEventKind<K> + fmt::Debug,
UE: PlayerError,
EventHandlerError<K,UE>: From<UE>,
usize: From<K> {
fn add_handler<F>(&mut self, id:K, handler:F) where F: FnMut(&T,&E) ->
Result<(), EventHandlerError<K,UE>> + 'b;
fn add_once_handler<F>(&mut self, id:K, handler:F) where F: FnMut(&T,&E) ->
Result<(), EventHandlerError<K,UE>> + 'b;
fn dispatch_events<'a>(&mut self, ctx:&T, event_queue:&'a Mutex<EventQueue<E,K>>) ->
Result<(), EventDispatchError<'a,EventQueue<E,K>,E,UE>>
where E: fmt::Debug, K: fmt::Debug,
UE: Error + fmt::Debug,
EventHandlerError<K,UE>: From<UE>,
usize: From<K>;
}
pub struct USIEventDispatcher<'b,K,E,T,L,UE>
where K: MaxIndex + fmt::Debug,
E: MapEventKind<K> + fmt::Debug,
L: Logger,
UE: PlayerError,
EventHandlerError<K,UE>: From<UE>,
usize: From<K> {
on_error_handler:Arc<Mutex<OnErrorHandler<L>>>,
context_type:PhantomData<T>,
event_kind:PhantomData<K>,
handlers:Vec<Vec<Box<dyn FnMut(&T,&E) -> Result<(), EventHandlerError<K,UE>> + 'b>>>,
once_handlers:Vec<Vec<Box<dyn FnMut(&T, &E) -> Result<(), EventHandlerError<K,UE>> + 'b>>>,
}
impl<'b,K,E,T,L,UE> USIEventDispatcher<'b,K,E,T,L,UE>
where K: MaxIndex + fmt::Debug,
E: MapEventKind<K> + fmt::Debug,
L: Logger,
UE: PlayerError,
EventHandlerError<K,UE>: From<UE>,
usize: From<K> {
pub fn new(on_error_handler:&Arc<Mutex<OnErrorHandler<L>>>) -> USIEventDispatcher<'b,K,E,T,L,UE>
where K: MaxIndex + fmt::Debug, usize: From<K>,
E: MapEventKind<K> + fmt::Debug,
L: Logger,
UE: PlayerError,
EventHandlerError<K,UE>: From<UE>, {
let mut o = USIEventDispatcher {
on_error_handler:on_error_handler.clone(),
context_type:PhantomData::<T>,
event_kind:PhantomData::<K>,
handlers:Vec::with_capacity(K::max_index()+1),
once_handlers:Vec::with_capacity(K::max_index()+1),
};
for _ in 0..K::max_index() + 1 {
o.handlers.push(Vec::new());
o.once_handlers.push(Vec::new());
}
o
}
}
impl<'b,K,E,T,L,UE> EventDispatcher<'b,K,E,T,UE> for USIEventDispatcher<'b,K,E,T,L,UE> where K: MaxIndex + fmt::Debug,
E: MapEventKind<K> + fmt::Debug,
L: Logger,
UE: PlayerError,
EventHandlerError<K,UE>: From<UE>,
usize: From<K> {
fn add_handler<F>(&mut self, id:K, handler:F) where F: FnMut(&T,&E) ->
Result<(), EventHandlerError<K,UE>> + 'b {
self.handlers[usize::from(id)].push(Box::new(handler));
}
fn add_once_handler<F>(&mut self, id:K, handler:F) where F: FnMut(&T,&E) ->
Result<(), EventHandlerError<K,UE>> + 'b {
self.once_handlers[usize::from(id)].push(Box::new(handler));
}
fn dispatch_events<'a>(&mut self, ctx:&T, event_queue:&'a Mutex<EventQueue<E,K>>) ->
Result<(), EventDispatchError<'a,EventQueue<E,K>,E,UE>>
where E: fmt::Debug, K: fmt::Debug, usize: From<K> {
let events = {
event_queue.lock()?.drain_events()
};
let mut has_error = false;
for e in &events {
for h in self.handlers[usize::from(e.event_kind())].iter_mut() {
match h(ctx, e) {
Ok(_) => true,
Err(ref e) => {
has_error = true;
self.on_error_handler.lock().map(|h| h.call(e)).is_err()
}
};
}
if !self.once_handlers[usize::from(e.event_kind())].is_empty() {
let mut once_handlers:Vec<Box<dyn FnMut(&T, &E) -> Result<(), EventHandlerError<K,UE>>>> =
self.once_handlers[usize::from(e.event_kind())].drain(0..)
.collect();
for h in once_handlers.iter_mut() {
match h(ctx, e) {
Ok(_) => true,
Err(ref e) => {
has_error = true;
self.on_error_handler.lock().map(|h| h.call(e)).is_err()
}
};
}
}
}
match has_error {
true => Err(EventDispatchError::ContainError),
false => Ok(()),
}
}
}
pub type SystemEventQueue = EventQueue<SystemEvent,SystemEventKind>;
pub type SystemEventDispatcher<'a,T,E,L> = USIEventDispatcher<'a,SystemEventKind,SystemEvent,T,L,E>;
pub type UserEventQueue = EventQueue<UserEvent,UserEventKind>;
pub type UserEventDispatcher<'a,T,E,L> = USIEventDispatcher<'a,UserEventKind,UserEvent,T,L,E>;
pub type SelfMatchEventQueue = EventQueue<SelfMatchEvent,SelfMatchEventKind>;
pub type SelfMatchEventDispatcher<'a,E,L> = USIEventDispatcher<'a,SelfMatchEventKind,SelfMatchEvent,SelfMatchEngine<E>,L,E>;