function brataSubmit($msg, $team, $station, $stationType) { $isCorrect = false; $challengeComplete = false; $json = $team->getChallengeData(); trace("the jsons is" . print_r($json, true)); $this->rs['_1st_on'] = $json['hmb_vibration_pattern_ms'][0]; $this->rs['_1st_off'] = $json['hmb_vibration_pattern_ms'][1]; $this->rs['_2nd_on'] = $json['hmb_vibration_pattern_ms'][2]; $this->rs['_2nd_off'] = $json['hmb_vibration_pattern_ms'][3]; $this->rs['_3rd_on'] = $json['hmb_vibration_pattern_ms'][4]; $this->rs['_3rd_off'] = $json['hmb_vibration_pattern_ms'][5]; // $this->rs['cycle'] = $json['hmb_vibration_pattern_ms'][6]; $count = $team->get('count'); $points = 1; // 1 for showing up $team->set('count', $count + 1); $team->updateHMBScore(0); // save count $isCorrect = $this->testSolution($msg); $challengeComplete = $count + 1 < 3 ? false : true; if ($challengeComplete) { $points = 2; } if ($isCorrect) { $points = 3; } $rpi = RPI::getFromStationId($station->get('OID')); if ($rpi != null) { $json = $rpi->handle_hmb_submission($stationType->get('delay'), $isCorrect, $challengeComplete); } else { trace("RPI null not allowed for HMB configuration see admin for DB integrity check.", __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(500, "MS DB configuration is suspect as HMB requires an RPI."); } trace("count before update = " . $count); $team->updateHMBScore($points); if ($challengeComplete) { $station->endChallenge(); $team->endChallenge(); } if (Event::createEvent(Event::TYPE_SUBMIT, $team, $station, $points) === false) { trace("can't create event object", __FILE__, __LINE__, __METHOD__); throw new InternalError("Can't create event object"); } if ($isCorrect) { $msg = $stationType->get('success_msg'); } else { if ($count >= 2) { $msg = $stationType->get('failed_msg'); } else { $msg = $stationType->get('retry_msg'); } } //$msg = $team->expandMessage($msg, $parms ); // nothing to expand $msg = $team->encodeText($msg); return $msg; }
function _join($stationTag = null) { try { if ($stationTag === false) { trace("stationTag not present", __FILE__, __LINE__, __METHOD__); throw new RestException(400, "stationTag not present"); } $station = Station::getFromTag($stationTag); if ($station === false) { trace("_join station not found stationTag=" . $stationTag, __FILE__, __LINE__, __METHOD__); throw new RestException(500, "station not found stationTag=" . $stationTag); } trace("tag=" . $stationTag . " station OID=" . $station->get('OID'), __FILE__, __LINE__, __METHOD__); $json = json_getObjectFromRequest("POST"); // if ($json === NULL) return; json_checkMembers("message_version,station_type,station_url", $json); $rpi = RPI::getFromStationId($station->get('OID')); if ($rpi === false) { // output $rpi = new RPI(); $rpi->set('stationId', $station->get('OID')); $rpi->set_contact_data($stationTag, $json); if ($rpi->create() === false) { trace("create failed", __FILE__, __LINE__, __METHOD__); throw new RestException(500, "join create failed"); } } else { $rpi->set_contact_data($stationTag, $json); if ($rpi->update() === false) { trace("update failed", __FILE__, __LINE__, __METHOD__); throw new RestException(500, "join update failed"); } } rest_sendSuccessResponse(202, "Accepted", "{$stationTag} has joined M"); } catch (RestException $e) { rest_sendBadRequestResponse($e->statusCode, $e->statusMsg); // doesn't return } }
function brataSubmit($msg, $team, $station, $stationType) { $isCorrect = false; $challengeComplete = false; $rpi = null; if ($stationType->get('hasrPI')) { $rpi = RPI::getFromStationId($station->get('OID')); if ($rpi === false) { trace("_start_challenge can't find RPI stationTag=" . $stationTag, __FILE__, __LINE__, __METHOD__); throw new InternalError("can't find RPI stationTag=" . $stationTag); } } $count = $team->get('count'); switch ($stationType->get('typeCode')) { case StationType::STATION_TYPE_CTS: break; case StationType::STATION_TYPE_FSL: break; case StationType::STATION_TYPE_HMB: $team->set('count', $count + 1); break; case StationType::STATION_TYPE_CPA: break; case StationType::STATION_TYPE_EXT: break; } $isCorrect = $this->testRpiSolution($team->getChallengeData()); $points = 1; // one for showing up if (!$isCorrect) { $challenge_complete = $count < 3 ? false : true; if ($challenge_complete) { $points = 2; // two points even if you fail out completely } } else { $challenge_complete = true; $points = 3; // full points as long as you eventually got it right trace('SUCCESS'); } $this->updateTeamScore($team, $points); // save score if ($challenge_complete) { $station->endChallenge(); $team->endChallenge(); } if (Event::createEvent(Event::TYPE_SUBMIT, $team, $station, $points) === false) { trace("can't create event object", __FILE__, __LINE__, __METHOD__); throw new InternalError("Can't create event object"); } if ($isCorrect) { $msg = $stationType->get('success_msg'); } else { if ($count >= 2) { $msg = $stationType->get('failed_msg'); } else { $msg = $stationType->get('retry_msg'); } } if ($stationType->get('typeCode') == StationType::STATION_TYPE_CPA) { // The CPA has 5 messages not 4 but new DB structure only supports 4 so shift if ($isCorrect) { $msg = $stationType->get('failed_msg'); } else { if ($count >= 2) { $msg = $stationType->get('retry_msg'); } else { $msg = "Miss! Try again!"; } } } // TODO this is broek from merge //$msg = $team->expandMessage($msg, $parms ); trace("isCorrect=" . $isCorrect . " challenge complete=" . $challenge_complete . " msg =" . $msg); trace("success_msg=" . $stationType->get('success_msg')); // TODO ineresing this is still needed afer the refactoring $msg = $team->encodeText($msg); return $msg; }
function _start_challengeOLD($stationTag = null) { if ($stationTag === null) { rest_sendBadRequestResponse(400, "missing station Tag"); // doesn't return } $station = Station::getFromTag($stationTag); if ($station === false) { rest_sendBadRequestResponse(404, "can find station stationTag=" . $stationTag); // doesn't return } $stationType = new StationType($station->get('typeId'), -1); if ($stationType === false) { trace("can't find station type stationTag = " . $stationTag, __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(500, "can't find station type stationTag=" . $stationTag); } if ($stationType->get('hasrPI')) { $rpi = RPI::getFromStationId($station->get('OID')); if ($rpi === false) { trace("_start_challenge can't find RPI stationTag=" . $stationTag, __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(500, "can't find RPI stationTag=" . $stationTag); } } else { $rpi = null; } $json = json_getObjectFromRequest("POST"); json_checkMembers("team_id,message", $json); $teamPIN = $json['team_id']; $team = Team::getFromPin($teamPIN); if ($team === false) { trace("_start_challenge can't find team teamPin=" . $teamPIN, __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(404, "team not found PIN=" . $teamPIN); // doesn't return } $stationId = $station->get('OID'); $parms = null; // compute challenge parameters into a php hash which will be sent to rPI and used to populate message sent to team $state = null; switch ($stationType->get('typeCode')) { case StationType::STATION_TYPE_CTS: $parms = CTSData::_startChallenge($stationId); break; case StationType::STATION_TYPE_HMB: $parms = HMBData::_startChallenge($stationId); break; case StationType::STATION_TYPE_CPA: $parms = CPAData::_startChallenge($stationId); break; case StationType::STATION_TYPE_FSL: $state = FSLData::_startChallenge($stationId); $parms = $state['msg_values']; break; case StationType::STATION_TYPE_EXT: $state = EXTData::_startChallenge($stationId); $parms = $state; break; } if ($rpi != null) { trace("sending to rPI"); $rpi->start_challenge($stationType->get('delay'), $parms); } trace("station and team start calls"); //TODO transaction $station->startChallenge($team); $team->startChallenge($parms, $stationType->get('typeCode'), $state); // $state if (Event::createEvent(Event::TYPE_START, $team, $station, 0, $state) === false) { trace("create event failed", __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(500, "database create failed"); } $msg = $team->expandMessage($stationType->get('instructions'), $parms); if ($GLOBALS['SYSCONFIG_ENCODE'] == 1) { // if not in student mode encode, if in student mode we only encrypt the even team numbers responses if ($GLOBALS['SYSCONFIG_STUDENT'] == 0 or $GLOBALS['SYSCONFIG_STUDENT'] == 1 and $teamPIN % 2 == 0) { $msg = $team->encodeText($msg); } } json_sendObject(array('message' => $msg)); }
function _submit($stationTag = null) { if ($stationTag === null) { trace("brata missing stationId", __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(400, "missing stationId"); // doesn't return } $station = Station::getFromTag($stationTag); if ($station === false) { trace("brata can't find station stationTag=" . $stationTag, __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(404, "can't find station stationTag=" . $stationTag); // doesn't return } $stationType = new StationType($station->get('typeId'), -1); if ($stationType === false) { trace("can't find station type stationTag = " . $stationTag, __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(500, "can't find station type stationTag=" . $stationTag); } if ($stationType->get('hasrPI')) { $rpi = RPI::getFromStationId($station->get('OID')); if ($rpi === false) { trace("_submit can't find RPI stationTag=" . $stationTag, __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(500, "can't find RPI stationTag=" . $stationTag); } } else { $rpi = null; } $json = json_getObjectFromRequest("POST"); // won't return if an error happens json_checkMembers("message,team_id", $json); $team = Team::getFromPin($json['team_id']); if ($team === false) { trace("can't find team from team " . $json['team_id']); rest_sendBadRequestResponse(404, "can't find team pin=" . $json['team_id']); } $count = $team->get('count'); $isCorrect = false; $challengeComplete = false; $matches = array(); // for regex matches switch ($stationType->get('typeCode')) { case StationType::STATION_TYPE_FSL: break; case StationType::STATION_TYPE_HMB: preg_match("/.*answer.*=.*(\\d)/", $json['message'], $matches); trace('calling handle_challenge'); $json = $rpi->handle_submission($stationType->get('delay'), $isCorrect, $challengeComplete); //TODO if ($json === false) ERROR??? //TODO json_checkMembers("message,team_id", $json); //$isCorrect=$json['is_correct']; break; case StationType::STATION_TYPE_EXT: $ext = new ExtData(1, -1); //TODO get from team $ext->submit($json['message'], $team); goto hack; } $count = $team->get('count'); $points = 3 - $count; $team->updateScore($stationType, $points); if (!$json['is_correct']) { $count++; $team->set('count', $count); $challenge_complete = $count < 3 ? false : true; } else { $challenge_complete = true; } if ($challenge_complete) { $station->endChallenge(); $team->endChallenge(); } if (Event::createEvent(Event::TYPE_SUBMIT, $team, $station, $points) === false) { trace("can't create event object", __FILE__, __LINE__, __METHOD__); json_sendBadRequestResponse(500, "Can't create event object"); } if ($isCorrect) { $msg = $stationType->get('success_msg'); } else { if ($count >= 3) { $msg = $stationType->get('failed_msg'); } else { $msg = $stationType->get('retry_msg'); } } $msg = $team->expandMessage($msg, $parms); if ($GLOBALS['SYSCONFIG_ENCODE'] == 1) { // if not in student mode encode, if in student mode we only encrypt the even team numbers responses if ($GLOBALS['SYSCONFIG_STUDENT'] == 0 or $GLOBALS['SYSCONFIG_STUDENT'] == 1 and $teamPIN % 2 == 0) { $msg = $team->encodeText($msg); } } hack: json_sendObject(array('message' => $msg)); }
function _submit($stationTag = null) { if ($stationTag === null) { trace("missing station id"); rest_sendBadRequestResponse(401, "missing stationId"); // doesn't return } $station = Station::getFromTag($stationTag); if ($station === false) { trace("can't find station from tag"); rest_sendBadRequestResponse(404, "can find station stationTag=" . $stationTag); // doesn't return } $team = new Team($station->get('teamAtStation'), -1); if ($team === false) { trace("can't find team", __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(404, "can find team at station stationTag=" . $stationTag . " teamId=" . $team->get('OID')); // doesn't return } $stationType = new StationType($station->get('typeId'), -1); if ($stationType === false) { trace("can't find station type stationTag = " . $stationTag, __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(500, "can't find station type stationTag=" . $stationTag); } $rpi = RPI::getFromStationId($station->get('OID')); if ($rpi === false) { trace("_start_challenge can't find RPI stationTag=" . $stationTag, __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(500, "can't find RPI stationTag=" . $stationTag); } $json = json_getObjectFromRequest("POST"); // won't return if an error happens json_checkMembers("candidate_answer,is_correct,fail_message", $json); $count = $team->get('count'); $team->setChallengeData($json); trace("count=" . $count); $points = 3 - $count; if (!$json['is_correct']) { $count++; $team->set('count', $count); trace("count=" . $count); $challenge_complete = $count > 2; $team->_updateScore($stationType, 0); //TODO add legacy update score } else { $challenge_complete = true; } if ($challenge_complete) { switch ($stationType->get('typeCode')) { case StationType::STATION_TYPE_HMB: // Leave scoring up to brata submit for CPA and CTS but HMB needs it $team->_updateScore($stationType, $points); $station->endChallenge(); $team->endChallenge(); break; default: // FSL does not have pi, CPA and CTS not done until scan last qr // so technically challenge not complete until the brata submit break; } } if (Event::createEvent(Event::TYPE_SUBMIT, $team, $station, $points) === false) { trace("create event failed " . $team->get('OID') . " " . $station->get('OID'), __FILE__, __LINE__, __METHOD__); rest_sendBadRequestResponse(500, "database create failed"); } $json = array("message_version" => 0, "message_timestamp" => date("Y-m-d H:i:s"), "theatric_delay_ms" => $stationType->get('delay'), "challenge_complete" => $challenge_complete); json_sendObject($json); }