mirror of
https://github.com/FAUSheppy/skillbird-sourcemod
synced 2025-12-06 15:11:36 +01:00
refactoring and basic balance impl
This commit is contained in:
@@ -11,7 +11,7 @@ public Action:Event_RoundStart(Handle:event, const String:name[], bool:dontBroad
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Action:Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast){
|
public Action:Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast){
|
||||||
SubmittEventActiveClients();
|
SubmittEventActiveClients("round_winner");
|
||||||
new team_id = GetEventInt(event, "winner");
|
new team_id = GetEventInt(event, "winner");
|
||||||
SubmittEventWinnerTeam(team_id);
|
SubmittEventWinnerTeam(team_id);
|
||||||
SubmittEventRoundEnd();
|
SubmittEventRoundEnd();
|
||||||
@@ -19,11 +19,11 @@ public Action:Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadca
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Action:Event_PlayerDisconnect(Handle:event, const String:name[], bool:dontBroadcast){
|
public Action:Event_PlayerDisconnect(Handle:event, const String:name[], bool:dontBroadcast){
|
||||||
SubmittEventActiveClients();
|
SubmittEventActiveClients("disconnect");
|
||||||
return Plugin_Continue;
|
return Plugin_Continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Action:Event_PlayerChangedTeam(Handle:event, const String:name[], bool:dontBroadcast){
|
public Action:Event_PlayerChangedTeam(Handle:event, const String:name[], bool:dontBroadcast){
|
||||||
SubmittEventActiveClients();
|
SubmittEventActiveClients("change_team");
|
||||||
return Plugin_Continue;
|
return Plugin_Continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,68 @@
|
|||||||
#define VAR_SESSION_ID "session_id"
|
#define VAR_SESSION_ID "session_id"
|
||||||
|
|
||||||
public void HttpResponseCallback(bool success, const char[] error, System2HTTPRequest request, System2HTTPResponse response, HTTPRequestMethod method) {
|
public void HttpResponseCallback(bool success, const char[] error, System2HTTPRequest request, System2HTTPResponse response, HTTPRequestMethod method) {
|
||||||
|
if (success) {
|
||||||
|
char mimeType[128];
|
||||||
|
response.GetHeader("Content-Type", mimeType, sizeof(mimeType));
|
||||||
|
if(StrEqual(mimeType, "application/json", false)){
|
||||||
|
char[] content = new char[response.ContentLength + 1];
|
||||||
|
response.GetContent(content, response.ContentLength + 1);
|
||||||
|
JSON_Object obj = json_decode(content)
|
||||||
|
|
||||||
|
char action[256];
|
||||||
|
obj.GetString("action", action, sizeof(action));
|
||||||
|
|
||||||
|
if(StrEqual(action, "balance", false)){
|
||||||
|
JSON_Array teamA = view_as<JSON_Array>(obj.GetObject("teamA"));
|
||||||
|
JSON_Array teamB = view_as<JSON_Array>(obj.GetObject("teamB"));
|
||||||
|
|
||||||
|
/* strmap for teamA */
|
||||||
|
StringMap teamMapA = new StringMap();
|
||||||
|
for (new i = 0; i < teamA.Length; i++) {
|
||||||
|
new teamId = teamA.GetObject(i).GetInt("team");
|
||||||
|
char playerId[SNAME_LEN];
|
||||||
|
teamA.GetObject(i).GetString("id", playerId, sizeof(playerId));
|
||||||
|
teamMapA.setValue(playerId, teamId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*strmap for teamB */
|
||||||
|
StringMap teamMapB = new StringMap();
|
||||||
|
for (new i = 0; i < teamB.Length; i++){
|
||||||
|
new teamId = teamB.GetObject(i).GetInt("team");
|
||||||
|
new playerId = teamB.GetObject(i).GetString("id");
|
||||||
|
teamMapB.setValue(playerId, teamId);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(new i = 1; i <= MaxClients;i++){
|
||||||
|
new String:clientId[DEFAULT_LEN];
|
||||||
|
|
||||||
|
/* check client auth */
|
||||||
|
if(!GetClientAuthId(i, AuthId_SteamID64, clientId, DEFAULT_LEN, true)){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* change team when nessesary */
|
||||||
|
int mapTeamId;
|
||||||
|
if(teamMapAGetValue(clientId, &value)){
|
||||||
|
ChangeTeam(clientId, value);
|
||||||
|
}else if(teamMapAGetValue(clientId, &value)){
|
||||||
|
ChangeTeam(clientId, value);
|
||||||
|
}else{
|
||||||
|
ChangeTeam(clientId, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
obj.Cleanup();
|
||||||
|
delete obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SubmittViaHTTP(char[] jsonString){
|
public void SubmittViaHTTP(char[] jsonString, char[] type){
|
||||||
|
|
||||||
new String:url[SNAME_LEN];
|
new String:url[SNAME_LEN];
|
||||||
|
# TODO type switch case
|
||||||
Format(url, sizeof(url), "http://localhost:%d/single-event?session=%d", GetConVarInt(FindConVar(VAR_TARGET_PORT)), GetConVarInt(FindConVar(VAR_SESSION_ID)) );
|
Format(url, sizeof(url), "http://localhost:%d/single-event?session=%d", GetConVarInt(FindConVar(VAR_TARGET_PORT)), GetConVarInt(FindConVar(VAR_SESSION_ID)) );
|
||||||
|
|
||||||
System2HTTPRequest httpRequest = new System2HTTPRequest(HttpResponseCallback, url);
|
System2HTTPRequest httpRequest = new System2HTTPRequest(HttpResponseCallback, url);
|
||||||
@@ -26,7 +83,7 @@ public void SubmittViaHTTP(char[] jsonString){
|
|||||||
httpRequest.POST();
|
httpRequest.POST();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SubmittEventActiveClients(){
|
public void SubmittEventActiveClients(const char[] type){
|
||||||
|
|
||||||
/* primary json */
|
/* primary json */
|
||||||
JSON_Object obj = new JSON_Object();
|
JSON_Object obj = new JSON_Object();
|
||||||
@@ -46,8 +103,8 @@ public void SubmittEventActiveClients(){
|
|||||||
new String:clientId[DEFAULT_LEN];
|
new String:clientId[DEFAULT_LEN];
|
||||||
new String:clientName[SNAME_LEN];
|
new String:clientName[SNAME_LEN];
|
||||||
if(!GetClientAuthId(i, AuthId_SteamID64, clientId, DEFAULT_LEN, true)){
|
if(!GetClientAuthId(i, AuthId_SteamID64, clientId, DEFAULT_LEN, true)){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
GetClientName(i, clientName, SNAME_LEN);
|
GetClientName(i, clientName, SNAME_LEN);
|
||||||
|
|
||||||
player.SetString("id", clientId);
|
player.SetString("id", clientId);
|
||||||
@@ -58,10 +115,14 @@ public void SubmittEventActiveClients(){
|
|||||||
|
|
||||||
/* add players array to primary object */
|
/* add players array to primary object */
|
||||||
obj.SetObject("players", players);
|
obj.SetObject("players", players);
|
||||||
|
obj.SetString("type", type);
|
||||||
|
|
||||||
char output[2048];
|
char output[2048];
|
||||||
obj.Encode(output, sizeof(output));
|
obj.Encode(output, sizeof(output));
|
||||||
SubmittViaHTTP(output);
|
SubmittViaHTTP(output, type);
|
||||||
|
|
||||||
|
obj.Cleanup();
|
||||||
|
delete obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SubmittEventMapInformation(){
|
public void SubmittEventMapInformation(){
|
||||||
@@ -97,5 +158,6 @@ public void SubmittEventRoundEnd(){
|
|||||||
|
|
||||||
char output[2048];
|
char output[2048];
|
||||||
obj.Encode(output, sizeof(output));
|
obj.Encode(output, sizeof(output));
|
||||||
|
SubmittEventActiveClients("round_end")
|
||||||
SubmittViaHTTP(output);
|
SubmittViaHTTP(output);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user