示例#1
0
 protected function process($sock, $message)
 {
     $pi = ParsedInstruction::parse($message);
     if (isset($pi->getData()["WATCH_CLIENT"]) && $pi->getData()["WATCH_CLIENT"] == 'true') {
         $this->send($sock, $this->proto->process_watch_instruction($pi, $sock));
         return;
     }
     $this->send($sock, $this->proto->process_instruction($pi, $sock));
 }
示例#2
0
 protected function process($sock, $message)
 {
     $pi = ParsedInstruction::parse($message);
     $this->send($sock, process_instruction($pi));
 }
    function process_instruction($pi, $sock)
    {
        //sock IS ONLY PROPAGATED TO OTHER FUNCTIONS!!!
        $instruction = $pi->getInstruction();
        $data = $pi->getData();
        $mysqli = new mysqli(HOST, USER, PASSWORD, SB_DATABASE);
        $for = "general";
        $other = "";
        switch ($instruction) {
            case 'NOTIFY':
                $addr = GetSafeValue($data, 'email');
                $cont = GetSafeValue($data, 'content');
                $success = mail($addr, 'QuizBowl Notification', $cont, 'From: notifications@quizbowl.us');
                break;
                //Make sure to use NodeJS if you decide to reprogram the server. I only used PHP because the original hosting plan didn't support NodeJS.//
                //Little did I know we would need the Virtual Server plan (which supports Node) anyway to support the Websocket server! This is the story of the birth of this program.//
            //Make sure to use NodeJS if you decide to reprogram the server. I only used PHP because the original hosting plan didn't support NodeJS.//
            //Little did I know we would need the Virtual Server plan (which supports Node) anyway to support the Websocket server! This is the story of the birth of this program.//
            case 'CREATE_GAME':
                $gnm = $mysqli->real_escape_string(GetSafeValue($data, 'room'));
                $comp_pwd = $mysqli->real_escape_string(GetSafeValue($data, 'password'));
                $access_id = $mysqli->real_escape_string(GetSafeValue($data, 'access_id'));
                $t1n = $mysqli->real_escape_string(GetSafeValue($data, 'Team1_Name'));
                $t2n = $mysqli->real_escape_string(GetSafeValue($data, 'Team2_Name'));
                if (empty($gnm) || empty($t1n) || empty($t2n)) {
                    return "ERROR\n" . "message: Insufficient data given to create game.";
                }
                $comp_rows = $mysqli->query("SELECT * FROM `competitions` WHERE access_id='{$access_id}'");
                if (!$comp_rows || !$comp_rows->num_rows) {
                    $output = "ERROR\n" . "message: Invalid Access ID.";
                    return $output;
                }
                $comp_row = $comp_rows->fetch_assoc();
                if (!password_verify($comp_pwd, $comp_row['pwd'])) {
                    $output = "ERROR\n" . "message: Invalid Password.";
                    return $output;
                }
                $comp = $comp_row['name'];
                $comp_id = $comp_row['cid'];
                $query = "INSERT INTO `{$access_id}` VALUES ( '{$t1n}', '{$t2n}', 0, 0, {$comp_id}, '{$gnm}', 0, '' )";
                $mysqli->query($query);
                if ($mysqli->error) {
                    return "ERROR\n" . "message: " . $mysqli->error;
                }
                $for = "login";
                $cnm = $comp_row['name'];
                $gid = $mysqli->insert_id;
                $other = "cid: {$comp_id}\n" . "gid: {$gid}\n" . "cnm: {$cnm}";
                $usepi = ParsedInstruction::parse("CREATE_GAME\n" . "gid:{$gid}\n" . "T1S:0\n" . "T2S:0\n" . "T1N:{$t1n}\n" . "T2N:{$t2n}\n" . "access_id:{$access_id}\n" . "finished:false");
                $this->process_watch_instruction($usepi, $sock);
                break;
            case 'RECOVER_GAME':
                $comp_pwd = $mysqli->real_escape_string(GetSafeValue($data, 'password'));
                $access_id = $mysqli->real_escape_string(GetSafeValue($data, 'access_id'));
                $gid = $mysqli->real_escape_string(GetSafeValue($data, 'gid'));
                if (empty($comp_pwd) || empty($access_id) || empty($gid)) {
                    return "ERROR\n" . "message: Insufficient data given to recover game.";
                }
                $comp_rows = $mysqli->query("SELECT * FROM `competitions` WHERE access_id='{$access_id}'");
                if (!$comp_rows || !$comp_rows->num_rows) {
                    $output = "ERROR\n" . "message: Invalid Access ID.";
                    return $output;
                }
                $comp_row = $comp_rows->fetch_assoc();
                if (!password_verify($comp_pwd, $comp_row['pwd'])) {
                    $output = "ERROR\n" . "message: Invalid Password.";
                    return $output;
                }
                $comp = $comp_row['name'];
                $comp_id = $comp_row['cid'];
                $query = "SELECT * FROM `{$access_id}` WHERE gid={$gid}";
                $resset = $mysqli->query($query);
                if (!$resset || !$resset->num_rows) {
                    return "ERROR\n" . "message: Game with GID {$gid} not found.";
                }
                $res = $resset->fetch_assoc();
                $t1s = $res['Team1_Score'];
                $t2s = $res['Team2_Score'];
                $t1n = $res['Team1_Name'];
                $t2n = $res['Team2_Name'];
                $gnm = $res['room'];
                $cnm = $comp_row['name'];
                $for = "recover";
                $other = "cid: {$comp_id}\n" . "gid: {$gid}\n" . "T1S: {$t1s}\n" . "T2S: {$t2s}\n" . "T1N: {$t1n}\n" . "T2N: {$t2n}\n" . "room: {$gnm}\n" . "cnm: {$cnm}";
                break;
            case 'SAVE_SCORE':
                $game = GetSafeValue($data, 'gid');
                $t1s = GetSafeValue($data, 'T1S');
                $t2s = GetSafeValue($data, 'T2S');
                $comp = GetSafeValue($data, 'comp');
                $pwd = GetSafeValue($data, 'comp_pwd');
                $rows = $mysqli->query("SELECT * FROM `competitions` WHERE cid={$comp}");
                if (!$rows) {
                    $output = "ERROR\n" . "message: Invalid Access ID or Password.";
                    return $output;
                }
                $row = $rows->fetch_assoc();
                $cname = $row['name'];
                $caid = $row['access_id'];
                if (empty($game) && $game !== "0" || empty($t1s) && $t1s !== "0" || empty($t2s) && $t2s !== "0") {
                    return ERR_INSUFFICIENT_DATA;
                }
                $query = "UPDATE `{$caid}` SET Team1_Score = {$t1s}, Team2_Score = {$t2s} WHERE gid={$game}";
                $mysqli->query($query);
                if ($mysqli->error) {
                    return "ERROR\nmessage: " . $mysqli->error;
                }
                $this->process_watch_instruction($pi, $sock);
                $for = "saving";
                break;
            case 'FINALIZE':
                $pwd = $mysqli->real_escape_string(GetSafeValue($data, 'pwd'));
                $gid = $mysqli->real_escape_string(GetSafeValue($data, 'gid'));
                $cid = $mysqli->real_escape_string(GetSafeValue($data, 'cid'));
                $row = $mysqli->query("SELECT * FROM `competitions` WHERE cid={$cid}");
                if ($mysqli->error) {
                    return "ERROR\n" . "message: " . $mysqli->error;
                }
                if (!$row) {
                    return "ERROR\n" . "message: No competition with that ID was found!";
                }
                $row = $row->fetch_assoc();
                //Makes things easier for us to just use the same variable here
                $password = $row['pwd'];
                $access_id = $row['access_id'];
                if (!password_verify($pwd, $password)) {
                    return "ERROR\n" . "message: Invalid password for competition\n#{$cid}; will not finalize scores.";
                }
                $query = "UPDATE `{$access_id}` SET finished=1 WHERE gid={$gid}\n";
                $mysqli->query($query);
                if ($mysqli->error) {
                    return "ERROR\n" . "message: " . $mysqli->error;
                }
                $this->process_watch_instruction($pi, $sock);
                $for = "finalizing";
                break;
            case 'ECHO':
                //Testing scenarios
                return GetSafeValue($data, 'data');
            default:
                return ERR_INVALID_INSTRUCTION . ': ' . $instruction;
        }
        return <<<OUTPUT
OK
for:{$for}
{$other}
OUTPUT;
    }