forked from yendtm/Mohawk_Battleship
/
CMatchInfo.cs
73 lines (66 loc) · 3.04 KB
/
CMatchInfo.cs
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
using MBC.Core.Util;
using MBC.Shared;
using System;
using System.Collections.Generic;
namespace MBC.Core
{
/// <summary>
/// Derives a <see cref="MatchInfo"/> for the sole purpose of initializing its field members.
/// </summary>
/// <seealso cref="MatchInfo"/>
[Configuration("mbc_field_width", 10)]
[Configuration("mbc_field_height", 10)]
[Configuration("mbc_ship_sizes", 2, 3, 3, 4, 5)]
[Configuration("mbc_timeout", 200)]
[Configuration("mbc_game_mode", GameMode.Classic, null)]
public class CMatchInfo : MatchInfo
{
/// <summary>
/// Initializes the underlying <see cref="MatchInfo"/> with values provided by a <see cref="Configuration"/>
/// and associates the <see cref="MatchInfo"/> with the given <see cref="ControllerInformation"/>.
/// </summary>
/// <param name="config">The <see cref="Configuration"/> to load settings from.</param>
/// <param name="controllerInfos">A variable number of <see cref="ControllerInformation"/>
/// to associate with.</param>
/// <exception cref="ControllerIncompatibleException">A controller is not designed for the
/// configured <see cref="GameMode"/>.</exception>
public CMatchInfo(Configuration config, params ControllerInformation[] controllerInfos)
{
//Get the game mode from the Configuration.
DetermineGameMode(config);
//Make sure all of the controllers are compatible with the given game mode.
foreach (var controllerInfo in controllerInfos)
{
if (!controllerInfo.Capabilities.CompatibleWith(gameMode))
{
throw new ControllerIncompatibleException(controllerInfo, gameMode);
}
}
this.controllerNames = new List<string>();
foreach (var controllerInfo in controllerInfos)
{
this.controllerNames.Add(controllerInfo.ToString());
}
//Configuration setting for a list of ships that the controllers start with.
initShips = new ShipList(config.GetList<int>("mbc_ship_sizes").ToArray());
//Configuration setting for the size of the field.
fieldSize = new Coordinates(
config.GetValue<int>("mbc_field_width"),
config.GetValue<int>("mbc_field_height"));
//Configuration setting for the amount of time a controller is allowed per method invoke.
methodTimeLimit = config.GetValue<int>("mbc_timeout");
}
private void DetermineGameMode(Configuration config)
{
gameMode = 0;
foreach (var gmStr in config.GetValue<List<GameMode>>("mbc_game_mode"))
{
gameMode |= gmStr;
if (gmStr == GameMode.Salvo || gmStr == GameMode.Powered || gmStr == GameMode.Teams)
{
throw new NotImplementedException("The "+gmStr.ToString()+" game mode is not supported.");
}
}
}
}
}