/** * Handle the event. * * @param \App\Models\Game $game * @param \App\WS\Connection $conn * @return void */ public function handle($game, $conn) { $game->delete(); foreach ($conn->clients() as $client) { $this->send($client, 'game.delete', $game); } }
/** * Handle the event. * * @param \App\Models\Game $game * @param \App\WS\Connection $conn * @return void */ public function handle($game, $user, $conn) { $game->load('countUsers'); foreach ($conn->clients() as $client) { $this->send($client, 'game.update', $game); } foreach ($conn->gameClients($game) as $client) { $this->send($client, 'game.left', $user); } }
/** * Loads game from session. * @return Game * @throws InvalidValueException */ public static function fromSession() { $session = Yii::$app->session->get('game'); if ($session === false) { throw new InvalidValueException('Saved game ID not found!'); } $game = new Game(); $game->load($session); return $game; }
public static function createSession($data) { $game = new Game(); $game->team_id = $data['team_id']; $game->author = $data['user_id']; $game->date = $data['date']; $game->topic = $data['topic']; $game->aims = $data['aims']; $game->save(); return $game; }
public static function createGame($userRole, $data) { $game = new Game(); $game->team_id = $userRole; $game->home_team = $data['home_team']; $game->away_team = $data['away_team']; $game->date = $data['date']; $game->location = $data['location']; $game->save(); return $game; }
function getCircleClass(Game $game) { if ($game->score1 == $game->score2) { return 'draw'; } elseif ($game->score1 < $game->score2 && $game->isHome()) { return 'loose'; } elseif ($game->score1 > $game->score2 && !$game->isHome()) { return 'loose'; } return 'win'; }
public function actionRegister($gameName, $port, $hostname = null) { $ip = ip2long(Yii::$app->request->userIP); $Game = Game::find()->where(['ip_address' => $ip, 'port' => $port])->one(); if (!$Game) { $Game = new Game(); $Game->name = $gameName; $Game->ip_address = $ip; $Game->port = $port; } return $Game->save(); }
public function actionUpdateGame() { $data = Yii::$app->request->post(); $ip = ip2long(Yii::$app->request->userIP); foreach ($data['Games'] as $game) { $Game = Game::find()->where(['id' => $game['id'], 'ip_address' => $ip])->one(); if (!$Game) { $Game = new Game(); } $Game->attributes = $game; $Game->ip_address = $ip; $Game->save(); } return true; }
/** * Create a new event instance. * * @return void */ public function __construct($user_id, $game_id) { $user = \App\Models\User::find($user_id); $game_users = \App\Models\Game_User::where('game_id', $game_id)->where('has_left', 0)->get(); for ($i = 0; $i < $game_users->count(); $i++) { if ($game_users[$i]['user_id'] == $user_id) { $game_users[$i]['have_turn'] = false; $game_users[$i]['cons_turn'] = true; $game_users[$i]->save(); if ($i + 1 == $game_users->count()) { $game_users[0]['have_turn'] = true; $game_users[0]->save(); } else { $game_users[$i + 1]['have_turn'] = true; $game_users[$i + 1]->save(); } } } $msg = 'El jugador ' . $user->first_name . ' paso turno.'; $cont = 0; foreach ($game_users as $key) { if ($key->cons_turn) { $cont++; } } if ($cont == $game_users->count()) { $game = \App\Models\Game::find($game_id); $game->ended = true; $game->save(); $msg = 'El juego a terminado.'; } $this->data = ['game_id' => $game_id, 'msg' => $msg]; }
public function postEditTeam(LoggedInRequest $request, $teamID) { // $teamKey = $request->input('teamKey'); $team = Team::findOrFail($teamID); $team->name = $request->input('teamName'); $team->abb = strtoupper($request->input('teamAbb')); if ($request->input('delete') == "delete") { $games = Game::all(); foreach ($games as $game) { if ($game->winner == $team->team_key || $game->loser == $team->team_key) { $game->delete(); } } $team->delete(); $request->session()->flash('msg', 'Team ' . $team->name . ' deleted!'); } else { if ($request->input('reset') == "reset") { $team->games = 0; $team->wins = 0; $request->session()->flash('msg', 'Team ' . $team->name . ' reset!'); $team->save(); } else { $request->session()->flash('msg', 'Team ' . $team->name . ' updated!'); $team->save(); } } return $this->getEditTeam($request); }
/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $user = $message->user(); $hash = $message->get('hash'); $game = Game::findByHash($hash); $game->load('users', 'allPieces'); $message->reply($game); }
public static function getAll() { // SQL $sql = "\n SELECT *\n FROM game"; // Execute $rows = DB::select($sql); $games = []; foreach ($rows as $row) { $games[] = Game::createGameFromRow($row); } return $games; }
public function getGame($winner, $loser) { $winner = Team::where("abb", $winner)->first(); if ($winner != null) { $winner->wins++; $winner->games++; $winner->save(); $winnerKey = $winner->team_key; } $loser = Team::where("abb", $loser)->first(); if ($loser != null) { $loser->games++; $loser->save(); $loserKey = $loser->team_key; } if ($winner != null && $loser != null) { $game = new Game(); $game->winner = $winnerKey; $game->loser = $loserKey; $game->save(); } return "Ok"; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Game::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Game::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->where(['>', 'updated_at', time() - Yii::$app->params['game_list_timeout']]); $query->andFilterWhere(['id' => $this->id, 'ip_address' => $this->ip_address]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'launch_url', $this->host_search]); return $dataProvider; }
private function parseStats(Game $game, Htmldom $html) { $table = $html->find('table.season-list', 0); if (!$table) { $this->error('No stats table for game ' . $game->id); return null; } $stats = []; $team_class = $game->isHome() ? 'home_event' : 'away_event'; foreach ($table->find('tr') as $row) { $statRow = $row->find('td.' . $team_class, 0); if ($statRow) { array_push($stats, $this->parseAndDetermineStat($statRow)); } } return $stats; }
/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { if ($message->user()->activeGame()) { return $message->reply(['You\'ve already started a game.'], 422); } $this->validate($message, ['name' => 'required|between:2,100', 'players' => 'required|numeric|min:2|max:4', 'matches' => 'required|numeric|min:1|max:4', 'points' => 'required|numeric|min:10']); $game = Game::create(['hash' => Str::random(32), 'name' => $message->get('name'), 'players' => $message->get('players'), 'matches' => $message->get('matches'), 'points' => $message->get('points'), 'user_id' => $message->user()->id]); // Add the current user to the game. $game->addUser($message->user()); $game->load('user', 'countUsers'); // Send response the the user that created the game. $message->reply($game, 201); // Notify all users of a new game. foreach ($conn->clients() as $client) { $this->send($client, 'game.new', $game, 201); } }
/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $game = Game::findByHash($message->get('hash')); $started = $message->user()->startedGame(); if ($game->id != $started->id) { return $message->reply('Game not found.', 404); } if ($game->status != 'started') { return $message->reply('Game not started.', 422); } $game->load('users', 'pieces'); if (!$game->player_turn) { $this->generatePieces($game); } // Current player pieces. $pieces = json_decode($game->users->find($message->user()->id)->pivot->pieces, true); $player_turn = $game->player_turn; $message->reply(compact('game', 'pieces', 'player_turn')); }
public function store(Request $request) { $request->merge(['slug' => $this->createSlugFromRequest($request)]); $v = Validator::make($request->all(), ['slug' => 'required|min:3|unique:Games', 'capacity' => 'required|integer|min:' . config('game.capacity.min') . '|max:' . config('game.capacity.max'), 'spec_capacity' => 'required|integer|min:' . config('game.spec_capacity.min') . '|max:' . config('game.spec_capacity.max')]); $v->sometimes('password', 'min:6', function () { return Auth::check(); }); $this->validateWith($v); if (Auth::check()) { $data = $request->all(); if ($data['password']) { $data['private'] = true; } } else { $data = $request->only(['name', 'slug', 'capacity', 'spec_capacity']); } $game = Game::create($data); return redirect()->route('game.show', [$game]); }
public function clearAll() { $users = User::all(); foreach ($users as $u) { $u->delete(); } $drills = Drill::all(); foreach ($drills as $d) { $d->delete(); } $games = Game::all(); foreach ($games as $g) { $g->delete(); } $clubs = Club::all(); foreach ($clubs as $c) { $c->delete(); } }
/** * Execute the console command. * * @return mixed */ public function handle() { $this->startLog(); $date = Carbon::now(); $gameList = Game::where('date', '>=', $date->format('Y-m-d 00:00:00'))->where('date', '<=', $date->addDays(self::PERIOD)->format('Y-m-d 23:59:59'))->where('reminder', Game::MSG_NOT_SENT)->get(); if ($gameList->count() < 1) { $this->error('No games to remind'); exit; } foreach ($gameList as $game) { $msg = $this->getMsg($game); $result = sendVkMsg($msg); if (preg_match('/"response":[0-9]+/', $result)) { $game->reminder = Game::MSG_SENT; $game->save(); $this->comment($result); $this->comment('Sent msg for game: ' . $game->id); } } $this->endLog(); }
/** * Handle the event. * * @param GameVisitChange $event * @return void */ public function handle(GameVisitChange $event) { $player = Player::find($event->playerId); $game = Game::find($event->gameId); $users = \App\Models\User::whereNotNull('email')->where('email', '!=', '')->get(); $visitList = Stat::$visitList; if (isset($event->visit) && $event->visit) { $status = $visitList[$event->visit]; } else { $status = 'Cancelled'; } $subject = str_limit($player->name, 50) . ' ' . str_limit($game->team, 20) . ' ' . $status; Mail::send('emails.game_visit', ['player' => $player, 'game' => $game, 'status' => $status], function ($m) use($users, $subject) { foreach ($users as $user) { if ($user->isAdmin()) { $m->to($user->email); } } $m->subject($subject); }); }
/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $game = Game::findByHash($message->get('hash')); $game->load('users', 'countUsers'); if ($started = $message->user()->activeGame()) { if ($game->id != $started->id) { return $message->reply('You\'ve already started a game.', 422); } $message->reply($game); } else { if ($game->users->count() == $game->players) { return $message->reply('Game already full.', 422); } $message->reply($game); $game->addUser($message->user()); event('game.joined', [$game, $message->user(), $conn]); if ($game->users->count() == $game->players) { $game->status = 'started'; $game->save(); } } }
public function games() { return Game::whereHas('places', function ($query) { $query->where('user_id', $this->id); })->get(); }
/** * Display a list of all of the user's task. * * @param Request $request * @return Response */ public function index(Request $request) { return view('lots.index', ['games' => Game::take(20)->get()]); }
<?php use app\models\Game; $game = array_column(Game::find()->select(['id'])->asArray()->all(), 'id'); return ['type' => $faker->randomElement($array = ['audio', 'photo', 'group', 'video']), 'url' => $faker->url, 'content_name' => $faker->word, 'game_id' => $faker->randomElement($game), 'has_been_played' => 0];
/** * Defines many-to-many relation with game model via table game_has_player * @return list of games where user has marked his presence */ public function getGames() { return $this->hasMany(Game::className(), ['id' => 'game_id'])->viaTable('game_has_player', ['player_id' => 'id'])->select('*, (SELECT presence FROM game_has_player WHERE player_id=' . $this->id . ' AND game_id=games.id LIMIT 1) as presence'); }
public static function has_ended($game_id) { $game = \App\Models\Game::find($game_id); if ($game->ended) { return true; } return false; }
public function setUserId($game_id, $user_id) { Game::where('$id', $game_id)->update(['user_id' => $user_id]); }
/** * Execute the console command. * * @return mixed */ public function handle() { // Clear the DB first $con = Propel::getWriteConnection(GameTableMap::DATABASE_NAME); //$sql = "DELETE FROM game WHERE name<>'test'"; $queries = array('DELETE FROM game'); $tables = array('game_match', 'match_state', 'turn', 'empire_order', 'empire', 'territory_template'); foreach ($tables as $t) { $queries[] = "ALTER TABLE {$t} AUTO_INCREMENT = 1"; } foreach ($queries as $q) { $stmt = $con->prepare($q); $stmt->execute(); } //$config->system->db->useDebug(true); // Create or use $game = null; $games = GameQuery::create()->filterByName('test%', Criteria::LIKE); $game_base_name = 'test'; $game_name = $game_base_name . '_' . $games->count(); $p_objs = json_decode(file_get_contents(Config::get('paths.games') . "/{$game_base_name}/empires.json"), false); $t_objs = json_decode(file_get_contents(Config::get('paths.games') . "/{$game_base_name}/territories.json"), false); $game = Game::create($game_name, 1861, 'spring'); $game->loadEmpires($p_objs); $game->loadTerritories($t_objs); $game->save(); // $texas = Territory::findTerritoryByName($territories, 'Texas'); // $sequoia = Territory::findTerritoryByName($territories, 'Sequoia'); // $ohio = Territory::findTerritoryByName($territories, 'Ohio'); // print "Texas-Sequoia? " . ($texas->isNeighbour($sequoia) ? 'PASS':'******') . "\n"; // print "Sequoia-Texas? " . ($sequoia->isNeighbour($texas) ? 'PASS':'******') . "\n"; // print "Texas-Ohio? " . ($texas->isNeighbour($ohio) ? 'FAIL':'PASS') . "\n"; // Empires $red = EmpireQuery::create()->filterByGame($game)->filterByAbbr('RED')->findOne(); $blue = EmpireQuery::create()->filterByGame($game)->filterByAbbr('BLU')->findOne(); $green = EmpireQuery::create()->filterByGame($game)->filterByAbbr('GRN')->findOne(); $match = Match::create($game, "Matt Test"); $turn = $match->getCurrentTurn(); print "\n" . $match . "\n"; // Territories // Crate the $t_<territory> magic variables. $t_names = array('A', 'B', 'C', 'D', 'E'); foreach ($t_names as $n) { $c = new Criteria(); $c->add(TerritoryTemplateTableMap::COL_NAME, $n); $tt = $game->getGameTerritories($c); $varname = "t_" . strtolower($n); ${$varname} = StateQuery::create()->filterByTerritory($tt)->findOne(); } print "{$t_a} neighbours:\n"; $neighbours = $t_a->getTerritory()->getNeighbours(); foreach ($neighbours as $n) { print "{$n}\n"; } print "\n" . Unit::printUnitTable($match->getCurrentTurn()); $case = 3; switch ($case) { case 1: // Test move conflict $turn->addOrder(Move::createNS($red, $t_a, $t_b)); $turn->addOrder(Move::createNS($red, $t_a, $t_c)); $turn->addOrder(Move::createNS($blue, $t_a, $t_b)); $turn->addOrder(Move::createNS($green, $t_e, $t_d)); $turn->save(); break; case 2: // Test support $turn->addOrder(Move::createNS($red, $t_a, $t_b)); $turn->addOrder(Move::createNS($blue, $t_b, $t_c)); $turn->addOrder(Support::createNS($green, $t_a, $t_e, $t_b)); $turn->save(); break; case 3: //$config->system->db->useDebug(true); try { $turn->addOrder(Order::interpretText('MOVE "A" "B"', $match, $red)); } catch (\DiplomacyOrm\InvalidOrderException $e) { print "[" . Config::get('ansi.red') . "Error" . Config::get('ansi.clear') . "]: Red cannot MOVE A-B: " . $e->getMessage() . "\n"; exit; } catch (DiplomacyOrm\TurnClosedToOrdersException $e) { print "[" . Config::get('ansi.red') . "Error" . Config::get('ansi.clear') . "]: Some how the turn state is empty again: " . $e->getMessage() . "\n"; exit; } $turn->addOrder(Order::interpretText('SUPPORT "E" "A" "B"', $match, $green)); $turn->save(); break; case 4: // Test the case with multiple contendors stalemat. standoff.svg $turn->addOrder(Order::interpretText('MOVE "A" "B"', $match, $red)); $turn->addOrder(Order::interpretText('SUPPORT "A" "F" "B"', $match, $red)); $turn->addOrder(Order::interpretText('MOVE "I" "B"', $match, $green)); $turn->addOrder(Order::interpretText('SUPPORT "E" "H" "B"', $match, $green)); $turn->save(); // RED and GREEN should loose in statemates, B should belong to BLUE break; case 5: $turn->addOrder(Order::interpretText('MOVE "E" "C"', $match, $green)); break; case 6: // No orders $result = $match->getCurrentTurn()->processOrders(); print $match . "\n"; $result = $match->getCurrentTurn()->processOrders(); $result = $match->getCurrentTurn()->processOrders(); $result = $match->getCurrentTurn()->processOrders(); break; } $match->getCurrentTurn()->printOrders(); $result = $match->getCurrentTurn()->processOrders(); //print "\n" . Unit::printUnitTable($match->getCurrentTurn()); //$match->getCurrentTurn()->printOrders(); //print_r($result->__toArray()); //print json_encode($result->__toArray()); if ($result->retreatsRequired()) { //print $result; $retreat = Retreat::createNS($blue, $t_b, $t_c); print "Adding retreat order: {$retreat}\n"; $match->getCurrentTurn()->addOrder($retreat); print "----------------------------------------\n"; print "After adding retreat order..\n"; $match->getCurrentTurn()->printOrders(); $result = $match->getCurrentTurn()->processOrders(); print $result; } // Show which orders have failed, etc print "\n" . $match . "\n"; }