first commit
This commit is contained in:
121
assets/scripts/chat/ChatCommand.ts
Normal file
121
assets/scripts/chat/ChatCommand.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { _decorator } from 'cc';
|
||||
|
||||
import { NetManager } from "../network/socket/NetManager";
|
||||
import { ServerConfig } from "../config/ServerConfig";
|
||||
import ChatProxy from "./ChatProxy";
|
||||
import { EventMgr } from '../utils/EventMgr';
|
||||
|
||||
export default class ChatCommand {
|
||||
//单例
|
||||
protected static _instance: ChatCommand;
|
||||
public static getInstance(): ChatCommand {
|
||||
if (this._instance == null) {
|
||||
this._instance = new ChatCommand();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
|
||||
//数据model
|
||||
protected _proxy:ChatProxy = new ChatProxy();
|
||||
|
||||
public static destory(): boolean {
|
||||
if (this._instance) {
|
||||
this._instance.onDestory();
|
||||
this._instance = null;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//数据model
|
||||
|
||||
constructor() {
|
||||
// EventMgr.on(ServerConfig.chat_chat, this.onChat, this)
|
||||
EventMgr.on(ServerConfig.chat_history, this.onChatHistory, this)
|
||||
EventMgr.on(ServerConfig.chat_push, this.onChat, this)
|
||||
}
|
||||
|
||||
protected onChat(data:any):void{
|
||||
console.log("onChat:",data)
|
||||
if (data.code == 0) {
|
||||
if(data.msg.type == 0){
|
||||
this._proxy.updateWorldChat(data.msg);
|
||||
}else if (data.msg.type == 1){
|
||||
this._proxy.updateUnionChat(data.msg);
|
||||
}
|
||||
EventMgr.emit("update_chat_history");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected onChatHistory(data:any):void{
|
||||
console.log("onChatHistory:",data)
|
||||
if (data.code == 0) {
|
||||
if(data.msg.type == 0){
|
||||
this._proxy.updateWorldChatList(data.msg.msgs);
|
||||
}else if(data.msg.type == 1){
|
||||
this._proxy.updateUnionChatList(data.msg.msgs);
|
||||
}
|
||||
EventMgr.emit("update_chat_history");
|
||||
}
|
||||
}
|
||||
|
||||
public onDestory(): void {
|
||||
EventMgr.targetOff(this);
|
||||
}
|
||||
|
||||
public clearData(): void {
|
||||
this._proxy.clearData();
|
||||
}
|
||||
|
||||
public get proxy(): ChatProxy {
|
||||
return this._proxy;
|
||||
}
|
||||
|
||||
|
||||
public chat(msg:string,type:number = 0):void{
|
||||
let sendData: any = {
|
||||
name: ServerConfig.chat_chat,
|
||||
msg: {
|
||||
msg:msg,
|
||||
type:type,
|
||||
}
|
||||
};
|
||||
NetManager.getInstance().send(sendData);
|
||||
}
|
||||
|
||||
public join(type:number,id:number):void{
|
||||
let sendData: any = {
|
||||
name: ServerConfig.chat_join,
|
||||
msg: {
|
||||
type:type,
|
||||
id:id,
|
||||
}
|
||||
};
|
||||
NetManager.getInstance().send(sendData);
|
||||
}
|
||||
|
||||
public exit(type:number,id:number):void{
|
||||
let sendData: any = {
|
||||
name: ServerConfig.chat_exit,
|
||||
msg: {
|
||||
type:type,
|
||||
id:id,
|
||||
}
|
||||
};
|
||||
NetManager.getInstance().send(sendData);
|
||||
}
|
||||
|
||||
public chatHistory(type:number):void{
|
||||
let sendData: any = {
|
||||
name: ServerConfig.chat_history,
|
||||
msg: {
|
||||
type:type,
|
||||
}
|
||||
};
|
||||
NetManager.getInstance().send(sendData);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
assets/scripts/chat/ChatCommand.ts.meta
Normal file
11
assets/scripts/chat/ChatCommand.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "8b45e0cf-3e10-42ca-b583-92de9b2db1e8",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
24
assets/scripts/chat/ChatItemLogic.ts
Normal file
24
assets/scripts/chat/ChatItemLogic.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
import DateUtil from "../utils/DateUtil";
|
||||
import { ChatMsg } from "./ChatProxy";
|
||||
import { _decorator, Component, Label } from "cc";
|
||||
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
@ccclass('ChatItemLogic')
|
||||
export default class ChatItemLogic extends Component {
|
||||
|
||||
|
||||
@property(Label)
|
||||
nameLabel: Label = null;
|
||||
|
||||
|
||||
protected onLoad():void{
|
||||
}
|
||||
|
||||
protected updateItem(data:ChatMsg):void{
|
||||
var time = DateUtil.converTimeStr(data.time * 1000);
|
||||
this.nameLabel.string = time + " " + data.nick_name + ":" +data.msg;
|
||||
}
|
||||
|
||||
}
|
||||
11
assets/scripts/chat/ChatItemLogic.ts.meta
Normal file
11
assets/scripts/chat/ChatItemLogic.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ea99c0d7-de2f-4b87-b258-a97be3b9938d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
88
assets/scripts/chat/ChatLogic.ts
Normal file
88
assets/scripts/chat/ChatLogic.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
|
||||
|
||||
import { _decorator, Component, EditBox, ScrollView } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
import { MapCityData } from "../map/MapCityProxy";
|
||||
import MapCommand from "../map/MapCommand";
|
||||
import ChatCommand from "./ChatCommand";
|
||||
import { ChatMsg } from "./ChatProxy";
|
||||
import ListLogic from '../utils/ListLogic';
|
||||
import { EventMgr } from '../utils/EventMgr';
|
||||
|
||||
@ccclass('ChatLogic')
|
||||
export default class ChatLogic extends Component {
|
||||
|
||||
@property(EditBox)
|
||||
editConent: EditBox = null;
|
||||
|
||||
@property(ScrollView)
|
||||
chatView:ScrollView = null;
|
||||
|
||||
_type:number = 0;
|
||||
|
||||
protected onLoad():void{
|
||||
EventMgr.on("update_chat_history", this.updateChat, this);
|
||||
EventMgr.on("unionChange", this.updateChat, this);
|
||||
}
|
||||
|
||||
protected onEnable():void{
|
||||
console.log("onEnable")
|
||||
this.updateUnion();
|
||||
this.updateView();
|
||||
}
|
||||
|
||||
protected updateUnion():void{
|
||||
let city:MapCityData = MapCommand.getInstance().cityProxy.getMyMainCity();
|
||||
if (city.unionId > 0){
|
||||
//加入联盟频道
|
||||
ChatCommand.getInstance().join(1, city.unionId);
|
||||
}else{
|
||||
ChatCommand.getInstance().exit(1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
protected updateChat(data:any[]){
|
||||
if(this._type == 0){
|
||||
var comp = this.chatView.node.getComponent(ListLogic);
|
||||
var list:ChatMsg[] = ChatCommand.getInstance().proxy.getWorldChatList();
|
||||
comp.setData(list);
|
||||
}else if (this._type == 1){
|
||||
var comp = this.chatView.node.getComponent(ListLogic);
|
||||
var list:ChatMsg[] = ChatCommand.getInstance().proxy.getUnionChatList();
|
||||
console.log("list:", list)
|
||||
comp.setData(list);
|
||||
}
|
||||
}
|
||||
|
||||
protected onClickClose(): void {
|
||||
this.node.active = false;
|
||||
}
|
||||
|
||||
public updateView():void{
|
||||
console.log("type:", this._type)
|
||||
ChatCommand.getInstance().chatHistory(this._type);
|
||||
}
|
||||
|
||||
protected onClickChat(): void {
|
||||
if (this._type == 0){
|
||||
ChatCommand.getInstance().chat(this.editConent.string, this._type);
|
||||
}else if (this._type == 1){
|
||||
let city:MapCityData = MapCommand.getInstance().cityProxy.getMyMainCity();
|
||||
if (city.unionId > 0){
|
||||
ChatCommand.getInstance().chat(this.editConent.string, this._type);
|
||||
}
|
||||
}
|
||||
this.editConent.string = "";
|
||||
}
|
||||
|
||||
protected onClickWorld(): void {
|
||||
this._type = 0;
|
||||
this.updateView();
|
||||
}
|
||||
|
||||
protected onClickUnion(): void {
|
||||
this._type = 1;
|
||||
this.updateView();
|
||||
}
|
||||
}
|
||||
11
assets/scripts/chat/ChatLogic.ts.meta
Normal file
11
assets/scripts/chat/ChatLogic.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "d26dd91f-18f3-47ec-a556-dcedf0c54493",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
78
assets/scripts/chat/ChatProxy.ts
Normal file
78
assets/scripts/chat/ChatProxy.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { _decorator } from 'cc';
|
||||
|
||||
export class ChatMsg {
|
||||
rid: number = 0;
|
||||
nick_name: string = "";
|
||||
type:number = 0;
|
||||
msg:string = "";
|
||||
time:number = 0;
|
||||
}
|
||||
|
||||
|
||||
export default class ChatProxy {
|
||||
|
||||
|
||||
private _worldMsgList:ChatMsg[] = [];
|
||||
private _unionMsgList:ChatMsg[] = [];
|
||||
|
||||
public clearData(): void {
|
||||
|
||||
}
|
||||
|
||||
public updateWorldChatList(data:any[]):void{
|
||||
this._worldMsgList = [];
|
||||
for(var i = 0; i < data.length;i++){
|
||||
var chat = new ChatMsg();
|
||||
chat.msg = data[i].msg;
|
||||
chat.rid = data[i].rid;
|
||||
chat.type = data[i].type;
|
||||
chat.time = data[i].time;
|
||||
chat.nick_name = data[i].nickName
|
||||
this._worldMsgList.push(chat);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public updateUnionChatList(data:any[]):void{
|
||||
this._unionMsgList = [];
|
||||
for(var i = 0; i < data.length;i++){
|
||||
var chat = new ChatMsg();
|
||||
chat.msg = data[i].msg;
|
||||
chat.rid = data[i].rid;
|
||||
chat.type = data[i].type;
|
||||
chat.time = data[i].time;
|
||||
chat.nick_name = data[i].nickName
|
||||
this._unionMsgList.push(chat);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public updateWorldChat(data:any):void{
|
||||
var chat = new ChatMsg();
|
||||
chat.msg = data.msg;
|
||||
chat.rid = data.rid;
|
||||
chat.type = data.type;
|
||||
chat.time = data.time;
|
||||
chat.nick_name = data.nickName
|
||||
this._worldMsgList.push(chat);
|
||||
}
|
||||
|
||||
public updateUnionChat(data:any):void{
|
||||
var chat = new ChatMsg();
|
||||
chat.msg = data.msg;
|
||||
chat.rid = data.rid;
|
||||
chat.type = data.type;
|
||||
chat.time = data.time;
|
||||
chat.nick_name = data.nickName
|
||||
this._unionMsgList.push(chat);
|
||||
}
|
||||
|
||||
|
||||
public getWorldChatList():ChatMsg[]{
|
||||
return this._worldMsgList;
|
||||
}
|
||||
|
||||
public getUnionChatList():ChatMsg[]{
|
||||
return this._unionMsgList;
|
||||
}
|
||||
}
|
||||
11
assets/scripts/chat/ChatProxy.ts.meta
Normal file
11
assets/scripts/chat/ChatProxy.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "1731b4bb-b7ac-4011-983f-2420dc454954",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user