Game
Games
BIN
laravel/.DS_Store
vendored
@ -1,44 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
|
|
||||||
define('YOUR_SERVER_URL', 'http://badmemory.test');
|
|
||||||
// Check "oauth_clients" table for next 2 values:
|
|
||||||
define('CLIENT_ID', '2');
|
|
||||||
define('CLIENT_SECRET','OkRo6zlYScTO8jwlBZuCw6aILMef7e92o1SAVnoj');
|
|
||||||
|
|
||||||
class LoginControllerAPI extends Controller
|
|
||||||
{
|
|
||||||
//
|
|
||||||
public function login(Request $request)
|
|
||||||
{
|
|
||||||
$http = new \GuzzleHttp\Client;
|
|
||||||
$response = $http->post(YOUR_SERVER_URL.'/oauth/token', [
|
|
||||||
|
|
||||||
'form_params' => [
|
|
||||||
'grant_type' => 'password',
|
|
||||||
'client_id' => CLIENT_ID,
|
|
||||||
'client_secret' => CLIENT_SECRET,
|
|
||||||
'username' => $request->email,
|
|
||||||
'password' => $request->password,
|
|
||||||
'scope' => ''],
|
|
||||||
|
|
||||||
'exceptions' => false,]);
|
|
||||||
|
|
||||||
$errorCode= $response->getStatusCode();
|
|
||||||
if ($errorCode=='200') {
|
|
||||||
return json_decode((string) $response->getBody(), true);
|
|
||||||
} else {
|
|
||||||
return response()->json(['msg'=>'User credentials are invalid'], $errorCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function logout()
|
|
||||||
{
|
|
||||||
\Auth::guard('api')->user()->token()->revoke();
|
|
||||||
\Auth::guard('api')->user()->token()->delete();
|
|
||||||
return response()->json(['msg'=>'Token revoked'], 200);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,83 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use App\Http\Resources\Game as GameResource;
|
|
||||||
use App\Game;
|
|
||||||
|
|
||||||
class MemoryGameControllerAPI extends Controller
|
|
||||||
{
|
|
||||||
public function index()
|
|
||||||
{
|
|
||||||
return GameResource::collection(Game::all());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function lobby()
|
|
||||||
{
|
|
||||||
return GameResource::collection(Game::where('status', 'pending')->get());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function gamesStatus($status)
|
|
||||||
{
|
|
||||||
return GameResource::collection(Game::where('status', $status)->get());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getGame($id)
|
|
||||||
{
|
|
||||||
return new GameResource(Game::find($id));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function store(Request $request)
|
|
||||||
{
|
|
||||||
$request->validate([
|
|
||||||
'player1' => 'required',
|
|
||||||
]);
|
|
||||||
$game = new Game();
|
|
||||||
$game->fill($request->all());
|
|
||||||
// No matter what status and winner was defined on the client.
|
|
||||||
// When creating a game it will always assume "pending" status
|
|
||||||
// and winner will be null
|
|
||||||
$game->status = 'pending';
|
|
||||||
$game->winner = null;
|
|
||||||
$game->save();
|
|
||||||
return response()->json(new GameResource($game), 201);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function joinAndStart(Request $request, $id)
|
|
||||||
{
|
|
||||||
$player2 = $request->all()["player2"];
|
|
||||||
$game = Game::findOrFail($id);
|
|
||||||
if (!(is_null($game->player2) || ($game->player2 == ""))) {
|
|
||||||
return response()->json(array('code'=> 409, 'message' => 'Cannot join a game that already has a second player'), 409);
|
|
||||||
}
|
|
||||||
if (is_null($game->status) || ($game->status != "pending")) {
|
|
||||||
return response()->json(array('code'=> 409, 'message' => 'Cannot join a game whose status is not "pending"'), 409);
|
|
||||||
}
|
|
||||||
$game->player2 = $player2;
|
|
||||||
$game->status = 'active';
|
|
||||||
$game->save();
|
|
||||||
return new GameResource($game);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function endgame($id, $winner)
|
|
||||||
{
|
|
||||||
$game = Game::findOrFail($id);
|
|
||||||
if (is_null($game->player1) || ($game->player1 == "")) {
|
|
||||||
return response()->json(array('code'=> 409, 'message' => 'Cannot end a game that has no first player'), 409);
|
|
||||||
}
|
|
||||||
if (is_null($game->player2) || ($game->player2 == "")) {
|
|
||||||
return response()->json(array('code'=> 409, 'message' => 'Cannot end a game that has no second player'), 409);
|
|
||||||
}
|
|
||||||
if (is_null($game->status) || ($game->status != "active")) {
|
|
||||||
return response()->json(array('code'=> 409, 'message' => 'Cannot end a game whose status is not "active"'), 409);
|
|
||||||
}
|
|
||||||
if (($winner != 0) && ($winner != 1) && ($winner != 2)) {
|
|
||||||
return response()->json(array('code'=> 409, 'message' => 'To end a game winner must be 0 (tie), 1 (player1) or 2 (player2)'), 409);
|
|
||||||
}
|
|
||||||
$game->winner = $winner;
|
|
||||||
$game->status = 'complete';
|
|
||||||
$game->save();
|
|
||||||
return new GameResource($game);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use Illuminate\Database\Seeder;
|
|
||||||
|
|
||||||
class UsersTableSeeder extends Seeder
|
|
||||||
{
|
|
||||||
private $numberOfUsers = 30;
|
|
||||||
/**
|
|
||||||
* Run the database seeds.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function run()
|
|
||||||
{
|
|
||||||
|
|
||||||
factory(App\User::class, 50)->create();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
BIN
laravel/public/.DS_Store
vendored
Before Width: | Height: | Size: 302 B |
Before Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 4.4 KiB |
Before Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 3.3 KiB |
Before Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 2.6 KiB |
Before Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 3.8 KiB |
Before Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 3.8 KiB |
Before Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 2.6 KiB |
Before Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 2.8 KiB |
Before Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 2.8 KiB |
Before Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 3.3 KiB |
Before Width: | Height: | Size: 4.1 KiB |
Before Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 3.6 KiB |
Before Width: | Height: | Size: 286 B |
@ -1,83 +0,0 @@
|
|||||||
/******/ (function(modules) { // webpackBootstrap
|
|
||||||
/******/ // The module cache
|
|
||||||
/******/ var installedModules = {};
|
|
||||||
/******/
|
|
||||||
/******/ // The require function
|
|
||||||
/******/ function __webpack_require__(moduleId) {
|
|
||||||
/******/
|
|
||||||
/******/ // Check if module is in cache
|
|
||||||
/******/ if(installedModules[moduleId]) {
|
|
||||||
/******/ return installedModules[moduleId].exports;
|
|
||||||
/******/ }
|
|
||||||
/******/ // Create a new module (and put it into the cache)
|
|
||||||
/******/ var module = installedModules[moduleId] = {
|
|
||||||
/******/ i: moduleId,
|
|
||||||
/******/ l: false,
|
|
||||||
/******/ exports: {}
|
|
||||||
/******/ };
|
|
||||||
/******/
|
|
||||||
/******/ // Execute the module function
|
|
||||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
|
||||||
/******/
|
|
||||||
/******/ // Flag the module as loaded
|
|
||||||
/******/ module.l = true;
|
|
||||||
/******/
|
|
||||||
/******/ // Return the exports of the module
|
|
||||||
/******/ return module.exports;
|
|
||||||
/******/ }
|
|
||||||
/******/
|
|
||||||
/******/
|
|
||||||
/******/ // expose the modules object (__webpack_modules__)
|
|
||||||
/******/ __webpack_require__.m = modules;
|
|
||||||
/******/
|
|
||||||
/******/ // expose the module cache
|
|
||||||
/******/ __webpack_require__.c = installedModules;
|
|
||||||
/******/
|
|
||||||
/******/ // define getter function for harmony exports
|
|
||||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
|
||||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
|
||||||
/******/ Object.defineProperty(exports, name, {
|
|
||||||
/******/ configurable: false,
|
|
||||||
/******/ enumerable: true,
|
|
||||||
/******/ get: getter
|
|
||||||
/******/ });
|
|
||||||
/******/ }
|
|
||||||
/******/ };
|
|
||||||
/******/
|
|
||||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
|
||||||
/******/ __webpack_require__.n = function(module) {
|
|
||||||
/******/ var getter = module && module.__esModule ?
|
|
||||||
/******/ function getDefault() { return module['default']; } :
|
|
||||||
/******/ function getModuleExports() { return module; };
|
|
||||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
|
||||||
/******/ return getter;
|
|
||||||
/******/ };
|
|
||||||
/******/
|
|
||||||
/******/ // Object.prototype.hasOwnProperty.call
|
|
||||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
|
||||||
/******/
|
|
||||||
/******/ // __webpack_public_path__
|
|
||||||
/******/ __webpack_require__.p = "";
|
|
||||||
/******/
|
|
||||||
/******/ // Load entry module and return exports
|
|
||||||
/******/ return __webpack_require__(__webpack_require__.s = 74);
|
|
||||||
/******/ })
|
|
||||||
/************************************************************************/
|
|
||||||
/******/ ({
|
|
||||||
|
|
||||||
/***/ 74:
|
|
||||||
/***/ (function(module, exports, __webpack_require__) {
|
|
||||||
|
|
||||||
module.exports = __webpack_require__(75);
|
|
||||||
|
|
||||||
|
|
||||||
/***/ }),
|
|
||||||
|
|
||||||
/***/ 75:
|
|
||||||
/***/ (function(module, exports) {
|
|
||||||
|
|
||||||
throw new Error("Module build failed: Error: ENOENT: no such file or directory, open '/home/vagrant/projeto/resources/assets/js/tictactoe.js'\n at Error (native)");
|
|
||||||
|
|
||||||
/***/ })
|
|
||||||
|
|
||||||
/******/ });
|
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"/js/vueapp.js": "/js/vueapp.js"
|
|
||||||
}
|
|
@ -1,131 +0,0 @@
|
|||||||
<template>
|
|
||||||
<div class="gameseparator">
|
|
||||||
<div>
|
|
||||||
<h2 class="text-center">Game {{ game.gameID }}</h2>
|
|
||||||
<br>
|
|
||||||
</div>
|
|
||||||
<div class="game-zone-content">
|
|
||||||
<div class="alert" :class="alerttype">
|
|
||||||
<strong>{{ message }} <a v-show="game.gameEnded" v-on:click.prevent="closeGame">Close Game</a></strong>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<p><button class="btn btn-xs btn-success" v-on:click.prevent="startGame(game)">Start Game</button></p>
|
|
||||||
</div>
|
|
||||||
<div class="board">
|
|
||||||
<div class="board">
|
|
||||||
<div class="line" v-for="(line, x) in game.boardImages" >
|
|
||||||
<div v-for="(piece, y) in line" >
|
|
||||||
{{x}}{{y}}
|
|
||||||
<img v-bind:src="pieceImageURL(piece, x, y)" v-on:click="clickPiece(x,y)">
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<hr>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
export default {
|
|
||||||
props: ['game'],
|
|
||||||
data: function(){
|
|
||||||
return {
|
|
||||||
}
|
|
||||||
},
|
|
||||||
computed: {
|
|
||||||
ownPlayerNumber(){
|
|
||||||
if (this.game.player1SocketID == this.$parent.socketId) {
|
|
||||||
return 1;
|
|
||||||
} else if (this.game.player2SocketID == this.$parent.socketId) {
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
},
|
|
||||||
ownPlayerName(){
|
|
||||||
var ownNumber = this.ownPlayerNumber;
|
|
||||||
if (ownNumber == 1)
|
|
||||||
return this.game.player1;
|
|
||||||
if (ownNumber == 2)
|
|
||||||
return this.game.player2;
|
|
||||||
return "Unknown";
|
|
||||||
},
|
|
||||||
adversaryPlayerName(){
|
|
||||||
var ownNumber = this.ownPlayerNumber;
|
|
||||||
if (ownNumber == 1)
|
|
||||||
return this.game.player2;
|
|
||||||
if (ownNumber == 2)
|
|
||||||
return this.game.player1;
|
|
||||||
return "Unknown";
|
|
||||||
},
|
|
||||||
message(){
|
|
||||||
if (!this.game.gameStarted) {
|
|
||||||
return "Game has not started yet";
|
|
||||||
} else if (this.game.gameEnded) {
|
|
||||||
if (this.game.winner == this.ownPlayerNumber) {
|
|
||||||
return "Game has ended. You Win.";
|
|
||||||
} else if (this.game.winner == 0) {
|
|
||||||
return "Game has ended. There was a tie.";
|
|
||||||
}
|
|
||||||
return "Game has ended and " + this.adversaryPlayerName + " has won. You lost.";
|
|
||||||
} else {
|
|
||||||
if (this.game.playerTurn == this.ownPlayerNumber) {
|
|
||||||
return "It's your turn";
|
|
||||||
} else {
|
|
||||||
return "It's " + this.adversaryPlayerName + " turn";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "Game is inconsistent";
|
|
||||||
},
|
|
||||||
alerttype(){
|
|
||||||
if (!this.game.gameStarted) {
|
|
||||||
return "alert-warning";
|
|
||||||
} else if (this.game.gameEnded) {
|
|
||||||
if (this.game.winner == this.ownPlayerNumber) {
|
|
||||||
return "alert-success";
|
|
||||||
} else if (this.game.winner == 0) {
|
|
||||||
return "alert-info";
|
|
||||||
}
|
|
||||||
return "alert-danger";
|
|
||||||
}
|
|
||||||
if (this.game.playerTurn == this.ownPlayerNumber) {
|
|
||||||
return "alert-success";
|
|
||||||
} else {
|
|
||||||
return "alert-info";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
startGame(game){
|
|
||||||
this.$socket.emit('start_game', {gameID: game.gameID});
|
|
||||||
},
|
|
||||||
pieceImageURL (pieceNumber, x,y) {
|
|
||||||
var imgSrc = String(pieceNumber);
|
|
||||||
return 'img/' + imgSrc + '.png';
|
|
||||||
},
|
|
||||||
closeGame (){
|
|
||||||
this.$parent.close(this.game);
|
|
||||||
},
|
|
||||||
clickPiece(x,y){
|
|
||||||
if (!this.game.gameEnded) {
|
|
||||||
if (this.game.playerTurn != this.ownPlayerNumber) {
|
|
||||||
alert("It's not your turn to play");
|
|
||||||
} else {
|
|
||||||
if (this.game.board[index] == 0) {
|
|
||||||
this.$parent.play(this.game, index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style scoped>
|
|
||||||
.gameseparator{
|
|
||||||
border-style: solid;
|
|
||||||
border-width: 2px 0 0 0;
|
|
||||||
border-color: black;
|
|
||||||
}
|
|
||||||
</style>
|
|
@ -1,43 +0,0 @@
|
|||||||
/*jshint esversion: 6 */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* First we will load all of this project's JavaScript dependencies which
|
|
||||||
* includes Vue and other libraries. It is a great starting point when
|
|
||||||
* building robust, powerful web applications using Vue and Laravel.
|
|
||||||
*/
|
|
||||||
|
|
||||||
require('./bootstrap');
|
|
||||||
|
|
||||||
window.Vue = require('vue');
|
|
||||||
|
|
||||||
import VueRouter from 'vue-router';
|
|
||||||
Vue.use(VueRouter);
|
|
||||||
|
|
||||||
const user = Vue.component('user', require('./components/user.vue'));
|
|
||||||
const department = Vue.component('department', require('./components/departmentList.vue'));
|
|
||||||
|
|
||||||
const singleplayerMode = Vue.component('singleplayerMode', require('./components/singleplayerMode.vue'));
|
|
||||||
//const multiplayerMode = Vue.component('multiplayerMode', require('./components/multiplayerMode.vue'));
|
|
||||||
|
|
||||||
const routes = [
|
|
||||||
{ path: '/', redirect: '/users' },
|
|
||||||
{ path: '/users', component: user },
|
|
||||||
{ path: '/departments', component: department },
|
|
||||||
|
|
||||||
{ path: '/singleplayerMode', component: singleplayerMode },
|
|
||||||
//{ path: '/multiplayerMode', component: multiplayerMode }
|
|
||||||
|
|
||||||
];
|
|
||||||
|
|
||||||
const router = new VueRouter({
|
|
||||||
routes:routes
|
|
||||||
});
|
|
||||||
|
|
||||||
const app = new Vue({
|
|
||||||
router,
|
|
||||||
data:{
|
|
||||||
player1:undefined,
|
|
||||||
player2: undefined,
|
|
||||||
departments: [],
|
|
||||||
}
|
|
||||||
}).$mount('#app');
|
|
BIN
laravel/app/.DS_Store → laravel_server/.DS_Store
vendored
33
laravel_server/app/Game.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Game extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'status',
|
||||||
|
'player1',
|
||||||
|
'player2',
|
||||||
|
'winner',
|
||||||
|
];
|
||||||
|
public function getWinnerName()
|
||||||
|
{
|
||||||
|
if ($this->winner == 1) {
|
||||||
|
return $this->player1;
|
||||||
|
} else if ($this->winner == 2) {
|
||||||
|
return $this->player2;
|
||||||
|
} else if (is_null($this->winner)) {
|
||||||
|
return '';
|
||||||
|
} else if ($this->winner == 0) {
|
||||||
|
return 'tie';
|
||||||
|
}
|
||||||
|
return "Unknown Winner";
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Resources\Department as DepartmentResource;
|
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use App\Http\Resources\Department as DepartmentResource;
|
||||||
use App\Department;
|
use App\Department;
|
||||||
|
|
||||||
class DepartmentControllerAPI extends Controller
|
class DepartmentControllerAPI extends Controller
|
83
laravel_server/app/Http/Controllers/GameControllerAPI.php
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Http\Resources\Game as GameResource;
|
||||||
|
use App\Game;
|
||||||
|
|
||||||
|
class GameControllerAPI extends Controller
|
||||||
|
{
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
return GameResource::collection(Game::all());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function lobby()
|
||||||
|
{
|
||||||
|
return GameResource::collection(Game::where('status', 'pending')->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function gamesStatus($status)
|
||||||
|
{
|
||||||
|
return GameResource::collection(Game::where('status', $status)->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getGame($id)
|
||||||
|
{
|
||||||
|
return new GameResource(Game::find($id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'player1' => 'required',
|
||||||
|
]);
|
||||||
|
$game = new Game();
|
||||||
|
$game->fill($request->all());
|
||||||
|
// No matter what status and winner was defined on the client.
|
||||||
|
// When creating a game it will always assume "pending" status
|
||||||
|
// and winner will be null
|
||||||
|
$game->status = 'pending';
|
||||||
|
$game->winner = null;
|
||||||
|
$game->save();
|
||||||
|
return response()->json(new GameResource($game), 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function joinAndStart(Request $request, $id)
|
||||||
|
{
|
||||||
|
$player2 = $request->all()["player2"];
|
||||||
|
$game = Game::findOrFail($id);
|
||||||
|
if (!(is_null($game->player2) || ($game->player2 == ""))) {
|
||||||
|
return response()->json(array('code'=> 409, 'message' => 'Cannot join a game that already has a second player'), 409);
|
||||||
|
}
|
||||||
|
if (is_null($game->status) || ($game->status != "pending")) {
|
||||||
|
return response()->json(array('code'=> 409, 'message' => 'Cannot join a game whose status is not "pending"'), 409);
|
||||||
|
}
|
||||||
|
$game->player2 = $player2;
|
||||||
|
$game->status = 'active';
|
||||||
|
$game->save();
|
||||||
|
return new GameResource($game);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function endgame($id, $winner)
|
||||||
|
{
|
||||||
|
$game = Game::findOrFail($id);
|
||||||
|
if (is_null($game->player1) || ($game->player1 == "")) {
|
||||||
|
return response()->json(array('code'=> 409, 'message' => 'Cannot end a game that has no first player'), 409);
|
||||||
|
}
|
||||||
|
if (is_null($game->player2) || ($game->player2 == "")) {
|
||||||
|
return response()->json(array('code'=> 409, 'message' => 'Cannot end a game that has no second player'), 409);
|
||||||
|
}
|
||||||
|
if (is_null($game->status) || ($game->status != "active")) {
|
||||||
|
return response()->json(array('code'=> 409, 'message' => 'Cannot end a game whose status is not "active"'), 409);
|
||||||
|
}
|
||||||
|
if (($winner != 0) && ($winner != 1) && ($winner != 2)) {
|
||||||
|
return response()->json(array('code'=> 409, 'message' => 'To end a game winner must be 0 (tie), 1 (player1) or 2 (player2)'), 409);
|
||||||
|
}
|
||||||
|
$game->winner = $winner;
|
||||||
|
$game->status = 'complete';
|
||||||
|
$game->save();
|
||||||
|
return new GameResource($game);
|
||||||
|
}
|
||||||
|
}
|
43
laravel_server/app/Http/Controllers/LoginControllerAPI.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
define('YOUR_SERVER_URL', 'http://memorygame.dev');
|
||||||
|
// Check "oauth_clients" table for next 2 values:
|
||||||
|
define('CLIENT_ID', '2');
|
||||||
|
define('CLIENT_SECRET','O6XUA7EQ51qEG4CCEVgUuN41lln3XXSFYbmeb2G4');
|
||||||
|
|
||||||
|
class LoginControllerAPI extends Controller
|
||||||
|
{
|
||||||
|
public function login(Request $request)
|
||||||
|
{
|
||||||
|
$http = new \GuzzleHttp\Client;
|
||||||
|
$response = $http->post(YOUR_SERVER_URL.'/oauth/token', [
|
||||||
|
'form_params' => [
|
||||||
|
'grant_type' => 'password',
|
||||||
|
'client_id' => CLIENT_ID,
|
||||||
|
'client_secret' => CLIENT_SECRET,
|
||||||
|
'username' => $request->email,
|
||||||
|
'password' => $request->password,
|
||||||
|
'scope' => ''
|
||||||
|
],
|
||||||
|
'exceptions' => false,
|
||||||
|
]);
|
||||||
|
$errorCode= $response->getStatusCode();
|
||||||
|
if ($errorCode=='200') {
|
||||||
|
return json_decode((string) $response->getBody(), true);
|
||||||
|
} else {
|
||||||
|
return response()->json(['msg'=>'User credentials are invalid'], $errorCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function logout()
|
||||||
|
{
|
||||||
|
\Auth::guard('api')->user()->token()->revoke();
|
||||||
|
\Auth::guard('api')->user()->token()->delete();
|
||||||
|
return response()->json(['msg'=>'Token revoked'], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -33,7 +33,7 @@ class UserControllerAPI extends Controller
|
|||||||
$request->validate([
|
$request->validate([
|
||||||
'name' => 'required',
|
'name' => 'required',
|
||||||
'email' => 'required|email|unique:users,email',
|
'email' => 'required|email|unique:users,email',
|
||||||
'age' => 'integer|between:18,75',
|
'username' => 'required|unique:email',
|
||||||
'password' => 'min:3'
|
'password' => 'min:3'
|
||||||
]);
|
]);
|
||||||
$user = new User();
|
$user = new User();
|
@ -8,5 +8,4 @@ class VueController extends Controller
|
|||||||
{
|
{
|
||||||
return view('vue.index');
|
return view('vue.index');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
27
laravel_server/app/Http/Resources/Game.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\Resource;
|
||||||
|
|
||||||
|
class Game extends Resource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray($request)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'status' => $this->status,
|
||||||
|
'player1' => $this->player1,
|
||||||
|
'player2' => $this->player2,
|
||||||
|
'winner' => $this->winner,
|
||||||
|
'winnerName' => $this->getWinnerName(),
|
||||||
|
];
|
||||||
|
return parent::toArray($request);
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
@ -25,8 +25,7 @@ class AuthServiceProvider extends ServiceProvider
|
|||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
$this->registerPolicies();
|
$this->registerPolicies();
|
||||||
Passport::routes();
|
Passport::routes();
|
||||||
|
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
}
|
}
|
14
laravel/composer.lock → laravel_server/composer.lock
generated
@ -2892,16 +2892,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "zendframework/zend-diactoros",
|
"name": "zendframework/zend-diactoros",
|
||||||
"version": "1.6.1",
|
"version": "1.7.0",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/zendframework/zend-diactoros.git",
|
"url": "https://github.com/zendframework/zend-diactoros.git",
|
||||||
"reference": "c8664b92a6d5bc229e48b0923486c097e45a7877"
|
"reference": "ed6ce7e2105c400ca10277643a8327957c0384b7"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/zendframework/zend-diactoros/zipball/c8664b92a6d5bc229e48b0923486c097e45a7877",
|
"url": "https://api.github.com/repos/zendframework/zend-diactoros/zipball/ed6ce7e2105c400ca10277643a8327957c0384b7",
|
||||||
"reference": "c8664b92a6d5bc229e48b0923486c097e45a7877",
|
"reference": "ed6ce7e2105c400ca10277643a8327957c0384b7",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@ -2920,8 +2920,8 @@
|
|||||||
"type": "library",
|
"type": "library",
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-master": "1.6-dev",
|
"dev-master": "1.7.x-dev",
|
||||||
"dev-develop": "1.7-dev"
|
"dev-develop": "1.8.x-dev"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
@ -2940,7 +2940,7 @@
|
|||||||
"psr",
|
"psr",
|
||||||
"psr-7"
|
"psr-7"
|
||||||
],
|
],
|
||||||
"time": "2017-10-12T15:24:51+00:00"
|
"time": "2018-01-04T18:21:48+00:00"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"packages-dev": [
|
"packages-dev": [
|