UserModel.lua 28.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
cc.exports.UserModel={};

UserModel.isLogin = false;
UserModel.isTick = false;

UserModel.account = "";
UserModel.login_type = 0;
UserModel.where = 0;    --用户所在位置,EnumWhere.HALL  EnumWhere.ROOM_MULTI_GD EnumWhere.ROOM_SINGLE
UserModel.phone_num = "";
UserModel.wall = 0;
UserModel.furnitures = {};

UserModel.user_info = {uid=123,nick="未登录",head_frame=0,head_url=""};

UserModel.last_get_time_second = 0;
UserModel.time_second = os.time();
UserModel.exp = 0;  --单机场经验

UserModel.level_point = 2;  --当前牌级,本局打几
UserModel.isTrust = false;  --是否进入托管模式

UserModel.signs = {};
UserModel.signs[1] = 5;
UserModel.signs[2] = 7;
UserModel.role_reward = 1;
UserModel.role_reward_time = 0;
UserModel.first_pay_id = {};
UserModel.first_pay_daily_id = {};
UserModel.first_cycle_pay_id = {};
UserModel.achievement = {};

UserModel.online_reward = {};
UserModel.turntable_data = 0;
UserModel.relief_cnt = 0;
UserModel.user_rank_type_no = {};

UserModel.lobby_id = {};
UserModel.room_cfg = {};  --房间信息 押注底分,在线人数
UserModel.cur_lobby_id = 0;

local items = {};
local task_list = {};
local mail_list = {};
local score_map = {};
local win_map = {};
local lose_map = {};
local escape_map = {};
local user_info_map = {};

local function copyMailInfo(protoV)
    local info = {};
    info.idx = protoV.idx;   --邮件idx
    info.uid = protoV.uid;   --玩家id
    info.state = protoV.state;   --邮件状态(0:未读,1:已读 2.已领取)
    info.mail_id = protoV.mail_id;   --配置表中的mail_id
    info.attachment = protoV.attachment; --邮件附件, 依据mail_id来确定存储的protobuf
    info.bin_data = protoV.bin_data; --依据mail_id来确定存储的protobuf
    info.time = protoV.time; --邮件到期时间
    return info;
end

local function updateTimeFun()
    SocketClient:getInstance():send("gateway_msg.sys_information_msg", 
    {
    });

    if UserModel.isLogin and os.time() - UserModel.last_get_time_second > 30 then
        print("socket disconnect");
        EventListener.dispatchEvent(EnumEvent.SOCKET_DISCONNECT);
    end
end

function UserModel.init()
    cmsg.on("gateway_msg.login_msg_res",UserModel.onLoginSuccessFun);
    cmsg.on("gateway_msg.notify_game_data_msg_res",UserModel.onLoginGameDataFun);
    cmsg.on("gateway_msg.notify_item_msg_res",UserModel.onUpdateItems);
    cmsg.on("gateway_msg.notify_score_msg_res",UserModel.onUpdateScore);
    cmsg.on("gateway_msg.notify_task_daily_msg_res",UserModel.onUpdateTask);
    cmsg.on("gateway_msg.notify_mail_msg_res",UserModel.onUpdateMails);
    cmsg.on("gateway_msg.notify_win_cnt_msg_res",UserModel.onUpdateWin);
    cmsg.on("gateway_msg.notify_lost_cnt_msg_res",UserModel.onUpdateLost);
    cmsg.on("gateway_msg.get_event_msg_res",UserModel.onGetEventFun);
    cmsg.on("gateway_msg.sys_information_msg_res",UserModel.onUpdateTimeFun);
    cmsg.on("gateway_msg.use_item_msg_res",UserModel.onUseResFun);
    cmsg.on("gateway_msg.notify_item_cnt_max_msg_res",UserModel.onNotifyItemCntMaxResFun);
    cmsg.on("gateway_msg.get_online_reward_msg_res",UserModel.onOnlineRewardResFun);
    cmsg.on("gateway_msg.change_head_frame_msg_res",UserModel.onHeadFrameResFun);
    cmsg.on("gateway_msg.get_phone_num_msg_res",UserModel.onGetPhoneNumResFun);
    cmsg.on("gateway_msg.notify_achievement_msg_res",UserModel.onAchievementResFun);
    cmsg.on("gateway_msg.get_user_info_msg_res",UserModel.onGetUserInfoResFun);

    local exp = cc.UserDefault:getInstance():getStringForKey("exp");
    if exp and exp ~= "" then
        UserModel.exp = tonumber(exp);
    end
end

function UserModel.getUserInfo(uid)
    SocketClient:getInstance():send("gateway_msg.get_user_info_msg",{uid=uid});
end

function UserModel.onGetUserInfoResFun(params)
    local _user_info = params.data.user_info;
    user_info_map[tostring(_user_info.user_show.uid)] = _user_info;
    EventListener.dispatchEvent(EnumEvent.USER_INFO,_user_info);
end

function UserModel.onAchievementResFun(params)
    for i,v in ipairs(params.data.achievement) do
        local has_this_achievement = false;
        for ii,vv in ipairs(UserModel.achievement) do
            if v.type_id == vv.type_id then
                vv.get_reward_id = v.get_reward_id;
                vv.data = v.data;
                has_this_achievement = true;
            end
        end
        if not has_this_achievement then
            local obj = {};
            obj.type_id = v.type_id;
            obj.get_reward_id = v.get_reward_id;
            obj.data = v.data;
            UserModel.achievement[#UserModel.achievement+1] = obj;
        end
    end
end

function UserModel.onGetPhoneNumResFun(params)
    UserModel.phone_num = params.data.phone_num;
end

function UserModel.onHeadFrameResFun(params)
    if params.errcode == EnumErrorCode.EC_SUCCESS then
        UserModel.user_info.head_frame = params.data.head_frame;
        EventListener.dispatchEvent(EnumEvent.HEAD_FRAME_CHANGE_SUCCESS);
    else
        EffectManager.showFntPop("无法使用该头像框");
    end
end

function UserModel.onOnlineRewardResFun(params)
    if params.errcode == EnumErrorCode.EC_SUCCESS then
        UserModel.online_reward = {id=params.data.online_reward.id,time_sec=params.data.online_reward.time_sec};
    end
end

function UserModel.onUseResFun(params)
    if params.errcode == EnumErrorCode.EC_SUCCESS then
        local data = {uid=params.data.uid,peer_uid=params.data.peer_uid,item_id=params.data.item_id,item_cnt=params.data.item_cnt};
        EventListener.dispatchEvent(EnumEvent.USE_ITEM,data);
    elseif params.errcode == EnumErrorCode.EC_VALUE_INVALID then
        EffectManager.showFntPop(strings.msg_2012);
    elseif params.errcode == EnumErrorCode.EC_INEXISTENT_ITEM then
        EffectManager.showFntPop(strings.msg_2011);
    end
end

function UserModel.onNotifyItemCntMaxResFun(params)
    local item_list = params.data.item;
    if #item_list > 0 then
        local info = ItemInfo.getItemInfo(item_list[1].id);
        local msg = string.format(strings.msg_1025,info.name);
        EffectManager.showFntPop(msg);
    end
end

function UserModel.onUpdateTimeFun(params)
    UserModel.last_get_time_second = os.time();
    UserModel.time_second = params.data.time_second;
--    print("time_second:"..UserModel.time_second);
end

--道具更新
function UserModel.onUpdateItems(params)
    for i,v in ipairs(params.data.items) do
        if not items[tostring(v.id)] then
            --新增道具
            if UserModel.where ~= EnumWhere.ROOM_MULTI_XZDD and UserModel.where ~= EnumWhere.ROOM_PERSONAL_XZDD then
                if ItemInfo.isMedal(v.id) then
                    MedalItemManager.showPopMedal(v.id,v.cnt);
                else
                    ItemManager.addPopItem(v.id,v.cnt);
                end
            end
        else
            local num = v.cnt - items[tostring(v.id)];
            if num > 0 then
                --道具增加
                if UserModel.where ~= EnumWhere.ROOM_MULTI_XZDD and UserModel.where ~= EnumWhere.ROOM_PERSONAL_XZDD then
                    if ItemInfo.isMedal(v.id) then
                        MedalItemManager.showPopMedal(v.id,num);
                    else
                        ItemManager.addPopItem(v.id,num);
                    end
                end
            elseif num < 0 then
                --道具减少
            end
        end
        print("道具更新:"..v.id,v.cnt)
        items[tostring(v.id)] = v.cnt;
        if v.id == 100000 then
            EventListener.dispatchEvent(EnumEvent.UPDATE_COUPONS);
        elseif v.id == 100001 then
            EventListener.dispatchEvent(EnumEvent.UPDATE_COIN);
        elseif v.id == 100004 then
            EventListener.dispatchEvent(EnumEvent.UPDATE_CHARM);
        end
        EventListener.dispatchEvent(EnumEvent.UPDATE_ITEMS);
    end
end

function UserModel.onUpdateScore(params)
    score_map = {};
    for i,v in ipairs(params.data.score) do
        score_map[tostring(v.game_id)] = v.cnt;
    end
    EventListener.dispatchEvent(EnumEvent.UPDATE_SCORE);
end

function UserModel.onUpdateWin(params)
    win_map = {};
    for i,v in ipairs(params.data.win) do
        win_map[tostring(v.game_id)] = v.cnt;
    end
    EventListener.dispatchEvent(EnumEvent.UPDATE_WIN);
end

function UserModel.onUpdateLost(params)
    lose_map = {};
    for i,v in ipairs(params.data.lost) do
        lose_map[tostring(v.game_id)] = v.cnt;
    end
    EventListener.dispatchEvent(EnumEvent.UPDATE_LOST);
end

--任务更新
function UserModel.onUpdateTask(params)
    local info;
    for i,v in ipairs(task_list) do
        if v.game_id == params.data.game_id and v.id == params.data.type then
            info = v;
        end
    end
    if not info then
        info = {};
        info.step = 0;
        task_list[#task_list+1] = info;
    end
    info.id = params.data.type;
    info.param = params.data.param;
    EventListener.dispatchEvent(EnumEvent.UPDATE_TASK);
end

--读取邮件
function UserModel.readMail(idx)
    for ii=#mail_list,1,-1 do
        if mail_list[ii].idx == idx then
            mail_list[ii].state = 1;
            break;
        end
    end
    EventListener.dispatchEvent(EnumEvent.UPDATE_MAIL);
    SocketClient:getInstance():send("gateway_msg.mail_read_msg", 
    {
            idx = idx,
    });
end

--领取附件
function UserModel.getMailAttachment(idx)
    for ii=#mail_list,1,-1 do
        if mail_list[ii].idx == idx then
            mail_list[ii].state = 2;
            break;
        end
    end
    SocketClient:getInstance():send("gateway_msg.mail_attachment_msg", 
    {
            idx = idx,
    });
end

--删除邮件
function UserModel.deleteMails(idxList)
    for i,v in ipairs(idxList) do
        for ii=#mail_list,1,-1 do
            if mail_list[ii].idx == v then
                table.remove(mail_list,ii);
                break;
            end
        end
    end
    SocketClient:getInstance():send("gateway_msg.mail_del_msg", 
    {
            idx = idxList,
    });
end

--使用道具
function UserModel.useItem(item_id,peer_uid,game_id,item_cnt,timeoutCallBack)
    local function timeoutFn(params)
        if timeoutCallBack then
            timeoutCallBack(params);
        end
    end
    
    SocketClient:getInstance():send("gateway_msg.use_item_msg", 
    {
            item_id = item_id,
            peer_uid = peer_uid,
            game_id = game_id,
            item_cnt = item_cnt,
    },timeoutFn);
end

--使用道具
function UserModel.useHeadFrame(head_frame,timeoutCallBack)
    local function timeoutFn(params)
        if timeoutCallBack then
            timeoutCallBack(params);
        end
    end
    
    SocketClient:getInstance():send("gateway_msg.change_head_frame_msg", 
    {
            head_frame = head_frame,
    },timeoutFn);
end

function UserModel.onUpdateMails(params)
    local mail = params.data.mail;
    local hasMail = false;
    for ii=#mail_list,1,-1 do
        if mail_list[ii].idx == mail.idx then
            local info = copyMailInfo(mail);
            mail_list[ii] = info;
            hasMail = true;
            break;
        end
    end
    if not hasMail then
        local info = copyMailInfo(mail);
        logE("add new mail:"..mail.mail_id);
        mail_list[#mail_list + 1] = info;
    end
end

function UserModel.setTaskStep(game_id,taskID,step)
    local info;
    for i,v in ipairs(task_list) do
        if v.game_id == game_id and v.id == taskID then
            info = v;
        end
    end
    if not info then
        info = {};
        info.id = taskID;
        info.step = tonumber(step);
        info.param = 0;
        task_list[#task_list+1] = info;
    end
    info.step = step;
    EventListener.dispatchEvent(EnumEvent.UPDATE_TASK);
end

local account,session;
--先连接,然后登录服务器
function UserModel.login(host,port,_account,_session)
    account = _account;
    session = _session;
    SocketClient:getInstance():connect(host,port,UserModel.onConnectSuccess,UserModel.onConnectFaild);
end

--连接服务器成功
function UserModel.onConnectSuccess(params)
    logUI("on connect success");
    SocketClient:getInstance():send("gateway_msg.login_msg", 
    {
            platform = PLATFORM,
            account = account,
            session = session,
    });
end

function UserModel.onConnectFaild(params)
    logUI("on connect faild");
    EventListener.dispatchEvent(EnumEvent.LOGIN_FAILD,params);
end

function UserModel.createRoleCallback(params)
    cmsg.off("gateway_msg.create_role_msg_res",UserModel.createRoleCallback);
    dprint("创建角色成功");
end

function UserModel.onLoginSuccessFun(params)
    if params.errcode == EnumErrorCode.EC_SUCCESS then
        if params.data.has_role == 0 then
            dprint("没有角色,创建一个角色");
            local arr = HeadInfo.freeHeadList();
            local headInfo = arr[math.random(1,#arr)];
            cmsg.on("gateway_msg.create_role_msg_res",UserModel.createRoleCallback);
            SocketClient:getInstance():send("gateway_msg.create_role_msg", 
            {
--                    nick = account,
                    head = headInfo.id,
            });
        else
            dprint("登录成功");
            UserModel.isLogin = true;
            UserModel.user_info.uid = params.data.user_show.uid;    --用户ID
            UserModel.user_info.nick = params.data.user_show.nick;  --昵称
--            UserModel.user_info.nick = "玩家玩家玩家";
            UserModel.user_info.head = params.data.user_show.head;    --头像
            UserModel.user_info.head_frame = params.data.user_show.head_frame;
--            UserModel.user_info.head_url = "http://wx.qlogo.cn/mmopen/vi_32/2md7cCAEqIetd5ra60uwcibrejGRicnftic7xzibkClQEHO2mdsFNy3LAdJcn5k40jfmsol4ibQqYdYI3AqsZ6cGibXQ/96";
            UserModel.user_info.head_url = params.data.user_show.head_url;
            UserModel.user_info.head_data = params.data.user_show.head_data;
            UserModel.account = account;
            dprint("account-----------------",UserModel.account);

            UserModel.relief_cnt = params.data.relief_cnt;

            cc.UserDefault:getInstance():setStringForKey("Account", account);

            items = {};
            for i,v in ipairs(params.data.items) do
                items[tostring(v.id)] = v.cnt;
            end

            task_list = {};
            for i,v in ipairs(params.data.task_daily) do
                local info = {};
                info.id = v.type;
                info.step = v.step_id;
                info.param = v.param;
                task_list[#task_list+1] = info;
            end

            mail_list = {};
            for i,v in ipairs(params.data.mail) do
                local info = copyMailInfo(v);
                mail_list[i] = info;
            end

            UserModel.signs = params.data.sign;     --签到信息
            UserModel.sign_reward = TableUtil.copyTab(params.data.sign_reward);    --已领取的签到奖励
            UserModel.role_reward = params.data.role_reward;    --已领取的新手角色奖励
            UserModel.role_reward_time = params.data.role_reward_time;

            UserModel.time_second = params.data.time_second;

            UserModel.create_reward = params.data.create_reward;

            UserModel.pay_day = {}
            for i,v in ipairs(params.data.pay_day) do
                UserModel.pay_day[i] = {key=v.key,val1=v.val1,val2=v.val2};
            end

            UserModel.first_pay_id = TableUtil.copyTab(params.data.first_pay_id);
            UserModel.first_pay_daily_id = TableUtil.copyTab(params.data.pay_daily_id);
            UserModel.first_cycle_pay_id = TableUtil.copyTab(params.data.first_cycle_pay_id);

            UserModel.achievement = {};
            for i,v in ipairs(params.data.achievement) do
                local obj = {};
                obj.type_id = v.type_id;
                obj.get_reward_id = v.get_reward_id;
                obj.data = v.data;
                UserModel.achievement[#UserModel.achievement+1] = obj;
            end

            UserModel.last_get_time_second = os.time();
            UserModel.update_time_schedule_id = cc.Director:getInstance():getScheduler():scheduleScriptFunc(updateTimeFun, 5, false);

            --在线礼包奖励
            UserModel.online_reward = {id=params.data.online_reward.id,time_sec=params.data.online_reward.time_sec};
            local function updateOnlineRewardFun()
                if UserModel.online_reward.time_sec - 1 >= 0 then
                    UserModel.online_reward.time_sec = UserModel.online_reward.time_sec - 1;
                else
                    UserModel.online_reward.time_sec = 0;
                end
            end
            if UserModel.online_reward_schedule_id then
                cc.Director:getInstance():getScheduler():unscheduleScriptEntry(UserModel.online_reward_schedule_id);
                UserModel.online_reward_schedule_id = nil;
            end
            UserModel.online_reward_schedule_id = cc.Director:getInstance():getScheduler():scheduleScriptFunc(updateOnlineRewardFun,1, false);

            UserModel.wall = params.data.wall;
            for i,v in ipairs(params.data.furnitures) do
                print("---------------furnitures:",v.id,v.x,v.y);
                UserModel.furnitures[i] = {id=v.id,x=v.x,y=v.y};
            end

            SocketClient:getInstance():send("gateway_msg.get_event_msg", 
            {
                    type = 49000001,
                    id = GAME_ID
            });
	        UmengHelper.onProfileSignIn(UserModel.account,nil);

            EventListener.dispatchEvent(EnumEvent.LOGIN_SUCCESS,params);
        end
    elseif params.errcode == EnumErrorCode.EC_LOGIN_T then
        local function okFun()
            cc.Director:getInstance():endToLua();
        end
        local function cancelFun()
            cc.Director:getInstance():endToLua();
        end
        UserModel.isTick = true;
        Alert.showOne(strings.msg_1005,okFun,cancelFun);
    end
end

--断线重连数据
function UserModel.onLoginGameDataFun(params)
    local in_game_reconnect_data = params.data.in_game_reconnect_data;
    print("reconnect game data:"..in_game_reconnect_data.lobby_id);

    UserModel.room_cfg = in_game_reconnect_data.room_cfg;
    UserModel.cur_lobby_id = in_game_reconnect_data.lobby_id;
    local game_id = math.floor(in_game_reconnect_data.lobby_id/100000);
    GAME_ID = game_id;
    if game_id == EnumGame.XZDD or game_id == EnumGame.XZ_HUAN_3 or game_id == EnumGame.XZ_HUAN_4 then
        require "core.mahjong.MahjongTool";
        require("app.model.MahjongRoomModel");

        SCALE_MJ_NORMAL = 1;   --普通牌显示的缩放值

        MahjongRoomModel.addListeners();
        MahjongRoomModel.enterReconnectRoom(in_game_reconnect_data);
    elseif game_id == EnumGame.GD then
        require "core.poker.PokerTool";
        require("app.model.RoomModel");

        SCALE_POKER_NORMAL = 1;   --普通牌显示的缩放值
        SCALE_POKER_SHOT = 0.8;     --出牌显示的缩放值

        RoomModel.addListeners();
        RoomModel.enterReconnectRoom(in_game_reconnect_data);
    elseif game_id == EnumGame.NN then
        
    elseif game_id == EnumGame.DDZ then
        require "core.ddz.DdzPokerTool";
        require("app.model.DdzModel");

        SCALE_POKER_NORMAL = 1.5;   --普通牌显示的缩放值
        SCALE_POKER_SHOT = 0.8;     --出牌显示的缩放值

        DdzModel.addListeners();
        DdzModel.enterReconnectRoom(in_game_reconnect_data);
    end
end

function UserModel.onGetEventFun(params)
    if UserModel.exp == 0 then
        if params.data.event.data == 0 then
            UserModel.exp = 0;
        else
            UserModel.exp = params.data.event.data-1000000000;
        end
        cc.UserDefault:getInstance():setStringForKey("exp", UserModel.exp);
    end
end

function UserModel.getTaskData(id)
    local info;
    for i,v in ipairs(task_list) do
        if v.id == id then
            info = v;
        end
    end
    if not info then
        info = {};
        info.id = id;
        info.step = 0;
        info.param = 0;
    end
    return info;
end

function UserModel.getTaskList()
    return task_list;
end

function UserModel.hasTaskReward()
    local b = false;
    local _list = UserModel.getTaskList();
    for i,data in ipairs(_list) do
        local currentStep = nil;
        local info = TaskInfo.getInfoByGameIDAndTaskID(data.game_id,data.id)
        local currentStep = nil;
        for ii,vv in ipairs(info.steps) do
            if tonumber(vv.id) <= data.step then
                --步骤奖励已领取
            else
                currentStep = vv;
                local currentProgress = tonumber(currentStep.id);
                if data.param >= currentProgress then
                    b = true;
                end
                break;
            end
        end
    end
    return b;
end

function UserModel.hasRankReward()
    local b = false;
    for i,v in ipairs(UserModel.user_rank_type_no) do
        if v.receive == 0 then
            b = true;
            break;
        end
    end
    return b;
end

function UserModel.getMailList()
    local _list = {};
    for i,v in ipairs(mail_list) do
        local mailInfo = MailInfo.getMailInfo(v.mail_id);
        if mailInfo.id > 100000 then
            _list[#_list+1] = v;
        end
    end
    return _list;
end

function UserModel.getFightRecordList(_game_id)
    local _list = {};
    for i,v in ipairs(mail_list) do
        local mailInfo = MailInfo.getMailInfo(v.mail_id);
        if mailInfo.id == _game_id then
            _list[#_list+1] = v;
        end
    end
    return _list;
end

function UserModel.hasNewMail()
    local hasNewMail = false;
    for i,v in ipairs(mail_list) do
        local mailInfo = MailInfo.getMailInfo(v.mail_id);
        if mailInfo.id > 100000 then
            if v.state == 0 then
                hasNewMail = true;
                break;
            end
        end
    end
    
    return hasNewMail;
end

function UserModel.getCoupons()
    return UserModel.getItemNum(100000);
end

function UserModel.getCoin()
    return UserModel.getItemNum(100001);
end

function UserModel.getCharm()
    local num = 1000000000;
    local itemnum = items[tostring(100004)];
    if itemnum and itemnum > 0 then
        num = itemnum;
    end
    return num - 1000000000;
end

function UserModel.getScore(game_id)
    game_id = game_id or 1;
    local num = 1000000000;
    local cnt = 0;
    if game_id ~= 0 then
        cnt = score_map[tostring(game_id)];
    else
        for i,v in pairs(score_map) do
            cnt = cnt + v;
        end
    end
    if cnt then
        num = cnt;
    end
    return num-1000000000;
end

function UserModel.getWinNUM(game_id)
    game_id = game_id or GAME_ID;
    local num = 0;
    local cnt = 0;
    if game_id ~= 0 then
        cnt = win_map[tostring(game_id)];
    else
        for i,v in pairs(win_map) do
            cnt = cnt + v;
        end
    end
    if cnt then
        num = cnt;
    end
    return num;
end

function UserModel.getLoseNUM(game_id)
    game_id = game_id or GAME_ID;
    local num = 0;
    local cnt = 0;
    if game_id ~= 0 then
        cnt = lose_map[tostring(game_id)];
    else
        for i,v in pairs(lose_map) do
            cnt = cnt + v;
        end
    end
    if cnt then
        num = cnt;
    end
    return num;
end

function UserModel.getEscapeNUM(game_id)
    game_id = game_id or GAME_ID;
    local num = 0;
    local cnt = 0;
    if game_id ~= 0 then
        cnt = escape_map[tostring(game_id)];
    else
        for i,v in pairs(escape_map) do
            cnt = cnt + v;
        end
    end
    if cnt then
        num = cnt;
    end
    return num;
end

--获取拥有道具数量
function UserModel.getItemNum(id)
    if id == 100004 then
        return UserModel.getCharm();
    end

    local num = 0;
    local cnt = items[tostring(id)];
    if cnt then
        num = cnt;
    end
    return num;
end


--kind 1墙纸  2.墙上 3.地上
function UserModel.getFurnitureListByKind(_kind)
    local list = {};
    for i,v in ipairs(UserModel.furnitures) do
        if _kind == 2 and v.id > 910000 and v.id < 920000 then
            --墙上家具
            list[#list+1] = v;
        elseif _kind == 3 and v.id > 920000 and v.id < 930000 then
            --地上家具
            list[#list+1] = v;
        end
    end

    if _kind == 1 then
        --墙纸从items道具背包里取
        for i,v in pairs(items) do
            local id = tonumber(i);
            if v > 0 and id > 700000 and id < 800000 then
                list[#list+1] = {id=id,x=0,y=0};
            end
        end
    end

--    for i=1,10 do
--        list[#list+1] = list[1];
--    end

    return list;
end

function UserModel.getKindByFurnitureID(_id)
    if _id > 700000 and _id < 800000 then
        return 1;
    elseif _id > 910000 and _id < 920000 then
        return 2;
    elseif _id > 920000 and _id < 930000 then
        return 3;
    end
end

function UserModel.addExp(value)
    UserModel.exp = UserModel.exp + value;
    cc.UserDefault:getInstance():setStringForKey("exp", UserModel.exp);

    local event = {type=49000001,id=GAME_ID,data=UserModel.exp+1000000000};
    SocketClient:getInstance():send("gateway_msg.update_event_msg", 
    {
            event = event,
    });
end

function UserModel.getVipByID(id)
    local info = nil;
    for i,v in ipairs(UserModel.pay_day) do
        if v.key == id then
            info = v;
            break;
        end
    end
    return info;
end

function UserModel.setVipDayTime(id,time)
    for i,v in ipairs(UserModel.pay_day) do
        if v.key == id then
            v.val1 = time;
        end
    end
end

function UserModel.getLeftVipDay()
    local vipList = ShopInfo.getVipList();
    local vipInfo = vipList[1];
    local data = UserModel.getVipByID(vipInfo.id);
    local day = 0;
    if data then
        day = math.floor((data.val2-UserModel.time_second)/(24*3600)) + 1;
        if data.val1 > 0 and TimeUtil.isToday(UserModel.time_second,data.val1) then
            day = day - 1;
        end
        if day < 0 then
            day = 0;
        end
    end
    return day;
end

function UserModel.addVip(id)
    local data = UserModel.getVipByID(id);
    local shopInfo = ShopInfo.getPayInfo(id);
    local day = tonumber(shopInfo.day);
    if not data then
        local today = os.date("*t",UserModel.time_second);
        local secondOfToday = os.time({day=today.day, month=today.month,year=today.year, hour=0, min=0, sec=0});

        local obj = {key=id,val1=0,val2=secondOfToday+day*(24*3600)-1};
        table.insert(UserModel.pay_day,obj);
    else
        data.val2 = data.val2 + day*(24*3600);
    end
    EventListener.dispatchEvent(EnumEvent.UPDATE_VIP);
end

function UserModel.addFirstPayID(id)
    if not TableUtil.IsInTable(UserModel.first_pay_id,id) then
        table.insert(UserModel.first_pay_id,id);
    end
    EventListener.dispatchEvent(EnumEvent.UPDATE_FIRST_PAY);
end

function UserModel.addFirstPayDailyID(id)
    if not TableUtil.IsInTable(UserModel.first_pay_daily_id,id) then
        table.insert(UserModel.first_pay_daily_id,id);
    end
    EventListener.dispatchEvent(EnumEvent.UPDATE_FIRST_PAY);
end

function UserModel.addFirstCyclePayID(id)
    if not TableUtil.IsInTable(UserModel.first_cycle_pay_id,id) then
        table.insert(UserModel.first_cycle_pay_id,id);
    end
    EventListener.dispatchEvent(EnumEvent.UPDATE_FIRST_CYCLE_PAY);
end

function UserModel.getStringUID()
    local str_hi = math.floor(UserModel.user_info.uid/100000000000);
    local str_low = StringUtil.toStringFixedZeroPrefix(tostring(UserModel.user_info.uid%100000000000),11);
    return str_hi..str_low;
end

function UserModel.getNormalRoomList()
    local _list = {};
    for i,v in ipairs(UserModel.room_cfg) do
        if v.score > 1 then
            _list[#_list + 1] = v;
        end
    end
    return _list;
end

function UserModel.getFightRecordList(_game_id)
    local _list = {};
    for i,v in ipairs(mail_list) do
        local mailInfo = MailInfo.getMailInfo(v.mail_id);
        if mailInfo.id == _game_id then
            _list[#_list+1] = v;
        end
    end
    return _list;
end

function UserModel.getAchievementNum()
    local num = 0;
    for i,v in ipairs(UserModel.achievement) do
        local achievement_info = AchievementInfo.getAchievementListByID(v.type_id);
        for ii,vv in ipairs(achievement_info.list) do
            if v.data >= tonumber(vv.data) then
                num = num + 1;
            end
        end
    end
    return num;
end

function UserModel.getAchievementByID(achievement_id)
    for i,v in ipairs(UserModel.achievement) do
        if v.type_id == achievement_id then
            return v;
        end
    end
    return nil;
end

function UserModel.hasAchivementReward()
    local b = false;
    for i,achievement_data in ipairs(UserModel.achievement) do
        print("achievement_data.type_id",achievement_data.type_id)
        local achievement_info = AchievementInfo.getAchievementListByID(achievement_data.type_id);
        for ii,vv in ipairs(achievement_info.list) do
            if achievement_data.data >= vv.data and not TableUtil.IsInTable(achievement_data.get_reward_id,ii) then
                b = true;
            end
        end
    end
    return b;
end

return UserModel