Skip to content

Commit e9ed5e2

Browse files
author
Jerjou Cheng
committed
Add GAE standard + firebase tictactoe sample
1 parent 9474200 commit e9ed5e2

File tree

16 files changed

+1277
-0
lines changed

16 files changed

+1277
-0
lines changed
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Tic Tac Toe on Google App Engine Standard using Firebase
2+
3+
This directory contains a project that implements a realtime two-player game of
4+
Tic Tac Toe on Google [App Engine Standard][standard], using the [Firebase] database
5+
for realtime notifications when the board changes.
6+
7+
[Firebase]: https://firebase.google.com
8+
[standard]: https://cloud.google.com/appengine/docs/about-the-standard-environment
9+
10+
## Prerequisites
11+
12+
* Install [Apache Maven][maven] 3.0.5 or later
13+
* Install the [Google Cloud SDK][sdk]
14+
* Create a project in the [Firebase Console][fb-console]
15+
* In the [Overview section][fb-overview] of the Firebase console, click 'Add
16+
Firebase to your web app' and replace the contents of the file
17+
`src/main/webapp/WEB-INF/view/firebase_config.jspf` with that code snippet.
18+
* If using the development appserver to run the app locally, you must supply
19+
credentials that would otherwise be inferred from the App Engine environment.
20+
Download [service account credentials][creds] and set the
21+
`GOOGLE_APPLICATION_CREDENTIALS` environment variable to its path:
22+
23+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials.json
24+
25+
26+
[fb-console]: https://console.firebase.google.com
27+
[sdk]: https://cloud.google.com/sdk
28+
[creds]: https://console.firebase.google.com/iam-admin/serviceaccounts/project?project=_&consoleReturnUrl=https:%2F%2Fconsole.firebase.google.com%2Fproject%2F_%2Fsettings%2Fgeneral%2F
29+
[fb-overview]: https://console.firebase.google.com/project/_/overview
30+
31+
32+
## Run the sample
33+
34+
* To run the app locally using the development appserver:
35+
36+
```sh
37+
$ mvn appengine:run
38+
```
39+
40+
## Troubleshooting
41+
42+
* If you see the error `Google Cloud SDK path was not provided ...`:
43+
* Make sure you've installed the [Google Cloud SDK][sdk]
44+
* Make sure the Google Cloud SDK's `bin/` directory is in your `PATH`. If
45+
you prefer it not to be, you can also set the environment variable
46+
`GOOGLE_CLOUD_SDK_HOME` to point to where you installed the SDK:
47+
48+
```sh
49+
export GOOGLE_CLOUD_SDK_HOME=/path/to/google-cloud-sdk
50+
```
51+
52+
## Contributing changes
53+
54+
See [CONTRIBUTING.md](../../CONTRIBUTING.md).
55+
56+
## Licensing
57+
58+
See [LICENSE](../../LICENSE).
59+

appengine/firebase-tictactoe/pom.xml

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!--
2+
Copyright 2015 Google Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.appengine</groupId>
21+
<artifactId>appengine-firebase</artifactId>
22+
<parent>
23+
<groupId>com.google.cloud</groupId>
24+
<artifactId>doc-samples</artifactId>
25+
<version>1.0.0</version>
26+
<relativePath>../..</relativePath>
27+
</parent>
28+
<properties>
29+
<objectify.version>5.1.13</objectify.version>
30+
<servlet-api.version>2.5</servlet-api.version>
31+
<gson.version>2.7</gson.version>
32+
<guava.version>19.0</guava.version>
33+
<google-api-client.version>1.22.0</google-api-client.version>
34+
<junit.version>4.12</junit.version>
35+
<mockito.version>1.10.19</mockito.version>
36+
<google-truth.version>0.30</google-truth.version>
37+
<appengine-maven.version>1.0.0</appengine-maven.version>
38+
</properties>
39+
<!-- [START set_versions] -->
40+
<prerequisites>
41+
<maven>3.3.9</maven>
42+
</prerequisites>
43+
<!-- [END set_versions] -->
44+
<dependencies>
45+
<dependency>
46+
<groupId>com.google.appengine</groupId>
47+
<artifactId>appengine-api-1.0-sdk</artifactId>
48+
<version>${appengine.sdk.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>javax.servlet</groupId>
52+
<artifactId>servlet-api</artifactId>
53+
<version>${servlet-api.version}</version>
54+
<type>jar</type>
55+
<scope>provided</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.google.code.gson</groupId>
59+
<artifactId>gson</artifactId>
60+
<version>2.7</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>com.googlecode.objectify</groupId>
64+
<artifactId>objectify</artifactId>
65+
<version>${objectify.version}</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.google.guava</groupId>
69+
<artifactId>guava</artifactId>
70+
<version>${guava.version}</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>com.google.api-client</groupId>
74+
<artifactId>google-api-client-appengine</artifactId>
75+
<version>${google-api-client.version}</version>
76+
</dependency>
77+
78+
79+
<!-- Test Dependencies -->
80+
<dependency>
81+
<groupId>junit</groupId>
82+
<artifactId>junit</artifactId>
83+
<version>${junit.version}</version>
84+
<scope>test</scope>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.mockito</groupId>
88+
<artifactId>mockito-all</artifactId>
89+
<version>${mockito.version}</version>
90+
<scope>test</scope>
91+
</dependency>
92+
<dependency>
93+
<groupId>com.google.appengine</groupId>
94+
<artifactId>appengine-testing</artifactId>
95+
<version>${appengine.sdk.version}</version>
96+
<scope>test</scope>
97+
</dependency>
98+
<dependency>
99+
<groupId>com.google.appengine</groupId>
100+
<artifactId>appengine-api-stubs</artifactId>
101+
<version>${appengine.sdk.version}</version>
102+
<scope>test</scope>
103+
</dependency>
104+
<dependency>
105+
<groupId>com.google.appengine</groupId>
106+
<artifactId>appengine-tools-sdk</artifactId>
107+
<version>${appengine.sdk.version}</version>
108+
<scope>test</scope>
109+
</dependency>
110+
<dependency>
111+
<groupId>com.google.truth</groupId>
112+
<artifactId>truth</artifactId>
113+
<version>${google-truth.version}</version>
114+
<scope>test</scope>
115+
</dependency>
116+
</dependencies>
117+
<build>
118+
<!-- for hot reload of the web application -->
119+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes
120+
</outputDirectory>
121+
<plugins>
122+
<plugin>
123+
<groupId>com.google.cloud.tools</groupId>
124+
<artifactId>appengine-maven-plugin</artifactId>
125+
<version>${appengine-maven.version}</version>
126+
</plugin>
127+
</plugins>
128+
</build>
129+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine.firetactoe;
18+
19+
import com.google.appengine.api.users.UserService;
20+
import com.google.appengine.api.users.UserServiceFactory;
21+
import com.googlecode.objectify.Objectify;
22+
import com.googlecode.objectify.ObjectifyService;
23+
24+
import java.io.IOException;
25+
import javax.servlet.http.HttpServlet;
26+
import javax.servlet.http.HttpServletRequest;
27+
import javax.servlet.http.HttpServletResponse;
28+
29+
/**
30+
* Handler that deletes the Firebase database that serves as the realtime communication channel.
31+
* This handler should be invoked after a game has finished, to clean up the channel.
32+
*/
33+
public class DeleteServlet extends HttpServlet {
34+
@Override
35+
public void doPost(HttpServletRequest request, HttpServletResponse response)
36+
throws IOException {
37+
String gameId = request.getParameter("gameKey");
38+
Objectify ofy = ObjectifyService.ofy();
39+
Game game = ofy.load().type(Game.class).id(gameId).safe();
40+
41+
UserService userService = UserServiceFactory.getUserService();
42+
String currentUserId = userService.getCurrentUser().getUserId();
43+
44+
game.deleteChannel(currentUserId);
45+
}
46+
}

0 commit comments

Comments
 (0)