remove setnameexecution because unused, create DisplayName on Player

This commit is contained in:
Evan
2024-10-31 20:30:17 -07:00
parent d08d0c473f
commit a31a35c038
9 changed files with 78 additions and 122 deletions
-20
View File
@@ -1,5 +1,3 @@
import twemoji from 'twemoji';
import DOMPurify from 'dompurify';
export function renderTroops(troops: number): string {
let troopsStr = ''
@@ -32,21 +30,3 @@ export function createCanvas(): HTMLCanvasElement {
return canvas
}
export function processName(name: string): string {
const sanitized = Array.from(name).slice(0, 10).join('').replace(/[^\p{L}\p{N}\s\p{Emoji}\p{Emoji_Component}]/gu, '');
// First sanitize the raw input - strip everything except text and emojis
const withEmojis = twemoji.parse(sanitized, {
base: 'https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/', // Use jsDelivr CDN
folder: 'svg', // or 'png' if you prefer
ext: '.svg' // or '.png' if you prefer
});
return DOMPurify.sanitize(withEmojis, {
ALLOWED_TAGS: ['img'],
ALLOWED_ATTR: ['src', 'alt', 'class'],
// Only allow twemoji CDN URLs
ALLOWED_URI_REGEXP: /^https:\/\/cdn\.jsdelivr\.net\/gh\/twitter\/twemoji/
});
}
+2 -2
View File
@@ -234,7 +234,7 @@ export class EventsDisplay implements Layer {
}
if (event.message.recipient == myPlayer) {
this.addEvent({
description: `${event.message.sender.name()}:${event.message.emoji}`,
description: `${event.message.sender.displayName()}:${event.message.emoji}`,
type: MessageType.INFO,
highlight: true,
createdAt: this.game.ticks(),
@@ -242,7 +242,7 @@ export class EventsDisplay implements Layer {
}
if (event.message.sender == myPlayer && event.message.recipient != AllPlayers) {
this.addEvent({
description: `Sent ${event.message.recipient.name()} ${event.message.emoji}`,
description: `Sent ${event.message.recipient.displayName()} ${event.message.emoji}`,
type: MessageType.INFO,
highlight: true,
createdAt: this.game.ticks(),
+2 -5
View File
@@ -4,9 +4,6 @@ import { Layer } from './Layer';
import { Game, Player } from '../../../core/game/Game';
import { ClientID } from '../../../core/Schemas';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
import { processName } from '../Utils';
interface Entry {
name: string
@@ -68,7 +65,7 @@ export class Leaderboard extends LitElement implements Layer {
this.players.pop()
this.players.push({
name: myPlayer.name(),
name: myPlayer.displayName(),
position: place,
score: formatPercentage(myPlayer.numTilesOwned() / this.game.numLandTiles()),
isMyPlayer: true,
@@ -166,7 +163,7 @@ export class Leaderboard extends LitElement implements Layer {
.map((player, index) => html`
<tr class="${player.isMyPlayer ? 'myPlayer' : 'otherPlayer'}">
<td>${player.position}</td>
<td>${unsafeHTML(processName(player.name))}</td>
<td>${unsafeHTML(player.name)}</td>
<td>${player.score}</td>
</tr>
`)}
-8
View File
@@ -7,7 +7,6 @@ export type ClientID = string
export type Intent = SpawnIntent
| AttackIntent
| BoatAttackIntent
| UpdateNameIntent
| AllianceRequestIntent
| AllianceRequestReplyIntent
| BreakAllianceIntent
@@ -19,7 +18,6 @@ export type Intent = SpawnIntent
export type AttackIntent = z.infer<typeof AttackIntentSchema>
export type SpawnIntent = z.infer<typeof SpawnIntentSchema>
export type BoatAttackIntent = z.infer<typeof BoatAttackIntentSchema>
export type UpdateNameIntent = z.infer<typeof UpdateNameIntentSchema>
export type AllianceRequestIntent = z.infer<typeof AllianceRequestIntentSchema>
export type AllianceRequestReplyIntent = z.infer<typeof AllianceRequestReplyIntentSchema>
export type BreakAllianceIntent = z.infer<typeof BreakAllianceIntentSchema>
@@ -101,11 +99,6 @@ export const BoatAttackIntentSchema = BaseIntentSchema.extend({
y: z.number(),
})
export const UpdateNameIntentSchema = BaseIntentSchema.extend({
type: z.literal('updateName'),
name: z.string(),
})
export const AllianceRequestIntentSchema = BaseIntentSchema.extend({
type: z.literal('allianceRequest'),
requestor: z.string(),
@@ -157,7 +150,6 @@ const IntentSchema = z.union([
AttackIntentSchema,
SpawnIntentSchema,
BoatAttackIntentSchema,
UpdateNameIntentSchema,
AllianceRequestIntentSchema,
AllianceRequestReplyIntentSchema,
BreakAllianceIntentSchema,
+22
View File
@@ -1,4 +1,6 @@
import { v4 as uuidv4 } from 'uuid';
import twemoji from 'twemoji';
import DOMPurify from 'dompurify';
import { Cell, Game, Player, TerraNullius, Tile } from "./game/Game";
@@ -140,3 +142,23 @@ export function getMode(list: string[]): string {
return mode;
}
export function sanitize(name: string): string {
return Array.from(name).slice(0, 10).join('').replace(/[^\p{L}\p{N}\s\p{Emoji}\p{Emoji_Component}]/gu, '');
}
export function processName(name: string): string {
// First sanitize the raw input - strip everything except text and emojis
const withEmojis = twemoji.parse(sanitize(name), {
base: 'https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/', // Use jsDelivr CDN
folder: 'svg', // or 'png' if you prefer
ext: '.svg' // or '.png' if you prefer
});
return DOMPurify.sanitize(withEmojis, {
ALLOWED_TAGS: ['img'],
ALLOWED_ATTR: ['src', 'alt', 'class'],
// Only allow twemoji CDN URLs
ALLOWED_URI_REGEXP: /^https:\/\/cdn\.jsdelivr\.net\/gh\/twitter\/twemoji/
});
}
+2 -8
View File
@@ -5,10 +5,9 @@ import {SpawnExecution} from "./SpawnExecution";
import { BotSpawner } from "./BotSpawner";
import { BoatAttackExecution } from "./BoatAttackExecution";
import { PseudoRandom } from "../PseudoRandom";
import {UpdateNameExecution} from "./UpdateNameExecution";
import { FakeHumanExecution } from "./FakeHumanExecution";
import Usernames from '../../../resources/Usernames.txt'
import {simpleHash} from "../Util";
import { processName, sanitize, simpleHash } from "../Util";
import { AllianceRequestExecution } from "./alliance/AllianceRequestExecution";
import { AllianceRequestReplyExecution } from "./alliance/AllianceRequestReplyExecution";
import { BreakAllianceExecution } from "./alliance/BreakAllianceExecution";
@@ -48,7 +47,7 @@ export class Executor {
)
} else if (intent.type == "spawn") {
return new SpawnExecution(
new PlayerInfo(intent.name.slice(0, 18), intent.playerType, intent.clientID, intent.playerID),
new PlayerInfo(sanitize(intent.name), intent.playerType, intent.clientID, intent.playerID),
new Cell(intent.x, intent.y)
)
} else if (intent.type == "boat") {
@@ -58,11 +57,6 @@ export class Executor {
new Cell(intent.x, intent.y),
intent.troops
)
} else if (intent.type == "updateName") {
return new UpdateNameExecution(
intent.name,
intent.clientID
)
} else if (intent.type == "allianceRequest") {
return new AllianceRequestExecution(intent.requestor, intent.recipient)
} else if (intent.type == "allianceRequestReply") {
-35
View File
@@ -1,35 +0,0 @@
import {Execution, MutableGame, MutablePlayer, PlayerID} from "../game/Game"
import {ClientID} from "../Schemas"
export class UpdateNameExecution implements Execution {
private active = true
private mg: MutableGame
constructor(private newName: string, private clientID: ClientID) {
}
init(mg: MutableGame, ticks: number) {
this.mg = mg
}
tick(ticks: number) {
const player = this.mg.players().find(p => p.clientID() == this.clientID)
if (player == null) {
return
}
player.setName(this.newName)
this.active = false
}
owner(): MutablePlayer {
return null
}
isActive(): boolean {
return this.active
}
activeDuringSpawnPhase(): boolean {
return true
}
}
+1
View File
@@ -163,6 +163,7 @@ export interface TerraNullius {
export interface Player {
info(): PlayerInfo
name(): string
displayName(): string
clientID(): ClientID
id(): PlayerID
type(): PlayerType
+6 -1
View File
@@ -1,6 +1,6 @@
import { MutablePlayer, Tile, PlayerInfo, PlayerID, PlayerType, Player, TerraNullius, Cell, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance, Alliance, Tick, TargetPlayerEvent, EmojiMessage, EmojiMessageEvent, AllPlayers, Currency } from "./Game";
import { ClientID } from "../Schemas";
import {simpleHash} from "../Util";
import { processName, simpleHash } from "../Util";
import { CellString, GameImpl } from "./GameImpl";
import { BoatImpl } from "./BoatImpl";
import { TileImpl } from "./TileImpl";
@@ -29,6 +29,7 @@ export class PlayerImpl implements MutablePlayer {
public _tiles: Map<CellString, Tile> = new Map<CellString, Tile>();
private _name: string;
private _displayerName: string;
public pastOutgoingAllianceRequests: AllianceRequest[] = []
@@ -40,11 +41,15 @@ export class PlayerImpl implements MutablePlayer {
constructor(private gs: GameImpl, private readonly playerInfo: PlayerInfo, private _troops) {
this._name = playerInfo.name;
this._displayerName = processName(this._name)
}
name(): string {
return this._name;
}
displayName(): string {
return this._displayerName
}
clientID(): ClientID {
return this.playerInfo.clientID;