Skip to content

Commit f5f4bce

Browse files
Merge pull request #35 from LootLocker/feature/add-player-file-storage-methods
Feature/add player file storage methods
2 parents 100804f + b72807b commit f5f4bce

File tree

6 files changed

+170
-13
lines changed

6 files changed

+170
-13
lines changed

Runtime/Client/LootLockerEndPoints.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public class LootLockerEndPoints
4444
public static EndPointClass getPlayerName = new EndPointClass("player/name", LootLockerHTTPMethod.GET);
4545
public static EndPointClass setPlayerName = new EndPointClass("player/name", LootLockerHTTPMethod.PATCH);
4646
public static EndPointClass lookupPlayerNames = new EndPointClass("player/lookup/name", LootLockerHTTPMethod.GET);
47+
public static EndPointClass getplayerFiles = new EndPointClass("player/files", LootLockerHTTPMethod.GET);
48+
public static EndPointClass getSingleplayerFile = new EndPointClass("player/files/{0}", LootLockerHTTPMethod.GET);
49+
public static EndPointClass uploadPlayerFile = new EndPointClass("player/files", LootLockerHTTPMethod.UPLOAD);
50+
public static EndPointClass deletePlayerFile = new EndPointClass("/player/files/{0}", LootLockerHTTPMethod.DELETE);
4751

4852
// Character
4953
[Header("Character")]

Runtime/Client/LootLockerServerRequest.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,9 @@ public class LootLockerResponse
6565
public string EventId;
6666

6767
public static void Serialize<T>(Action<T> onComplete, LootLockerResponse serverResponse)
68-
where T : LootLockerResponse
68+
where T : LootLockerResponse, new()
6969
{
70-
if (!string.IsNullOrEmpty(serverResponse.Error)) return;
71-
72-
var response = JsonConvert.DeserializeObject<T>(serverResponse.text);
73-
74-
response.text = serverResponse.text;
75-
response.success = serverResponse.success;
76-
response.Error = serverResponse.Error;
77-
response.statusCode = serverResponse.statusCode;
78-
onComplete?.Invoke(response);
70+
onComplete?.Invoke(Serialize<T>(serverResponse));
7971
}
8072

8173
public static T Serialize<T>(LootLockerResponse serverResponse)
@@ -90,7 +82,7 @@ public static T Serialize<T>(LootLockerResponse serverResponse)
9082
return new T() { success = false, Error = serverResponse.Error };
9183
}
9284

93-
var response = JsonConvert.DeserializeObject<T>(serverResponse.text);
85+
var response = JsonConvert.DeserializeObject<T>(serverResponse.text) ?? new T();
9486

9587
response.text = serverResponse.text;
9688
response.success = serverResponse.success;
@@ -183,6 +175,12 @@ public static void CallAPI(string endPoint, LootLockerHTTPMethod httpMethod, str
183175

184176
new LootLockerServerRequest(endPoint, httpMethod, body, headers, callerRole: callerRole).Send((response) => { onComplete?.Invoke(response); });
185177
}
178+
179+
public static void CallAPI(EndPointClass endpoint, string body = null, Action<LootLockerResponse> onComplete = null, bool useAuthToken = true,
180+
LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User)
181+
{
182+
CallAPI(endpoint.endPoint, endpoint.httpMethod, body, onComplete: (serverResponse) => { LootLockerResponse.Serialize(onComplete, serverResponse); }, useAuthToken, callerRole);
183+
}
186184

187185
public static void CallDomainAuthAPI(string endPoint, LootLockerHTTPMethod httpMethod, string body = null, Action<LootLockerResponse> onComplete = null)
188186
{
@@ -225,6 +223,12 @@ public static void UploadFile(string endPoint, LootLockerHTTPMethod httpMethod,
225223

226224
new LootLockerServerRequest(endPoint, httpMethod, file, fileName, fileContentType, body, headers, callerRole: callerRole).Send((response) => { onComplete?.Invoke(response); });
227225
}
226+
227+
public static void UploadFile(EndPointClass endPoint, byte[] file, string fileName = "file", string fileContentType = "text/plain", Dictionary<string, string> body = null, Action<LootLockerResponse> onComplete = null,
228+
bool useAuthToken = true, LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User)
229+
{
230+
UploadFile(endPoint.endPoint, endPoint.httpMethod, file, fileName, fileContentType, body, onComplete: (serverResponse) => { LootLockerResponse.Serialize(onComplete, serverResponse); }, useAuthToken, callerRole);
231+
}
228232

229233
#endregion
230234

Runtime/Game/LootLockerSDKManager.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.IO;
45
using UnityEngine;
56
using LootLocker.Requests;
67
using LootLocker;
@@ -789,6 +790,132 @@ public static void SetPlayerName(string name, Action<PlayerNameResponse> onCompl
789790

790791
LootLockerAPIManager.SetPlayerName(data, onComplete);
791792
}
793+
794+
public static void GetPlayerFile(int fileId, Action<LootLockerPlayerFile> onComplete)
795+
{
796+
if (!CheckInitialized())
797+
{
798+
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerPlayerFile>());
799+
return;
800+
}
801+
802+
var endpoint = LootLockerEndPoints.getSingleplayerFile;
803+
804+
endpoint.endPoint = string.Format(endpoint.endPoint, fileId);
805+
806+
LootLockerServerRequest.CallAPI(endpoint, null, onComplete: (serverResponse) => { LootLockerResponse.Serialize(onComplete, serverResponse); });
807+
}
808+
809+
public static void GetAllPlayerFiles(Action<LootLockerPlayerFilesResponse> onComplete)
810+
{
811+
if (!CheckInitialized())
812+
{
813+
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerPlayerFilesResponse>());
814+
return;
815+
}
816+
817+
LootLockerServerRequest.CallAPI(LootLockerEndPoints.getplayerFiles, null, onComplete: (serverResponse) => { LootLockerResponse.Serialize(onComplete, serverResponse); });
818+
}
819+
820+
public static void UploadPlayerFile(string pathToFile, string filePurpose, Action<LootLockerPlayerFile> onComplete)
821+
{
822+
if (!CheckInitialized())
823+
{
824+
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerPlayerFile>());
825+
return;
826+
}
827+
828+
var body = new Dictionary<string, string>()
829+
{
830+
{ "purpose", filePurpose }
831+
};
832+
833+
834+
var fileBytes = new byte[] { };
835+
try
836+
{
837+
fileBytes = File.ReadAllBytes(pathToFile);
838+
}
839+
catch (Exception e)
840+
{
841+
DebugMessage($"File error: {e.Message}");
842+
return;
843+
}
844+
845+
LootLockerServerRequest.UploadFile(LootLockerEndPoints.uploadPlayerFile, fileBytes, Path.GetFileName(pathToFile), "multipart/form-data", body,
846+
onComplete: (serverResponse) =>
847+
{
848+
LootLockerResponse.Serialize(onComplete, serverResponse);
849+
});
850+
}
851+
852+
public static void UploadPlayerFile(FileStream fileStream, string filePurpose, Action<LootLockerPlayerFile> onComplete)
853+
{
854+
if (!CheckInitialized())
855+
{
856+
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerPlayerFile>());
857+
return;
858+
}
859+
860+
var body = new Dictionary<string, string>()
861+
{
862+
{ "purpose", filePurpose }
863+
};
864+
865+
var fileBytes = new byte[fileStream.Length];
866+
try
867+
{
868+
fileStream.Read(fileBytes, 0, Convert.ToInt32(fileStream.Length));
869+
}
870+
catch (Exception e)
871+
{
872+
DebugMessage($"File error: {e.Message}");
873+
return;
874+
}
875+
876+
LootLockerServerRequest.UploadFile(LootLockerEndPoints.uploadPlayerFile, fileBytes, Path.GetFileName(fileStream.Name), "multipart/form-data", body,
877+
onComplete: (serverResponse) =>
878+
{
879+
LootLockerResponse.Serialize(onComplete, serverResponse);
880+
});
881+
}
882+
883+
884+
public static void UploadPlayerFile(byte[] fileBytes, string fileName, string filePurpose, Action<LootLockerPlayerFile> onComplete)
885+
{
886+
if (!CheckInitialized())
887+
{
888+
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerPlayerFile>());
889+
return;
890+
}
891+
892+
var body = new Dictionary<string, string>()
893+
{
894+
{ "purpose", filePurpose }
895+
};
896+
897+
LootLockerServerRequest.UploadFile(LootLockerEndPoints.uploadPlayerFile, fileBytes, Path.GetFileName(fileName), "multipart/form-data", body,
898+
onComplete: (serverResponse) =>
899+
{
900+
LootLockerResponse.Serialize(onComplete, serverResponse);
901+
});
902+
}
903+
904+
public static void DeletePlayerFile(int fileId, Action<LootLockerResponse> onComplete)
905+
{
906+
if (!CheckInitialized())
907+
{
908+
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerResponse>());
909+
return;
910+
}
911+
912+
var endpoint = LootLockerEndPoints.deletePlayerFile;
913+
914+
endpoint.endPoint = string.Format(endpoint.endPoint, fileId);
915+
916+
LootLockerServerRequest.CallAPI(endpoint, null, onComplete: (serverResponse) => { LootLockerResponse.Serialize(onComplete, serverResponse); });
917+
}
918+
792919
#endregion
793920

794921
#region Character

Runtime/Game/Requests/LootLockerVerifyRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public LootLockerVerifyRequest(string token)
2424

2525
public class LootLockerVerifySteamRequest : LootLockerVerifyRequest
2626
{
27-
public string platform => "Steam";
27+
public new string platform => "Steam";
2828

2929
public LootLockerVerifySteamRequest(string token) : base(token)
3030
{

Runtime/Game/Requests/PlayerRequest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ public class LootLockerInventory
137137

138138
public float balance;
139139
}
140+
141+
public class LootLockerPlayerFileRequest
142+
{
143+
public string purpose { get; set; }
144+
public string path_to_file { get; set; }
145+
}
146+
147+
public class LootLockerPlayerFilesResponse : LootLockerResponse
148+
{
149+
public LootLockerPlayerFile[] items { get; set; }
150+
}
151+
152+
public class LootLockerPlayerFile : LootLockerResponse
153+
{
154+
public int id { get; set; }
155+
public string name { get; set; }
156+
public int size { get; set; }
157+
public string purpose { get; set; }
158+
public string url { get; set; }
159+
public DateTime url_expires_at { get; set; }
160+
public DateTime created_at { get; set; }
161+
}
140162

141163
[System.Serializable]
142164
public class LootLockerAssetClass

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.lootlocker.lootlockersdk",
3-
"version": "1.1.24",
3+
"version": "1.1.25",
44
"displayName": "LootLocker",
55
"description": "LootLocker SDK",
66
"unity": "2019.2",

0 commit comments

Comments
 (0)