feat: basic implementation

This commit is contained in:
2023-09-21 10:14:47 +03:00
parent 8118fa058d
commit 3def73ac5d
13 changed files with 216 additions and 119 deletions

9
.editorconfig Normal file
View File

@@ -0,0 +1,9 @@
[*]
tab_width = 2
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false

2
.eslintignore Normal file
View File

@@ -0,0 +1,2 @@
templates/
scaffolds/

2
.prettierignore Normal file
View File

@@ -0,0 +1,2 @@
templates/
scaffolds/

15
.prettierrc Normal file
View File

@@ -0,0 +1,15 @@
{
"printWidth": 100,
"semi": false,
"singleQuote": true,
"trailingComma": "all",
"overrides": [
{
"files": "*.md",
"options": {
"printWidth": 100,
"proseWrap": "always"
}
}
]
}

View File

@@ -1,16 +1,4 @@
# mudblock
A new Flutter project.
## Getting Started
This project is a starting point for a Flutter application.
A few resources to get you started if this is your first Flutter project:
- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
This is an MUD ([Multi User Dungeon](https://en.wikipedia.org/wiki/Multi-user_dungeon)) client,
designed to be cross platform for both mobile and desktop.

2
lib/core/consts.dart Normal file
View File

@@ -0,0 +1,2 @@
const newline = '\n';
const ansiEscapePattern = r'\x1B\[[0-?]*[ -/]*[@-~]';

83
lib/core/store.dart Normal file
View File

@@ -0,0 +1,83 @@
import 'dart:math';
import 'package:ctelnet/ctelnet.dart';
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
import 'consts.dart';
const maxLines = 2000;
class GameStore extends ChangeNotifier {
final List<String> _lines = [];
late final CTelnetClient _client;
late final ScrollController scrollController;
GameStore init() {
addLine('Connecting...');
_client = CTelnetClient(
host: 'smud.ourmmo.com',
port: 3000,
onConnect: _onConnect,
onDisconnect: onDisconnect,
onData: onData,
onError: onError,
);
scrollController = ScrollController();
_client.connect();
return this;
}
void _onConnect() {
addLine('Connected');
}
void onDisconnect() {
addLine('Disconnected');
}
void onData(String data) {
debugPrint('onData: $data');
// final pattern = RegExp("$newline|$ansiEscapePattern");
// ignore: unnecessary_string_interpolations
final pattern = RegExp("$newline");
for (final line in data.split(pattern)) {
onLine(line);
}
}
void onError(Object error) {
onData('Error: $error');
}
void onLine(String line) {
addLine(line);
}
List<String> get lines => _lines.sublist(max(0, _lines.length - maxLines), _lines.length);
void addLine(String line) {
_lines.add(line);
notifyListeners();
}
void send(String line) {
debugPrint('send: $line');
_client.send(line + newline);
}
void submitInput(String text) {
addLine(text);
send(text);
scrollController.animateTo(
scrollController.offset,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
}
}
mixin GameStoreMixin<T extends StatefulWidget> on State<T> {
GameStore get store => Provider.of<GameStore>(context, listen: false);
}

View File

@@ -1,4 +1,8 @@
import 'package:flutter/material.dart';
import 'package:mudblock/core/store.dart';
import 'package:provider/provider.dart';
import 'pages/home_page.dart';
void main() {
runApp(const MyApp());
@@ -13,113 +17,18 @@ class MyApp extends StatelessWidget {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a blue toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
home: Scaffold(
body: ChangeNotifierProvider(
create: (_) => GameStore().init(),
builder: (context, snapshot) {
return const HomePage();
}
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

59
lib/pages/home_page.dart Normal file
View File

@@ -0,0 +1,59 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../core/store.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with GameStoreMixin {
final TextEditingController _input = TextEditingController();
@override
Widget build(BuildContext context) {
const textStyle = TextStyle(color: Colors.white);
return Material(
color: Colors.black,
child: Column(
children: [
Expanded(
child: Consumer<GameStore>(
builder: (context, store, child) {
final lines = store.lines;
return ListView.builder(
controller: store.scrollController,
shrinkWrap: true,
itemBuilder: (context, index) {
return RichText(
text: TextSpan(
children: [
TextSpan(text: lines[index], style: textStyle),
],
),
);
},
itemCount: lines.length,
);
},
),
),
TextField(
controller: _input,
onSubmitted: (text) {
store.submitInput(text);
},
style: textStyle,
decoration: InputDecoration(
hintText: 'Enter command',
hintStyle: textStyle.copyWith(color: Colors.grey),
),
),
],
),
);
}
}

View File

@@ -8,5 +8,7 @@
<true/>
<key>com.apple.security.network.server</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>

View File

@@ -4,5 +4,7 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>

View File

@@ -41,6 +41,13 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.17.2"
ctelnet:
dependency: "direct main"
description:
path: "../ctelnet"
relative: true
source: path
version: "1.0.0"
cupertino_icons:
dependency: "direct main"
description:
@@ -107,6 +114,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.9.1"
nested:
dependency: transitive
description:
name: nested
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
path:
dependency: transitive
description:
@@ -115,6 +130,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.3"
provider:
dependency: "direct main"
description:
name: provider
sha256: cdbe7530b12ecd9eb455bdaa2fcb8d4dad22e80b8afb4798b41479d5ce26847f
url: "https://pub.dev"
source: hosted
version: "6.0.5"
sky_engine:
dependency: transitive
description: flutter
@@ -186,3 +209,4 @@ packages:
version: "0.1.4-beta"
sdks:
dart: ">=3.1.2 <4.0.0"
flutter: ">=1.16.0"

View File

@@ -28,13 +28,15 @@ environment:
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
ctelnet:
path: ../ctelnet
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
provider: ^6.0.5
dev_dependencies:
flutter_test:
@@ -52,12 +54,10 @@ dev_dependencies:
# The following section is specific to Flutter packages.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg