其中 Queue/GroupA 為特定群組,以群組為節點存放等待資訊,也以此節點為 Transaction 執行的標的。其中 game_id, score, low, high 為玩家屬性,實際使用應為變化數值
FIRDatabaseReference* pair_ref = [[[FIRDatabase database] reference] child:@"Queue/GroupA"];
[pair_ref runTransactionBlock:^FIRTransactionResult* _Nonnull(FIRMutableData* _Nonnull currentData) {
NSMutableArray* datas = currentData.value;
if (!datas || [datas isEqual:[NSNull null]]) {
// Queue 空白,將自己排入 Queue
NSDictionary* data = @{
@"game_id" : "UUID GAME ID", // 新建遊戲房的 ID
@"score" : 100, // 玩家目前積分
@"low" : 80, // 配對玩家的積分下限
@"high" : 120, // 配對玩家的積分上限
};
datas = [[NSMutableArray alloc] initWithObjects:data, nil];
} else {
BOOL pair = NO;
for (NSDictionary* data in datas) {
if ([[data objectForKey:@"score"] intValue] >= 80
&& [[data objectForKey:@"score"] intValue] <= 120
&& 100 >= [[data objectForKey:@"low"] intValue]
&& 100 <= [[data objectForKey:@"high"] intValue]) {
// 配對成功 Pair,將等待訊息移除,目標前往加入 [data objectForKey:@"game_id"]
[datas removeObject:data];
break;
}
}
if (!pair) {
// 配對失敗,將自己排入 Queue
NSDictionary* data = @{
@"game_id" : "UUID GAME ID",
@"score" : 100,
@"low" : 80,
@"high" : 120,
};
[datas addObject:data];
}
}
[currentData setValue:datas];
return [FIRTransactionResult successWithValue:currentData];
}
andCompletionBlock:^(NSError* _Nullable error, BOOL committed, FIRDataSnapshot* _Nullable snapshot) {
if (error || !committed) {
// 配對失敗
} else {
// 成功配對,進入下個階段,前往 game
}
}
withLocalEvents:NO];
Player ID A 與 user_json_dic 為此玩家的專屬 ID 及其玩家資料,使用上應替換為玩家數值
FIRDatabaseReference* game_ref = [[[FIRDatabase database] reference] child:@"UUID GAME ID/Player"];
NSData* user_json_string = [[NSString stringWithUTF8String:user_json] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* user_json_dic = [NSJSONSerialization JSONObjectWithData:user_json_string options:0 error:nil];
NSDictionary* player = @{
@"Player ID A" : user_json_dic,
};
[game_ref setValue:player];
FIRDatabaseReference* listen_ref = [[[FIRDatabase database] reference] child:@"Game/UUID GAME ID"];
FIRDatabaseHandle handle = [listen_ref observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot* _Nonnull snapshot) {
if (snapshot.value != [NSNull null]) {
// 監聽遊戲房間副本
}
}];