Skip to content

Commit dfbdc24

Browse files
committed
feat(sdk): limit nickname length automatically
1 parent 93badf9 commit dfbdc24

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

engine/core/src/main/java/com/codingame/gameengine/core/AbstractPlayer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ public final String getNicknameToken() {
4141
return "$" + this.index;
4242
}
4343

44+
/**
45+
* Returns a string that will be converted into the real nickname by the viewer.
46+
*
47+
* @param maxLength the maximum amount of characters of the nickname to display.
48+
* If it is longer, an ellipsis will be appended ("...")
49+
*
50+
* @return the player's nickname token.
51+
*/
52+
public final String getNicknameToken(int maxLength) {
53+
if (maxLength <= 0) {
54+
throw new IllegalArgumentException("Invalid width: " + maxLength);
55+
}
56+
return "$" + this.index + "ε" + maxLength;
57+
}
58+
4459
/**
4560
* Returns a string that will be converted into the real avatar by the viewer, if it exists.
4661
*

engine/modules/entities/src/main/resources/view/entity-module/properties.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,21 @@ export const PROPERTIES = {
9999
...stringOpts,
100100
convert (value, globalData) {
101101
value = unescape(value)
102-
const regexp = /\$(\d)/g
102+
const regexp = /\$(\d)(?:ε(\d+))?/g
103103
let match = regexp.exec(value)
104104
let res = ''
105105
let prevIdx = 0
106106
while (match) {
107107
res += value.substring(prevIdx, match.index)
108-
res += globalData.players[+match[1]].name
108+
let name = globalData.players[+match[1]].name
109+
110+
if (match[2] != null) {
111+
const maxChars = +match[2]
112+
if (name.length > maxChars) {
113+
name = name.substring(0, maxChars) + '...'
114+
}
115+
}
116+
res += name
109117
prevIdx = match.index + match[0].length
110118
match = regexp.exec(value)
111119
}

playground/misc/misc-3-release-notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The CodinGame SDK is regularly updated and improved. This document lets you know
44

55
## Next Release
66

7+
### 🎁 Features
8+
9+
- It is now possible to limit the number of characters display given by `getNicknameToken`.
10+
711
### 🐞 Bug fix
812

913
- Fixed an issue where games with a `stepByStepAnimateSpeed` would sometimes try to animate frame 0, causing a black screen.

0 commit comments

Comments
 (0)