Skip to content

Commit a3e9bdc

Browse files
committed
Fix formatter side
1 parent 9c25eb5 commit a3e9bdc

File tree

4 files changed

+90
-55
lines changed

4 files changed

+90
-55
lines changed

src/UI/components/rooms/BattleRoom.tsx

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HTMLAttributes, useState } from 'react';
22
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
3-
import { faCommentAlt, faUsers } from '@fortawesome/free-solid-svg-icons';
4-
import { AnimatePresence, cubicBezier, motion } from 'framer-motion';
3+
import { faUsers } from '@fortawesome/free-solid-svg-icons';
4+
import { AnimatePresence, motion } from 'framer-motion';
55

66
import ChatBox from '../single/chatbox';
77
import UserList from '../single/UserList';
@@ -35,15 +35,10 @@ export default function BattleRoom(props: HTMLAttributes<HTMLDivElement>) {
3535
setUserListOpen(!userListOpen);
3636
}}
3737
>
38-
{
39-
userListOpen ?
40-
<FontAwesomeIcon icon={faCommentAlt} height={16} width={16} /> :
41-
<FontAwesomeIcon icon={faUsers} height={16} width={16} />
42-
}
43-
38+
<FontAwesomeIcon icon={faUsers} height={16} width={16} className={userListOpen ? 'text-blue-200' : 'text-gray-400'}/>
4439
</div>
4540
<div
46-
className="dark:bg-gray-300 flex flex-col min-h-0 w-full max-w-full"
41+
className="dark:bg-gray-300 flex flex-col min-h-0 h-full w-full max-w-full"
4742
>
4843
<AnimatePresence initial={false} mode="wait">
4944
{userListOpen &&
@@ -86,8 +81,8 @@ export default function BattleRoom(props: HTMLAttributes<HTMLDivElement>) {
8681
</motion.div>
8782
}
8883
</AnimatePresence>
89-
<Chat className='p-2 transition-all'/>
90-
<ChatBox className='transition-all'/>
84+
<Chat className='p-2 h-full'/>
85+
<ChatBox className='p-2 flex-grow-1'/>
9186
</div>
9287

9388
</div>

src/client/client.ts

+24-10
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class Client {
171171
this.queryUser(user, (_json) => {
172172
// This is risky as we could be logged in but not get a queryResponse for some reason
173173
this.events.dispatchEvent(
174-
new CustomEvent('login', { detail: this.settings.getUsername() }),
174+
new CustomEvent('login', { detail: this.settings.username }),
175175
);
176176
});
177177
}
@@ -384,7 +384,7 @@ export class Client {
384384
private async send_assertion(assertion: string) {
385385
const username = assertion.split(',')[1];
386386

387-
const storedName = this.settings.getUsername();
387+
const storedName = this.settings.username;
388388
this.__send(
389389
`/trn ${toID(storedName) === toID(username) ? storedName : username
390390
},0,${assertion}`,
@@ -481,7 +481,7 @@ export class Client {
481481
) {
482482
const room = this.room(roomID);
483483
if (
484-
toID(message.user) !== toID(this.settings.getUsername()) &&
484+
toID(message.user) !== toID(this.settings.username) &&
485485
this.highlightMsg(roomID, message)
486486
) {
487487
this.events.dispatchEvent(
@@ -491,7 +491,7 @@ export class Client {
491491
if (room) {
492492
const settings = {
493493
selected: this.selectedRoom === roomID,
494-
selfSent: toID(this.settings.getUsername()) === toID(message.user),
494+
selfSent: toID(this.settings.username) === toID(message.user),
495495
};
496496
let shouldNotify = false;
497497
if (message.name) {
@@ -557,7 +557,7 @@ export class Client {
557557

558558
private setUsername(username: string) {
559559
// gotta re-run highlightMsg on all messages
560-
this.settings.setUsername(username);
560+
this.settings.username = username;
561561
this.rooms.forEach(async (room) => {
562562
room.runHighlight(this.forceHighlightMsg.bind(this));
563563
});
@@ -700,7 +700,7 @@ export class Client {
700700
const sender = toID(args[1]);
701701
const receiver = toID(args[2]);
702702
let inferredRoomid = '';
703-
if (sender === toID(this.settings.getUsername())) {
703+
if (sender === toID(this.settings.username)) {
704704
// sent message
705705
inferredRoomid = `pm-${receiver}`;
706706
} else {
@@ -711,7 +711,7 @@ export class Client {
711711
args.slice(3).join('|'),
712712
);
713713
this.__createPM(
714-
sender === toID(this.settings.getUsername()) ? args[2] : args[1],
714+
sender === toID(this.settings.username) ? args[2] : args[1],
715715
);
716716
this.addMessageToRoom(
717717
inferredRoomid,
@@ -751,7 +751,7 @@ export class Client {
751751

752752
try {
753753
const tmpjson = JSON.parse(args[2]);
754-
if (tmpjson.userid === toID(this.settings.getUsername())) {
754+
if (tmpjson.userid === toID(this.settings.username)) {
755755
if (tmpjson.status) {
756756
this.settings.setStatus(tmpjson.status);
757757
}
@@ -821,7 +821,7 @@ export class Client {
821821
this.autojoin(this.joinAfterLogin);
822822
this.loggedIn = true;
823823
assert(named === '1', 'Couldn\'t guard against guest');
824-
this.settings.updateUsername(username, avatar);
824+
this.settings.updateUser(username, avatar);
825825
this.setUsername(username);
826826
this.queryUserInternal(username);
827827
}
@@ -943,6 +943,21 @@ export class Client {
943943
case 'updatesearch':
944944
break;
945945
// battles
946+
case 'player':
947+
{
948+
console.log('player', args);
949+
const room = this.requiresRoom('player', roomID);
950+
if (!(room instanceof BattleRoom)) {
951+
console.error('Received |player| from non-battle room', roomID);
952+
return false;
953+
}
954+
const perspective = args[1];
955+
const playerName = args[2];
956+
if (toID(playerName) === this.settings.userID) {
957+
room.setFormatter(perspective);
958+
}
959+
}
960+
break;
946961
case 'move':
947962
case '-fail':
948963
case '-resisted':
@@ -964,7 +979,6 @@ export class Client {
964979
case 'switch':
965980
case 'battle':
966981
case 'gametype':
967-
case 'player':
968982
case 'gen':
969983
case 'tier':
970984
case 'sentchoice':

src/client/room/battleRoom.ts

+24-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Battle } from '@pkmn/client';
22
import { LogFormatter } from '@pkmn/view';
33
import { Generations } from '@pkmn/data';
4-
import { Dex } from '@pkmn/dex';
4+
import { Dex, SideID } from '@pkmn/dex';
55
import { Protocol } from '@pkmn/protocol';
66

77
import { Room, RoomType } from './room';
@@ -10,7 +10,7 @@ import { assert } from '@/lib/utils';
1010

1111

1212
export class BattleRoom extends Room {
13-
battle: Battle | null = null;
13+
battle: Battle;
1414
formatter: LogFormatter | null = null;
1515
constructor(
1616
{ ID, name, type, connected, open }: {
@@ -24,17 +24,19 @@ export class BattleRoom extends Room {
2424
super({ ID, name, type, connected, open });
2525

2626
this.battle = new Battle(new Generations(Dex));
27-
this.formatter = new LogFormatter('p1', this.battle); // TODO: dont use p1
27+
}
28+
29+
setFormatter(perspective: SideID) {
30+
assert(!this.formatter, 'Trying to create formatter twice');
31+
this.formatter = new LogFormatter(perspective, this.battle);
2832
}
2933

3034
/**
3135
* Returns whether a new message was added to the battle.
3236
*/
3337
feedBattle(line: string): boolean {
34-
assert(this.battle);
3538
const { args, kwArgs } = Protocol.parseBattleLine(line);
3639

37-
const html = this.formatter!.formatHTML(args, kwArgs); // fix type...
3840
try {
3941
// pre handler
4042
// add(pre, key, args, kwArgs);
@@ -47,18 +49,23 @@ export class BattleRoom extends Room {
4749

4850
this.battle.update();
4951

50-
if (html) {
51-
this.addMessage(
52-
newMessage({
53-
type: 'rawHTML',
54-
name: '',
55-
content: html,
56-
notify: false,
57-
hld: false,
58-
}),
59-
{ selected: true, selfSent: false },
60-
);
61-
return true;
52+
if (this.formatter) {
53+
const html = this.formatter.formatHTML(args, kwArgs);
54+
if (html) {
55+
this.addMessage(
56+
newMessage({
57+
type: 'rawHTML',
58+
name: '',
59+
content: html,
60+
notify: false,
61+
hld: false,
62+
}),
63+
{ selected: true, selfSent: false },
64+
);
65+
return true;
66+
}
67+
} else {
68+
console.debug('Ignoring line', line);
6269
}
6370
return false;
6471
}

src/client/settings.ts

+36-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { toID } from '@/utils/generic';
12
import { highlightMsg, stringsToRegex } from '../utils/highlightMsg';
23
import { Room } from './room/room';
34

@@ -41,10 +42,38 @@ export class Settings {
4142
highlightOnSelf: true,
4243
};
4344
private compileHighlightWords: { [key: string]: RegExp | null } = {};
44-
private username = '';
45+
private name = '';
4546
private status = ''; // if status is set, it will be restored on login
4647
private notes: Map<string, string> = new Map();
4748

49+
get username(): string {
50+
return this.name;
51+
}
52+
53+
updateUser(username: string, avatar: string) {
54+
this.name = username;
55+
this.userDefinedSettings.avatar = avatar;
56+
this.saveSettings();
57+
}
58+
59+
get avatar() {
60+
return this.userDefinedSettings.avatar;
61+
}
62+
63+
set avatar(avatar: string) {
64+
this.userDefinedSettings.avatar = avatar;
65+
this.saveSettings();
66+
}
67+
68+
set username(username: string) {
69+
this.name = username;
70+
this.saveSettings();
71+
}
72+
73+
get userID() {
74+
return toID(this.name);
75+
}
76+
4877
constructor() {
4978
if (typeof window === 'undefined') {
5079
return;
@@ -66,7 +95,7 @@ export class Settings {
6695
settings.rooms.forEach((r) => {
6796
this.rooms.push(r);
6897
});
69-
this.username = settings.username;
98+
this.name = settings.username;
7099
const userDefinedSettings = settings.userDefinedSettings;
71100
if (userDefinedSettings) {
72101
this.userDefinedSettings = userDefinedSettings;
@@ -93,7 +122,11 @@ export class Settings {
93122
username: this.username,
94123
userDefinedSettings: this.userDefinedSettings,
95124
};
96-
localStorage.setItem('settings', JSON.stringify(savedSettings));
125+
try {
126+
localStorage.setItem('settings', JSON.stringify(savedSettings));
127+
} catch (e) {
128+
console.error('Error saving settings', e, savedSettings);
129+
}
97130
localStorage.setItem('theme', this.theme);
98131
}
99132

@@ -104,12 +137,6 @@ export class Settings {
104137
}
105138
}
106139

107-
updateUsername(username: string, avatar: string) {
108-
this.username = username;
109-
this.userDefinedSettings.avatar = avatar;
110-
this.saveSettings();
111-
}
112-
113140
getAvatar() {
114141
return this.userDefinedSettings.avatar;
115142
}
@@ -131,14 +158,6 @@ export class Settings {
131158
this.saveSettings();
132159
}
133160

134-
getUsername() {
135-
return this.username;
136-
}
137-
setUsername(username: string) {
138-
this.username = username;
139-
this.saveSettings();
140-
}
141-
142161
getServerURL() {
143162
return this.userDefinedSettings.serverURL || this.defaultServerURL;
144163
}

0 commit comments

Comments
 (0)