forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gamelift): add MatchmakingRuleSet L2 Construct for GameLift (aws…
…#23091) Following aws/aws-cdk-rfcs#436 I have written the Gamelift MatchmakingRuleSet L2 resource which create an MatchmakingRuleSet resource. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
0bc7d83
commit 74873db
Showing
16 changed files
with
1,109 additions
and
2 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
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
119 changes: 119 additions & 0 deletions
119
packages/@aws-cdk/aws-gamelift/lib/matchmaking-ruleset-body.ts
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import * as fs from 'fs'; | ||
import { Construct } from 'constructs'; | ||
|
||
/** | ||
* Interface to represent Matchmaking RuleSet schema | ||
*/ | ||
export interface IRuleSetBody {} | ||
|
||
/** | ||
* Interface to represent output result of a RuleSetContent binding | ||
*/ | ||
export interface RuleSetBodyConfig { | ||
/** | ||
* Inline ruleSet body. | ||
*/ | ||
readonly ruleSetBody: string; | ||
} | ||
|
||
/** | ||
* Interface to represent a Matchmaking RuleSet content | ||
*/ | ||
export interface IRuleSetContent { | ||
|
||
/** | ||
* RuleSet body content | ||
* | ||
* @attribute | ||
*/ | ||
readonly content: IRuleSetBody; | ||
|
||
/** | ||
* Called when the matchmaking ruleSet is initialized to allow this object to bind | ||
* to the stack and add resources. | ||
* | ||
* @param _scope The binding scope. | ||
*/ | ||
bind(_scope: Construct): RuleSetBodyConfig; | ||
} | ||
|
||
/** | ||
* Properties for a new matchmaking ruleSet content | ||
*/ | ||
export interface RuleSetContentProps { | ||
|
||
/** | ||
* RuleSet body content | ||
* | ||
* @default use a default empty RuleSet body | ||
*/ | ||
readonly content?: IRuleSetBody; | ||
} | ||
|
||
/** | ||
* The rule set determines the two key elements of a match: your game's team structure and size, and how to group players together for the best possible match. | ||
* | ||
* For example, a rule set might describe a match like this: | ||
* - Create a match with two teams of five players each, one team is the defenders and the other team the invaders. | ||
* - A team can have novice and experienced players, but the average skill of the two teams must be within 10 points of each other. | ||
* - If no match is made after 30 seconds, gradually relax the skill requirements. | ||
*/ | ||
export class RuleSetContent implements IRuleSetContent { | ||
|
||
/** | ||
* Matchmaking ruleSet body from a file | ||
* @returns `RuleSetContentBase` based on JSON file content. | ||
* @param path The path to the ruleSet body file | ||
*/ | ||
public static fromJsonFile(path: string): IRuleSetContent { | ||
if (!fs.existsSync(path)) { | ||
throw new Error(`RuleSet path does not exist, please verify it, actual ${path}`); | ||
} | ||
|
||
if (!fs.lstatSync(path).isFile()) { | ||
throw new Error(`RuleSet path is not link to a single file, please verify your path, actual ${path}`); | ||
} | ||
const file = fs.readFileSync(path); | ||
|
||
return this.fromInline(file.toString()); | ||
} | ||
|
||
/** | ||
* Inline body for Matchmaking ruleSet | ||
* @returns `RuleSetContent` with inline code. | ||
* @param body The actual ruleSet body (maximum 65535 characters) | ||
*/ | ||
public static fromInline(body: string): IRuleSetContent { | ||
if (body && body.length > 65535) { | ||
throw new Error(`RuleSet body cannot exceed 65535 characters, actual ${body.length}`); | ||
} | ||
try { | ||
return new RuleSetContent({ | ||
content: JSON.parse(body), | ||
}); | ||
} catch (err) { | ||
throw new Error('RuleSet body has an invalid Json format'); | ||
} | ||
} | ||
|
||
/** | ||
* RuleSet body content | ||
*/ | ||
public readonly content: IRuleSetBody; | ||
|
||
constructor(props: RuleSetContentProps) { | ||
this.content = props.content || {}; | ||
} | ||
|
||
/** | ||
* Called when the matchmaking ruleSet is initialized to allow this object to bind | ||
* to the stack and add resources. | ||
* | ||
* @param _scope The binding scope. | ||
*/ | ||
public bind(_scope: Construct): RuleSetBodyConfig { | ||
return { | ||
ruleSetBody: JSON.stringify(this.content), | ||
}; | ||
} | ||
} |
Oops, something went wrong.