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:
+16
-2
@@ -87,6 +87,13 @@ const fa: Dict = {
|
||||
"common.soon": "بهزودی",
|
||||
"common.copy": "کپی",
|
||||
"common.copied": "کپی شد",
|
||||
"common.free": "رایگان",
|
||||
|
||||
"chat.title": "گفتگو",
|
||||
"chat.placeholder": "پیام بنویسید…",
|
||||
"chat.send": "ارسال",
|
||||
"chat.empty": "گفتگو را شروع کنید",
|
||||
"friends.message": "پیام",
|
||||
|
||||
"profile.title": "پروفایل",
|
||||
"profile.stats": "آمار",
|
||||
@@ -128,7 +135,7 @@ const fa: Dict = {
|
||||
"room.empty": "خالی",
|
||||
"room.waiting": "در انتظار…",
|
||||
"room.start": "شروع بازی",
|
||||
"room.stake": "شرط",
|
||||
"room.stake": "سکه ورودی",
|
||||
"room.leave": "ترک اتاق",
|
||||
"room.pickFriend": "یک دوست را انتخاب کنید",
|
||||
|
||||
@@ -265,6 +272,13 @@ const en: Dict = {
|
||||
"common.soon": "Coming soon",
|
||||
"common.copy": "Copy",
|
||||
"common.copied": "Copied",
|
||||
"common.free": "Free",
|
||||
|
||||
"chat.title": "Chat",
|
||||
"chat.placeholder": "Type a message…",
|
||||
"chat.send": "Send",
|
||||
"chat.empty": "Start the conversation",
|
||||
"friends.message": "Message",
|
||||
|
||||
"profile.title": "Profile",
|
||||
"profile.stats": "Stats",
|
||||
@@ -306,7 +320,7 @@ const en: Dict = {
|
||||
"room.empty": "Empty",
|
||||
"room.waiting": "Waiting…",
|
||||
"room.start": "Start game",
|
||||
"room.stake": "Stake",
|
||||
"room.stake": "Entry coins",
|
||||
"room.leave": "Leave room",
|
||||
"room.pickFriend": "Pick a friend",
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { create } from "zustand";
|
||||
import { CreateRoomOptions, MatchmakingOptions, getService } from "./online/service";
|
||||
import {
|
||||
ChatMessage,
|
||||
Friend,
|
||||
FriendRequest,
|
||||
LeaderboardEntry,
|
||||
@@ -35,11 +36,19 @@ interface OnlineStore {
|
||||
cancelMatchmaking: () => Promise<void>;
|
||||
|
||||
loadLeaderboard: () => Promise<void>;
|
||||
|
||||
// chat
|
||||
activeChatFriend: Friend | null;
|
||||
chatMessages: ChatMessage[];
|
||||
openChat: (friend: Friend) => Promise<void>;
|
||||
sendChat: (text: string) => Promise<void>;
|
||||
closeChat: () => void;
|
||||
}
|
||||
|
||||
let roomUnsub: (() => void) | null = null;
|
||||
let mmUnsub: (() => void) | null = null;
|
||||
let friendUnsub: (() => void) | null = null;
|
||||
let chatUnsub: (() => void) | null = null;
|
||||
|
||||
export const useOnlineStore = create<OnlineStore>((set, get) => ({
|
||||
friends: [],
|
||||
@@ -128,4 +137,36 @@ export const useOnlineStore = create<OnlineStore>((set, get) => ({
|
||||
const leaderboard = await getService().getLeaderboard();
|
||||
set({ leaderboard });
|
||||
},
|
||||
|
||||
activeChatFriend: null,
|
||||
chatMessages: [],
|
||||
|
||||
openChat: async (friend) => {
|
||||
const svc = getService();
|
||||
set({ activeChatFriend: friend, chatMessages: await svc.getMessages(friend.id) });
|
||||
await svc.markRead(friend.id);
|
||||
if (chatUnsub) chatUnsub();
|
||||
chatUnsub = svc.onChat((friendId, msgs) => {
|
||||
const active = get().activeChatFriend;
|
||||
if (active && active.id === friendId) {
|
||||
set({ chatMessages: msgs });
|
||||
svc.markRead(friendId);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
sendChat: async (text) => {
|
||||
const friend = get().activeChatFriend;
|
||||
if (!friend || !text.trim()) return;
|
||||
await getService().sendMessage(friend.id, text);
|
||||
set({ chatMessages: await getService().getMessages(friend.id) });
|
||||
},
|
||||
|
||||
closeChat: () => {
|
||||
if (chatUnsub) {
|
||||
chatUnsub();
|
||||
chatUnsub = null;
|
||||
}
|
||||
set({ activeChatFriend: null, chatMessages: [] });
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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>;
|
||||
|
||||
@@ -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 }[] = [
|
||||
|
||||
@@ -12,6 +12,7 @@ export type Screen =
|
||||
| "matchmaking"
|
||||
| "leaderboard"
|
||||
| "shop"
|
||||
| "chat"
|
||||
| "game"; // the table (used for both ai + online)
|
||||
|
||||
interface UIStore {
|
||||
|
||||
Reference in New Issue
Block a user