Add character create, select rules, and delayed deletion.
Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -93,15 +93,15 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
93
93
94
94
## Characters
95
95
96
**Status:** not implemented
97
**Spec:** Unlimited characters per account, one in the world. First/last name rules, male/female, delayed deletion with inheritance (inheritance later).
98
**Code:** —
96
**Status:** full
97
**Spec:** Unlimited characters per account, one in the world. First name is not globally unique; last name is globally unique per account; first+last unique per account, case-insensitive. Male/female body. Spawn requires an open tile.
98
**Code:** `server/src/characters/service.js`, `server/src/persistence/stateDb.js`
99
99
100
100
## Character deletion and inheritance
101
101
102
**Status:** not implemented
103
**Spec:** 24h delayed delete, cancellable, email or stub log. Inventory/equipment/wallet/vault move to an account inheritance tab.
104
**Code:** —
102
**Status:** partial
103
**Spec:** 24h delayed delete, cancellable. Notify via stub log (or email when present). Names and surname claims are freed on permanent delete. Inventory/equipment/wallet/vault inheritance is not implemented (no items yet).
104
**Code:** `server/src/characters/service.js` (`scheduleDelete`, `cancelDelete`, `processDueDeletions`)
105
105
106
106
## 3×3 chunk replication
107
107
@@ -93,15 +93,15 @@ Each entry is a short note on behaviour and where the code lives. Update this fi93
93
94
## Characters94
## Characters95
95
96
**Status:** not implemented 96
**Status:** full 97
**Spec:** Unlimited characters per account, one in the world. First/last name rules, male/female, delayed deletion with inheritance (inheritance later). 97
**Spec:** Unlimited characters per account, one in the world. First name is not globally unique; last name is globally unique per account; first+last unique per account, case-insensitive. Male/female body. Spawn requires an open tile. 98
**Code:** —98
**Code:** `server/src/characters/service.js`, `server/src/persistence/stateDb.js`99
99
100
## Character deletion and inheritance100
## Character deletion and inheritance101
101
102
**Status:** not implemented 102
**Status:** partial 103
**Spec:** 24h delayed delete, cancellable, email or stub log. Inventory/equipment/wallet/vault move to an account inheritance tab. 103
**Spec:** 24h delayed delete, cancellable. Notify via stub log (or email when present). Names and surname claims are freed on permanent delete. Inventory/equipment/wallet/vault inheritance is not implemented (no items yet). 104
**Code:** —104
**Code:** `server/src/characters/service.js` (`scheduleDelete`, `cancelDelete`, `processDueDeletions`)105
105
106
## 3×3 chunk replication106
## 3×3 chunk replication107
107
@@ -0,0 +1,110 @@
1
import { SEX } from "../../../shared/protocol/constants.js";
2
import { normalizeName } from "../persistence/stateDb.js";
3
4
export class CharacterService
5
{
6
constructor (stateDb, log = console)
7
{
8
this.stateDb = stateDb;
9
this.log = log;
10
}
11
12
list (accountId)
13
{
14
return this.stateDb.listCharacters(accountId).map(publicCharacter);
15
}
16
17
create (account, firstName, lastName, sex)
18
{
19
const first = String(firstName ?? "").trim();
20
const last = String(lastName ?? "").trim();
21
if (!first || !last)
22
{ throw userError("First and last name are required."); }
23
if (sex !== SEX.male && sex !== SEX.female)
24
{ throw userError("Sex must be male or female."); }
25
26
const firstNormalized = normalizeName(first);
27
const lastNormalized = normalizeName(last);
28
const claim = this.stateDb.findSurnameClaim(lastNormalized);
29
if (claim && claim.account_id !== account.account_id)
30
{ throw userError("That last name is already claimed by another account."); }
31
32
const existing = this.stateDb.listCharacters(account.account_id);
33
if (existing.some((row) => row.first_normalized === firstNormalized && row.last_normalized === lastNormalized))
34
{ throw userError("You already have a character with that name."); }
35
36
const character = this.stateDb.createCharacter(account.account_id, first, last, sex, 8, 8);
37
return publicCharacter(character);
38
}
39
40
getOwned (accountId, characterId)
41
{
42
const character = this.stateDb.getCharacter(characterId);
43
if (!character || character.account_id !== accountId)
44
{ throw userError("Character not found."); }
45
return character;
46
}
47
48
scheduleDelete (account, characterId, delayMs)
49
{
50
const character = this.getOwned(account.account_id, characterId);
51
if (character.deletion_scheduled_at)
52
{ throw userError("That character is already marked for deletion."); }
53
54
const at = Date.now() + delayMs;
55
this.stateDb.scheduleDeletion(characterId, at);
56
this.#notify(account, `Character ${character.first_name} ${character.last_name} is scheduled for deletion.`);
57
return publicCharacter(this.stateDb.getCharacter(characterId));
58
}
59
60
cancelDelete (account, characterId)
61
{
62
const character = this.getOwned(account.account_id, characterId);
63
if (!character.deletion_scheduled_at)
64
{ throw userError("That character is not marked for deletion."); }
65
66
this.stateDb.cancelDeletion(characterId);
67
this.#notify(account, `Deletion cancelled for ${character.first_name} ${character.last_name}.`);
68
return publicCharacter(this.stateDb.getCharacter(characterId));
69
}
70
71
processDueDeletions (now = Date.now())
72
{
73
for (const character of this.stateDb.listDueDeletions(now))
74
{
75
this.stateDb.deleteCharacter(character.character_id);
76
this.log.info?.(
77
`[mail-stub] Permanently deleted ${character.first_name} ${character.last_name} for account ${character.account_id}`
78
);
79
}
80
}
81
82
#notify (account, message)
83
{
84
if (account.email)
85
{ this.log.info?.(`[mail] ${account.email}: ${message}`); }
86
else
87
{ this.log.info?.(`[mail-stub] ${account.auth_subject}: ${message}`); }
88
}
89
}
90
91
export function publicCharacter (row)
92
{
93
return {
94
characterId: row.character_id,
95
firstName: row.first_name,
96
lastName: row.last_name,
97
sex: row.sex,
98
tileX: row.tile_x,
99
tileY: row.tile_y,
100
gait: row.gait,
101
deletionScheduledAt: row.deletion_scheduled_at ?? null
102
};
103
}
104
105
function userError (message)
106
{
107
const error = new Error(message);
108
error.userFacing = true;
109
return error;
110
}
@@ -0,0 +1,110 @@1
import { SEX } from "../../../shared/protocol/constants.js";2
import { normalizeName } from "../persistence/stateDb.js";3
4
export class CharacterService5
{6
constructor (stateDb, log = console)7
{8
this.stateDb = stateDb;9
this.log = log;10
}11
12
list (accountId)13
{14
return this.stateDb.listCharacters(accountId).map(publicCharacter);15
}16
17
create (account, firstName, lastName, sex)18
{19
const first = String(firstName ?? "").trim();20
const last = String(lastName ?? "").trim();21
if (!first || !last)22
{ throw userError("First and last name are required."); }23
if (sex !== SEX.male && sex !== SEX.female)24
{ throw userError("Sex must be male or female."); }25
26
const firstNormalized = normalizeName(first);27
const lastNormalized = normalizeName(last);28
const claim = this.stateDb.findSurnameClaim(lastNormalized);29
if (claim && claim.account_id !== account.account_id)30
{ throw userError("That last name is already claimed by another account."); }31
32
const existing = this.stateDb.listCharacters(account.account_id);33
if (existing.some((row) => row.first_normalized === firstNormalized && row.last_normalized === lastNormalized))34
{ throw userError("You already have a character with that name."); }35
36
const character = this.stateDb.createCharacter(account.account_id, first, last, sex, 8, 8);37
return publicCharacter(character);38
}39
40
getOwned (accountId, characterId)41
{42
const character = this.stateDb.getCharacter(characterId);43
if (!character || character.account_id !== accountId)44
{ throw userError("Character not found."); }45
return character;46
}47
48
scheduleDelete (account, characterId, delayMs)49
{50
const character = this.getOwned(account.account_id, characterId);51
if (character.deletion_scheduled_at)52
{ throw userError("That character is already marked for deletion."); }53
54
const at = Date.now() + delayMs;55
this.stateDb.scheduleDeletion(characterId, at);56
this.#notify(account, `Character ${character.first_name} ${character.last_name} is scheduled for deletion.`);57
return publicCharacter(this.stateDb.getCharacter(characterId));58
}59
60
cancelDelete (account, characterId)61
{62
const character = this.getOwned(account.account_id, characterId);63
if (!character.deletion_scheduled_at)64
{ throw userError("That character is not marked for deletion."); }65
66
this.stateDb.cancelDeletion(characterId);67
this.#notify(account, `Deletion cancelled for ${character.first_name} ${character.last_name}.`);68
return publicCharacter(this.stateDb.getCharacter(characterId));69
}70
71
processDueDeletions (now = Date.now())72
{73
for (const character of this.stateDb.listDueDeletions(now))74
{75
this.stateDb.deleteCharacter(character.character_id);76
this.log.info?.(77
`[mail-stub] Permanently deleted ${character.first_name} ${character.last_name} for account ${character.account_id}`78
);79
}80
}81
82
#notify (account, message)83
{84
if (account.email)85
{ this.log.info?.(`[mail] ${account.email}: ${message}`); }86
else87
{ this.log.info?.(`[mail-stub] ${account.auth_subject}: ${message}`); }88
}89
}90
91
export function publicCharacter (row)92
{93
return {94
characterId: row.character_id,95
firstName: row.first_name,96
lastName: row.last_name,97
sex: row.sex,98
tileX: row.tile_x,99
tileY: row.tile_y,100
gait: row.gait,101
deletionScheduledAt: row.deletion_scheduled_at ?? null102
};103
}104
105
function userError (message)106
{107
const error = new Error(message);108
error.userFacing = true;109
return error;110
}