<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/21
 * Time: 16:57
 */

namespace app\logic\cmdpro;

use app\lib\DataPack;
use app\lib\Wlog;
use app\logic\cmdpro\CmdInf as CmdInf;

class CmdSubser extends CmdInf
{
    public function index_msg($serv, $task)
    {
        go(function () use ($serv, $task) {
            $pack = json_decode($task->data->data, true);
            $msgarr = $pack['data'];
            if (!is_array($msgarr)) {
                return;
            }
            foreach ($msgarr as $msg) {
                $msg = DataPack::toJson(['mtype' => 'system_msg', 'stype' => 'index_msg', 'data' => $msg]);
                $this->onlineBroad($serv, $msg);
            }
        });
    }

    public function msg_board($serv, $task)
    {
        go(function () use ($serv, $task) {
            $pack = json_decode($task->data->data, true);
            $msgarr = $pack['data'];
            if (!is_array($msgarr)) {
                return;
            }
            foreach ($msgarr as $msg) {
                $stype = $msg['stype'];
                $data = $msg['data'];
                $msg = DataPack::toJson(['mtype' => 'system_msg', 'stype' => $stype, 'data' => $data]);
                $this->onlineBroad($serv, $msg);
            }
        });
    }

    public function msg_to_one_user($serv, $task)
    {
        go(function () use ($serv, $task) {
            $pack = json_decode($task->data->data, true);
            $msgarr = $pack['data'];
            if (!is_array($msgarr)) {
                return;
            }
            foreach ($msgarr as $msg) {
                $msgJson = DataPack::toJson($msg);
                $this->sendToUid($serv, $msg['to'], $msgJson);
            }
        });
    }

    public function do_tick($serv, $task)
    {
        $pack = json_decode($task->data->data, true);
        $datas = $pack['data'];
        if (!is_array($datas)) {
            Wlog::getInstance()->WriteLog("无效的数据格式dotick:" . $task->data->data);
            return;
        }
        foreach ($datas as $key => $val) {
            $uid = $val['uid'];
            $msg = $val['msg'];
            $fid = $serv->utable->get($uid, 'fid');
            if ($fid) {
                $this->sendTo($serv, $fid, $msg);
                $serv->disconnect($fid);
            }
        }
        return true;
    }


}