Skip to content

Commit

Permalink
Add friends, groups, notifications. Merge #4
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Nov 20, 2017
1 parent b8de50d commit 95a8571
Show file tree
Hide file tree
Showing 68 changed files with 3,665 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com/) and this project uses [semantic versioning](http://semver.org/).

## [Unreleased]
### Added
- Support for In-App Notifications feature.
- Support for Friends feature.
- Support for Groups feature.

### Changed
- Leaderboard sort order is now exposed as an enum rather than a number.

### Fixed
- Use correct name for inconsistently defined topic leave message builder method.

## [0.4.0] - 2017-11-07
### Changed
Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ bintray {

dependencies {
compileOnly 'org.projectlombok:lombok:1.16.18'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.google.protobuf:protobuf-lite:3.0.1'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.stumbleupon:async:1.4.1'
compile ('org.slf4j:slf4j-api:1.7.25') {
force = true // don't upgrade to "1.8.0-alpha2"
}
testCompile 'junit:junit:4.12'
testCompile 'ch.qos.logback:logback-classic:1.2.3'
}

gitPublish {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/heroiclabs/nakama/ClientListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.heroiclabs.nakama;

import java.util.List;

/**
* A listener for receiving {@code Client} events.
*/
Expand Down Expand Up @@ -59,4 +61,11 @@ public interface ClientListener {
* @param matchPresence The {@code MatchPresence} received.
*/
void onMatchPresence(MatchPresence matchPresence);

/**
* Called when the client receives new notifications.
*
* @param notifications The list of {@code Notification} received.
*/
void onNotifications(List<Notification> notifications);
}
42 changes: 42 additions & 0 deletions src/main/java/com/heroiclabs/nakama/DefaultClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ public void onMessage(WebSocket webSocket, ByteString bytes) {
case MATCHMAKE_MATCHED:
listener.onMatchmakeMatched(DefaultMatchmakeMatched.fromProto(envelope.getMatchmakeMatched()));
break;
case LIVE_NOTIFICATIONS:
final List<Notification> notifications = new ArrayList<>();
for (final com.heroiclabs.nakama.Api.Notification notification : envelope.getLiveNotifications().getNotificationsList()) {
notifications.add(DefaultNotification.fromProto(notification));
}
listener.onNotifications(notifications);
break;
default:
break;
}
Expand Down Expand Up @@ -281,6 +288,34 @@ public void onMessage(WebSocket webSocket, ByteString bytes) {
}
def.callback(new DefaultResultSet<User>(null, users));
break;
case FRIENDS:
final List<Friend> friends = new ArrayList<>();
for (final com.heroiclabs.nakama.Api.Friend friend : envelope.getFriends().getFriendsList()) {
friends.add(DefaultFriend.fromProto(friend));
}
def.callback(new DefaultResultSet<Friend>(null, friends));
break;
case GROUPS:
final List<Group> groups = new ArrayList<>();
for (final com.heroiclabs.nakama.Api.Group group : envelope.getGroups().getGroupsList()) {
groups.add(DefaultGroup.fromProto(group));
}
def.callback(new DefaultResultSet<Group>(new DefaultCursor(envelope.getGroups().getCursor()), groups));
break;
case GROUPS_SELF:
final List<GroupSelf> groupsSelf = new ArrayList<>();
for (final com.heroiclabs.nakama.Api.TGroupsSelf.GroupSelf groupSelf : envelope.getGroupsSelf().getGroupsSelfList()) {
groupsSelf.add(DefaultGroupSelf.fromProto(groupSelf));
}
def.callback(new DefaultResultSet<GroupSelf>(null, groupsSelf));
break;
case GROUP_USERS:
final List<GroupUser> groupUsers = new ArrayList<>();
for (final com.heroiclabs.nakama.Api.GroupUser groupUser : envelope.getGroupUsers().getUsersList()) {
groupUsers.add(DefaultGroupUser.fromProto(groupUser));
}
def.callback(new DefaultResultSet<GroupUser>(null, groupUsers));
break;
case STORAGE_DATA:
final List<StorageRecord> records = new ArrayList<>();
for (final com.heroiclabs.nakama.Api.TStorageData.StorageData data : envelope.getStorageData().getDataList()) {
Expand Down Expand Up @@ -346,6 +381,13 @@ public void onMessage(WebSocket webSocket, ByteString bytes) {
}
def.callback(new DefaultResultSet<LeaderboardRecord>(new DefaultCursor(envelope.getLeaderboardRecords().getCursor()), leaderboardRecords));
break;
case NOTIFICATIONS:
final List<Notification> notifications = new ArrayList<>();
for (final com.heroiclabs.nakama.Api.Notification notification : envelope.getNotifications().getNotificationsList()) {
notifications.add(DefaultNotification.fromProto(notification));
}
def.callback(new DefaultResultSet<Notification>(new DefaultCursor(envelope.getNotifications().getResumableCursor()), notifications));
break;
default:
def.callback(new DefaultError(envelope.getError().getMessage(), envelope.getError().getCode(), collationId));
break;
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/heroiclabs/nakama/DefaultFriend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2017 The Nakama Authors
*
* 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.heroiclabs.nakama;

import lombok.*;

@Data
@ToString(includeFieldNames = true)
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class DefaultFriend implements Friend {

private final String avatarUrl;

private final long createdAt;

private final String fullname;

private final String handle;

private final String id;

private final String lang;

private final long lastOnlineAt;

private final String location;

private final String metadata;

private final String timezone;

private final long updatedAt;

private final FriendType state;

public <T> T getMetadata(final Class<T> clazz) {
return DefaultClient.GSON.fromJson(metadata, clazz);
}

static Friend fromProto(final @NonNull com.heroiclabs.nakama.Api.Friend friend) {
return new DefaultFriend(friend.getUser().getAvatarUrl(), friend.getUser().getCreatedAt(),
friend.getUser().getFullname(), friend.getUser().getHandle(), friend.getUser().getId(),
friend.getUser().getLang(), friend.getUser().getLastOnlineAt(), friend.getUser().getLocation(),
friend.getUser().getMetadata(), friend.getUser().getTimezone(), friend.getUser().getUpdatedAt(),
Friend.FriendType.fromLong(friend.getState()));
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/heroiclabs/nakama/DefaultFriendsAddMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017 The Nakama Authors
*
* 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.heroiclabs.nakama;

import lombok.AccessLevel;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class DefaultFriendsAddMessage implements FriendsAddMessage {

private final com.heroiclabs.nakama.Api.Envelope.Builder payload;

public byte[] asBytes(final @NonNull String collationId) {
return payload.clone().setCollationId(collationId).build().toByteArray();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017 The Nakama Authors
*
* 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.heroiclabs.nakama;

import lombok.AccessLevel;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class DefaultFriendsBlockMessage implements FriendsBlockMessage {

private final com.heroiclabs.nakama.Api.Envelope.Builder payload;

public byte[] asBytes(final @NonNull String collationId) {
return payload.clone().setCollationId(collationId).build().toByteArray();
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/heroiclabs/nakama/DefaultFriendsListMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017 The Nakama Authors
*
* 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.heroiclabs.nakama;

import lombok.AccessLevel;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class DefaultFriendsListMessage implements FriendsListMessage {

private final com.heroiclabs.nakama.Api.Envelope.Builder payload;

public byte[] asBytes(final @NonNull String collationId) {
return payload.clone().setCollationId(collationId).build().toByteArray();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017 The Nakama Authors
*
* 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.heroiclabs.nakama;

import lombok.AccessLevel;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class DefaultFriendsRemoveMessage implements FriendsRemoveMessage {

private final com.heroiclabs.nakama.Api.Envelope.Builder payload;

public byte[] asBytes(final @NonNull String collationId) {
return payload.clone().setCollationId(collationId).build().toByteArray();
}

}
54 changes: 54 additions & 0 deletions src/main/java/com/heroiclabs/nakama/DefaultGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2017 The Nakama Authors
*
* 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.heroiclabs.nakama;

import lombok.*;

@Data
@ToString(includeFieldNames = true)
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class DefaultGroup implements Group {

private final String id;
@Getter(AccessLevel.PRIVATE)
private final boolean priv;
private final String creatorId;
private final String name;
private final String description;
private final String avatarUrl;
private final String lang;
private final long utcOffsetMs;
private final String metadata;
private final long count;
private final long createdAt;
private final long updatedAt;

public boolean isPrivate() {
return priv;
}

public <T> T getMetadata(final Class<T> clazz) {
return DefaultClient.GSON.fromJson(metadata, clazz);
}

static Group fromProto(final @NonNull com.heroiclabs.nakama.Api.Group group) {
return new DefaultGroup(group.getId(), group.getPrivate(), group.getCreatorId(), group.getName(),
group.getDescription(), group.getAvatarUrl(), group.getLang(), group.getUtcOffsetMs(),
group.getMetadata(), group.getCount(), group.getCreatedAt(), group.getUpdatedAt());
}

}
Loading

0 comments on commit 95a8571

Please sign in to comment.