public function Join($block = 0) { Auth::RequireLogin(true); if (!$this->auth->isLogged()) { $this->json->error("You need to be logged in."); } $this->Block = new Game_Block($block); // Make sure the game exists if (!$this->Block->exists()) { $this->json->error("Block could not be found."); } // Make sure the game is not in the past if ($this->Block->time != 0 && $this->Block->time < time()) { $this->json->error("This block is already in the past."); } $this->Account = new Account(Auth::Get("id")); if (!$this->Account->exists()) { $this->json->error("Your account could not be loaded."); } $this->Related_Account = $this->Block->account->where("id", Auth::Get("id"))->include_join_fields()->get(); if ($this->Related_Account->exists() && $this->Related_Account->join_active) { $this->json->error("You are already playing this block."); } // Get the Fixture $this->Fixture = $this->Block->fixture->get(); // Make sure the fixture exists if (!$this->Fixture->exists()) { $this->json->error("Fixture could not be loaded."); } // Check the limit of this block $this->PlayersCount = $this->Block->account->include_join_fields()->where(array("active" => 1))->count(); if ($this->PlayersCount >= (int) $this->Fixture->type * 2) { $this->json->error("This game is already full!"); } if ($this->Fixture->payp_cost > 0 && (!$this->Related_Account->exists() || $this->Related_Account->join_type == "game")) { $this->Account->credits -= $this->Fixture->payp_cost; $this->Account->save(); // Add Payment Record $payment = new Payment(); $payment->account_id = Auth::Get("id"); $payment->game_block_id = $this->Block->id; $payment->amount = $this->Fixture->payp_cost; $payment->date = time(); $payment->type = "game"; $payment->fixture_id = $this->Fixture->id; $payment->paid = 0; $payment->status = "active"; $payment->save(); } // Cancel refund if there is one if ($this->Related_Account->exists() && $this->Related_Account->join_type == "block") { $this->LastPayment = new Payment(); $this->LastPayment->where(array("account_id" => Auth::Get("id"), "game_block_id" => $this->Block->id, "amount <" => 0))->order_by("id", "DESC")->limit(1)->get(); if ($this->LastPayment->exists()) { $this->LastPayment->status = "canceled"; $this->LastPayment->save(); } } // Make all invites inactive for this fixture $Invite = new Invite(); $Invite->where(array("email" => Auth::Get("email"), "fixture_id" => $this->Fixture->id, "valid" => 1, "joined" => 0))->limit(1)->get(); // Check for Invitation if ($Invite->exists()) { $Invite->joined = 1; $Invite->valid = 0; $Invite->joined_on = time(); $Invite->joined_account = Auth::Get("id"); // If invitation exists, mark it as accepted $Invite->save(); } // Relate this player with a manager $this->PlayerManager = new Player_Manager(); $this->PlayerManager->player_id = Auth::Get("id"); $this->PlayerManager->manager_id = $this->Fixture->owner; $this->PlayerManager->save(); // Relate Account $this->Block->save($this->Account); $this->Block->set_join_field($this->Account, "active", 1); if (!$this->Related_Account->exists()) { $this->Block->set_join_field($this->Account, "type", "game"); } // Send success response $this->json->success(); }