Skip to content

Commit

Permalink
Update: 合成上应大小游戏支持了js调用上传成绩接口
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzqs committed Mar 14, 2022
1 parent 1987b64 commit be29b98
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/feature/game/entity/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ enum GameType {
}

int _getGameIndex(GameType type) => type.index;
GameType _fromGameIndex(int index) => GameType.values[index];

@HiveType(typeId: HiveTypeIdPool.gameRecordItem)
@JsonSerializable()
class GameRecord {
/// 游戏类型
@HiveField(0)
@JsonKey(name: 'game', toJson: _getGameIndex)
@JsonKey(name: 'game', toJson: _getGameIndex, fromJson: _fromGameIndex)
final GameType type;

/// 得分
Expand All @@ -55,15 +56,21 @@ class GameRecord {
/// 游戏开始的时间
@HiveField(2)
@JsonKey(name: 'dateTime')
final DateTime ts;
DateTime ts;

/// 该局用时 (秒)
@HiveField(3)
final int timeCost;

const GameRecord(this.type, this.score, this.ts, this.timeCost);
GameRecord(this.type, this.score, this.ts, this.timeCost);

Map<String, dynamic> toJson() => _$GameRecordToJson(this);
factory GameRecord.fromJson(Map<String, dynamic> json) => _$GameRecordFromJson(json);

@override
String toString() {
return 'GameRecord{type: $type, score: $score, ts: $ts, timeCost: $timeCost}';
}
}

@JsonSerializable()
Expand Down
31 changes: 30 additions & 1 deletion lib/feature/game/page/composeSit/index.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:kite/component/webview.dart';
import 'package:kite/feature/game/util/upload.dart';
import 'package:kite/feature/web_page/webview/page/index.dart';
import 'package:kite/util/logger.dart';
import 'package:kite/util/rule.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../entity/game.dart';

class ComposeSitPage extends StatelessWidget {
const ComposeSitPage({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return const SimpleWebViewPage(
return SimpleWebViewPage(
initialUrl: 'https://cdn.kite.sunnysab.cn/game/composeSit',
showLoadInBrowser: true,
injectJsRules: [
InjectJsRuleItem(injectTime: InjectJsTime.onPageStarted, rule: FunctionalRule((x) => true), javascript: '''
function uploadGameRecord(obj){
KiteGame.postMessage(JSON.stringify(obj));
}''')
],
javascriptChannels: {
JavascriptChannel(
name: 'KiteGame',
onMessageReceived: (JavascriptMessage message) async {
Log.info('收到上传游戏记录请求${message.message}');
final record = GameRecord.fromJson(jsonDecode(message.message));

Log.info('上传游戏记录$record');
record.ts = DateTime.now();

await uploadGameRecord(context, record);
},
),
},
);
}
}
3 changes: 2 additions & 1 deletion lib/feature/game/service/ranking.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import 'package:kite/abstract/abstract_service.dart';
import 'package:kite/abstract/abstract_session.dart';
import 'package:kite/util/logger.dart';

import '../dao/game.dart';
import '../entity/game.dart';
Expand All @@ -38,7 +39,7 @@ class RankingService extends AService implements RankingServiceDao {

@override
Future<void> postScore(GameRecord record) async {
print(record.toJson());
Log.info(record.toJson());
await session.post(_scoreUploading, data: record.toJson());
}
}
5 changes: 5 additions & 0 deletions lib/feature/web_page/webview/page/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class SimpleWebViewPage extends StatefulWidget {
/// 异步注入cookie
final Future<List<WebViewCookie>>? initialAsyncCookies;

/// 暴露dart回调到js接口
final Set<JavascriptChannel>? javascriptChannels;

const SimpleWebViewPage({
Key? key,
required this.initialUrl,
Expand All @@ -53,6 +56,7 @@ class SimpleWebViewPage extends StatefulWidget {
this.userAgent,
this.postData,
this.initialAsyncCookies,
this.javascriptChannels,
}) : super(key: key);

@override
Expand Down Expand Up @@ -131,6 +135,7 @@ class _SimpleWebViewPageState extends State<SimpleWebViewPage> {
setState(() {});
}
},
javascriptChannels: widget.javascriptChannels,
userAgent: widget.userAgent,
postData: widget.postData,
initialAsyncCookies: widget.initialAsyncCookies,
Expand Down

0 comments on commit be29b98

Please sign in to comment.