Skip to content

Commit

Permalink
Began advertismeents
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruhlmann committed May 31, 2024
1 parent 77090c2 commit 585d4e4
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 45 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ ifneq (,$(wildcard ./.env))
export
endif

x:
set

include make/ocaml/main.mk
10 changes: 10 additions & 0 deletions lib/api/community/community_event.mli
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
(** Community event endpoints
Represents all endpoints in [/community/CommunityEvent/*]
- [GET /community/CommunityEvent/getAvailableCommunityEvents]
(explain the concept of a community event here) *)

(** [get g d s] retrieves a list of currently available community events in the
game [g] on domain [d]. The request is sent with the method [s]. *)

val get : Data.Game.t -> string -> Data.Requester.Json.t -> Models.Response.Community.Community_event.t option Lwt.t
13 changes: 13 additions & 0 deletions lib/api/community/news.mli
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
(** Community news endpoints
Represents all endpoints in [/community/news/*]
- [GET /community/news/getNews]
News are collections of text with an associated (optional) image.
News items are valid only until their expirytime is exceeded.
Although some news items have expired they may still be present
in the response.*)

(** [get g d s] retrieves a list of currently available news in the
game [g] on domain [d]. The request is sent with the method [s]. *)

val get : Data.Game.t -> string -> Data.Requester.Json.t -> Models.Response.Community.News.t option Lwt.t
29 changes: 29 additions & 0 deletions lib/api/game/advertisement.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
open Lwt.Syntax
open Data.Sort

let find_observable ?(start = 1) ?(count = 100) ?(sort = Descending) game domain send =
let should_descend = match sort with Ascending -> 0 | Descending -> 1 in
let base_url = Uri.make ~scheme:"https" ~host:domain ~path:"/game/advertisement/findObservableAdvertisements" () in
let url =
Uri.with_query'
base_url
[ "title", Data.Game.to_str game
; "start", string_of_int start
; "count", string_of_int count
; "desc", string_of_int should_descend
; "sortOrder", string_of_int should_descend
; "dataChecksum", "0"
; "modDLLFile", "INVALID"
; "modName", "INVALID"
; "modVersion", "INVALID"
; "modDLLChecksum", "0"
; "dataChecksum", "-888"
; "appBinaryChecksum", "113358"
; "versionFlags", "56950784"
]
in
let* json = send url in
match json with
| Some j -> Lwt.return @@ Some (Models.Response.Game.Observable_advertisements.from_json j)
| None -> Lwt.return None
;;
27 changes: 23 additions & 4 deletions lib/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,35 @@ let get_json ?(cookie = None) (url : Uri.t) =
in
let* resp, body = Cohttp_lwt_unix.Client.get ~headers url_with_params in
let status = Cohttp.Response.status resp in
let* body_str = Cohttp_lwt.Body.to_string body in
let curl_command =
Printf.sprintf
"curl -i -H 'Cookie: %s' '%s'"
(match cookie with
| Some c ->
Printf.sprintf
"ApplicationGatewayAffinity=%s; ApplicationGatewayAffinityCORS=%s; reliclink=%s"
c.application_gateway_affinity
c.application_gateway_affinity_cors
c.reliclink
| None -> "")
(Uri.to_string url_with_params)
in
let* _ = Lwt_io.printl curl_command in
if Cohttp.Code.code_of_status status = 200
then
let* body_str = Cohttp_lwt.Body.to_string body in
then (
let json = Yojson.Basic.from_string body_str in
Lwt.return (Some json)
Lwt.return (Some json))
else (
(* TODO: Find out what to do with this later. What's the return type? Do we have Result<a,b>? *)
let url_str = Uri.to_string url_with_params in
let* _ =
Lwt_io.printl @@ Printf.sprintf "HTTP Error: %s for URL: %s" (Cohttp.Code.string_of_status status) url_str
Lwt_io.printl
@@ Printf.sprintf
"HTTP Error: %s for URL: %s\nResponse: %s"
(Cohttp.Code.string_of_status status)
url_str
body_str
in
Lwt.return None)
;;
Expand Down
3 changes: 2 additions & 1 deletion lib/data/platform/cookie.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ let get_cookie_value cookies name =
|> List.tl
|> String.concat "="
with
| Not_found -> failwith @@ Printf.sprintf "Cookie '%s' not found in '%s'" name (String.concat ";" cookies)
| Not_found ->
failwith @@ Printf.sprintf "Invalid credentials. Cookie '%s' not found in '%s'" name (String.concat ";" cookies)
;;

let extract_session_id body_str =
Expand Down
3 changes: 3 additions & 0 deletions lib/data/sort.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type t =
| Ascending
| Descending
2 changes: 1 addition & 1 deletion lib/models/response/game/news.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type t =
; tail : Yojson.Basic.t list
}

let to_json n = `List ([ `Int n.id; `List (List.map Stub.Game.News.to_json n.news) ] @ n.tail)
let to_json n = `List ([ `Int n.status; `List (List.map Stub.Game.News.to_json n.news) ] @ n.tail)

let from_json json =
match json with
Expand Down
6 changes: 4 additions & 2 deletions lib/models/response/game/observable_advertisements.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ type t =
; tail : Yojson.Basic.t list
}

let to_json a = `List ([ `Int a.id; `List (List.map Stub.Game.Observable_advertisement.to_json a.news) ] @ a.tail)
let to_json a =
`List ([ `Int a.status; `List (List.map Stub.Game.Observable_advertisement.to_json a.advertisements) ] @ a.tail)
;;

let from_json json =
match json with
| `List (`Int status :: `List items :: tail) ->
{ status; advertisements = List.map Stub.Game.Observable_advertisement.from_json items; tail }
| _ -> failwith "Unexpected JSON format for the news response"
| _ -> failwith "Unexpected JSON format for the observable advertisements response"
;;
8 changes: 4 additions & 4 deletions lib/models/stub/game/news.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type t =
{ id : int
{ status : int
; image_url : string option
; title : string
; subtitle : string option
Expand All @@ -14,7 +14,7 @@ type t =
let from_json json =
match json with
| `List
[ id
[ status
; image_url
; title
; subtitle
Expand All @@ -25,7 +25,7 @@ let from_json json =
; start_time
; end_time
] ->
{ id = Yojson.Basic.Util.to_int id
{ status = Yojson.Basic.Util.to_int status
; image_url = Yojson.Basic.Util.to_string_option image_url
; title = Yojson.Basic.Util.to_string title
; subtitle = Yojson.Basic.Util.to_string_option subtitle
Expand All @@ -41,7 +41,7 @@ let from_json json =

let to_json n =
`List
[ `Int n.id
[ `Int n.status
; (match n.image_url with Some url -> `String url | None -> `Null)
; `String n.title
; (match n.subtitle with Some sub -> `String sub | None -> `Null)
Expand Down
94 changes: 68 additions & 26 deletions lib/models/stub/game/observable_advertisement.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type t =
; int5 : int
; slotinfo : string
; int6 : int
; metadata : Yojson.Basic.t
; metadata : Yojson.Basic.t list
; int7 : int
; int8 : int
; int9 : int
Expand All @@ -28,42 +28,84 @@ type t =
let from_json json =
match json with
| `List
[ id
; image_url
[ match_id
; int1
; string1
; host_relic_link_id
; int2
; title
; subtitle
; description
; short_description
; long_description
; category
; start_time
; end_time
; int3
; map
; options
; int4
; int5
; slotinfo
; int6
; metadata
; int7
; int8
; int9
; int10
; int11
; int12
; time_started
; server
; unknown
] ->
{ id = Yojson.Basic.Util.to_int id
; image_url = Yojson.Basic.Util.to_string_option image_url
{ match_id = Yojson.Basic.Util.to_int match_id
; int1 = Yojson.Basic.Util.to_int int1
; string1 = Yojson.Basic.Util.to_string string1
; host_relic_link_id = Yojson.Basic.Util.to_int host_relic_link_id
; int2 = Yojson.Basic.Util.to_int int2
; title = Yojson.Basic.Util.to_string title
; subtitle = Yojson.Basic.Util.to_string_option subtitle
; description = Yojson.Basic.Util.to_string description
; short_description = Yojson.Basic.Util.to_string short_description
; long_description = Yojson.Basic.Util.to_string long_description
; category = Yojson.Basic.Util.to_string category
; start_time = Yojson.Basic.Util.to_int start_time
; end_time = Yojson.Basic.Util.to_int end_time
; int3 = Yojson.Basic.Util.to_int int3
; map = Yojson.Basic.Util.to_string map
; options = Yojson.Basic.Util.to_string options (* TODO: Parse this*)
; int4 = Yojson.Basic.Util.to_int int4
; int5 = Yojson.Basic.Util.to_int int5
; slotinfo = Yojson.Basic.Util.to_string slotinfo (* TODO: Parse this*)
; int6 = Yojson.Basic.Util.to_int int6
; metadata = Yojson.Basic.Util.to_list metadata
; int7 = Yojson.Basic.Util.to_int int7
; int8 = Yojson.Basic.Util.to_int int8
; int9 = Yojson.Basic.Util.to_int int9
; int10 = Yojson.Basic.Util.to_int int10
; int11 = Yojson.Basic.Util.to_int int11
; int12 = Yojson.Basic.Util.to_int int12
; time_started = Yojson.Basic.Util.to_int time_started
; server = Yojson.Basic.Util.to_string server
; unknown = Yojson.Basic.Util.to_int_option unknown
}
| _ -> failwith "Unexpected news item format"
| _ -> failwith "Unexpected observable advertisement format"
;;

let to_json n =
`List
[ `Int n.id
; (match n.image_url with Some url -> `String url | None -> `Null)
[ `Int n.match_id
; `Int n.int1
; `String n.string1
; `Int n.host_relic_link_id
; `Int n.int2
; `String n.title
; (match n.subtitle with Some sub -> `String sub | None -> `Null)
; `String n.description
; `String n.short_description
; `String n.long_description
; `String n.category
; `Int n.start_time
; `Int n.end_time
; `Int n.int3
; `String n.map
; `String n.options
; `Int n.int4
; `Int n.int5
; `String n.slotinfo
; `Int n.int6
; `List n.metadata
; `Int n.int7
; `Int n.int8
; `Int n.int9
; `Int n.int10
; `Int n.int11
; `Int n.int12
; `String n.server
; `Int n.time_started
; (match n.unknown with Some u -> `Int u | None -> `Null)
]
;;
11 changes: 9 additions & 2 deletions tests/integration/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ open Relic_sdk
open Alcotest_lwt
open Lwt.Syntax

let vgetenv k = try Sys.getenv k with Not_found -> failwith @@ Printf.sprintf "Environment variable %s not found" k

let setup_community () =
let open Test_state.Community in
let domain = "aoe-api.worldsedgelink.com" in
Expand All @@ -21,7 +23,7 @@ let setup_community () =
let setup_game () =
let open Test_state.Game in
let open Data.Platform.Steam_login in
let login = Some { alias = Sys.getenv "STEAM_USER_ALIAS"; app_ticket = Sys.getenv "STEAM_APP_TICKET" } in
let login = Some { alias = vgetenv "STEAM_USER_ALIAS"; app_ticket = vgetenv "STEAM_APP_TICKET" } in
let domain = "aoe-api.worldsedgelink.com" in
let game = Data.Game.Age2 in
let endpoint = Api.Community.Leaderboard.get_leaderboard_2 ~count:1 in
Expand Down Expand Up @@ -52,7 +54,12 @@ let () =
`Slow
(Test_case.Community.Achievements.test_get_user_achievements_no_user setup_data_community)
] )
; "Game", [ test_case "News" `Slow (Test_case.Game.News.test_get_news setup_data_game) ]
; ( "Game"
, [ test_case
"Advertisements"
`Slow
(Test_case.Game.Advertisements.test_get_observable_advertisements setup_data_game)
] )
]
in
Alcotest_lwt.run "Relic SDK" suite
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/test_case/game/advertisements.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
open Relic_sdk
open Test_state.Game
open Lwt.Syntax

let test_get_observable_advertisements test_state _ () =
let open Data.Sort in
let endpoint_asc = Api.Game.Advertisement.find_observable ~sort:Ascending ~count:2 in
let endpoint_dsc = Api.Game.Advertisement.find_observable ~sort:Descending ~count:2 in
let* lobbies_asc = Client.get endpoint_asc test_state.client in
let* lobbies_dsc = Client.get endpoint_dsc test_state.client in
match lobbies_asc, lobbies_dsc with
| Some a, Some d ->
let* _ = Lwt_io.printf "Yepge %d\n" (List.length a.advertisements) in
let id_asc = (List.hd a.advertisements).match_id in
let id_dsc = (List.hd d.advertisements).match_id in
Alcotest.(check int) "Different match IDs" id_asc id_dsc;
if id_asc = id_dsc
then Lwt.fail_with "Expected different advertisement IDs in ascending and descending order"
else Lwt.return_unit
| _ -> Lwt.fail_with "No observable advertisements response"
;;
2 changes: 1 addition & 1 deletion tests/integration/test_state/game.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ open Relic_sdk

type t =
{ client : Client.t
; test_member : Models.Stub.Avatar.t
; test_member : Models.Stub.Community.Avatar.t
}
5 changes: 4 additions & 1 deletion tests/unit/test_cases/api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ let test_find_clans () =
let requester = Mock.Json_file.create_requester "findClan.json" in
let* client = Client.create "aoe-api.worldsedgelink.com" Data.Game.Age2 in
let endpoint =
Api.Community.Clan.find ~name:"My name" ~join_policies:[ Models.Stub.Join_policy.Open ] ~tags:[ "My tag" ]
Api.Community.Clan.find
~name:"My name"
~join_policies:[ Models.Stub.Community.Join_policy.Open ]
~tags:[ "My tag" ]
in
let* response = Client.get endpoint client ~requester in
match response with
Expand Down

0 comments on commit 585d4e4

Please sign in to comment.