TurnSystem API Documentation

Authors: Leonardo Mota-Villaraldo & Cheuk-Hang Tse
Professor: Kevin Sung
Course: University of Washington Bothell CSS 452 Winter 2021

Module Description

The TurnSystem API provides a local turn system. Users can create a turn system that can adapt to different turn types (Timed, Conditional, Priority).

There are 3 main classes in the API: Setting.Builder, Setting, and TurnSystem. There is also 1 supportive class in the API: UserState. Setting.Builder class allows the developer to set the turn type, max number of player allowed, and required parameters based on the turn type. Setting class must be built using the Setting.Builder class. It provide functions that allows the developer to access the parameters set by the Setting.Builder object. At last, the TurnSystem Object must be initialize by the Setting object. It provides function for switching turns, getting user's information within the turn system, and adding/removing users.

The API only supports 2 or more user in the turn system.

Demo

Link to the Demo: Demo link

Tutorial

How to Get Started

To get started on using the turn system, you must download the folder, locate the folder inside src folder, and include the API javascript files in the index.html in the project.

Add the following lines above your scene files in the index.html:

<script type="text/javascript" src="src/TurnSystemAPI/Settings.js"></script>
<script type="text/javascript" src="src/TurnSystemAPI/TurnSystem.js"></script>
<script type="text/javascript" src="src/TurnSystemAPI/UserState.js"></script>
<script type="text/javascript" src="src/TurnSystemAPI/Action.js"></script>


Initialize the TurnSystem in Scene

To initialize the turn system in the scene, you must have following parameters:
 - turntype ('timed', 'conditional', 'priority')
 - max number of player in the game.

Do the following 4 steps to initialize the turn system:

1. Construct the Setting.Builder class:

const settingsBuilder = new Settings.Builder();
settingsBuilder.setTurnType(this.mTurnType);
settingsBuilder.setMaxUsers(this.mMaxPlayers);

2. Set parameters based on turn type (Description in later section)

3. Construct the Setting object using the Setting.Builder object:

const settings = settingsBuilder.build();

4. Construct the TurnSystem object using the Setting object:

this.turnSystem = new TurnSystem(settings);


How to Set Timed Turn-Type

To set the parameters for the timed turn-type, the developer must set the turn-type to 'timed' and provide a time limit when constructing the Settings object. This can be achieved by building the Settings like so:
const settingsBuilder = new Settings.Builder();
settingsBuilder.setTurnType('timed');
settingsBuilder.setTurnTime(60);
When building your scene, ensure to call TurnSystem's calculateNextTurn() function within the update function of your scene.
You are not required to set a callback function for this turn type. If you do set a callback function, the turn system ignores it.

How to Set Conditional Turn-Type

To set the parameters for conditional turn-type, the developer must provide a conditional function to pass into the Setting.Builder object.
This function must return true to move onto the next player and return false to stay as current player's turn.

Set the conditional function by passing the function into Setting.Builder object:

const settingsBuilder = new Settings.Builder();
settingsBuilder.setTurnType('conditional');
settingsBuilder.setCallbackFunction(conditionalFunction);

Tips: If the user object is extended from UserState, turn isMetCondition to true when condition is made. For condition function, pass a function that return isMetCondition. TurnSystem will reset isMetCondition when condition is true.


How to Set Priority Turn-Type

The priority turn-type, like the conditional turn-type, require a callback function when creating your Settings object. The callback function you provide must accept a single parameter, an array of UserStates. The callback function should sort the array based on the conditions/priorities the developer decides on. To use the priority turn-type follow the code below:
const settingsBuilder = new Settings.Builder();
settingsBuilder.setTurnType('priority');
settings.builder.setCallbackFunction((usersArr) => userArr.sort());
Where to call TurnSystem's calculateNextTurn() is flexible here. You could call it within your Scene's update function, and have the array repeatedly be sorted. However, you can also limit the calls to calculateNextTurn() by only calling it when necessary. For example, if you have users that roll dice, you can call calculateNextTurn() after all users have rolled.

How to Add and Remove Players

To add players, the developer can self-define a user object that extends from the UserState class and add the user object to the turn system.

const user = new UserState();
this.turnSystem.addUser(user);

To remove a specific player, the developer can get the user object by index inside the TurnSystem storage. The developer should use some self-define parameters to locate specific user and then remove the user from the system by:

const lastPlayer = this.turnSystem.getUserByIndex(this.numPlayers - 1);
this.turnSystem.removeUser(lastPlayer);


How to Switch Turns

To check if it is time to switch turns based on the turn type. Call the function to calculate if it is time to switch turn:

this.turnSystem.calculateNextTurn();


Documentation

Settings.js

Settings.Builder

Function Names Description Parameter(s) (if any) Return Type (if any)
Settings.Builder() Constructor of the Settings.Builder class None None
setTurnType(type) Set the turn-type to 'timed'/'conditional'/'priority' type : string ('timed'/'conditional'/'priority') None
setMaxUsers(max) Set the max number of player. Minimum of 2 players max : Number None
setCallbackFunction(callback) Set the callback function for 'conditional' or 'priority' turn-type callback : function None
setTurnTime(time) Set the timer for 'timed' turn-type. Value should be in seconds. time : Number None
build() Build and return the Settings object None Setting

Settings

Function Names Description Parameter(s) (if any) Return Type (if any)
Settings(builder) Constructor of the Settings class builder : Setting.Builder None
getTurnType() Get the turn-type None String
getMaxUsers() Get the max number of player None Number
getCallbackFunction() Get the callback function for 'conditional' or 'priority' turn-type None Function
getTurnTime() Get the timer for 'timed' turn-type None Number

TurnSystem.js

TurnSystem

Function Names Description Parameter(s) (if any) Return Type (if any)
TurnSystem(settings) Constructor of the TurnSystem class settings : Settings None
calculateNextTurn() calculate if conditions are reached, if true, switch to next turn None None
maxOutTimeTicker() Maxes out the time ticker to one update before the max None None
remainingTime() Returns the remaining amount of time left before the timer reaches 0 None Number
getCurrentUser() Returns the current user object None the current UserState instance
getNextUser() Returns the user that immediately succeeds the current user None a UserState instance
getUserByIndex(index) Return the user at specific index index : Number the UserState instance at the provided index, if any.
getAllUsers() Return the list of all users None List of all UserState instances
getNumUsers() Return the number of users in the system None Number
addUser(user) Returns true if the user passed in is successfully added. Otherwise, returns false. user : UserState Boolean
removeUser(user) Returns true if the user passed in is successfully removed. Otherwise, returns false. user : UserState Boolean

UserState.js

UserState

This is not a class, and should instead be treated as an interface.
Function Names Description Parameter(s) (if any) Return Type (if any)
UserState() Constructor of the UserState class None. None.
initialize() Initializes the UserState Object None. None.
update() Updates the state of the UserState object. None. None.
action() If the user can do an action of some sort, execute it. Should return an Action class object. None. None.
equals(otherUserState) Checks if two UserState objects are the same. otherUserState: UserState Boolean: true if the UserStates are equal.

Action.js

Action

This is not a class, and should instead be treated as an interface.
Function Names Description Parameter(s) (if any) Return Type (if any)
Action(user, action) Constructor of the Action class Sets the properties for the Action class. User should be a UserState instance. Action should be the result of the UserState action() function, whatever it may be. None.