-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
62 lines (54 loc) · 1.72 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace ReversiSharpClient
{
internal class Program
{
private Timer _timer;
private AutoResetEvent _autoResetEvent;
private readonly Game _game;
private readonly GameClient _client;
private Program()
{
_game = Game.GetInstance();
_client = new GameClient(_game.BaseUrl);
}
/// <summary>
/// Creates a thread that calls AttemptToPlay method once in a second
/// </summary>
private void Connect()
{
_autoResetEvent = new AutoResetEvent(false);
var timerDelegate = new TimerCallback(AttemptToPlay);
_timer = new Timer(timerDelegate, null, 1000, 1000);
_autoResetEvent.WaitOne();
}
/// <summary>
/// Communicates with the server
/// </summary>
/// <param name="obj"></param>
private void AttemptToPlay(Object obj)
{
_game.Status = _client.GetStatus(); //HTTP GETs the status
if (_game.Status.Cancelled)
{
_timer.Dispose(_autoResetEvent); //Exits the thread
}
else if (!_game.Status.Started)
{
_timer.Dispose(_autoResetEvent); //Exits the thread
}
else if (_game.Status.CurrentPlayer == _game.Player) //If it is our turn
{
_client.Move(_game.AuthCode, _game.GetNextMove()); //HTTP POST our move
}
}
private static void Main(string[] args)
{
var program = new Program();
program.Connect();
}
}
}