11import { Player , system , world } from '@minecraft/server'
22import { table } from 'lib/database/abstract'
3- import { i18n , noI18n } from 'lib/i18n/text'
4- import { Mail } from 'lib/mail'
5- import { Rewards } from 'lib/utils/rewards'
63import './command'
74
85interface StoredClan {
@@ -19,7 +16,7 @@ interface StoredClan {
1916}
2017
2118export class Clan {
22- static database = table < StoredClan > ( 'clan' )
19+ private static database = table < StoredClan > ( 'clan' )
2320
2421 static getPlayerClan ( playerId : string ) {
2522 for ( const clan of this . instances . values ( ) ) {
@@ -47,40 +44,68 @@ export class Clan {
4744 createdAt : new Date ( ) . toISOString ( ) ,
4845 } )
4946
50- return new Clan ( name , this . database . get ( name ) )
47+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
48+ return new Clan ( name , this . database . get ( name ) ! )
49+ }
50+
51+ static getInvites ( playerId : string ) {
52+ return [ ...Clan . getAll ( ) ] . filter ( e => e . isInvited ( playerId ) )
5153 }
5254
5355 private static instances = new Map < string , Clan > ( )
5456
5557 static {
5658 world . afterEvents . worldLoad . subscribe ( ( ) =>
5759 system . run ( ( ) => {
58- for ( const [ id , db ] of this . database . entries ( ) ) new Clan ( id , db )
60+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61+ for ( const [ id , db ] of this . database . entries ( ) ) new Clan ( id , db ! )
5962 } ) ,
6063 )
6164 }
6265
63- db ! : StoredClan
64-
6566 constructor (
6667 private readonly id : string ,
67- db ? : StoredClan ,
68+ public readonly db : StoredClan ,
6869 ) {
6970 const clan = Clan . instances . get ( this . id )
7071 if ( clan ) return clan
7172
72- if ( ! db ) {
73- db = Clan . database . get ( id )
74- if ( ! db ) throw new ReferenceError ( noI18n . error `Clan with id ${ id } does not exists.` )
75- } else this . db = db
76-
7773 Clan . instances . set ( this . id , this )
7874 }
7975
76+ get name ( ) {
77+ return this . db . name
78+ }
79+ set name ( v ) {
80+ this . db . name = v
81+ }
82+
83+ get shortname ( ) {
84+ return this . db . shortname
85+ }
86+ set shortname ( v ) {
87+ this . db . shortname = v
88+ }
8089 get createdAt ( ) {
8190 return new Date ( this . db . createdAt )
8291 }
8392
93+ get members ( ) {
94+ return this . db . members as Readonly < StoredClan [ 'members' ] >
95+ }
96+
97+ get owners ( ) {
98+ return this . db . owners as Readonly < StoredClan [ 'owners' ] >
99+ }
100+
101+ get joinRequests ( ) {
102+ return this . db . joinRequests as Readonly < StoredClan [ 'joinRequests' ] >
103+ }
104+
105+ get invites ( ) {
106+ return this . db . invites as Readonly < StoredClan [ 'invites' ] >
107+ }
108+
84109 isMember ( playerId : string ) {
85110 return this . db . members . includes ( playerId )
86111 }
@@ -89,57 +114,44 @@ export class Clan {
89114 return this . db . owners . includes ( playerId )
90115 }
91116
117+ isInvited ( playerId : string ) {
118+ return this . db . invites . includes ( playerId )
119+ }
120+
92121 setRole ( playerId : string , role : 'member' | 'owner' ) {
93122 if ( role === 'member' ) this . db . owners = this . db . owners . filter ( e => e !== playerId )
94123 if ( role === 'owner' ) this . db . owners . push ( playerId )
95124 }
96125
97- kickMember ( playerId : string , whoKicked : string , message : string ) {
98- Mail . send (
99- playerId ,
100- i18n . nocolor `Вы выгнаны из клана '${ this . db . name } '` ,
101- i18n `Вы были выгнаны из клана игроком '${ whoKicked } '. Причина: ${ message } ` ,
102- new Rewards ( ) ,
103- )
126+ remove ( playerId : string ) {
104127 this . db . members = this . db . members . filter ( e => e !== playerId )
105128 this . db . owners = this . db . owners . filter ( e => e !== playerId )
106129 }
107130
108- invite ( playerId : string ) {
131+ sendInvite ( playerId : string ) {
109132 if ( Clan . getPlayerClan ( playerId ) ) return false
110133
111134 this . db . invites . push ( playerId )
112- Mail . send (
113- playerId ,
114- i18n . nocolor `Приглашение в клан '${ this . db . name } '` ,
115- i18n `Вы были приглашены в клан! Чтобы вступить, используйте .clan или раздел кланов из основого меню` ,
116- new Rewards ( ) ,
117- )
118135 return true
119136 }
120137
121138 requestJoin ( player : Player ) {
122- if ( Clan . getPlayerClan ( player . id ) ) return
123- if ( this . db . joinRequests . includes ( player . id ) )
124- return player . fail ( i18n . error `Вы отправили заявку в клан ${ this . db . name } !` )
125-
126- Mail . sendMultiple (
127- this . db . owners ,
128- i18n . nocolor `Запрос на вступление в клан от ${ player . name } ` ,
129- i18n `Игрок хочет вступить в ваш клан, вы можете принять или отклонить его через меню кланов` ,
130- new Rewards ( ) ,
131- )
139+ if ( Clan . getPlayerClan ( player . id ) ) return false
140+ if ( this . db . joinRequests . includes ( player . id ) ) return false
141+
132142 this . db . joinRequests . push ( player . id )
133- player . success ( i18n `Заявка на вступление в клан ' ${ this . db . name } ' отправлена!` )
143+ return true
134144 }
135145
136- add ( playerOrId : Player | string ) {
137- const id = playerOrId instanceof Player ? playerOrId . id : playerOrId
138- const message = i18n . nocolor `Вы приняты в клан ${ this . db . name } `
139- if ( playerOrId instanceof Player ) {
140- playerOrId . success ( message )
141- } else Mail . send ( id , message , i18n `Ура` , new Rewards ( ) )
146+ rejectJoin ( id : string ) {
147+ this . db . joinRequests = this . db . joinRequests . filter ( e => e !== id )
148+ }
142149
150+ undoInvite ( id : string ) {
151+ this . db . invites = this . db . invites . filter ( e => e !== id )
152+ }
153+
154+ add ( id : string ) {
143155 for ( const clan of Clan . getAll ( ) ) {
144156 clan . db . joinRequests = clan . db . joinRequests . filter ( e => e !== id )
145157 clan . db . invites = clan . db . invites . filter ( e => e !== id )
@@ -148,12 +160,6 @@ export class Clan {
148160 }
149161
150162 delete ( ) {
151- Mail . sendMultiple (
152- this . db . members ,
153- i18n . nocolor `Клан '${ this . db . name } ' распущен` ,
154- i18n `К сожалению, клан был распущен. Хз че создателю не понравилось, найдите клан получше или создайте новый, печалиться смысла нет. Ну базы еще можете залутать, врятли создатель успел вас удалить из всех клановых баз.` ,
155- new Rewards ( ) ,
156- )
157163 Clan . database . delete ( this . id )
158164 Clan . instances . delete ( this . id )
159165 }
0 commit comments