-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRestApi.cs
121 lines (105 loc) · 4.25 KB
/
RestApi.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
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
using System;
using System.Collections.Generic;
using System.Net;
using RestSharp;
namespace Battleships
{
internal class RestApi : Battleships.IGameApi
{
private const string ServiceAddress = "https://10.12.216.168";
private const string AuthHeader = "X-Auth-ShipsApiKey";
// Roberto
private const string UserToken = "3907c0dfa1ce4c59be420815085d9c4b";
// Stefan
//private const string UserToken = "35b2573c0dc74dc49864ce8ba18ed91c";
private const string ContentTypeHeader = "Content-Type";
private const string ContentTypeHeaderValue = "application/json; charset=utf-8";
private readonly RestClient _client;
public RestApi()
{
// ignore HTTPS certificate errors
ServicePointManager.ServerCertificateValidationCallback =
(sender, cert, chain, policy) => true;
_client = new RestClient(ServiceAddress);
}
private IRestResponse<Dictionary<string, string>> SendRequest(string resource, Method method)
{
return SendRequest(resource, method, null);
}
private IRestResponse<Dictionary<string, string>> SendRequest(string resource, Method method, object body)
{
var request = new RestRequest(resource, method);
request.AddHeader(AuthHeader, UserToken);
request.AddHeader(ContentTypeHeader, ContentTypeHeaderValue);
if (body != null)
{
request.RequestFormat = DataFormat.Json;
request.AddBody(body);
}
return _client.Execute<Dictionary<string, string>>(request);
}
public string CreateNewGame()
{
var response = SendRequest("/Game", Method.POST);
return response.Data["Id"];
}
private const string ColumnNames = "ABCDEFGHIJ";
private static dynamic CreateShot(int row, int column)
{
var c = ColumnNames.Substring(column, 1);
var r = row + 1;
return new { Location = c + r };
}
static ShotResult GetShotResult(string resultString)
{
switch (resultString)
{
case "1":
return ShotResult.Miss;
case "2":
return ShotResult.Hit;
case "3":
return ShotResult.HitAndSunk;
default:
throw new Exception("unknown shot result string: " + resultString);
}
}
public GameState Shoot(string gameId, int row, int column)
{
var response = SendRequest("/Attack/" + gameId, Method.POST, CreateShot(row, column));
var shotResult = response.Data["Result"];
var isFinished = response.Data["IsGameFinished"];
return new GameState
{
IsFinished = isFinished == "True",
LastShot = GetShotResult(shotResult)
};
}
public int GetCurrentScore()
{
var response = SendRequest("/Score", Method.GET);
return int.Parse(response.Data["Score"]);
}
public int GetBestScore()
{
return GetCurrentScore() / 10;
}
public bool SetNick(string nickName)
{
var response = SendRequest("/Nick/" + nickName, Method.PUT);
return response.StatusCode == HttpStatusCode.OK;
}
public object GetPlayers()
{
// TODO
//[{"Id":1,"{"Nick":"MegaZordon","Score":10000,"Picture":"http://res.cloudinary.com/demo/image/gravatar/d_retro/unknown_id.jpg"},
throw new NotImplementedException();
}
public object GetGames()
{
// /Games/{PlayerID}
//[{"Title":"123 - 12:34","Board":["A1","B2","C3","C4","C5"],"Moves":["A1","B1","C1","D1","E1","F1","G1","I1","J1","A2"],”Score”:23},{"Title":"23 - 11:12","Board":["A1","B2","C3","C4","C5"],"Moves":["A1","B1","C1","D1","E1","F1","G1","I1","J1","A2"],”Score”:31}]
throw new NotImplementedException();
}
}
}