-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db2649c
commit ae191e1
Showing
30 changed files
with
669 additions
and
444 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ public enum GameState | |
Draw = 5 | ||
} | ||
|
||
|
||
|
||
|
||
public enum CellContent | ||
{ | ||
Empty = 0, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,103 @@ | ||
using System; | ||
using System.Net.Http; | ||
|
||
namespace Connect4.ExampleBot | ||
|
||
internal static class API | ||
{ | ||
internal static class API | ||
/// <summary> | ||
/// Get the current state of the game | ||
/// </summary> | ||
/// <param name="playerID"></param> | ||
/// <param name="serverURL"></param> | ||
/// <returns></returns> | ||
internal static Game GetGame(Guid playerID, string serverURL) | ||
{ | ||
/// <summary> | ||
/// Get the current state of the game | ||
/// </summary> | ||
/// <param name="playerID"></param> | ||
/// <param name="serverURL"></param> | ||
/// <returns></returns> | ||
internal static Game GetGame(Guid playerID, string serverURL) | ||
{ | ||
var httpClient = new HttpClient(); | ||
httpClient.BaseAddress = new Uri(serverURL); | ||
var httpClient = new HttpClient(); | ||
httpClient.BaseAddress = new Uri(serverURL); | ||
|
||
var httpResponseMessage = httpClient.GetAsync($"api/GameState?playerID={playerID}").Result; | ||
if (!httpResponseMessage.IsSuccessStatusCode) | ||
{ | ||
// Something has gone wrong | ||
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result; | ||
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "GameState", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors)); | ||
} | ||
else | ||
{ | ||
// All good | ||
var result = httpResponseMessage.Content.ReadAsAsync<Game>().Result; | ||
return result; | ||
} | ||
var httpResponseMessage = httpClient.GetAsync($"api/GameState?playerID={playerID}").Result; | ||
if (!httpResponseMessage.IsSuccessStatusCode) | ||
{ | ||
// Something has gone wrong | ||
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result; | ||
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "GameState", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors)); | ||
} | ||
|
||
/// <summary> | ||
/// Register your team to get your unique player ID | ||
/// </summary> | ||
/// <param name="TeamName"></param> | ||
/// <param name="teamPassword"></param> | ||
/// <param name="serverURL"></param> | ||
/// <returns></returns> | ||
internal static Guid RegisterTeam(string TeamName, string teamPassword, string serverURL) | ||
else | ||
{ | ||
var httpClient = new HttpClient(); | ||
httpClient.BaseAddress = new Uri(serverURL); | ||
// All good | ||
var result = httpResponseMessage.Content.ReadAsAsync<Game>().Result; | ||
return result; | ||
} | ||
} | ||
|
||
var httpResponseMessage = httpClient.PostAsync($"api/Register?teamName={TeamName}&password={teamPassword}", null).Result; | ||
if (!httpResponseMessage.IsSuccessStatusCode) | ||
{ | ||
// Something has gone wrong | ||
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result; | ||
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "RegisterTeam", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors)); | ||
} | ||
else | ||
{ | ||
// All good | ||
var result = httpResponseMessage.Content.ReadAsAsync<string>().Result; | ||
return new Guid(result); | ||
} | ||
/// <summary> | ||
/// Register your team to get your unique player ID | ||
/// </summary> | ||
/// <param name="TeamName"></param> | ||
/// <param name="teamPassword"></param> | ||
/// <param name="serverURL"></param> | ||
/// <returns></returns> | ||
internal static Guid RegisterTeam(string TeamName, string teamPassword, string serverURL) | ||
{ | ||
var httpClient = new HttpClient(); | ||
httpClient.BaseAddress = new Uri(serverURL); | ||
|
||
var httpResponseMessage = httpClient.PostAsync($"api/Register?teamName={TeamName}&password={teamPassword}", null).Result; | ||
if (!httpResponseMessage.IsSuccessStatusCode) | ||
{ | ||
// Something has gone wrong | ||
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result; | ||
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "RegisterTeam", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors)); | ||
} | ||
|
||
/// <summary> | ||
/// Plays a move. ColumnNumber should be between 0 and 6 | ||
/// </summary> | ||
/// <param name="playerID"></param> | ||
/// <param name="serverURL"></param> | ||
/// <param name="columnNumber"></param> | ||
internal static void MakeMove(Guid playerID, string serverURL, int columnNumber, string teamPassword) | ||
else | ||
{ | ||
var httpClient = new HttpClient(); | ||
httpClient.BaseAddress = new Uri(serverURL); | ||
|
||
var httpResponseMessage = httpClient.PostAsync($"api/MakeMove?playerID={playerID}&columnNumber={columnNumber}&password={teamPassword}", null).Result; | ||
if (!httpResponseMessage.IsSuccessStatusCode) | ||
{ | ||
// Something has gone wrong | ||
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result; | ||
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "MakeMove", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors)); | ||
} | ||
// All good | ||
var result = httpResponseMessage.Content.ReadAsAsync<string>().Result; | ||
return new Guid(result); | ||
} | ||
|
||
/// <summary> | ||
/// Starts a new game against the same player as the previous game. Your colours will | ||
/// however be swapped (red => yellow and yellow => red) | ||
/// </summary> | ||
/// <param name="playerID"></param> | ||
/// <param name="serverURL"></param> | ||
internal static void NewGame(Guid playerID, string serverURL) | ||
} | ||
|
||
/// <summary> | ||
/// Plays a move. ColumnNumber should be between 0 and 6 | ||
/// </summary> | ||
/// <param name="playerID"></param> | ||
/// <param name="serverURL"></param> | ||
/// <param name="columnNumber"></param> | ||
internal static void MakeMove(Guid playerID, string serverURL, int columnNumber, string teamPassword) | ||
{ | ||
var httpClient = new HttpClient(); | ||
httpClient.BaseAddress = new Uri(serverURL); | ||
|
||
var httpResponseMessage = httpClient.PostAsync($"api/MakeMove?playerID={playerID}&columnNumber={columnNumber}&password={teamPassword}", null).Result; | ||
if (!httpResponseMessage.IsSuccessStatusCode) | ||
{ | ||
var httpClient = new HttpClient(); | ||
httpClient.BaseAddress = new Uri(serverURL); | ||
// Something has gone wrong | ||
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result; | ||
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "MakeMove", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors)); | ||
} | ||
} | ||
|
||
var httpResponseMessage = httpClient.PostAsync($"api/NewGame?playerID={playerID}", null).Result; | ||
if (!httpResponseMessage.IsSuccessStatusCode) | ||
{ | ||
// Something has gone wrong | ||
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result; | ||
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "NewGame", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors)); | ||
} | ||
/// <summary> | ||
/// Starts a new game against the same player as the previous game. Your colours will | ||
/// however be swapped (red => yellow and yellow => red) | ||
/// </summary> | ||
/// <param name="playerID"></param> | ||
/// <param name="serverURL"></param> | ||
internal static void NewGame(Guid playerID, string serverURL) | ||
{ | ||
var httpClient = new HttpClient(); | ||
httpClient.BaseAddress = new Uri(serverURL); | ||
|
||
var httpResponseMessage = httpClient.PostAsync($"api/NewGame?playerID={playerID}", null).Result; | ||
if (!httpResponseMessage.IsSuccessStatusCode) | ||
{ | ||
// Something has gone wrong | ||
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result; | ||
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "NewGame", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors)); | ||
} | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.