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
#![allow(missing_docs)]
use crate::{FromProto, IntoProto};
use num_traits::FromPrimitive;
use sc2_proto::{
common::Race as ProtoRace,
sc2api::{
AIBuild as ProtoAIBuild, Difficulty as ProtoDifficulty, PlayerType as ProtoPlayerType,
Result as ProtoGameResult,
},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[variant_checkers]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, FromStr)]
pub enum Race {
Terran,
Zerg,
Protoss,
Random,
}
impl FromProto<ProtoRace> for Race {
fn from_proto(race: ProtoRace) -> Self {
match race {
ProtoRace::Terran => Race::Terran,
ProtoRace::Zerg => Race::Zerg,
ProtoRace::Protoss => Race::Protoss,
ProtoRace::Random => Race::Random,
ProtoRace::NoRace => Race::Random,
}
}
}
impl IntoProto<ProtoRace> for Race {
fn into_proto(self) -> ProtoRace {
match self {
Race::Terran => ProtoRace::Terran,
Race::Zerg => ProtoRace::Zerg,
Race::Protoss => ProtoRace::Protoss,
Race::Random => ProtoRace::Random,
}
}
}
impl Default for Race {
fn default() -> Self {
Race::Random
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, FromPrimitive, FromStr)]
#[enum_from_str(use_primitives)]
pub enum Difficulty {
VeryEasy,
Easy,
Medium,
MediumHard,
Hard,
Harder,
VeryHard,
CheatVision,
CheatMoney,
CheatInsane,
}
impl FromProto<ProtoDifficulty> for Difficulty {
fn from_proto(difficulty: ProtoDifficulty) -> Self {
match difficulty {
ProtoDifficulty::VeryEasy => Difficulty::VeryEasy,
ProtoDifficulty::Easy => Difficulty::Easy,
ProtoDifficulty::Medium => Difficulty::Medium,
ProtoDifficulty::MediumHard => Difficulty::MediumHard,
ProtoDifficulty::Hard => Difficulty::Hard,
ProtoDifficulty::Harder => Difficulty::Harder,
ProtoDifficulty::VeryHard => Difficulty::VeryHard,
ProtoDifficulty::CheatVision => Difficulty::CheatVision,
ProtoDifficulty::CheatMoney => Difficulty::CheatMoney,
ProtoDifficulty::CheatInsane => Difficulty::CheatInsane,
}
}
}
impl IntoProto<ProtoDifficulty> for Difficulty {
fn into_proto(self) -> ProtoDifficulty {
match self {
Difficulty::VeryEasy => ProtoDifficulty::VeryEasy,
Difficulty::Easy => ProtoDifficulty::Easy,
Difficulty::Medium => ProtoDifficulty::Medium,
Difficulty::MediumHard => ProtoDifficulty::MediumHard,
Difficulty::Hard => ProtoDifficulty::Hard,
Difficulty::Harder => ProtoDifficulty::Harder,
Difficulty::VeryHard => ProtoDifficulty::VeryHard,
Difficulty::CheatVision => ProtoDifficulty::CheatVision,
Difficulty::CheatMoney => ProtoDifficulty::CheatMoney,
Difficulty::CheatInsane => ProtoDifficulty::CheatInsane,
}
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, FromStr)]
pub enum AIBuild {
RandomBuild,
Rush,
Timing,
Power,
Macro,
Air,
}
impl FromProto<ProtoAIBuild> for AIBuild {
fn from_proto(ai_build: ProtoAIBuild) -> Self {
match ai_build {
ProtoAIBuild::RandomBuild => AIBuild::RandomBuild,
ProtoAIBuild::Rush => AIBuild::Rush,
ProtoAIBuild::Timing => AIBuild::Timing,
ProtoAIBuild::Power => AIBuild::Power,
ProtoAIBuild::Macro => AIBuild::Macro,
ProtoAIBuild::Air => AIBuild::Air,
}
}
}
impl IntoProto<ProtoAIBuild> for AIBuild {
fn into_proto(self) -> ProtoAIBuild {
match self {
AIBuild::RandomBuild => ProtoAIBuild::RandomBuild,
AIBuild::Rush => ProtoAIBuild::Rush,
AIBuild::Timing => ProtoAIBuild::Timing,
AIBuild::Power => ProtoAIBuild::Power,
AIBuild::Macro => ProtoAIBuild::Macro,
AIBuild::Air => ProtoAIBuild::Air,
}
}
}
impl Default for AIBuild {
fn default() -> Self {
AIBuild::RandomBuild
}
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum PlayerType {
Participant,
Computer,
Observer,
}
impl FromProto<ProtoPlayerType> for PlayerType {
fn from_proto(player_type: ProtoPlayerType) -> Self {
match player_type {
ProtoPlayerType::Participant => PlayerType::Participant,
ProtoPlayerType::Computer => PlayerType::Computer,
ProtoPlayerType::Observer => PlayerType::Observer,
}
}
}
impl IntoProto<ProtoPlayerType> for PlayerType {
fn into_proto(self) -> ProtoPlayerType {
match self {
PlayerType::Participant => ProtoPlayerType::Participant,
PlayerType::Computer => ProtoPlayerType::Computer,
PlayerType::Observer => ProtoPlayerType::Observer,
}
}
}
pub struct Computer {
pub race: Race,
pub difficulty: Difficulty,
pub ai_build: Option<AIBuild>,
}
impl Computer {
pub fn new(race: Race, difficulty: Difficulty, ai_build: Option<AIBuild>) -> Self {
Self {
race,
difficulty,
ai_build,
}
}
}
#[variant_checkers]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum GameResult {
Victory,
Defeat,
Tie,
Undecided,
}
impl FromProto<ProtoGameResult> for GameResult {
fn from_proto(player_type: ProtoGameResult) -> Self {
match player_type {
ProtoGameResult::Victory => GameResult::Victory,
ProtoGameResult::Defeat => GameResult::Defeat,
ProtoGameResult::Tie => GameResult::Tie,
ProtoGameResult::Undecided => GameResult::Undecided,
}
}
}