Add friend chat; replace betting wording (شرط) with entry coins

- Friend-to-friend chat outside the game (ChatScreen) with mock replies,
  per-friend history, unread tracking; chat button on each friend row
- OnlineService + mock + online-store extended with chat (list/get/send/markRead)
- Reframe gambling term: "شرط"/"Stake" -> "سکه ورودی"/"Entry coins";
  free entry labeled رایگان/Free

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
soroush.asadi
2026-06-04 10:21:44 +03:30
parent e2d0a602b6
commit 5776036d78
10 changed files with 290 additions and 4 deletions
+88
View File
@@ -12,6 +12,8 @@ import {
import {
AVATARS,
AuthSession,
ChatMessage,
Conversation,
DailyRewardState,
Friend,
FriendRequest,
@@ -52,8 +54,22 @@ const LS = {
session: "hokm.session",
profile: "hokm.profile",
daily: "hokm.daily",
chats: "hokm.chats",
};
const CANNED_REPLIES = [
"سلام! 👋",
"بزن بریم 🔥",
"یه دست دیگه؟",
"من آماده‌ام",
"آفرین، کارت خوب بود",
"حکم چی بکنیم؟",
"😂😂",
"الان میام بازی",
"حتماً!",
"تو رو خدا این دفعه کُتمون نکن 😅",
];
function load<T>(key: string): T | null {
if (!isBrowser()) return null;
try {
@@ -132,14 +148,19 @@ export class MockOnlineService implements OnlineService {
private currentOppRating = 1000;
private lastOtp = "";
private messages: Record<string, ChatMessage[]> = {};
private unread: Record<string, number> = {};
private roomCbs = new Set<(r: Room) => void>();
private mmCbs = new Set<(s: MatchmakingState) => void>();
private friendCbs = new Set<(f: Friend[]) => void>();
private chatCbs = new Set<(friendId: string, m: ChatMessage[]) => void>();
private timers: ReturnType<typeof setTimeout>[] = [];
constructor() {
this.session = load<AuthSession>(LS.session);
this.profile = load<UserProfile>(LS.profile);
this.messages = load<Record<string, ChatMessage[]>>(LS.chats) ?? {};
this.seedFriends();
}
@@ -320,6 +341,73 @@ export class MockOnlineService implements OnlineService {
return () => this.friendCbs.delete(cb);
}
/* ------------------------------- chat ------------------------------ */
private saveChats() {
save(LS.chats, this.messages);
}
private emitChat(friendId: string) {
const msgs = this.messages[friendId] ?? [];
for (const cb of this.chatCbs) cb(friendId, [...msgs]);
}
async listConversations(): Promise<Conversation[]> {
const convs: Conversation[] = [];
for (const friend of this.friends) {
const msgs = this.messages[friend.id];
if (!msgs || msgs.length === 0) continue;
convs.push({
friend,
lastMessage: msgs[msgs.length - 1],
unread: this.unread[friend.id] ?? 0,
});
}
return convs.sort(
(a, b) => (b.lastMessage?.ts ?? 0) - (a.lastMessage?.ts ?? 0)
);
}
async getMessages(friendId: string): Promise<ChatMessage[]> {
return [...(this.messages[friendId] ?? [])];
}
async sendMessage(friendId: string, text: string): Promise<ChatMessage> {
const msg: ChatMessage = {
id: rid("m"),
fromMe: true,
text: text.trim(),
ts: Date.now(),
};
this.messages[friendId] = [...(this.messages[friendId] ?? []), msg];
this.saveChats();
this.emitChat(friendId);
// simulate a reply from the friend
this.after(randInt(900, 1900), () => {
const reply: ChatMessage = {
id: rid("m"),
fromMe: false,
text: pick(CANNED_REPLIES),
ts: Date.now(),
};
this.messages[friendId] = [...(this.messages[friendId] ?? []), reply];
this.unread[friendId] = (this.unread[friendId] ?? 0) + 1;
this.saveChats();
this.emitChat(friendId);
});
return msg;
}
async markRead(friendId: string) {
this.unread[friendId] = 0;
}
onChat(cb: (friendId: string, m: ChatMessage[]) => void): Unsubscribe {
this.chatCbs.add(cb);
return () => this.chatCbs.delete(cb);
}
/* ------------------------------ rooms ------------------------------ */
private seatYou(): RoomSeat {
+9
View File
@@ -4,6 +4,8 @@
import {
AuthSession,
ChatMessage,
Conversation,
DailyRewardState,
Friend,
FriendRequest,
@@ -53,6 +55,13 @@ export interface OnlineService {
removeFriend(id: string): Promise<void>;
onFriends(cb: (friends: Friend[]) => void): Unsubscribe;
/* ----- chat ----- */
listConversations(): Promise<Conversation[]>;
getMessages(friendId: string): Promise<ChatMessage[]>;
sendMessage(friendId: string, text: string): Promise<ChatMessage>;
markRead(friendId: string): Promise<void>;
onChat(cb: (friendId: string, messages: ChatMessage[]) => void): Unsubscribe;
/* ----- rooms ----- */
createRoom(opts: CreateRoomOptions): Promise<Room>;
setPartner(roomId: string, friendId: string | null): Promise<Room>;
+15
View File
@@ -245,6 +245,21 @@ export interface DailyRewardState {
available: boolean;
}
/* ------------------------------- Chat -------------------------------- */
export interface ChatMessage {
id: string;
fromMe: boolean;
text: string;
ts: number;
}
export interface Conversation {
friend: Friend;
lastMessage: ChatMessage | null;
unread: number;
}
/* ------------------------------ Avatars ------------------------------ */
export const AVATARS: { id: string; emoji: string }[] = [