# Code standard

## Server (BrainCloud: JavaScript)

##### Scripts Naming (Voir BrainCloud:Scripts)

---

- On organise le nom des scripts de la manière suivantes : {folderName}\_{subFolderName}\_{ScriptName}
- Dans le cas des scripts appelable par le client on ajoute "Cmd" comme prefix du "ScriptName"
- Exemples: 
    - acc1\_CmdCreateTeam (pour les commandes de create team)
    - ent1\_Utils (pour les scripts utilaire spécifique à un jeu)
    - acc1\_Bills
    - CmdSaveSegment (Pour une commande générique)

##   
Server (GS: JavaScript)

#### **Collection**

- **shortenedCollectionName** = **collectionName** (**collectionKey**) 
    - eval = evaluation (studentUid)
    - evalTeam = evaluationTeam (teamUid)
    - result = results (simUid)
    - sim = simulation (simUid)
    - team = teams (teamU
    - user = userdetails (userUid)

#### How to name **Events** (generic code)

- {**Action**}{**Collection**}{**WhatItDoes**} PascalCase 
    - GetSimGroup
    - CreateTeam
    - CreateSimCustomEval
    - GetEvalScenario
    - JoinSim
    - JoinTeam
    - ReopenSimYear
    - EditUserStudent

#### How to get a **Collection**

- `var {<strong>shortenedCollectionName</strong>}Entry = API.getItem("{<strong>collectionName</strong>}", {<strong>collectionKey</strong>}).document();`  
    `var {<strong>shortenedCollectionName</strong>}Data = {<strong>shortenedCollectionName</strong>}Entry.getData();`

#### Set a **Response**

- use `sendMessageTo{Receiver}({specificToTheMethod}, {<strong>messageType</strong>}, {<strong>messageData</strong>})` when you <span style="background-color: #ffff00;">MODIFY</span> data 
    - **messageType**
        - Actions 
            - deleted, changed, created
        - {dataObject}\_{field}\_{action} 
            - ex.: team\_cash\_changed
        - {dataObject}\_{fieldOne}\_{fieldTwo}\_{etc.}\_{action} 
            - ex.: sim\_year\_yearState\_changed
        - {dataObject}\_{fieldParent}\_{fieldChild}\_{etc.}\_{action} 
            - ex.: team\_yearData\_factory\_changed
    - **messageData**
        - Information you want to send to the **Client**.
        - send `teamUid` and/or `studentUid` when you send message to **teacher** (so that the teacher can filter the message)
- use `Spark.setScriptData('{<strong>ObjectName</strong>}', {<strong>Object</strong>})` when you <span style="color: #000000; background-color: #ffff00;">GET</span> data

## Client (VS: C#)

#### **Standard**

- **Cases**
    - **Class** PascalCase
    - **Methods** PascalCase
    - **Private members**  \_camelCase
    - **Public members** camelCase
- **Document** 
    - **Formatting order** (each of those section should be surrounded by region with sub-region if necessary) 
        1. **Public members**
        2. **Public property**
        3. **Constructors**
        4. **Methods**
        5. **Private property**
        6. **Private Members**
- Whenever possible surround **code** with `#region MyRegion {<strong>code</strong>} #endregion` to make the **.cs** clearer
- use **Stubs** when the interface only need partial information instead of loading all the data. 
    - exemples : studentStub, EvalStudentStub.
- if you want to do **Lazy loading** or **Lazy Initialization** make a Property (the reference is always private) 
    - **public** : under public members
    - **private** : over private members
    - ex.:   
        `private Dictionary<string, TextMeshProUGUI> TeamEvalTexts<br></br>{<br></br>    get<br></br>    {<br></br>    	if (_teamEvalTexts == null)<br></br>        {<br></br>        	_teamEvalTexts = new Dictionary<string, TextMeshProUGUI>();<br></br>        	_teamEvalTexts[Simulation.SIM_PERF_NAME] = simPerfNoteText;<br></br>                _teamEvalTexts[Simulation.LOGBOOK_NAME] = logbookNoteText;<br></br>        }<br></br>        return _teamEvalTexts;<br></br>     }<br></br>}<br></br>private Dictionary<string, TextMeshProUGUI> _teamEvalTexts;`
- Plugin edition :

```C#
//UNIKSIM BEGIN [name] [reason]
//old code in comment
....
//UNIKSIM END
```

#### **Data**

- **SetListeners** in the data **contructors**
- the data **Update** itself and fire a relevent `Action`
- Filter the **Response** with `teamUid` and/or `studentUid` when the **teacher** receive the message

## Client (Unity)

#### **Boutons**

- **ConfirmButton** are always to the right
- **CancelButton** are always to the left

#### **Prefabs**

- Use the basic **Prefab**  when ever you need them. (NormalButton, EditButton, InputText, Dropdown, etc.)

#### **Popup**

- **X on the top-right corner VS CancelButton**
    - When there is no action, only information, put X on the top-right corner and close on click outside.
    - When there is an action, inputs, put a CancelButton with a confirmation to close.

#### <span style="font-size: 2.8275em; font-weight: 400;">  
Google sheets </span>

- **shared\_text** is used by all the projects.
- Each project has a Google Sheet containing their project specific texts.