Skip to content

Commit

Permalink
Add simple quest view, front-end code for it and fake back-end
Browse files Browse the repository at this point in the history
  • Loading branch information
gbdrm committed Jul 26, 2016
1 parent 8c7aadd commit 60c8239
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 47 deletions.
4 changes: 3 additions & 1 deletion ClientApp/boot-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { provideRouter } from '@angular/router';
import { HTTP_PROVIDERS } from '@angular/http';
import { App } from './components/app/app';
import { routes } from './routes';
import { disableDeprecatedForms, provideForms } from '@angular/forms';

bootstrap(App, [
...HTTP_PROVIDERS,
FormBuilder,
provideRouter(routes)
provideRouter(routes),
provideForms()
]);

// Basic hot reloading support. Automatically reloads and restarts the Angular 2 app each time
Expand Down
16 changes: 3 additions & 13 deletions ClientApp/components/home/home.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import * as ng from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import { Cookie } from 'ng2-cookies/ng2-cookies';
import { Http } from '@angular/http';

@ng.Component({
selector: 'home',
template: require('./home.html'),
directives: [...ROUTER_DIRECTIVES],
directives: [...ROUTER_DIRECTIVES]
})
export class Home {
constructor(private http: Http) { }

ngOnInit() {
let token = Cookie.get('playerToken');
if (!token) {
this.http.get('/home/registernewplayer').subscribe(result => {
token = result.text();
Cookie.set('playerToken', token);
});
}
constructor() {
}
}
}
29 changes: 23 additions & 6 deletions ClientApp/components/quest/quest.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
<h2>Квест</h2>
<div style="margin-top: 50px">
<div>
<p class="lead">
{{task.title}}
</p>

<p>This is a simple example of an Angular 2 component.</p>

<p>Current count: <strong>{{ currentCount }}</strong></p>

<button (click)="incrementCounter()">Increment</button>
<h3 style="max-width: 400px">{{task.content}}</h3>
<br />
<form (submit)="submit()">
<div class="form-group">
<input type="text" name="answer" class="form-control" autofocus="autofocus" placeholder="" [(ngModel)]="answer">
<br />
<a class="btn btn-success" (click)="submit()">Отправить</a>
</div>
</form>
<br />
<div ng-style="{ 'color' : hintColor }">
<b>{{hints}}</b>
</div>
</div>
<br />
<div>Просмотрено - {{task.watched}}</div>
<div>Пройдено - {{task.done}}</div>
</div>
26 changes: 23 additions & 3 deletions ClientApp/components/quest/quest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import * as ng from '@angular/core';
import {Inject} from '@angular/core';
import { Http } from '@angular/http';
import { Cookie } from 'ng2-cookies/ng2-cookies';

@ng.Component({
selector: 'quest',
template: require('./quest.html')
})
export class Quest {
public currentTask = 0;
private task: any = {};
private answer: string = '';

public nextTask() {
// call API
constructor( @Inject(Http) private http: Http) {
this.http.get('/home/getcurrentstate', { withCredentials: true })
.subscribe(result => {
this.task = result.json().task;
let token = Cookie.get('playerToken');
if (!token) {
token = result.json().player.token;
Cookie.set('playerToken', token);
}
});
}

submit() {
this.http.get('/home/submitanswer', { withCredentials: true })
.subscribe(result => {
this.task = result.json().task;
this.answer = '';
});
}
}
60 changes: 50 additions & 10 deletions Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Threading.Tasks;
using HabraQuest.Model;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -26,17 +24,59 @@ public IActionResult Error()
return View();
}

public string RegisterNewPlayer()
public Player InitializePlayer()
{
Player player = new Player
Player player = null;
var token = Request.Cookies["playerToken"];
if (token == null)
{
Token = Guid.NewGuid()
};
player = new Player
{
Token = Guid.NewGuid()
};

dataContext.Add(player);
dataContext.SaveChanges();
}

return player ?? dataContext.Players.Single(p => p.Token.ToString() == token);
}

dataContext.Add(player);
dataContext.SaveChanges();
public dynamic GetCurrentState()
{
return new
{
Task = new QuestTask
{
Id = 2,
Title = "Шта?",
Content = "Превед, креведко"
},
Player = InitializePlayer(),
};
}

return player.Token.ToString();
public dynamic SubmitAnswer()
{
return new
{
Task = new QuestTask
{
Id = 3,
Title = "Шта?2",
Content = "Превед, креведко. ololo"
},
Player = InitializePlayer(),
};
}
}

public class QuestTask
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int Watched { get; set; }
public int Done { get; set; }
}
}
4 changes: 0 additions & 4 deletions Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
@{
ViewData["Title"] = "Home Page";
}

<app asp-prerender-module="ClientApp/boot-server"
asp-prerender-webpack-config="webpack.config.js">Loading...</app>

Expand Down
4 changes: 2 additions & 2 deletions Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - HabraQuest</title>
<title>HabraQuest</title>
<base href="/" />
<link rel="stylesheet" href="~/dist/vendor.css" asp-append-version="true" />
<link rel="stylesheet" href="~/dist/styles.css" asp-append-version="true" />
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "HabraQuest",
"name": "habraquest",
"version": "0.0.0",
"devDependencies": {
"bootstrap": "^3.3.6",
Expand All @@ -18,13 +18,14 @@
"webpack-hot-middleware": "^2.10.0"
},
"dependencies": {
"@angular/common": "2.0.0-rc.3",
"@angular/compiler": "2.0.0-rc.3",
"@angular/core": "2.0.0-rc.3",
"@angular/http": "2.0.0-rc.3",
"@angular/platform-browser": "2.0.0-rc.3",
"@angular/platform-browser-dynamic": "2.0.0-rc.3",
"@angular/platform-server": "2.0.0-rc.3",
"@angular/common": "2.0.0-rc.4",
"@angular/compiler": "2.0.0-rc.4",
"@angular/core": "2.0.0-rc.4",
"@angular/forms": "^0.2.0",
"@angular/http": "2.0.0-rc.4",
"@angular/platform-browser": "2.0.0-rc.4",
"@angular/platform-browser-dynamic": "2.0.0-rc.4",
"@angular/platform-server": "2.0.0-rc.4",
"@angular/router": "3.0.0-alpha.8",
"angular2-universal": "^0.104.1",
"aspnet-prerendering": "^1.0.2",
Expand Down

0 comments on commit 60c8239

Please sign in to comment.