Skip to content

Commit

Permalink
Add GAE standard + firebase tictactoe sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerjou Cheng committed Oct 12, 2016
1 parent 012874e commit 187e8f7
Show file tree
Hide file tree
Showing 16 changed files with 1,125 additions and 0 deletions.
54 changes: 54 additions & 0 deletions appengine/firebase-tictactoe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Tic Tac Toe on Google App Engine Standard using Firebase

This directory contains a project that implements a realtime two-player game of
Tic Tac Toe on [Google App Engine] Standard, using the [Firebase] database
for realtime notifications when the board changes.

[Firebase]: https://firebase.google.com
[Google App Engine]: https://cloud.google.com/appengine

## Prerequisites

* Install [Apache Maven][maven]
* Create a project in the [Firebase Console][fb-console]
* Install the [Google Cloud SDK][sdk]
* Download [service account credentials][creds] and move it to
`src/main/webapp/resources/credentials.json`.
* In the [Overview section][fb-overview] of the Firebase console, click 'Add
Firebase to your web app' and replace the contents of the file
`src/main/webapp/resources/firebase_config.html` with that code snippet.

[fb-console]: https://console.firebase.google.com
[sdk]: https://cloud.google.com/sdk
[creds]: https://console.firebase.google.com/iam-admin/serviceaccounts/project?project=_&consoleReturnUrl=https:%2F%2Fconsole.firebase.google.com%2Fproject%2F_%2Fsettings%2Fgeneral%2F
[fb-overview]: https://console.firebase.google.com/project/_/overview


## Run the sample

* To run the app locally:

```sh
$ mvn appengine:run
```

## Troubleshooting

* If you see the error `Google Cloud SDK path was not provided ...`:
* Make sure you've installed the [Google cloud SDK][sdk]
* You may have installed it in a non-standard path. In that case, set the
environment variable `GOOGLE_CLOUD_SDK_HOME` to point to where you
installed the SDK:
```sh
export GOOGLE_CLOUD_SDK_HOME=/path/to/google-cloud-sdk
```
## Contributing changes
See [CONTRIBUTING.md](../../CONTRIBUTING.md).
## Licensing
See [LICENSE](../../LICENSE).
120 changes: 120 additions & 0 deletions appengine/firebase-tictactoe/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<!--
Copyright 2015 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.appengine</groupId>
<artifactId>appengine-firebase</artifactId>
<parent>
<groupId>com.google.cloud</groupId>
<artifactId>doc-samples</artifactId>
<version>1.0.0</version>
<relativePath>../..</relativePath>
</parent>
<properties>
<objectify.version>5.1.13</objectify.version>
</properties>
<!-- [START set_versions] -->
<prerequisites>
<maven>3.3.9</maven>
</prerequisites>
<!-- [END set_versions] -->
<dependencies>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.sdk.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>com.googlecode.objectify</groupId>
<artifactId>objectify</artifactId>
<version>${objectify.version}</version>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-tools-sdk</artifactId>
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.30</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-server-sdk</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-appengine</artifactId>
<version>1.22.0</version>
</dependency>
</dependencies>
<build>
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes
</outputDirectory>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.0.0</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine.firetactoe;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String gameId = req.getParameter("gameKey");
Objectify ofy = ObjectifyService.ofy();
Game game = ofy.load().type(Game.class).id(gameId).safe();

UserService userService = UserServiceFactory.getUserService();
String currentUserId = userService.getCurrentUser().getUserId();

game.deleteChannel(currentUserId);
}
}
Loading

0 comments on commit 187e8f7

Please sign in to comment.